github.com/xraypb/xray-core@v1.6.6/infra/conf/socks.go (about) 1 package conf 2 3 import ( 4 "encoding/json" 5 "strings" 6 7 "github.com/golang/protobuf/proto" 8 "github.com/xraypb/xray-core/common/protocol" 9 "github.com/xraypb/xray-core/common/serial" 10 "github.com/xraypb/xray-core/proxy/socks" 11 ) 12 13 type SocksAccount struct { 14 Username string `json:"user"` 15 Password string `json:"pass"` 16 } 17 18 func (v *SocksAccount) Build() *socks.Account { 19 return &socks.Account{ 20 Username: v.Username, 21 Password: v.Password, 22 } 23 } 24 25 const ( 26 AuthMethodNoAuth = "noauth" 27 AuthMethodUserPass = "password" 28 ) 29 30 type SocksServerConfig struct { 31 AuthMethod string `json:"auth"` 32 Accounts []*SocksAccount `json:"accounts"` 33 UDP bool `json:"udp"` 34 Host *Address `json:"ip"` 35 Timeout uint32 `json:"timeout"` 36 UserLevel uint32 `json:"userLevel"` 37 } 38 39 func (v *SocksServerConfig) Build() (proto.Message, error) { 40 config := new(socks.ServerConfig) 41 switch v.AuthMethod { 42 case AuthMethodNoAuth: 43 config.AuthType = socks.AuthType_NO_AUTH 44 case AuthMethodUserPass: 45 config.AuthType = socks.AuthType_PASSWORD 46 default: 47 // newError("unknown socks auth method: ", v.AuthMethod, ". Default to noauth.").AtWarning().WriteToLog() 48 config.AuthType = socks.AuthType_NO_AUTH 49 } 50 51 if len(v.Accounts) > 0 { 52 config.Accounts = make(map[string]string, len(v.Accounts)) 53 for _, account := range v.Accounts { 54 config.Accounts[account.Username] = account.Password 55 } 56 } 57 58 config.UdpEnabled = v.UDP 59 if v.Host != nil { 60 config.Address = v.Host.Build() 61 } 62 63 config.Timeout = v.Timeout 64 config.UserLevel = v.UserLevel 65 return config, nil 66 } 67 68 type SocksRemoteConfig struct { 69 Address *Address `json:"address"` 70 Port uint16 `json:"port"` 71 Users []json.RawMessage `json:"users"` 72 } 73 74 type SocksClientConfig struct { 75 Servers []*SocksRemoteConfig `json:"servers"` 76 Version string `json:"version"` 77 } 78 79 func (v *SocksClientConfig) Build() (proto.Message, error) { 80 config := new(socks.ClientConfig) 81 config.Server = make([]*protocol.ServerEndpoint, len(v.Servers)) 82 switch strings.ToLower(v.Version) { 83 case "4": 84 config.Version = socks.Version_SOCKS4 85 case "4a": 86 config.Version = socks.Version_SOCKS4A 87 case "", "5": 88 config.Version = socks.Version_SOCKS5 89 default: 90 return nil, newError("failed to parse socks server version: ", v.Version).AtError() 91 } 92 for idx, serverConfig := range v.Servers { 93 server := &protocol.ServerEndpoint{ 94 Address: serverConfig.Address.Build(), 95 Port: uint32(serverConfig.Port), 96 } 97 for _, rawUser := range serverConfig.Users { 98 user := new(protocol.User) 99 if err := json.Unmarshal(rawUser, user); err != nil { 100 return nil, newError("failed to parse Socks user").Base(err).AtError() 101 } 102 account := new(SocksAccount) 103 if err := json.Unmarshal(rawUser, account); err != nil { 104 return nil, newError("failed to parse socks account").Base(err).AtError() 105 } 106 if config.Version != socks.Version_SOCKS5 && account.Password != "" { 107 return nil, newError("password is only supported in socks5").AtError() 108 } 109 user.Account = serial.ToTypedMessage(account.Build()) 110 server.User = append(server.User, user) 111 } 112 config.Server[idx] = server 113 } 114 return config, nil 115 }