Skip to content

Commit

Permalink
Add shadowsocks 2022 relay config
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhan6665 committed Aug 9, 2022
1 parent 3402341 commit b673147
Showing 1 changed file with 63 additions and 36 deletions.
99 changes: 63 additions & 36 deletions infra/conf/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ func cipherFromString(c string) shadowsocks.CipherType {
}

type ShadowsocksUserConfig struct {
Cipher string `json:"method"`
Password string `json:"password"`
Level byte `json:"level"`
Email string `json:"email"`
Cipher string `json:"method"`
Password string `json:"password"`
Level byte `json:"level"`
Email string `json:"email"`
Address *Address `json:"address"`
Port uint16 `json:"port"`
}

type ShadowsocksServerConfig struct {
Expand All @@ -50,38 +52,7 @@ type ShadowsocksServerConfig struct {

func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
if C.Contains(shadowaead_2022.List, v.Cipher) {
if len(v.Users) > 0 {
if v.Cipher == "" {
return nil, newError("shadowsocks 2022 (multi-user): missing server method")
}
if !strings.Contains(v.Cipher, "aes") {
return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
}

config := new(shadowsocks_2022.MultiUserServerConfig)
config.Method = v.Cipher

config.Key = v.Password
config.Network = v.NetworkList.Build()

for _, user := range v.Users {
if user.Cipher != "" {
return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
}
config.Users = append(config.Users, &shadowsocks_2022.User{
Key: user.Password,
Email: user.Email,
})
}
return config, nil
}

config := new(shadowsocks_2022.ServerConfig)
config.Method = v.Cipher
config.Key = v.Password
config.Network = v.NetworkList.Build()
config.Email = v.Email
return config, nil
return buildShadowsocks2022(v)
}

config := new(shadowsocks.ServerConfig)
Expand Down Expand Up @@ -129,6 +100,62 @@ func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
return config, nil
}

func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
if len(v.Users) == 0 {
config := new(shadowsocks_2022.ServerConfig)
config.Method = v.Cipher
config.Key = v.Password
config.Network = v.NetworkList.Build()
config.Email = v.Email
return config, nil
}

if v.Cipher == "" {
return nil, newError("shadowsocks 2022 (multi-user): missing server method")
}
if !strings.Contains(v.Cipher, "aes") {
return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
}

if v.Users[0].Address == nil {
config := new(shadowsocks_2022.MultiUserServerConfig)
config.Method = v.Cipher
config.Key = v.Password
config.Network = v.NetworkList.Build()

for _, user := range v.Users {
if user.Cipher != "" {
return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
}
config.Users = append(config.Users, &shadowsocks_2022.User{
Key: user.Password,
Email: user.Email,
})
}
return config, nil
}

config := new(shadowsocks_2022.RelayServerConfig)
config.Method = v.Cipher
config.Key = v.Password
config.Network = v.NetworkList.Build()
for _, user := range v.Users {
if user.Cipher != "" {
return nil, newError("shadowsocks 2022 (relay): users must have empty method")
}
if user.Address == nil {
return nil, newError("shadowsocks 2022 (relay): all users must have relay address")
}
config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{
Key: user.Password,
Email: user.Email,
Address: user.Address.Build(),
Port: uint32(user.Port),
})
}
return config, nil
}

type ShadowsocksServerTarget struct {
Address *Address `json:"address"`
Port uint16 `json:"port"`
Expand Down

0 comments on commit b673147

Please sign in to comment.