mirror of https://github.com/v2ray/v2ray-core
Socks supports multiple accounts
parent
7cce04cc3a
commit
213aa2ecf0
|
@ -10,21 +10,41 @@ const (
|
||||||
AuthMethodUserPass = "password"
|
AuthMethodUserPass = "password"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SocksConfig struct {
|
type SocksAccount struct {
|
||||||
AuthMethod string `json:"auth"`
|
Username string `json:"user"`
|
||||||
Username string `json:"user"`
|
Password string `json:"pass"`
|
||||||
Password string `json:"pass"`
|
|
||||||
UDPEnabled bool `json:"udp"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config SocksConfig) IsNoAuth() bool {
|
type SocksConfig struct {
|
||||||
|
AuthMethod string `json:"auth"`
|
||||||
|
Accounts []SocksAccount `json:"accounts"`
|
||||||
|
UDPEnabled bool `json:"udp"`
|
||||||
|
|
||||||
|
accountMap map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *SocksConfig) Initialize() {
|
||||||
|
config.accountMap = make(map[string]string)
|
||||||
|
for _, account := range config.Accounts {
|
||||||
|
config.accountMap[account.Username] = account.Password
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *SocksConfig) IsNoAuth() bool {
|
||||||
return config.AuthMethod == AuthMethodNoAuth
|
return config.AuthMethod == AuthMethodNoAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config SocksConfig) IsPassword() bool {
|
func (config *SocksConfig) IsPassword() bool {
|
||||||
return config.AuthMethod == AuthMethodUserPass
|
return config.AuthMethod == AuthMethodUserPass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (config *SocksConfig) HasAccount(user, pass string) bool {
|
||||||
|
if actualPass, found := config.accountMap[user]; found {
|
||||||
|
return actualPass == pass
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
json.RegisterConfigType("socks", config.TypeInbound, func() interface{} {
|
json.RegisterConfigType("socks", config.TypeInbound, func() interface{} {
|
||||||
return new(SocksConfig)
|
return new(SocksConfig)
|
||||||
|
|
|
@ -107,8 +107,12 @@ type Socks5UserPassRequest struct {
|
||||||
password string
|
password string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (request Socks5UserPassRequest) IsValid(username string, password string) bool {
|
func (request Socks5UserPassRequest) Username() string {
|
||||||
return request.username == username && request.password == password
|
return request.username
|
||||||
|
}
|
||||||
|
|
||||||
|
func (request Socks5UserPassRequest) Password() string {
|
||||||
|
return request.password
|
||||||
}
|
}
|
||||||
|
|
||||||
func (request Socks5UserPassRequest) AuthDetail() string {
|
func (request Socks5UserPassRequest) AuthDetail() string {
|
||||||
|
|
|
@ -103,7 +103,7 @@ func (server *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.W
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
status := byte(0)
|
status := byte(0)
|
||||||
if !upRequest.IsValid(server.config.Username, server.config.Password) {
|
if server.config.HasAccount(upRequest.Username(), upRequest.Password()) {
|
||||||
status = byte(0xFF)
|
status = byte(0xFF)
|
||||||
}
|
}
|
||||||
upResponse := protocol.NewSocks5UserPassResponse(status)
|
upResponse := protocol.NewSocks5UserPassResponse(status)
|
||||||
|
|
|
@ -84,8 +84,12 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
|
||||||
ProtocolValue: "socks",
|
ProtocolValue: "socks",
|
||||||
SettingsValue: &json.SocksConfig{
|
SettingsValue: &json.SocksConfig{
|
||||||
AuthMethod: "noauth",
|
AuthMethod: "noauth",
|
||||||
Username: "userx",
|
Accounts: []json.SocksAccount{
|
||||||
Password: "passy",
|
json.SocksAccount{
|
||||||
|
Username: "userx",
|
||||||
|
Password: "passy",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
OutboundConfigValue: &mocks.ConnectionConfig{
|
OutboundConfigValue: &mocks.ConnectionConfig{
|
||||||
|
|
|
@ -8,8 +8,10 @@ import (
|
||||||
type SocksServerFactory struct {
|
type SocksServerFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory SocksServerFactory) Create(vp *core.Point, config interface{}) (core.InboundConnectionHandler, error) {
|
func (factory SocksServerFactory) Create(vp *core.Point, rawConfig interface{}) (core.InboundConnectionHandler, error) {
|
||||||
return NewSocksServer(vp, config.(*json.SocksConfig)), nil
|
config := rawConfig.(*json.SocksConfig)
|
||||||
|
config.Initialize()
|
||||||
|
return NewSocksServer(vp, config), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
Loading…
Reference in New Issue