1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-06 13:25:13 -04:00
v2fly/proxy/socks/socks.go

294 lines
7.9 KiB
Go
Raw Normal View History

package socks
2015-09-07 08:49:40 -04:00
import (
2015-10-13 07:55:06 -04:00
"errors"
2015-09-15 18:06:22 -04:00
"io"
2015-09-07 08:49:40 -04:00
"net"
2015-09-23 08:14:53 -04:00
"sync"
2015-10-06 11:24:57 -04:00
"time"
2015-09-09 06:13:52 -04:00
2015-10-14 08:51:19 -04:00
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common/alloc"
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-10-15 07:42:43 -04:00
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
2015-09-09 06:13:52 -04:00
)
2015-10-13 07:55:06 -04:00
var (
UnsupportedSocksCommand = errors.New("Unsupported socks command.")
UnsupportedAuthMethod = errors.New("Unsupported auth method.")
)
2015-09-07 08:49:40 -04:00
// SocksServer is a SOCKS 5 proxy server
type SocksServer struct {
2016-01-03 18:33:25 -05:00
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
accepting bool
space app.Space
config *Config
tcpListener *net.TCPListener
udpConn *net.UDPConn
udpAddress v2net.Destination
2015-09-10 18:24:18 -04:00
}
func NewSocksServer(space app.Space, config *Config) *SocksServer {
2015-09-16 10:27:36 -04:00
return &SocksServer{
2015-12-05 16:55:45 -05:00
space: space,
config: config,
2015-09-16 10:27:36 -04:00
}
2015-09-07 08:49:40 -04:00
}
func (this *SocksServer) Close() {
this.accepting = false
if this.tcpListener != nil {
2016-01-03 18:33:25 -05:00
this.tcpListener.Close()
this.tcpMutex.Lock()
this.tcpListener = nil
this.tcpMutex.Unlock()
}
if this.udpConn != nil {
2016-01-03 18:33:25 -05:00
this.udpConn.Close()
this.udpMutex.Lock()
this.udpConn = nil
this.udpMutex.Unlock()
}
}
2015-12-02 15:44:01 -05:00
func (this *SocksServer) Listen(port v2net.Port) error {
2015-10-15 07:42:43 -04:00
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: int(port),
Zone: "",
})
2015-09-07 08:49:40 -04:00
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to listen on port %d: %v", port, err)
2015-09-12 14:36:21 -04:00
return err
2015-09-07 08:49:40 -04:00
}
2015-11-27 15:50:28 -05:00
this.accepting = true
2016-01-04 02:40:24 -05:00
this.tcpMutex.Lock()
this.tcpListener = listener
2016-01-04 02:40:24 -05:00
this.tcpMutex.Unlock()
go this.AcceptConnections()
if this.config.UDPEnabled {
2015-11-27 15:50:28 -05:00
this.ListenUDP(port)
2015-10-03 15:42:03 -04:00
}
2015-09-07 08:49:40 -04:00
return nil
}
func (this *SocksServer) AcceptConnections() {
2015-11-27 15:50:28 -05:00
for this.accepting {
2015-10-15 07:42:43 -04:00
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
2016-01-03 18:33:25 -05:00
this.tcpMutex.RLock()
2016-01-03 19:19:27 -05:00
defer this.tcpMutex.RUnlock()
if !this.accepting {
2016-01-03 18:33:25 -05:00
return nil
}
connection, err := this.tcpListener.AcceptTCP()
2015-10-15 07:42:43 -04:00
if err != nil {
log.Error("Socks failed to accept new connection %v", err)
return err
}
2015-11-27 15:50:28 -05:00
go this.HandleConnection(connection)
2015-10-15 07:42:43 -04:00
return nil
})
2015-09-07 08:49:40 -04:00
}
}
2015-11-27 15:50:28 -05:00
func (this *SocksServer) HandleConnection(connection *net.TCPConn) error {
2015-09-09 06:13:52 -04:00
defer connection.Close()
2015-09-15 18:06:22 -04:00
reader := v2net.NewTimeOutReader(120, connection)
2015-09-09 06:13:52 -04:00
auth, auth4, err := protocol.ReadAuthentication(reader)
2015-10-13 07:55:06 -04:00
if err != nil && err != protocol.Socks4Downgrade {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to read authentication: %v", err)
2015-09-12 14:36:21 -04:00
return err
2015-09-09 06:13:52 -04:00
}
2015-10-13 07:55:06 -04:00
if err != nil && err == protocol.Socks4Downgrade {
2015-11-27 15:50:28 -05:00
return this.handleSocks4(reader, connection, auth4)
2015-09-17 11:37:04 -04:00
} else {
2015-11-27 15:50:28 -05:00
return this.handleSocks5(reader, connection, auth)
2015-10-03 15:42:03 -04:00
}
}
2015-09-17 11:37:04 -04:00
2015-11-27 15:50:28 -05:00
func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Writer, auth protocol.Socks5AuthenticationRequest) error {
2015-10-03 15:42:03 -04:00
expectedAuthMethod := protocol.AuthNotRequired
if this.config.AuthType == AuthTypePassword {
2015-10-03 15:42:03 -04:00
expectedAuthMethod = protocol.AuthUserPass
}
2015-09-17 11:37:04 -04:00
2015-10-03 15:42:03 -04:00
if !auth.HasAuthMethod(expectedAuthMethod) {
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
err := protocol.WriteAuthentication(writer, authResponse)
2015-09-15 18:06:22 -04:00
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to write authentication: %v", err)
2015-09-15 18:06:22 -04:00
return err
}
2015-10-03 15:42:03 -04:00
log.Warning("Socks client doesn't support allowed any auth methods.")
2015-10-13 07:55:06 -04:00
return UnsupportedAuthMethod
2015-10-03 15:42:03 -04:00
}
2015-09-17 11:37:04 -04:00
2015-10-03 15:42:03 -04:00
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
err := protocol.WriteAuthentication(writer, authResponse)
if err != nil {
log.Error("Socks failed to write authentication: %v", err)
return err
}
if this.config.AuthType == AuthTypePassword {
2015-10-03 15:42:03 -04:00
upRequest, err := protocol.ReadUserPassRequest(reader)
if err != nil {
2015-10-03 15:42:03 -04:00
log.Error("Socks failed to read username and password: %v", err)
return err
}
2015-10-03 15:42:03 -04:00
status := byte(0)
2015-11-27 15:50:28 -05:00
if !this.config.HasAccount(upRequest.Username(), upRequest.Password()) {
2015-10-03 15:42:03 -04:00
status = byte(0xFF)
}
upResponse := protocol.NewSocks5UserPassResponse(status)
err = protocol.WriteUserPassResponse(writer, upResponse)
if err != nil {
log.Error("Socks failed to write user pass response: %v", err)
return err
}
if status != byte(0) {
2015-10-13 07:55:06 -04:00
log.Warning("Invalid user account: %s", upRequest.AuthDetail())
return proxy.InvalidAuthentication
2015-10-03 15:42:03 -04:00
}
}
2015-09-09 06:13:52 -04:00
2015-10-03 15:42:03 -04:00
request, err := protocol.ReadRequest(reader)
if err != nil {
log.Error("Socks failed to read request: %v", err)
return err
}
2015-09-11 08:12:26 -04:00
if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled {
2015-11-27 15:50:28 -05:00
return this.handleUDP(reader, writer)
2015-10-03 18:21:06 -04:00
}
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
2015-10-03 15:42:03 -04:00
response := protocol.NewSocks5Response()
response.Error = protocol.ErrorCommandNotSupported
2015-12-02 15:44:01 -05:00
response.Port = v2net.Port(0)
2015-11-06 07:08:20 -05:00
response.SetIPv4([]byte{0, 0, 0, 0})
2015-10-08 17:06:12 -04:00
responseBuffer := alloc.NewSmallBuffer().Clear()
response.Write(responseBuffer)
_, err = writer.Write(responseBuffer.Value)
2015-10-10 11:01:05 -04:00
responseBuffer.Release()
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to write response: %v", err)
return err
}
2015-10-03 15:42:03 -04:00
log.Warning("Unsupported socks command %d", request.Command)
2015-10-13 07:55:06 -04:00
return UnsupportedSocksCommand
2015-10-03 15:42:03 -04:00
}
2015-10-13 16:00:12 -04:00
response := protocol.NewSocks5Response()
2015-10-03 15:42:03 -04:00
response.Error = protocol.ErrorSuccess
2015-10-03 18:21:06 -04:00
2015-10-05 11:09:57 -04:00
// Some SOCKS software requires a value other than dest. Let's fake one:
2015-12-02 15:44:01 -05:00
response.Port = v2net.Port(1717)
2015-11-02 18:07:19 -05:00
response.SetIPv4([]byte{0, 0, 0, 0})
2015-10-05 11:09:44 -04:00
2015-10-08 17:06:12 -04:00
responseBuffer := alloc.NewSmallBuffer().Clear()
response.Write(responseBuffer)
_, err = writer.Write(responseBuffer.Value)
2015-10-10 11:01:05 -04:00
responseBuffer.Release()
2015-10-03 15:42:03 -04:00
if err != nil {
log.Error("Socks failed to write response: %v", err)
return err
}
dest := request.Destination()
2015-12-12 14:57:47 -05:00
log.Info("TCP Connect request to %s", dest.String())
packet := v2net.NewPacket(dest, nil, true)
2015-11-27 15:50:28 -05:00
this.transport(reader, writer, packet)
2015-10-03 15:42:03 -04:00
return nil
}
2015-11-27 15:50:28 -05:00
func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer) error {
2015-10-03 18:21:06 -04:00
response := protocol.NewSocks5Response()
response.Error = protocol.ErrorSuccess
udpAddr := this.udpAddress
2015-10-03 18:21:06 -04:00
2015-12-02 15:44:01 -05:00
response.Port = udpAddr.Port()
2015-10-03 18:21:06 -04:00
switch {
2015-12-16 17:53:38 -05:00
case udpAddr.Address().IsIPv4():
response.SetIPv4(udpAddr.Address().IP())
case udpAddr.Address().IsIPv6():
response.SetIPv6(udpAddr.Address().IP())
case udpAddr.Address().IsDomain():
response.SetDomain(udpAddr.Address().Domain())
2015-10-03 18:21:06 -04:00
}
2015-10-08 17:06:12 -04:00
2015-10-11 09:18:35 -04:00
responseBuffer := alloc.NewSmallBuffer().Clear()
2015-10-08 17:06:12 -04:00
response.Write(responseBuffer)
_, err := writer.Write(responseBuffer.Value)
responseBuffer.Release()
2015-10-03 18:21:06 -04:00
if err != nil {
log.Error("Socks failed to write response: %v", err)
return err
}
reader.SetTimeOut(300) /* 5 minutes */
v2net.ReadFrom(reader, nil) // Just in case of anything left in the socket
2015-10-06 11:24:57 -04:00
// The TCP connection closes after this method returns. We need to wait until
// the client closes it.
// TODO: get notified from UDP part
2015-10-06 05:57:26 -04:00
<-time.After(5 * time.Minute)
2015-10-03 18:21:06 -04:00
return nil
}
2015-11-27 15:50:28 -05:00
func (this *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth protocol.Socks4AuthenticationRequest) error {
2015-10-03 15:42:03 -04:00
result := protocol.Socks4RequestGranted
if auth.Command == protocol.CmdBind {
result = protocol.Socks4RequestRejected
2015-09-11 08:12:26 -04:00
}
2015-10-03 15:42:03 -04:00
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth.Port, auth.IP[:])
2015-10-08 17:06:12 -04:00
responseBuffer := alloc.NewSmallBuffer().Clear()
socks4Response.Write(responseBuffer)
writer.Write(responseBuffer.Value)
responseBuffer.Release()
2015-09-17 11:37:04 -04:00
2015-10-03 15:42:03 -04:00
if result == protocol.Socks4RequestRejected {
2015-10-13 07:55:06 -04:00
log.Warning("Unsupported socks 4 command %d", auth.Command)
return UnsupportedSocksCommand
2015-10-03 15:42:03 -04:00
}
2015-12-16 17:53:38 -05:00
dest := v2net.TCPDestination(v2net.IPAddress(auth.IP[:]), auth.Port)
packet := v2net.NewPacket(dest, nil, true)
2015-11-27 15:50:28 -05:00
this.transport(reader, writer, packet)
2015-10-03 15:42:03 -04:00
return nil
}
2015-11-27 15:50:28 -05:00
func (this *SocksServer) transport(reader io.Reader, writer io.Writer, firstPacket v2net.Packet) {
2015-12-05 16:55:45 -05:00
ray := this.space.PacketDispatcher().DispatchToOutbound(firstPacket)
2015-09-10 18:24:18 -04:00
input := ray.InboundInput()
output := ray.InboundOutput()
2015-10-03 15:42:03 -04:00
var inputFinish, outputFinish sync.Mutex
inputFinish.Lock()
outputFinish.Lock()
2015-09-09 06:13:52 -04:00
2015-11-27 15:50:28 -05:00
go func() {
v2net.ReaderToChan(input, reader)
inputFinish.Unlock()
close(input)
}()
2015-09-10 18:24:18 -04:00
2015-11-27 15:50:28 -05:00
go func() {
v2net.ChanToWriter(writer, output)
outputFinish.Unlock()
}()
outputFinish.Lock()
2015-09-10 18:24:18 -04:00
}