mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
eccf2e71bd
A client's devices all carry the client's email, and the core removes a WireGuard user by email, one peer at a time. The hot diff therefore removed every peer of the email and added the survivors back, and a client that left for good counted as a user whose sessions had to be dropped. Measured on a running panel (Xray 26.11.9, kernel WireGuard clients): - disabling or deleting one device cut the client's other devices for about 15 s, the time a client waits before it handshakes again with a peer the server forgot; - disabling or deleting a connected WireGuard client restarted the whole core with the default restartXrayOnClientDisable, taking every inbound down. The diff is now per public key: a peer that stays is never touched, a changed peer is updated in place, and a removed one goes through RemoveWireguardPeer, which relabels that single peer and removes the label. A WireGuard session ends with its peer, so its removal no longer asks for the restart. After the change the other devices and other clients of the inbound keep their tunnels with no gap, and the core keeps its pid.
138 lines
4.7 KiB
Go
138 lines
4.7 KiB
Go
package xray
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
|
|
)
|
|
|
|
func hotConfigWithClients(clients string) *Config {
|
|
cfg := makeHotConfig()
|
|
for i := range cfg.InboundConfigs {
|
|
if cfg.InboundConfigs[i].Tag == "inbound-1080" {
|
|
cfg.InboundConfigs[i].Settings = json_util.RawMessage(`{"clients":` + clients + `}`)
|
|
}
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// Shadowsocks reaches the drop guard through the inbound's settings.clients compare,
|
|
// hysteria through its per-user diff; either way a dropped client must be reported.
|
|
func TestHotDiffDropsUsersOnShadowsocksAndHysteria(t *testing.T) {
|
|
for _, protocol := range []string{"shadowsocks", "hysteria"} {
|
|
t.Run(protocol, func(t *testing.T) {
|
|
withClients := func(clients string) *Config {
|
|
cfg := makeHotConfig()
|
|
ib := &cfg.InboundConfigs[1]
|
|
ib.Protocol = protocol
|
|
ib.Settings = json_util.RawMessage(`{"clients":` + clients + `}`)
|
|
return cfg
|
|
}
|
|
both := `[{"email":"a@x","password":"pa"},{"email":"b@x","password":"pb"}]`
|
|
onlyA := `[{"email":"a@x","password":"pa"}]`
|
|
|
|
diff, ok := ComputeHotDiff(withClients(both), withClients(onlyA))
|
|
if !ok {
|
|
t.Fatal("a dropped client must stay API-applicable")
|
|
}
|
|
if !diff.DropsUsers() {
|
|
t.Fatalf("DropsUsers = false for a dropped %s client (removed=%+v added=%+v dropped=%+v)",
|
|
protocol, diff.RemovedUsers, diff.AddedUsers, diff.DroppedClients)
|
|
}
|
|
|
|
edited, ok := ComputeHotDiff(withClients(both), withClients(`[{"email":"a@x","password":"pa"},{"email":"b@x","password":"pb","level":1}]`))
|
|
if !ok {
|
|
t.Fatal("a client edit must stay API-applicable")
|
|
}
|
|
if edited.DropsUsers() {
|
|
t.Fatal("an edited client is still served")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A disable or a delete takes the client out of the generated config; an edit
|
|
// keeps the email and re-adds it. Only the first leaves sessions running.
|
|
func TestHotDiffDropsUsers(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
old string
|
|
new string
|
|
want bool
|
|
}{
|
|
{
|
|
"client taken out of the config",
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
|
|
`[]`,
|
|
true,
|
|
},
|
|
{
|
|
"edited in place",
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","limitIp":5,"enable":true}]`,
|
|
false,
|
|
},
|
|
{
|
|
"added",
|
|
`[]`,
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
|
|
false,
|
|
},
|
|
{
|
|
"renamed",
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
|
|
`[{"email":"b@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
|
|
true,
|
|
},
|
|
{
|
|
"one dropped, one edited",
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true},{"email":"b@x","id":"22222222-2222-2222-2222-222222222222","enable":true}]`,
|
|
`[{"email":"b@x","id":"22222222-2222-2222-2222-222222222222","limitIp":5,"enable":true}]`,
|
|
true,
|
|
},
|
|
{
|
|
"unchanged",
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
|
|
`[{"email":"a@x","id":"11111111-1111-1111-1111-111111111111","enable":true}]`,
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
diff, ok := ComputeHotDiff(hotConfigWithClients(tc.old), hotConfigWithClients(tc.new))
|
|
if !ok {
|
|
t.Fatalf("diff of %s -> %s must be API-applicable", tc.old, tc.new)
|
|
}
|
|
if got := diff.DropsUsers(); got != tc.want {
|
|
t.Fatalf("DropsUsers = %v, want %v (removed=%+v added=%+v)",
|
|
got, tc.want, diff.RemovedUsers, diff.AddedUsers)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Removing a WireGuard peer ends its session in the core, so a dropped device or client
|
|
// must not ask for the restart that strands other protocols' live connections need.
|
|
func TestHotDiffWireguardPeerRemovalDropsNoLiveSession(t *testing.T) {
|
|
const kept = `{"publicKey":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=","allowedIPs":["10.0.0.2/32"],"email":"owner"}`
|
|
const gone = `{"publicKey":"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=","allowedIPs":["10.0.0.3/32"],"email":"owner"}`
|
|
withPeers := func(peers string) *Config {
|
|
cfg := makeHotConfig()
|
|
ib := &cfg.InboundConfigs[1]
|
|
ib.Protocol = "wireguard"
|
|
ib.Settings = json_util.RawMessage(`{"secretKey":"s","peers":[` + peers + `]}`)
|
|
return cfg
|
|
}
|
|
diff, ok := ComputeHotDiff(withPeers(kept+","+gone), withPeers(kept))
|
|
if !ok {
|
|
t.Fatal("a removed peer must stay API-applicable")
|
|
}
|
|
if len(diff.RemovedUsers) != 1 || diff.RemovedUsers[0].User["publicKey"] != "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=" || len(diff.AddedUsers) != 0 {
|
|
t.Fatalf("removed=%+v added=%+v, want the one removed peer and nothing re-added", diff.RemovedUsers, diff.AddedUsers)
|
|
}
|
|
if diff.DropsUsers() {
|
|
t.Fatal("DropsUsers = true for a removed WireGuard peer: the core would be restarted for nothing")
|
|
}
|
|
}
|