diff --git a/proxy/socks/server.go b/proxy/socks/server.go index b6c724484..6b65705a4 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -29,7 +29,7 @@ type Server struct { udpMutex sync.RWMutex accepting bool packetDispatcher dispatcher.PacketDispatcher - config *Config + config *ServerConfig tcpListener *internet.TCPHub udpHub *udp.UDPHub udpAddress v2net.Destination @@ -38,7 +38,7 @@ type Server struct { } // NewServer creates a new Server object. -func NewServer(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *Server { +func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) *Server { s := &Server{ config: config, meta: meta, @@ -95,7 +95,7 @@ func (this *Server) Start() error { this.tcpMutex.Lock() this.tcpListener = listener this.tcpMutex.Unlock() - if this.config.UDPEnabled { + if this.config.UdpEnabled { this.listenUDP() } return nil @@ -129,7 +129,7 @@ func (this *Server) handleConnection(connection internet.Connection) { func (this *Server) handleSocks5(clientAddr v2net.Destination, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks5AuthenticationRequest) error { expectedAuthMethod := protocol.AuthNotRequired - if this.config.AuthType == AuthTypePassword { + if this.config.AuthType == ServerConfig_PASSWORD { expectedAuthMethod = protocol.AuthUserPass } @@ -152,7 +152,7 @@ func (this *Server) handleSocks5(clientAddr v2net.Destination, reader *v2io.Buff log.Error("Socks: failed to write authentication: ", err) return err } - if this.config.AuthType == AuthTypePassword { + if this.config.AuthType == ServerConfig_PASSWORD { upRequest, err := protocol.ReadUserPassRequest(reader) if err != nil { log.Warning("Socks: failed to read username and password: ", err) @@ -182,7 +182,7 @@ func (this *Server) handleSocks5(clientAddr v2net.Destination, reader *v2io.Buff return err } - if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled { + if request.Command == protocol.CmdUdpAssociate && this.config.UdpEnabled { return this.handleUDP(reader, writer) } @@ -320,7 +320,7 @@ func (this *ServerFactory) StreamCapability() internet.StreamConnectionType { } func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) { - return NewServer(rawConfig.(*Config), space, meta), nil + return NewServer(rawConfig.(*ServerConfig), space, meta), nil } func init() { diff --git a/proxy/socks/server_config.go b/proxy/socks/server_config.go index e9a2b61ed..1d98a4e74 100644 --- a/proxy/socks/server_config.go +++ b/proxy/socks/server_config.go @@ -4,20 +4,7 @@ import ( v2net "v2ray.com/core/common/net" ) -const ( - AuthTypeNoAuth = byte(0) - AuthTypePassword = byte(1) -) - -type Config struct { - AuthType byte - Accounts map[string]string - Address v2net.Address - UDPEnabled bool - Timeout uint32 -} - -func (this *Config) HasAccount(username, password string) bool { +func (this *ServerConfig) HasAccount(username, password string) bool { if this.Accounts == nil { return false } @@ -27,3 +14,10 @@ func (this *Config) HasAccount(username, password string) bool { } return storedPassed == password } + +func (this *ServerConfig) GetNetAddress() v2net.Address { + if this.Address == nil { + return v2net.LocalHostIP + } + return this.Address.AsAddress() +} diff --git a/proxy/socks/server_config_json.go b/proxy/socks/server_config_json.go index 4605cfb28..2cfaa396f 100644 --- a/proxy/socks/server_config_json.go +++ b/proxy/socks/server_config_json.go @@ -17,7 +17,7 @@ const ( AuthMethodUserPass = "password" ) -func (this *Config) UnmarshalJSON(data []byte) error { +func (this *ServerConfig) UnmarshalJSON(data []byte) error { type SocksConfig struct { AuthMethod string `json:"auth"` Accounts []*Account `json:"accounts"` @@ -31,9 +31,9 @@ func (this *Config) UnmarshalJSON(data []byte) error { return errors.New("Socks: Failed to parse config: " + err.Error()) } if rawConfig.AuthMethod == AuthMethodNoAuth { - this.AuthType = AuthTypeNoAuth + this.AuthType = ServerConfig_NO_AUTH } else if rawConfig.AuthMethod == AuthMethodUserPass { - this.AuthType = AuthTypePassword + this.AuthType = ServerConfig_PASSWORD } else { log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod) return common.ErrBadConfiguration @@ -46,11 +46,9 @@ func (this *Config) UnmarshalJSON(data []byte) error { } } - this.UDPEnabled = rawConfig.UDP + this.UdpEnabled = rawConfig.UDP if rawConfig.Host != nil { - this.Address = rawConfig.Host.AsAddress() - } else { - this.Address = v2net.LocalHostIP + this.Address = rawConfig.Host } if rawConfig.Timeout >= 0 { @@ -60,5 +58,5 @@ func (this *Config) UnmarshalJSON(data []byte) error { } func init() { - registry.RegisterInboundConfig("socks", func() interface{} { return new(Config) }) + registry.RegisterInboundConfig("socks", func() interface{} { return new(ServerConfig) }) } diff --git a/proxy/socks/server_udp.go b/proxy/socks/server_udp.go index 09a6d1031..956b3ca3c 100644 --- a/proxy/socks/server_udp.go +++ b/proxy/socks/server_udp.go @@ -17,7 +17,7 @@ func (this *Server) listenUDP() error { return err } this.udpMutex.Lock() - this.udpAddress = v2net.UDPDestination(this.config.Address, this.meta.Port) + this.udpAddress = v2net.UDPDestination(this.config.GetNetAddress(), this.meta.Port) this.udpHub = udpHub this.udpMutex.Unlock() return nil