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