mirror of https://github.com/v2ray/v2ray-core
update socks code for server config
parent
ea2c491ade
commit
948f04921d
|
@ -29,7 +29,7 @@ type Server struct {
|
||||||
udpMutex sync.RWMutex
|
udpMutex sync.RWMutex
|
||||||
accepting bool
|
accepting bool
|
||||||
packetDispatcher dispatcher.PacketDispatcher
|
packetDispatcher dispatcher.PacketDispatcher
|
||||||
config *Config
|
config *ServerConfig
|
||||||
tcpListener *internet.TCPHub
|
tcpListener *internet.TCPHub
|
||||||
udpHub *udp.UDPHub
|
udpHub *udp.UDPHub
|
||||||
udpAddress v2net.Destination
|
udpAddress v2net.Destination
|
||||||
|
@ -38,7 +38,7 @@ type Server struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer creates a new Server object.
|
// 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{
|
s := &Server{
|
||||||
config: config,
|
config: config,
|
||||||
meta: meta,
|
meta: meta,
|
||||||
|
@ -95,7 +95,7 @@ func (this *Server) Start() error {
|
||||||
this.tcpMutex.Lock()
|
this.tcpMutex.Lock()
|
||||||
this.tcpListener = listener
|
this.tcpListener = listener
|
||||||
this.tcpMutex.Unlock()
|
this.tcpMutex.Unlock()
|
||||||
if this.config.UDPEnabled {
|
if this.config.UdpEnabled {
|
||||||
this.listenUDP()
|
this.listenUDP()
|
||||||
}
|
}
|
||||||
return nil
|
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 {
|
func (this *Server) handleSocks5(clientAddr v2net.Destination, reader *v2io.BufferedReader, writer *v2io.BufferedWriter, auth protocol.Socks5AuthenticationRequest) error {
|
||||||
expectedAuthMethod := protocol.AuthNotRequired
|
expectedAuthMethod := protocol.AuthNotRequired
|
||||||
if this.config.AuthType == AuthTypePassword {
|
if this.config.AuthType == ServerConfig_PASSWORD {
|
||||||
expectedAuthMethod = protocol.AuthUserPass
|
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)
|
log.Error("Socks: failed to write authentication: ", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if this.config.AuthType == AuthTypePassword {
|
if this.config.AuthType == ServerConfig_PASSWORD {
|
||||||
upRequest, err := protocol.ReadUserPassRequest(reader)
|
upRequest, err := protocol.ReadUserPassRequest(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warning("Socks: failed to read username and password: ", err)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled {
|
if request.Command == protocol.CmdUdpAssociate && this.config.UdpEnabled {
|
||||||
return this.handleUDP(reader, writer)
|
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) {
|
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() {
|
func init() {
|
||||||
|
|
|
@ -4,20 +4,7 @@ import (
|
||||||
v2net "v2ray.com/core/common/net"
|
v2net "v2ray.com/core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func (this *ServerConfig) HasAccount(username, password string) bool {
|
||||||
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 {
|
|
||||||
if this.Accounts == nil {
|
if this.Accounts == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -27,3 +14,10 @@ func (this *Config) HasAccount(username, password string) bool {
|
||||||
}
|
}
|
||||||
return storedPassed == password
|
return storedPassed == password
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *ServerConfig) GetNetAddress() v2net.Address {
|
||||||
|
if this.Address == nil {
|
||||||
|
return v2net.LocalHostIP
|
||||||
|
}
|
||||||
|
return this.Address.AsAddress()
|
||||||
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ const (
|
||||||
AuthMethodUserPass = "password"
|
AuthMethodUserPass = "password"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (this *Config) UnmarshalJSON(data []byte) error {
|
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
|
||||||
type SocksConfig struct {
|
type SocksConfig struct {
|
||||||
AuthMethod string `json:"auth"`
|
AuthMethod string `json:"auth"`
|
||||||
Accounts []*Account `json:"accounts"`
|
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())
|
return errors.New("Socks: Failed to parse config: " + err.Error())
|
||||||
}
|
}
|
||||||
if rawConfig.AuthMethod == AuthMethodNoAuth {
|
if rawConfig.AuthMethod == AuthMethodNoAuth {
|
||||||
this.AuthType = AuthTypeNoAuth
|
this.AuthType = ServerConfig_NO_AUTH
|
||||||
} else if rawConfig.AuthMethod == AuthMethodUserPass {
|
} else if rawConfig.AuthMethod == AuthMethodUserPass {
|
||||||
this.AuthType = AuthTypePassword
|
this.AuthType = ServerConfig_PASSWORD
|
||||||
} else {
|
} else {
|
||||||
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
|
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
|
||||||
return common.ErrBadConfiguration
|
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 {
|
if rawConfig.Host != nil {
|
||||||
this.Address = rawConfig.Host.AsAddress()
|
this.Address = rawConfig.Host
|
||||||
} else {
|
|
||||||
this.Address = v2net.LocalHostIP
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if rawConfig.Timeout >= 0 {
|
if rawConfig.Timeout >= 0 {
|
||||||
|
@ -60,5 +58,5 @@ func (this *Config) UnmarshalJSON(data []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
registry.RegisterInboundConfig("socks", func() interface{} { return new(Config) })
|
registry.RegisterInboundConfig("socks", func() interface{} { return new(ServerConfig) })
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ func (this *Server) listenUDP() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
this.udpMutex.Lock()
|
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.udpHub = udpHub
|
||||||
this.udpMutex.Unlock()
|
this.udpMutex.Unlock()
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue