v2ray-core/proxy/socks/server.go

336 lines
9.2 KiB
Go
Raw Normal View History

package socks
2015-09-07 14:49:40 +02:00
import (
2015-10-13 13:55:06 +02:00
"errors"
2015-09-16 00:06:22 +02:00
"io"
2016-08-14 17:08:01 +02:00
"net"
2015-09-23 14:14:53 +02:00
"sync"
2015-10-06 17:24:57 +02:00
"time"
2015-09-09 12:13:52 +02:00
2016-04-26 00:35:42 +02:00
"github.com/v2ray/v2ray-core/app"
2016-01-31 17:01:28 +01:00
"github.com/v2ray/v2ray-core/app/dispatcher"
2016-01-29 13:39:55 +00:00
v2io "github.com/v2ray/v2ray-core/common/io"
2015-09-20 00:50:21 +02:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
2016-04-26 00:35:42 +02:00
"github.com/v2ray/v2ray-core/proxy/internal"
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
2016-06-14 22:54:08 +02:00
"github.com/v2ray/v2ray-core/transport/internet"
"github.com/v2ray/v2ray-core/transport/internet/udp"
2015-09-09 12:13:52 +02:00
)
2015-10-13 13:55:06 +02:00
var (
2016-08-07 15:32:41 +02:00
ErrUnsupportedSocksCommand = errors.New("Unsupported socks command.")
ErrUnsupportedAuthMethod = errors.New("Unsupported auth method.")
2015-10-13 13:55:06 +02:00
)
2016-04-26 00:33:16 +02:00
// Server is a SOCKS 5 proxy server
type Server struct {
2016-01-31 17:01:28 +01:00
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
accepting bool
packetDispatcher dispatcher.PacketDispatcher
config *Config
2016-06-14 22:54:08 +02:00
tcpListener *internet.TCPHub
udpHub *udp.UDPHub
2016-01-31 17:01:28 +01:00
udpAddress v2net.Destination
2016-06-14 22:54:08 +02:00
udpServer *udp.UDPServer
2016-06-04 14:25:13 +02:00
meta *proxy.InboundHandlerMeta
2015-09-11 00:24:18 +02:00
}
2016-04-26 00:33:16 +02:00
// NewServer creates a new Server object.
2016-07-23 11:09:49 +02:00
func NewServer(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *Server {
s := &Server{
config: config,
meta: meta,
2015-09-16 22:27:36 +08:00
}
2016-07-23 11:09:49 +02:00
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
log.Error("Socks|Server: Dispatcher is not found in the space.")
return app.ErrMissingApplication
}
s.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return nil
})
return s
2015-09-07 14:49:40 +02:00
}
2016-02-06 18:01:27 +01:00
// Port implements InboundHandler.Port().
2016-04-26 00:33:16 +02:00
func (this *Server) Port() v2net.Port {
2016-06-04 14:25:13 +02:00
return this.meta.Port
2016-01-19 23:41:40 +01:00
}
2016-02-06 18:01:27 +01:00
// Close implements InboundHandler.Close().
2016-04-26 00:33:16 +02:00
func (this *Server) Close() {
this.accepting = false
if this.tcpListener != nil {
2016-01-04 00:33:25 +01:00
this.tcpMutex.Lock()
this.tcpListener.Close()
2016-01-04 00:33:25 +01:00
this.tcpListener = nil
this.tcpMutex.Unlock()
}
2016-02-03 22:43:09 +01:00
if this.udpHub != nil {
2016-01-04 00:33:25 +01:00
this.udpMutex.Lock()
2016-02-03 22:43:09 +01:00
this.udpHub.Close()
this.udpHub = nil
2016-01-04 00:33:25 +01:00
this.udpMutex.Unlock()
}
}
2016-02-06 18:01:27 +01:00
// Listen implements InboundHandler.Listen().
2016-06-04 00:38:22 +02:00
func (this *Server) Start() error {
2016-01-19 23:41:40 +01:00
if this.accepting {
2016-06-04 00:38:22 +02:00
return nil
2016-01-19 23:41:40 +01:00
}
2016-06-14 22:54:08 +02:00
listener, err := internet.ListenTCP(
2016-06-04 14:25:13 +02:00
this.meta.Address,
this.meta.Port,
2016-06-04 00:38:22 +02:00
this.handleConnection,
2016-06-14 22:54:08 +02:00
this.meta.StreamSettings)
2015-09-07 14:49:40 +02:00
if err != nil {
2016-06-04 14:25:13 +02:00
log.Error("Socks: failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
2015-09-12 20:36:21 +02:00
return err
2015-09-07 14:49:40 +02:00
}
2015-11-27 21:50:28 +01:00
this.accepting = true
2016-01-04 08:40:24 +01:00
this.tcpMutex.Lock()
this.tcpListener = listener
2016-01-04 08:40:24 +01:00
this.tcpMutex.Unlock()
if this.config.UDPEnabled {
2016-06-04 14:25:13 +02:00
this.listenUDP()
2015-10-03 21:42:03 +02:00
}
2015-09-07 14:49:40 +02:00
return nil
}
2016-06-14 22:54:08 +02:00
func (this *Server) handleConnection(connection internet.Connection) {
2015-09-09 12:13:52 +02:00
defer connection.Close()
2015-09-16 00:06:22 +02:00
timedReader := v2net.NewTimeOutReader(this.config.Timeout, connection)
reader := v2io.NewBufferedReader(timedReader)
2016-04-12 21:56:36 +02:00
defer reader.Release()
writer := v2io.NewBufferedWriter(connection)
2016-04-12 21:56:36 +02:00
defer writer.Release()
2015-09-09 12:13:52 +02:00
auth, auth4, err := protocol.ReadAuthentication(reader)
2015-10-13 13:55:06 +02:00
if err != nil && err != protocol.Socks4Downgrade {
2016-06-03 20:21:46 +02:00
if err != io.EOF {
log.Warning("Socks: failed to read authentication: ", err)
}
return
2015-09-09 12:13:52 +02:00
}
2016-08-14 17:08:01 +02:00
clientAddr := v2net.TCPDestinationFromAddr(connection.RemoteAddr().(*net.TCPAddr))
2015-10-13 13:55:06 +02:00
if err != nil && err == protocol.Socks4Downgrade {
2016-06-05 15:02:15 +02:00
this.handleSocks4(clientAddr, reader, writer, auth4)
2015-09-17 17:37:04 +02:00
} else {
2016-06-05 15:02:15 +02:00
this.handleSocks5(clientAddr, reader, writer, auth)
2015-10-03 21:42:03 +02:00
}
}
2015-09-17 17:37:04 +02:00
2016-08-14 17:08:01 +02:00
func (this *Server) handleSocks5(clientAddr v2net.Destination, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks5AuthenticationRequest) error {
2015-10-03 21:42:03 +02:00
expectedAuthMethod := protocol.AuthNotRequired
if this.config.AuthType == AuthTypePassword {
2015-10-03 21:42:03 +02:00
expectedAuthMethod = protocol.AuthUserPass
}
2015-09-17 17:37:04 +02:00
2015-10-03 21:42:03 +02:00
if !auth.HasAuthMethod(expectedAuthMethod) {
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
err := protocol.WriteAuthentication(writer, authResponse)
writer.Flush()
2015-09-16 00:06:22 +02:00
if err != nil {
2016-06-03 20:21:46 +02:00
log.Warning("Socks: failed to write authentication: ", err)
2015-09-16 00:06:22 +02:00
return err
}
2016-02-05 11:45:31 +00:00
log.Warning("Socks: client doesn't support any allowed auth methods.")
2016-08-07 15:32:41 +02:00
return ErrUnsupportedAuthMethod
2015-10-03 21:42:03 +02:00
}
2015-09-17 17:37:04 +02:00
2015-10-03 21:42:03 +02:00
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
2016-06-03 20:21:46 +02:00
protocol.WriteAuthentication(writer, authResponse)
err := writer.Flush()
2015-10-03 21:42:03 +02:00
if err != nil {
2016-01-18 12:24:33 +01:00
log.Error("Socks: failed to write authentication: ", err)
2015-10-03 21:42:03 +02:00
return err
}
if this.config.AuthType == AuthTypePassword {
2015-10-03 21:42:03 +02:00
upRequest, err := protocol.ReadUserPassRequest(reader)
if err != nil {
2016-06-03 20:21:46 +02:00
log.Warning("Socks: failed to read username and password: ", err)
return err
}
2015-10-03 21:42:03 +02:00
status := byte(0)
2015-11-27 21:50:28 +01:00
if !this.config.HasAccount(upRequest.Username(), upRequest.Password()) {
2015-10-03 21:42:03 +02:00
status = byte(0xFF)
}
upResponse := protocol.NewSocks5UserPassResponse(status)
err = protocol.WriteUserPassResponse(writer, upResponse)
writer.Flush()
2015-10-03 21:42:03 +02:00
if err != nil {
2016-01-18 12:24:33 +01:00
log.Error("Socks: failed to write user pass response: ", err)
2015-10-03 21:42:03 +02:00
return err
}
if status != byte(0) {
2016-01-18 12:24:33 +01:00
log.Warning("Socks: Invalid user account: ", upRequest.AuthDetail())
2016-06-27 08:53:35 +02:00
log.Access(clientAddr, "", log.AccessRejected, proxy.ErrInvalidAuthentication)
return proxy.ErrInvalidAuthentication
2015-10-03 21:42:03 +02:00
}
}
2015-09-09 12:13:52 +02:00
2015-10-03 21:42:03 +02:00
request, err := protocol.ReadRequest(reader)
if err != nil {
2016-06-03 20:21:46 +02:00
log.Warning("Socks: failed to read request: ", err)
2015-10-03 21:42:03 +02:00
return err
}
2015-09-11 14:12:26 +02:00
if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled {
2015-11-27 21:50:28 +01:00
return this.handleUDP(reader, writer)
2015-10-04 00:21:06 +02:00
}
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
2015-10-03 21:42:03 +02:00
response := protocol.NewSocks5Response()
response.Error = protocol.ErrorCommandNotSupported
2015-12-02 20:44:01 +00:00
response.Port = v2net.Port(0)
2015-11-06 13:08:20 +01:00
response.SetIPv4([]byte{0, 0, 0, 0})
2015-10-08 23:06:12 +02:00
response.Write(writer)
writer.Flush()
if err != nil {
2016-01-18 12:24:33 +01:00
log.Error("Socks: failed to write response: ", err)
return err
}
2016-01-18 12:24:33 +01:00
log.Warning("Socks: Unsupported socks command ", request.Command)
2016-08-07 15:32:41 +02:00
return ErrUnsupportedSocksCommand
2015-10-03 21:42:03 +02:00
}
2015-10-13 22:00:12 +02:00
response := protocol.NewSocks5Response()
2015-10-03 21:42:03 +02:00
response.Error = protocol.ErrorSuccess
2015-10-04 00:21:06 +02:00
2015-10-05 17:09:57 +02: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-03 00:07:19 +01:00
response.SetIPv4([]byte{0, 0, 0, 0})
2015-10-05 17:09:44 +02:00
response.Write(writer)
2015-10-03 21:42:03 +02:00
if err != nil {
2016-01-18 12:24:33 +01:00
log.Error("Socks: failed to write response: ", err)
2015-10-03 21:42:03 +02:00
return err
}
reader.SetCached(false)
writer.SetCached(false)
2015-10-03 21:42:03 +02:00
dest := request.Destination()
2016-08-14 17:08:01 +02:00
session := &proxy.SessionInfo{
Source: clientAddr,
Destination: dest,
}
2016-01-18 12:24:33 +01:00
log.Info("Socks: TCP Connect request to ", dest)
2016-06-05 15:02:15 +02:00
log.Access(clientAddr, dest, log.AccessAccepted, "")
2015-12-12 20:57:47 +01:00
2016-08-14 17:08:01 +02:00
this.transport(reader, writer, session)
2015-10-03 21:42:03 +02:00
return nil
}
2016-04-26 00:33:16 +02:00
func (this *Server) handleUDP(reader io.Reader, writer *v2io.BufferedWriter) error {
2015-10-04 00:21:06 +02:00
response := protocol.NewSocks5Response()
response.Error = protocol.ErrorSuccess
udpAddr := this.udpAddress
2015-10-04 00:21:06 +02:00
2015-12-02 20:44:01 +00:00
response.Port = udpAddr.Port()
2016-08-14 18:14:12 +02:00
switch udpAddr.Address().Family() {
case v2net.AddressFamilyIPv4:
2015-12-16 23:53:38 +01:00
response.SetIPv4(udpAddr.Address().IP())
2016-08-14 18:14:12 +02:00
case v2net.AddressFamilyIPv6:
2015-12-16 23:53:38 +01:00
response.SetIPv6(udpAddr.Address().IP())
2016-08-14 18:14:12 +02:00
case v2net.AddressFamilyDomain:
2015-12-16 23:53:38 +01:00
response.SetDomain(udpAddr.Address().Domain())
2015-10-04 00:21:06 +02:00
}
2015-10-08 23:06:12 +02:00
response.Write(writer)
err := writer.Flush()
2015-10-08 23:06:12 +02:00
2015-10-04 00:21:06 +02:00
if err != nil {
2016-01-18 12:24:33 +01:00
log.Error("Socks: failed to write response: ", err)
2015-10-04 00:21:06 +02:00
return err
}
2015-10-06 17:24:57 +02: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 11:57:26 +02:00
<-time.After(5 * time.Minute)
2015-10-04 00:21:06 +02:00
return nil
}
2016-08-14 17:08:01 +02:00
func (this *Server) handleSocks4(clientAddr v2net.Destination, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks4AuthenticationRequest) error {
2015-10-03 21:42:03 +02:00
result := protocol.Socks4RequestGranted
if auth.Command == protocol.CmdBind {
result = protocol.Socks4RequestRejected
2015-09-11 14:12:26 +02:00
}
2015-10-03 21:42:03 +02:00
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth.Port, auth.IP[:])
2015-10-08 23:06:12 +02:00
socks4Response.Write(writer)
2015-09-17 17:37:04 +02:00
2015-10-03 21:42:03 +02:00
if result == protocol.Socks4RequestRejected {
2016-01-18 12:24:33 +01:00
log.Warning("Socks: Unsupported socks 4 command ", auth.Command)
2016-08-07 15:32:41 +02:00
log.Access(clientAddr, "", log.AccessRejected, ErrUnsupportedSocksCommand)
return ErrUnsupportedSocksCommand
2015-10-03 21:42:03 +02:00
}
reader.SetCached(false)
writer.SetCached(false)
2015-12-16 23:53:38 +01:00
dest := v2net.TCPDestination(v2net.IPAddress(auth.IP[:]), auth.Port)
2016-08-14 17:08:01 +02:00
session := &proxy.SessionInfo{
Source: clientAddr,
Destination: dest,
}
2016-06-05 15:02:15 +02:00
log.Access(clientAddr, dest, log.AccessAccepted, "")
2016-08-14 17:08:01 +02:00
this.transport(reader, writer, session)
2015-10-03 21:42:03 +02:00
return nil
}
2016-08-14 17:08:01 +02:00
func (this *Server) transport(reader io.Reader, writer io.Writer, session *proxy.SessionInfo) {
ray := this.packetDispatcher.DispatchToOutbound(this.meta, session)
2015-09-11 00:24:18 +02:00
input := ray.InboundInput()
output := ray.InboundOutput()
2015-10-03 21:42:03 +02:00
var inputFinish, outputFinish sync.Mutex
inputFinish.Lock()
outputFinish.Lock()
2015-09-09 12:13:52 +02:00
2015-11-27 21:50:28 +01:00
go func() {
v2reader := v2io.NewAdaptiveReader(reader)
defer v2reader.Release()
v2io.Pipe(v2reader, input)
2015-11-27 21:50:28 +01:00
inputFinish.Unlock()
2016-04-18 18:44:10 +02:00
input.Close()
2015-11-27 21:50:28 +01:00
}()
2015-09-11 00:24:18 +02:00
2015-11-27 21:50:28 +01:00
go func() {
v2writer := v2io.NewAdaptiveWriter(writer)
defer v2writer.Release()
v2io.Pipe(output, v2writer)
2015-11-27 21:50:28 +01:00
outputFinish.Unlock()
2016-04-18 18:44:10 +02:00
output.Release()
2015-11-27 21:50:28 +01:00
}()
outputFinish.Lock()
2015-09-11 00:24:18 +02:00
}
2016-04-26 00:35:42 +02:00
2016-06-14 22:54:08 +02:00
type ServerFactory struct{}
func (this *ServerFactory) StreamCapability() internet.StreamConnectionType {
return internet.StreamConnectionTypeRawTCP
}
func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
2016-07-23 11:09:49 +02:00
return NewServer(rawConfig.(*Config), space, meta), nil
2016-06-14 22:54:08 +02:00
}
2016-04-26 00:35:42 +02:00
func init() {
2016-06-14 22:54:08 +02:00
internal.MustRegisterInboundHandlerCreator("socks", new(ServerFactory))
2016-04-26 00:35:42 +02:00
}