Skip to content

Commit 673cc92

Browse files
committed
🗃️ clientgroups: reorganize config
1 parent dc2e9d1 commit 673cc92

File tree

2 files changed

+88
-77
lines changed

2 files changed

+88
-77
lines changed

clientgroups/clientgroups.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,29 @@ const (
3535
PolicyMinMaxLatency ClientSelectionPolicy = "min-max-latency"
3636
)
3737

38-
// ClientGroupConfig is the configuration for a client group.
39-
type ClientGroupConfig struct {
40-
// Name is the name of the client group.
41-
Name string `json:"name"`
42-
43-
// TCPPolicy is the client selection policy for TCP clients.
38+
// ClientSelectionConfig is the configuration for client selection.
39+
type ClientSelectionConfig[PC TCPConnectivityProbeConfig | UDPConnectivityProbeConfig] struct {
40+
// Policy is the client selection policy.
4441
// See [ClientSelectionPolicy] for available policies.
45-
TCPPolicy ClientSelectionPolicy `json:"tcpPolicy"`
42+
Policy ClientSelectionPolicy `json:"policy"`
4643

47-
// UDPPolicy is the client selection policy for UDP clients.
48-
// See [ClientSelectionPolicy] for available policies.
49-
UDPPolicy ClientSelectionPolicy `json:"udpPolicy"`
44+
// Clients is the list of clients in the group, represented by their names.
45+
Clients []string `json:"clients"`
5046

51-
// TCPClients is the list of TCP clients in the group, represented by their names.
52-
TCPClients []string `json:"tcpClients"`
47+
// Probe is the configuration for connectivity probes.
48+
Probe PC `json:"probe"`
49+
}
5350

54-
// UDPClients is the list of UDP clients in the group, represented by their names.
55-
UDPClients []string `json:"udpClients"`
51+
// ClientGroupConfig is the configuration for a client group.
52+
type ClientGroupConfig struct {
53+
// Name is the name of the client group.
54+
Name string `json:"name"`
5655

57-
// TCPConnectivityProbe is the configuration for TCP connectivity probes.
58-
TCPConnectivityProbe TCPConnectivityProbeConfig `json:"tcpConnectivityProbe"`
56+
// TCP is the client selection configuration for TCP clients.
57+
TCP ClientSelectionConfig[TCPConnectivityProbeConfig] `json:"tcp"`
5958

60-
// UDPConnectivityProbe is the configuration for UDP connectivity probes.
61-
UDPConnectivityProbe UDPConnectivityProbeConfig `json:"udpConnectivityProbe"`
59+
// UDP is the client selection configuration for UDP clients.
60+
UDP ClientSelectionConfig[UDPConnectivityProbeConfig] `json:"udp"`
6261
}
6362

6463
// AddClientGroup creates a client group from the configuration and adds it to the client maps.
@@ -68,13 +67,13 @@ func (c *ClientGroupConfig) AddClientGroup(
6867
udpClientByName map[string]zerocopy.UDPClient,
6968
addProbeService func(shadowsocks.Service),
7069
) error {
71-
if len(c.TCPClients) == 0 && len(c.UDPClients) == 0 {
70+
if len(c.TCP.Clients) == 0 && len(c.UDP.Clients) == 0 {
7271
return errors.New("empty client group")
7372
}
7473

75-
if len(c.TCPClients) > 0 {
76-
clients := make([]tcpClient, len(c.TCPClients))
77-
for i, name := range c.TCPClients {
74+
if len(c.TCP.Clients) > 0 {
75+
clients := make([]tcpClient, len(c.TCP.Clients))
76+
for i, name := range c.TCP.Clients {
7877
client, ok := tcpClientByName[name]
7978
if !ok {
8079
return fmt.Errorf("TCP client not found: %q", name)
@@ -86,30 +85,30 @@ func (c *ClientGroupConfig) AddClientGroup(
8685
group zerocopy.TCPClient
8786
service *ProbeService[tcpClient]
8887
)
89-
switch c.TCPPolicy {
88+
switch c.TCP.Policy {
9089
case PolicyRoundRobin:
9190
group = newRoundRobinTCPClientGroup(clients)
9291
case PolicyRandom:
9392
group = newRandomTCPClientGroup(clients)
9493
case PolicyAvailability:
95-
group, service = c.TCPConnectivityProbe.newAvailabilityClientGroup(c.Name, logger, clients)
94+
group, service = c.TCP.Probe.newAvailabilityClientGroup(c.Name, logger, clients)
9695
addProbeService(service)
9796
case PolicyLatency:
98-
group, service = c.TCPConnectivityProbe.newLatencyClientGroup(c.Name, logger, clients)
97+
group, service = c.TCP.Probe.newLatencyClientGroup(c.Name, logger, clients)
9998
addProbeService(service)
10099
case PolicyMinMaxLatency:
101-
group, service = c.TCPConnectivityProbe.newMinMaxLatencyClientGroup(c.Name, logger, clients)
100+
group, service = c.TCP.Probe.newMinMaxLatencyClientGroup(c.Name, logger, clients)
102101
addProbeService(service)
103102
default:
104-
return fmt.Errorf("unknown TCP client selection policy: %q", c.TCPPolicy)
103+
return fmt.Errorf("unknown TCP client selection policy: %q", c.TCP.Policy)
105104
}
106105
tcpClientByName[c.Name] = group
107106
}
108107

109-
if len(c.UDPClients) > 0 {
110-
clients := make([]zerocopy.UDPClient, len(c.UDPClients))
108+
if len(c.UDP.Clients) > 0 {
109+
clients := make([]zerocopy.UDPClient, len(c.UDP.Clients))
111110
var info zerocopy.UDPClientInfo
112-
for i, name := range c.UDPClients {
111+
for i, name := range c.UDP.Clients {
113112
client, ok := udpClientByName[name]
114113
if !ok {
115114
return fmt.Errorf("UDP client not found: %q", name)
@@ -122,22 +121,22 @@ func (c *ClientGroupConfig) AddClientGroup(
122121
group zerocopy.UDPClient
123122
service *ProbeService[zerocopy.UDPClient]
124123
)
125-
switch c.UDPPolicy {
124+
switch c.UDP.Policy {
126125
case PolicyRoundRobin:
127126
group = newRoundRobinUDPClientGroup(clients, info)
128127
case PolicyRandom:
129128
group = newRandomUDPClientGroup(clients, info)
130129
case PolicyAvailability:
131-
group, service = c.UDPConnectivityProbe.newAvailabilityClientGroup(c.Name, logger, clients, info)
130+
group, service = c.UDP.Probe.newAvailabilityClientGroup(c.Name, logger, clients, info)
132131
addProbeService(service)
133132
case PolicyLatency:
134-
group, service = c.UDPConnectivityProbe.newLatencyClientGroup(c.Name, logger, clients, info)
133+
group, service = c.UDP.Probe.newLatencyClientGroup(c.Name, logger, clients, info)
135134
addProbeService(service)
136135
case PolicyMinMaxLatency:
137-
group, service = c.UDPConnectivityProbe.newMinMaxLatencyClientGroup(c.Name, logger, clients, info)
136+
group, service = c.UDP.Probe.newMinMaxLatencyClientGroup(c.Name, logger, clients, info)
138137
addProbeService(service)
139138
default:
140-
return fmt.Errorf("unknown UDP client selection policy: %q", c.UDPPolicy)
139+
return fmt.Errorf("unknown UDP client selection policy: %q", c.UDP.Policy)
141140
}
142141
udpClientByName[c.Name] = group
143142
}

docs/config.json

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -412,55 +412,67 @@
412412
"clientGroups": [
413413
{
414414
"name": "ss-2022-round-robin",
415-
"tcpPolicy": "round-robin",
416-
"udpPolicy": "round-robin",
417-
"tcpClients": [
418-
"ss-2022-a",
419-
"ss-2022-b"
420-
],
421-
"udpClients": [
422-
"ss-2022-a",
423-
"ss-2022-b"
424-
]
415+
"tcp": {
416+
"policy": "round-robin",
417+
"clients": [
418+
"ss-2022-a",
419+
"ss-2022-b"
420+
]
421+
},
422+
"udp": {
423+
"policy": "round-robin",
424+
"clients": [
425+
"ss-2022-a",
426+
"ss-2022-b"
427+
]
428+
}
425429
},
426430
{
427431
"name": "ss-2022-random",
428-
"tcpPolicy": "random",
429-
"udpPolicy": "random",
430-
"tcpClients": [
431-
"ss-2022-a",
432-
"ss-2022-b"
433-
],
434-
"udpClients": [
435-
"ss-2022-a",
436-
"ss-2022-b"
437-
]
432+
"tcp": {
433+
"policy": "random",
434+
"clients": [
435+
"ss-2022-a",
436+
"ss-2022-b"
437+
]
438+
},
439+
"udp": {
440+
"policy": "random",
441+
"clients": [
442+
"ss-2022-a",
443+
"ss-2022-b"
444+
]
445+
}
438446
},
439447
{
440448
"name": "ss-2022-availability",
441-
"tcpPolicy": "availability",
442-
"udpPolicy": "availability",
443-
"tcpClients": [
444-
"ss-2022-a",
445-
"ss-2022-b"
446-
],
447-
"udpClients": [
448-
"ss-2022-a",
449-
"ss-2022-b"
450-
],
451-
"tcpConnectivityProbe": {
452-
"timeout": "5s",
453-
"interval": "30s",
454-
"concurrency": 32,
455-
"address": "clients3.google.com:80",
456-
"escapedPath": "/generate_204",
457-
"host": "clients3.google.com"
449+
"tcp": {
450+
"policy": "availability",
451+
"clients": [
452+
"ss-2022-a",
453+
"ss-2022-b"
454+
],
455+
"probe": {
456+
"timeout": "5s",
457+
"interval": "30s",
458+
"concurrency": 32,
459+
"address": "clients3.google.com:80",
460+
"escapedPath": "/generate_204",
461+
"host": "clients3.google.com"
462+
}
458463
},
459-
"udpConnectivityProbe": {
460-
"timeout": "5s",
461-
"interval": "30s",
462-
"concurrency": 32,
463-
"address": "[2606:4700:4700::1111]:53"
464+
"udp": {
465+
"policy": "availability",
466+
"clients": [
467+
"ss-2022-a",
468+
"ss-2022-b"
469+
],
470+
"probe": {
471+
"timeout": "5s",
472+
"interval": "30s",
473+
"concurrency": 32,
474+
"address": "[2606:4700:4700::1111]:53"
475+
}
464476
}
465477
}
466478
],

0 commit comments

Comments
 (0)