1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/tools/conf/socks.go

96 lines
2.5 KiB
Go
Raw Normal View History

2016-10-17 08:35:13 -04:00
package conf
import (
"encoding/json"
2017-01-17 04:44:24 -05:00
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-10-17 08:35:13 -04:00
"v2ray.com/core/common/protocol"
2016-12-15 05:51:09 -05:00
"v2ray.com/core/common/serial"
2016-10-17 08:35:13 -04:00
"v2ray.com/core/proxy/socks"
)
type SocksAccount struct {
Username string `json:"user"`
Password string `json:"pass"`
}
2016-11-27 15:39:09 -05:00
func (v *SocksAccount) Build() *socks.Account {
2016-10-17 08:35:13 -04:00
return &socks.Account{
2016-11-27 15:39:09 -05:00
Username: v.Username,
Password: v.Password,
2016-10-17 08:35:13 -04:00
}
}
const (
AuthMethodNoAuth = "noauth"
AuthMethodUserPass = "password"
)
type SocksServerConfig struct {
AuthMethod string `json:"auth"`
Accounts []*SocksAccount `json:"accounts"`
UDP bool `json:"udp"`
Host *Address `json:"ip"`
Timeout uint32 `json:"timeout"`
}
2016-12-15 05:51:09 -05:00
func (v *SocksServerConfig) Build() (*serial.TypedMessage, error) {
2016-10-17 08:35:13 -04:00
config := new(socks.ServerConfig)
2016-11-27 15:39:09 -05:00
if v.AuthMethod == AuthMethodNoAuth {
2016-10-17 08:35:13 -04:00
config.AuthType = socks.AuthType_NO_AUTH
2016-11-27 15:39:09 -05:00
} else if v.AuthMethod == AuthMethodUserPass {
2016-10-17 08:35:13 -04:00
config.AuthType = socks.AuthType_PASSWORD
} else {
2017-01-17 04:44:24 -05:00
return nil, errors.New("Config: Unknown socks auth method: " + v.AuthMethod)
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
if len(v.Accounts) > 0 {
config.Accounts = make(map[string]string, len(v.Accounts))
for _, account := range v.Accounts {
2016-10-17 08:35:13 -04:00
config.Accounts[account.Username] = account.Password
}
}
2016-11-27 15:39:09 -05:00
config.UdpEnabled = v.UDP
if v.Host != nil {
config.Address = v.Host.Build()
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
config.Timeout = v.Timeout
2016-12-15 05:51:09 -05:00
return serial.ToTypedMessage(config), nil
2016-10-17 08:35:13 -04:00
}
type SocksRemoteConfig struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
Users []json.RawMessage `json:"users"`
}
type SocksClientConfig struct {
Servers []*SocksRemoteConfig `json:"servers"`
}
2016-12-15 05:51:09 -05:00
func (v *SocksClientConfig) Build() (*serial.TypedMessage, error) {
2016-10-17 08:35:13 -04:00
config := new(socks.ClientConfig)
2016-11-27 15:39:09 -05:00
config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
for idx, serverConfig := range v.Servers {
2016-10-17 08:35:13 -04:00
server := &protocol.ServerEndpoint{
Address: serverConfig.Address.Build(),
Port: uint32(serverConfig.Port),
}
for _, rawUser := range serverConfig.Users {
user := new(protocol.User)
if err := json.Unmarshal(rawUser, user); err != nil {
2017-01-17 04:44:24 -05:00
return nil, errors.Base(err).Message("Config: Failed to parse Socks user.")
2016-10-17 08:35:13 -04:00
}
account := new(SocksAccount)
if err := json.Unmarshal(rawUser, account); err != nil {
2017-01-17 04:44:24 -05:00
return nil, errors.Base(err).Message("Config: Failed to parse socks account.")
2016-10-17 08:35:13 -04:00
}
2016-12-15 05:51:09 -05:00
user.Account = serial.ToTypedMessage(account.Build())
2016-10-17 08:35:13 -04:00
server.User = append(server.User, user)
}
config.Server[idx] = server
}
2016-12-15 05:51:09 -05:00
return serial.ToTypedMessage(config), nil
2016-10-17 08:35:13 -04:00
}