1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-30 15:57:35 -04:00
v2fly/tools/conf/shadowsocks.go

132 lines
3.3 KiB
Go
Raw Normal View History

2016-10-17 08:35:13 -04:00
package conf
import (
"errors"
"strings"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/protocol"
"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"`
OTA *bool `json:"ota"`
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *ShadowsocksServerConfig) Build() (*loader.TypedSettings, error) {
2016-10-17 08:35:13 -04:00
config := new(shadowsocks.ServerConfig)
2016-11-27 15:39:09 -05:00
config.UdpEnabled = v.UDP
2016-10-17 08:35:13 -04:00
2016-11-27 15:39:09 -05:00
if len(v.Password) == 0 {
2016-10-17 08:35:13 -04:00
return nil, errors.New("Shadowsocks password is not specified.")
}
account := &shadowsocks.Account{
2016-11-27 15:39:09 -05:00
Password: v.Password,
2016-11-02 11:21:20 -04:00
Ota: shadowsocks.Account_Auto,
2016-10-17 08:35:13 -04:00
}
2016-11-27 15:39:09 -05:00
if v.OTA != nil {
if *v.OTA {
account.Ota = shadowsocks.Account_Enabled
} else {
account.Ota = shadowsocks.Account_Disabled
}
}
2016-11-27 15:39:09 -05:00
cipher := strings.ToLower(v.Cipher)
2016-10-17 08:35:13 -04: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":
account.CipherType = shadowsocks.CipherType_CHACHA20_IEFT
default:
return nil, errors.New("Unknown cipher method: " + cipher)
}
config.User = &protocol.User{
2016-11-27 15:39:09 -05:00
Email: v.Email,
Level: uint32(v.Level),
2016-10-17 08:35:13 -04:00
Account: loader.NewTypedSettings(account),
}
return loader.NewTypedSettings(config), nil
}
2016-10-31 11:46:15 -04: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 11:21:20 -04:00
Ota bool `json:"ota"`
2016-10-31 11:46:15 -04:00
}
type ShadowsocksClientConfig struct {
Servers []*ShadowsocksServerTarget `json:"servers"`
}
2016-11-27 15:39:09 -05:00
func (v *ShadowsocksClientConfig) Build() (*loader.TypedSettings, error) {
2016-10-31 11:46:15 -04:00
config := new(shadowsocks.ClientConfig)
2016-11-27 15:39:09 -05:00
if len(v.Servers) == 0 {
2016-10-31 11:46:15 -04:00
return nil, errors.New("0 Shadowsocks server configured.")
}
2016-11-27 15:39:09 -05:00
serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
for idx, server := range v.Servers {
2016-10-31 11:46:15 -04:00
if server.Address == nil {
return nil, errors.New("Shadowsocks server address is not set.")
}
if server.Port == 0 {
return nil, errors.New("Invalid Shadowsocks port.")
}
if len(server.Password) == 0 {
return nil, errors.New("Shadowsocks password is not specified.")
}
account := &shadowsocks.Account{
Password: server.Password,
2016-11-02 11:21:20 -04:00
Ota: shadowsocks.Account_Enabled,
}
if !server.Ota {
account.Ota = shadowsocks.Account_Disabled
2016-10-31 11:46:15 -04: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":
account.CipherType = shadowsocks.CipherType_CHACHA20_IEFT
default:
return nil, errors.New("Unknown cipher method: " + cipher)
}
ss := &protocol.ServerEndpoint{
Address: server.Address.Build(),
Port: uint32(server.Port),
User: []*protocol.User{
{
Email: server.Email,
Account: loader.NewTypedSettings(account),
},
},
}
2016-10-31 11:46:47 -04:00
serverSpecs[idx] = ss
2016-10-31 11:46:15 -04:00
}
2016-11-04 20:01:07 -04:00
config.Server = serverSpecs
2016-10-31 11:46:15 -04:00
return loader.NewTypedSettings(config), nil
}