1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/infra/conf/v4/shadowsocks.go

110 lines
3.0 KiB
Go
Raw Normal View History

package v4
2019-02-10 13:04:11 -05:00
import (
"github.com/golang/protobuf/proto"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/common/protocol"
"github.com/v2fly/v2ray-core/v4/common/serial"
2021-05-04 09:52:35 -04:00
"github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon"
2021-02-16 15:31:50 -05:00
"github.com/v2fly/v2ray-core/v4/proxy/shadowsocks"
2019-02-10 13:04:11 -05:00
)
type ShadowsocksServerConfig struct {
Cipher string `json:"method"`
Password string `json:"password"`
UDP bool `json:"udp"`
Level byte `json:"level"`
Email string `json:"email"`
NetworkList *cfgcommon.NetworkList `json:"network"`
IVCheck bool `json:"ivCheck"`
2019-02-10 13:04:11 -05:00
}
func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
config := new(shadowsocks.ServerConfig)
config.UdpEnabled = v.UDP
config.Network = v.NetworkList.Build()
if v.Password == "" {
2019-02-10 13:04:11 -05:00
return nil, newError("Shadowsocks password is not specified.")
}
account := &shadowsocks.Account{
Password: v.Password,
2021-03-05 03:02:41 -05:00
IvCheck: v.IVCheck,
2019-02-10 13:04:11 -05:00
}
account.CipherType = shadowsocks.CipherFromString(v.Cipher)
2019-02-10 13:04:11 -05:00
if account.CipherType == shadowsocks.CipherType_UNKNOWN {
return nil, newError("unknown cipher method: ", v.Cipher)
}
config.User = &protocol.User{
Email: v.Email,
Level: uint32(v.Level),
Account: serial.ToTypedMessage(account),
}
return config, nil
}
type ShadowsocksServerTarget struct {
Address *cfgcommon.Address `json:"address"`
Port uint16 `json:"port"`
Cipher string `json:"method"`
Password string `json:"password"`
Email string `json:"email"`
Ota bool `json:"ota"`
Level byte `json:"level"`
IVCheck bool `json:"ivCheck"`
2019-02-10 13:04:11 -05:00
}
type ShadowsocksClientConfig struct {
Servers []*ShadowsocksServerTarget `json:"servers"`
}
func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
config := new(shadowsocks.ClientConfig)
if len(v.Servers) == 0 {
return nil, newError("0 Shadowsocks server configured.")
}
serverSpecs := make([]*protocol.ServerEndpoint, len(v.Servers))
for idx, server := range v.Servers {
if server.Address == nil {
return nil, newError("Shadowsocks server address is not set.")
}
if server.Port == 0 {
return nil, newError("Invalid Shadowsocks port.")
}
if server.Password == "" {
2019-02-10 13:04:11 -05:00
return nil, newError("Shadowsocks password is not specified.")
}
account := &shadowsocks.Account{
Password: server.Password,
}
account.CipherType = shadowsocks.CipherFromString(server.Cipher)
2019-02-10 13:04:11 -05:00
if account.CipherType == shadowsocks.CipherType_UNKNOWN {
return nil, newError("unknown cipher method: ", server.Cipher)
}
2021-03-05 03:02:41 -05:00
account.IvCheck = server.IVCheck
2019-02-10 13:04:11 -05:00
ss := &protocol.ServerEndpoint{
Address: server.Address.Build(),
Port: uint32(server.Port),
User: []*protocol.User{
{
Level: uint32(server.Level),
Email: server.Email,
Account: serial.ToTypedMessage(account),
},
},
}
serverSpecs[idx] = ss
}
config.Server = serverSpecs
return config, nil
}