diff --git a/proxy/socks/account.go b/proxy/socks/account.go new file mode 100644 index 000000000..a4a40b803 --- /dev/null +++ b/proxy/socks/account.go @@ -0,0 +1,18 @@ +package socks + +import ( + "github.com/v2ray/v2ray-core/common/protocol" +) + +type Account struct { + Username string `json:"user"` + Password string `json:"pass"` +} + +func (this *Account) Equals(another protocol.Account) bool { + socksAccount, ok := another.(*Account) + if !ok { + return false + } + return this.Username == socksAccount.Username +} diff --git a/proxy/socks/client_config.go b/proxy/socks/client_config.go new file mode 100644 index 000000000..5cf0d5aa6 --- /dev/null +++ b/proxy/socks/client_config.go @@ -0,0 +1,9 @@ +package socks + +import ( + "github.com/v2ray/v2ray-core/common/protocol" +) + +type ClientConfig struct { + Servers []*protocol.ServerSpec +} diff --git a/proxy/socks/client_config_json.go b/proxy/socks/client_config_json.go new file mode 100644 index 000000000..87e47afc7 --- /dev/null +++ b/proxy/socks/client_config_json.go @@ -0,0 +1,47 @@ +package socks + +import ( + "encoding/json" + "errors" + + v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/common/protocol" + "github.com/v2ray/v2ray-core/proxy/internal" +) + +func (this *ClientConfig) UnmarshalJSON(data []byte) error { + type ServerConfig struct { + Address *v2net.AddressJson `json:"address"` + Port v2net.Port `json:"port"` + Users []json.RawMessage `json:"users"` + } + type JsonConfig struct { + Servers []*ServerConfig `json:"servers"` + } + jsonConfig := new(JsonConfig) + if err := json.Unmarshal(data, jsonConfig); err != nil { + return errors.New("Socks|Client: Failed to parse config: " + err.Error()) + } + this.Servers = make([]*protocol.ServerSpec, len(jsonConfig.Servers)) + for idx, serverConfig := range jsonConfig.Servers { + server := protocol.NewServerSpec(v2net.TCPDestination(serverConfig.Address.Address, serverConfig.Port), protocol.AlwaysValid()) + for _, rawUser := range serverConfig.Users { + user := new(protocol.User) + if err := json.Unmarshal(rawUser, user); err != nil { + return errors.New("Socks|Client: Failed to parse user: " + err.Error()) + } + account := new(Account) + if err := json.Unmarshal(rawUser, account); err != nil { + return errors.New("Socks|Client: Failed to parse socks account: " + err.Error()) + } + user.Account = account + server.AddUser(user) + } + this.Servers[idx] = server + } + return nil +} + +func init() { + internal.RegisterOutboundConfig("socks", func() interface{} { return new(ClientConfig) }) +} diff --git a/proxy/socks/config.go b/proxy/socks/server_config.go similarity index 100% rename from proxy/socks/config.go rename to proxy/socks/server_config.go diff --git a/proxy/socks/config_json.go b/proxy/socks/server_config_json.go similarity index 91% rename from proxy/socks/config_json.go rename to proxy/socks/server_config_json.go index 73b2af91f..9550cae3e 100644 --- a/proxy/socks/config_json.go +++ b/proxy/socks/server_config_json.go @@ -17,14 +17,9 @@ const ( ) func (this *Config) UnmarshalJSON(data []byte) error { - type SocksAccount struct { - Username string `json:"user"` - Password string `json:"pass"` - } - type SocksConfig struct { AuthMethod string `json:"auth"` - Accounts []*SocksAccount `json:"accounts"` + Accounts []*Account `json:"accounts"` UDP bool `json:"udp"` Host *v2net.AddressJson `json:"ip"` Timeout int `json:"timeout"` diff --git a/proxy/socks/config_json_test.go b/proxy/socks/server_config_json_test.go similarity index 100% rename from proxy/socks/config_json_test.go rename to proxy/socks/server_config_json_test.go