Skip to content

Commit 83c5370

Browse files
Config: Outbound proxy config no need to be nested (#5124)
Like eda8be6
1 parent 1a48453 commit 83c5370

File tree

8 files changed

+237
-21
lines changed

8 files changed

+237
-21
lines changed

infra/conf/http.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,27 @@ type HTTPRemoteConfig struct {
5151
}
5252

5353
type HTTPClientConfig struct {
54-
Servers []*HTTPRemoteConfig `json:"servers"`
55-
Headers map[string]string `json:"headers"`
54+
Address *Address `json:"address"`
55+
Port uint16 `json:"port"`
56+
Level uint32 `json:"level"`
57+
Email string `json:"email"`
58+
Username string `json:"user"`
59+
Password string `json:"pass"`
60+
Servers []*HTTPRemoteConfig `json:"servers"`
61+
Headers map[string]string `json:"headers"`
5662
}
5763

5864
func (v *HTTPClientConfig) Build() (proto.Message, error) {
5965
config := new(http.ClientConfig)
66+
if v.Address != nil {
67+
v.Servers = []*HTTPRemoteConfig{
68+
{
69+
Address: v.Address,
70+
Port: v.Port,
71+
Users: []json.RawMessage{{}},
72+
},
73+
}
74+
}
6075
config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
6176
for idx, serverConfig := range v.Servers {
6277
server := &protocol.ServerEndpoint{
@@ -65,12 +80,22 @@ func (v *HTTPClientConfig) Build() (proto.Message, error) {
6580
}
6681
for _, rawUser := range serverConfig.Users {
6782
user := new(protocol.User)
68-
if err := json.Unmarshal(rawUser, user); err != nil {
69-
return nil, errors.New("failed to parse HTTP user").Base(err).AtError()
83+
if v.Address != nil {
84+
user.Level = v.Level
85+
user.Email = v.Email
86+
} else {
87+
if err := json.Unmarshal(rawUser, user); err != nil {
88+
return nil, errors.New("failed to parse HTTP user").Base(err).AtError()
89+
}
7090
}
7191
account := new(HTTPAccount)
72-
if err := json.Unmarshal(rawUser, account); err != nil {
73-
return nil, errors.New("failed to parse HTTP account").Base(err).AtError()
92+
if v.Address != nil {
93+
account.Username = v.Username
94+
account.Password = v.Password
95+
} else {
96+
if err := json.Unmarshal(rawUser, account); err != nil {
97+
return nil, errors.New("failed to parse HTTP account").Base(err).AtError()
98+
}
7499
}
75100
user.Account = serial.ToTypedMessage(account.Build())
76101
server.User = append(server.User, user)

infra/conf/shadowsocks.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,44 @@ func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) {
162162
type ShadowsocksServerTarget struct {
163163
Address *Address `json:"address"`
164164
Port uint16 `json:"port"`
165+
Level byte `json:"level"`
166+
Email string `json:"email"`
165167
Cipher string `json:"method"`
166168
Password string `json:"password"`
167-
Email string `json:"email"`
168-
Level byte `json:"level"`
169169
IVCheck bool `json:"ivCheck"`
170170
UoT bool `json:"uot"`
171171
UoTVersion int `json:"uotVersion"`
172172
}
173173

174174
type ShadowsocksClientConfig struct {
175+
Address *Address `json:"address"`
176+
Port uint16 `json:"port"`
177+
Level byte `json:"level"`
178+
Email string `json:"email"`
179+
Cipher string `json:"method"`
180+
Password string `json:"password"`
181+
IVCheck bool `json:"ivCheck"`
182+
UoT bool `json:"uot"`
183+
UoTVersion int `json:"uotVersion"`
175184
Servers []*ShadowsocksServerTarget `json:"servers"`
176185
}
177186

178187
func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
188+
if v.Address != nil {
189+
v.Servers = []*ShadowsocksServerTarget{
190+
{
191+
Address: v.Address,
192+
Port: v.Port,
193+
Level: v.Level,
194+
Email: v.Email,
195+
Cipher: v.Cipher,
196+
Password: v.Password,
197+
IVCheck: v.IVCheck,
198+
UoT: v.UoT,
199+
UoTVersion: v.UoTVersion,
200+
},
201+
}
202+
}
179203
if len(v.Servers) == 0 {
180204
return nil, errors.New("0 Shadowsocks server configured.")
181205
}

infra/conf/socks.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,26 @@ type SocksRemoteConfig struct {
7070
}
7171

7272
type SocksClientConfig struct {
73-
Servers []*SocksRemoteConfig `json:"servers"`
73+
Address *Address `json:"address"`
74+
Port uint16 `json:"port"`
75+
Level uint32 `json:"level"`
76+
Email string `json:"email"`
77+
Username string `json:"user"`
78+
Password string `json:"pass"`
79+
Servers []*SocksRemoteConfig `json:"servers"`
7480
}
7581

7682
func (v *SocksClientConfig) Build() (proto.Message, error) {
7783
config := new(socks.ClientConfig)
84+
if v.Address != nil {
85+
v.Servers = []*SocksRemoteConfig{
86+
{
87+
Address: v.Address,
88+
Port: v.Port,
89+
Users: []json.RawMessage{{}},
90+
},
91+
}
92+
}
7893
config.Server = make([]*protocol.ServerEndpoint, len(v.Servers))
7994
for idx, serverConfig := range v.Servers {
8095
server := &protocol.ServerEndpoint{
@@ -83,12 +98,22 @@ func (v *SocksClientConfig) Build() (proto.Message, error) {
8398
}
8499
for _, rawUser := range serverConfig.Users {
85100
user := new(protocol.User)
86-
if err := json.Unmarshal(rawUser, user); err != nil {
87-
return nil, errors.New("failed to parse Socks user").Base(err).AtError()
101+
if v.Address != nil {
102+
user.Level = v.Level
103+
user.Email = v.Email
104+
} else {
105+
if err := json.Unmarshal(rawUser, user); err != nil {
106+
return nil, errors.New("failed to parse Socks user").Base(err).AtError()
107+
}
88108
}
89109
account := new(SocksAccount)
90-
if err := json.Unmarshal(rawUser, account); err != nil {
91-
return nil, errors.New("failed to parse socks account").Base(err).AtError()
110+
if v.Address != nil {
111+
account.Username = v.Username
112+
account.Password = v.Password
113+
} else {
114+
if err := json.Unmarshal(rawUser, account); err != nil {
115+
return nil, errors.New("failed to parse socks account").Base(err).AtError()
116+
}
92117
}
93118
user.Account = serial.ToTypedMessage(account.Build())
94119
server.User = append(server.User, user)

infra/conf/socks_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,36 @@ func TestSocksOutboundConfig(t *testing.T) {
8686
},
8787
},
8888
},
89+
{
90+
Input: `{
91+
"address": "127.0.0.1",
92+
"port": 1234,
93+
"user": "test user",
94+
"pass": "test pass",
95+
"email": "[email protected]"
96+
}`,
97+
Parser: loadJSON(creator),
98+
Output: &socks.ClientConfig{
99+
Server: []*protocol.ServerEndpoint{
100+
{
101+
Address: &net.IPOrDomain{
102+
Address: &net.IPOrDomain_Ip{
103+
Ip: []byte{127, 0, 0, 1},
104+
},
105+
},
106+
Port: 1234,
107+
User: []*protocol.User{
108+
{
109+
110+
Account: serial.ToTypedMessage(&socks.Account{
111+
Username: "test user",
112+
Password: "test pass",
113+
}),
114+
},
115+
},
116+
},
117+
},
118+
},
119+
},
89120
})
90121
}

infra/conf/trojan.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,37 @@ import (
2020
type TrojanServerTarget struct {
2121
Address *Address `json:"address"`
2222
Port uint16 `json:"port"`
23-
Password string `json:"password"`
24-
Email string `json:"email"`
2523
Level byte `json:"level"`
24+
Email string `json:"email"`
25+
Password string `json:"password"`
2626
Flow string `json:"flow"`
2727
}
2828

2929
// TrojanClientConfig is configuration of trojan servers
3030
type TrojanClientConfig struct {
31+
Address *Address `json:"address"`
32+
Port uint16 `json:"port"`
33+
Level byte `json:"level"`
34+
Email string `json:"email"`
35+
Password string `json:"password"`
36+
Flow string `json:"flow"`
3137
Servers []*TrojanServerTarget `json:"servers"`
3238
}
3339

3440
// Build implements Buildable
3541
func (c *TrojanClientConfig) Build() (proto.Message, error) {
42+
if c.Address != nil {
43+
c.Servers = []*TrojanServerTarget{
44+
{
45+
Address: c.Address,
46+
Port: c.Port,
47+
Level: c.Level,
48+
Email: c.Email,
49+
Password: c.Password,
50+
Flow: c.Flow,
51+
},
52+
}
53+
}
3654
if len(c.Servers) == 0 {
3755
return nil, errors.New("0 Trojan server configured.")
3856
}

infra/conf/vless_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,39 @@ func TestVLessOutbound(t *testing.T) {
5757
},
5858
},
5959
},
60+
{
61+
Input: `{
62+
"address": "example.com",
63+
"port": 443,
64+
"id": "27848739-7e62-4138-9fd3-098a63964b6b",
65+
"flow": "xtls-rprx-vision-udp443",
66+
"encryption": "none",
67+
"level": 0
68+
}`,
69+
Parser: loadJSON(creator),
70+
Output: &outbound.Config{
71+
Vnext: []*protocol.ServerEndpoint{
72+
{
73+
Address: &net.IPOrDomain{
74+
Address: &net.IPOrDomain_Domain{
75+
Domain: "example.com",
76+
},
77+
},
78+
Port: 443,
79+
User: []*protocol.User{
80+
{
81+
Account: serial.ToTypedMessage(&vless.Account{
82+
Id: "27848739-7e62-4138-9fd3-098a63964b6b",
83+
Flow: "xtls-rprx-vision-udp443",
84+
Encryption: "none",
85+
}),
86+
Level: 0,
87+
},
88+
},
89+
},
90+
},
91+
},
92+
},
6093
})
6194
}
6295

infra/conf/vmess.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,28 @@ type VMessOutboundTarget struct {
117117
}
118118

119119
type VMessOutboundConfig struct {
120-
Receivers []*VMessOutboundTarget `json:"vnext"`
120+
Address *Address `json:"address"`
121+
Port uint16 `json:"port"`
122+
Level uint32 `json:"level"`
123+
Email string `json:"email"`
124+
ID string `json:"id"`
125+
Security string `json:"security"`
126+
Experiments string `json:"experiments"`
127+
Receivers []*VMessOutboundTarget `json:"vnext"`
121128
}
122129

123130
// Build implements Buildable
124131
func (c *VMessOutboundConfig) Build() (proto.Message, error) {
125132
config := new(outbound.Config)
126-
133+
if c.Address != nil {
134+
c.Receivers = []*VMessOutboundTarget{
135+
{
136+
Address: c.Address,
137+
Port: c.Port,
138+
Users: []json.RawMessage{{}},
139+
},
140+
}
141+
}
127142
if len(c.Receivers) == 0 {
128143
return nil, errors.New("0 VMess receiver configured")
129144
}
@@ -141,12 +156,23 @@ func (c *VMessOutboundConfig) Build() (proto.Message, error) {
141156
}
142157
for _, rawUser := range rec.Users {
143158
user := new(protocol.User)
144-
if err := json.Unmarshal(rawUser, user); err != nil {
145-
return nil, errors.New("invalid VMess user").Base(err)
159+
if c.Address != nil {
160+
user.Level = c.Level
161+
user.Email = c.Email
162+
} else {
163+
if err := json.Unmarshal(rawUser, user); err != nil {
164+
return nil, errors.New("invalid VMess user").Base(err)
165+
}
146166
}
147167
account := new(VMessAccount)
148-
if err := json.Unmarshal(rawUser, account); err != nil {
149-
return nil, errors.New("invalid VMess user").Base(err)
168+
if c.Address != nil {
169+
account.ID = c.ID
170+
account.Security = c.Security
171+
account.Experiments = c.Experiments
172+
} else {
173+
if err := json.Unmarshal(rawUser, account); err != nil {
174+
return nil, errors.New("invalid VMess user").Base(err)
175+
}
150176
}
151177

152178
u, err := uuid.ParseString(account.ID)

infra/conf/vmess_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ func TestVMessOutbound(t *testing.T) {
5858
},
5959
},
6060
},
61+
{
62+
Input: `{
63+
"address": "127.0.0.1",
64+
"port": 80,
65+
"id": "e641f5ad-9397-41e3-bf1a-e8740dfed019",
66+
"email": "[email protected]",
67+
"level": 255
68+
}`,
69+
Parser: loadJSON(creator),
70+
Output: &outbound.Config{
71+
Receiver: []*protocol.ServerEndpoint{
72+
{
73+
Address: &net.IPOrDomain{
74+
Address: &net.IPOrDomain_Ip{
75+
Ip: []byte{127, 0, 0, 1},
76+
},
77+
},
78+
Port: 80,
79+
User: []*protocol.User{
80+
{
81+
82+
Level: 255,
83+
Account: serial.ToTypedMessage(&vmess.Account{
84+
Id: "e641f5ad-9397-41e3-bf1a-e8740dfed019",
85+
SecuritySettings: &protocol.SecurityConfig{
86+
Type: protocol.SecurityType_AUTO,
87+
},
88+
}),
89+
},
90+
},
91+
},
92+
},
93+
},
94+
},
6195
})
6296
}
6397

0 commit comments

Comments
 (0)