2016-10-17 12:35:13 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2017-01-03 00:37:27 +00:00
|
|
|
|
2016-10-17 12:35:13 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
2016-12-15 10:51:09 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-10-17 12:35:13 +00:00
|
|
|
"v2ray.com/core/proxy/shadowsocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ShadowsocksServerConfig struct {
|
|
|
|
Cipher string `json:"method"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
UDP bool `json:"udp"`
|
|
|
|
Level byte `json:"level"`
|
|
|
|
Email string `json:"email"`
|
2016-11-25 15:46:59 +00:00
|
|
|
OTA *bool `json:"ota"`
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
func (v *ShadowsocksServerConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-17 12:35:13 +00:00
|
|
|
config := new(shadowsocks.ServerConfig)
|
2016-11-27 20:39:09 +00:00
|
|
|
config.UdpEnabled = v.UDP
|
2016-10-17 12:35:13 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
if len(v.Password) == 0 {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("Shadowsocks password is not specified.")
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
account := &shadowsocks.Account{
|
2016-11-27 20:39:09 +00:00
|
|
|
Password: v.Password,
|
2016-11-02 15:21:20 +00:00
|
|
|
Ota: shadowsocks.Account_Auto,
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.OTA != nil {
|
|
|
|
if *v.OTA {
|
2016-11-25 15:46:59 +00:00
|
|
|
account.Ota = shadowsocks.Account_Enabled
|
|
|
|
} else {
|
|
|
|
account.Ota = shadowsocks.Account_Disabled
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 20:39:09 +00:00
|
|
|
cipher := strings.ToLower(v.Cipher)
|
2016-10-17 12:35:13 +00:00
|
|
|
switch cipher {
|
|
|
|
case "aes-256-cfb":
|
|
|
|
account.CipherType = shadowsocks.CipherType_AES_256_CFB
|
|
|
|
case "aes-128-cfb":
|
|
|
|
account.CipherType = shadowsocks.CipherType_AES_128_CFB
|
|
|
|
case "chacha20":
|
|
|
|
account.CipherType = shadowsocks.CipherType_CHACHA20
|
|
|
|
case "chacha20-ietf":
|
2017-01-03 00:37:27 +00:00
|
|
|
account.CipherType = shadowsocks.CipherType_CHACHA20_IETF
|
2016-10-17 12:35:13 +00:00
|
|
|
default:
|
2017-04-09 13:04:04 +00:00
|
|
|
return nil, newError("unknown cipher method: " + cipher)
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.User = &protocol.User{
|
2016-11-27 20:39:09 +00:00
|
|
|
Email: v.Email,
|
|
|
|
Level: uint32(v.Level),
|
2016-12-15 10:51:09 +00:00
|
|
|
Account: serial.ToTypedMessage(account),
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-17 12:35:13 +00:00
|
|
|
}
|
2016-10-31 15:46:15 +00:00
|
|
|
|
|
|
|
type ShadowsocksServerTarget struct {
|
|
|
|
Address *Address `json:"address"`
|
|
|
|
Port uint16 `json:"port"`
|
|
|
|
Cipher string `json:"method"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Email string `json:"email"`
|
2016-11-02 15:21:20 +00:00
|
|
|
Ota bool `json:"ota"`
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ShadowsocksClientConfig struct {
|
|
|
|
Servers []*ShadowsocksServerTarget `json:"servers"`
|
|
|
|
}
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
func (v *ShadowsocksClientConfig) Build() (*serial.TypedMessage, error) {
|
2016-10-31 15:46:15 +00:00
|
|
|
config := new(shadowsocks.ClientConfig)
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
if len(v.Servers) == 0 {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("0 Shadowsocks server configured.")
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
|
|
|
|
for idx, server := range v.Servers {
|
2016-10-31 15:46:15 +00:00
|
|
|
if server.Address == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("Shadowsocks server address is not set.")
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
if server.Port == 0 {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("Invalid Shadowsocks port.")
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
if len(server.Password) == 0 {
|
2017-04-08 23:43:25 +00:00
|
|
|
return nil, newError("Shadowsocks password is not specified.")
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
account := &shadowsocks.Account{
|
|
|
|
Password: server.Password,
|
2016-11-02 15:21:20 +00:00
|
|
|
Ota: shadowsocks.Account_Enabled,
|
|
|
|
}
|
|
|
|
if !server.Ota {
|
|
|
|
account.Ota = shadowsocks.Account_Disabled
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
cipher := strings.ToLower(server.Cipher)
|
|
|
|
switch cipher {
|
|
|
|
case "aes-256-cfb":
|
|
|
|
account.CipherType = shadowsocks.CipherType_AES_256_CFB
|
|
|
|
case "aes-128-cfb":
|
|
|
|
account.CipherType = shadowsocks.CipherType_AES_128_CFB
|
|
|
|
case "chacha20":
|
|
|
|
account.CipherType = shadowsocks.CipherType_CHACHA20
|
|
|
|
case "chacha20-ietf":
|
2017-01-03 00:37:27 +00:00
|
|
|
account.CipherType = shadowsocks.CipherType_CHACHA20_IETF
|
2016-10-31 15:46:15 +00:00
|
|
|
default:
|
2017-04-09 13:04:04 +00:00
|
|
|
return nil, newError("unknown cipher method: " + cipher)
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ss := &protocol.ServerEndpoint{
|
|
|
|
Address: server.Address.Build(),
|
|
|
|
Port: uint32(server.Port),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Email: server.Email,
|
2016-12-15 10:51:09 +00:00
|
|
|
Account: serial.ToTypedMessage(account),
|
2016-10-31 15:46:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-10-31 15:46:47 +00:00
|
|
|
|
|
|
|
serverSpecs[idx] = ss
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-05 00:01:07 +00:00
|
|
|
config.Server = serverSpecs
|
|
|
|
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.ToTypedMessage(config), nil
|
2016-10-31 15:46:15 +00:00
|
|
|
}
|