Skip to content

Commit b673147

Browse files
committed
Add shadowsocks 2022 relay config
1 parent 3402341 commit b673147

File tree

1 file changed

+63
-36
lines changed

1 file changed

+63
-36
lines changed

infra/conf/shadowsocks.go

+63-36
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ func cipherFromString(c string) shadowsocks.CipherType {
3232
}
3333

3434
type ShadowsocksUserConfig struct {
35-
Cipher string `json:"method"`
36-
Password string `json:"password"`
37-
Level byte `json:"level"`
38-
Email string `json:"email"`
35+
Cipher string `json:"method"`
36+
Password string `json:"password"`
37+
Level byte `json:"level"`
38+
Email string `json:"email"`
39+
Address *Address `json:"address"`
40+
Port uint16 `json:"port"`
3941
}
4042

4143
type ShadowsocksServerConfig struct {
@@ -50,38 +52,7 @@ type ShadowsocksServerConfig struct {
5052

5153
func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
5254
if C.Contains(shadowaead_2022.List, v.Cipher) {
53-
if len(v.Users) > 0 {
54-
if v.Cipher == "" {
55-
return nil, newError("shadowsocks 2022 (multi-user): missing server method")
56-
}
57-
if !strings.Contains(v.Cipher, "aes") {
58-
return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
59-
}
60-
61-
config := new(shadowsocks_2022.MultiUserServerConfig)
62-
config.Method = v.Cipher
63-
64-
config.Key = v.Password
65-
config.Network = v.NetworkList.Build()
66-
67-
for _, user := range v.Users {
68-
if user.Cipher != "" {
69-
return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
70-
}
71-
config.Users = append(config.Users, &shadowsocks_2022.User{
72-
Key: user.Password,
73-
Email: user.Email,
74-
})
75-
}
76-
return config, nil
77-
}
78-
79-
config := new(shadowsocks_2022.ServerConfig)
80-
config.Method = v.Cipher
81-
config.Key = v.Password
82-
config.Network = v.NetworkList.Build()
83-
config.Email = v.Email
84-
return config, nil
55+
return buildShadowsocks2022(v)
8556
}
8657

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

103+
func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
104+
if len(v.Users) == 0 {
105+
config := new(shadowsocks_2022.ServerConfig)
106+
config.Method = v.Cipher
107+
config.Key = v.Password
108+
config.Network = v.NetworkList.Build()
109+
config.Email = v.Email
110+
return config, nil
111+
}
112+
113+
if v.Cipher == "" {
114+
return nil, newError("shadowsocks 2022 (multi-user): missing server method")
115+
}
116+
if !strings.Contains(v.Cipher, "aes") {
117+
return nil, newError("shadowsocks 2022 (multi-user): only blake3-aes-*-gcm methods are supported")
118+
}
119+
120+
if v.Users[0].Address == nil {
121+
config := new(shadowsocks_2022.MultiUserServerConfig)
122+
config.Method = v.Cipher
123+
config.Key = v.Password
124+
config.Network = v.NetworkList.Build()
125+
126+
for _, user := range v.Users {
127+
if user.Cipher != "" {
128+
return nil, newError("shadowsocks 2022 (multi-user): users must have empty method")
129+
}
130+
config.Users = append(config.Users, &shadowsocks_2022.User{
131+
Key: user.Password,
132+
Email: user.Email,
133+
})
134+
}
135+
return config, nil
136+
}
137+
138+
config := new(shadowsocks_2022.RelayServerConfig)
139+
config.Method = v.Cipher
140+
config.Key = v.Password
141+
config.Network = v.NetworkList.Build()
142+
for _, user := range v.Users {
143+
if user.Cipher != "" {
144+
return nil, newError("shadowsocks 2022 (relay): users must have empty method")
145+
}
146+
if user.Address == nil {
147+
return nil, newError("shadowsocks 2022 (relay): all users must have relay address")
148+
}
149+
config.Destinations = append(config.Destinations, &shadowsocks_2022.RelayDestination{
150+
Key: user.Password,
151+
Email: user.Email,
152+
Address: user.Address.Build(),
153+
Port: uint32(user.Port),
154+
})
155+
}
156+
return config, nil
157+
}
158+
132159
type ShadowsocksServerTarget struct {
133160
Address *Address `json:"address"`
134161
Port uint16 `json:"port"`

0 commit comments

Comments
 (0)