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