Files
mirror-3x-ui/internal/web/runtime/local_wireguard_devices_test.go
T
Artem K f25cecfb40 feat(wireguard): support multiple devices per client (#7143)
A WireGuard client had one key pair and one tunnel address, so an operator who gave the same config to a phone and a laptop saw the two fight over the peer endpoint.

One client can now own extra devices, up to a per-client limit: each has its own keys and address, shares the client's quota, expiry and switch, and gets its own config, QR code and subscription. Devices become extra peers under the client's email, built by the same core-user map the other paths use, and a device change is applied to the running inbound in place instead of rebuilding it.

Nodes advertise a wg-devices capability; the master refuses a device for a node that would ignore it, before storing it and again when pushing the inbound.
2026-11-24 22:16:16 +03:30

129 lines
3.9 KiB
Go

package runtime
import (
"bytes"
"context"
"encoding/base64"
"net"
"testing"
"github.com/xtls/xray-core/app/proxyman/command"
"github.com/xtls/xray-core/common/protocol"
"github.com/xtls/xray-core/proxy/wireguard"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
type wireguardDeviceServer struct {
command.UnimplementedHandlerServiceServer
users chan *protocol.User
}
func (s *wireguardDeviceServer) AlterInbound(_ context.Context, request *command.AlterInboundRequest) (*command.AlterInboundResponse, error) {
operation := new(command.AddUserOperation)
if err := proto.Unmarshal(request.Operation.Value, operation); err != nil {
return nil, err
}
s.users <- operation.User
return &command.AlterInboundResponse{}, nil
}
func newWireguardDeviceRuntime(t *testing.T) (*Local, *wireguardDeviceServer) {
t.Helper()
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
core := &wireguardDeviceServer{users: make(chan *protocol.User, 3)}
server := grpc.NewServer()
command.RegisterHandlerServiceServer(server, core)
go func() { _ = server.Serve(listener) }()
t.Cleanup(server.Stop)
port := listener.Addr().(*net.TCPAddr).Port
local := NewLocal(LocalDeps{APIPort: func() int { return port }})
return local, core
}
func wireguardDeviceClient(keepAlive *model.KeepAliveValue) model.Client {
key := func(seed byte) string {
return base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{seed}, 32))
}
return model.Client{
Email: "owner", Enable: true, PublicKey: key(1), AllowedIPs: []string{"10.0.0.2/32"},
KeepAlive: keepAlive,
WireguardDevices: []model.WireguardDevice{
{Id: "phone", Enable: true, PublicKey: key(2), AllowedIPs: []string{"10.0.0.3/32"}},
{Id: "off", Enable: false, PublicKey: key(3), AllowedIPs: []string{"10.0.0.4/32"}},
},
}
}
func assertWireguardDevicePeers(t *testing.T, core *wireguardDeviceServer, keepAlive string) {
t.Helper()
if len(core.users) != 2 {
t.Fatalf("added %d peers, want primary and enabled phone", len(core.users))
}
for i := range 2 {
user := <-core.users
if user.Email != "owner" {
t.Fatalf("peer attribution = %q", user.Email)
}
peer := new(wireguard.PeerConfig)
if err := proto.Unmarshal(user.Account.Value, peer); err != nil {
t.Fatal(err)
}
if peer.KeepAlive != keepAlive || peer.AllowedIps[0] != []string{"10.0.0.2/32", "10.0.0.3/32"}[i] {
t.Fatalf("unexpected peer %v", peer)
}
}
}
func TestLocalWireguardAddsEveryEnabledDevice(t *testing.T) {
for _, test := range []struct {
name string
keepAlive *model.KeepAliveValue
want string
}{
{name: "omitted"},
{name: "zero", keepAlive: model.KeepAlivePtr(0)},
{name: "positive", keepAlive: model.KeepAlivePtr(25), want: "25"},
} {
t.Run(test.name, func(t *testing.T) {
local, core := newWireguardDeviceRuntime(t)
if err := local.AddClient(context.Background(), &model.Inbound{Tag: "wg", Protocol: model.WireGuard}, wireguardDeviceClient(test.keepAlive)); err != nil {
t.Fatal(err)
}
assertWireguardDevicePeers(t, core, test.want)
})
}
}
func TestLocalWireguardAddUserAcceptsLegacyKeepAliveStrings(t *testing.T) {
for _, keepAlive := range []string{"", "0", "25"} {
t.Run("keepAlive="+keepAlive, func(t *testing.T) {
local, core := newWireguardDeviceRuntime(t)
client := wireguardDeviceClient(nil)
user := map[string]any{
"email": client.Email,
"publicKey": client.PublicKey,
"allowedIPs": client.AllowedIPs,
"wireguardDevices": client.WireguardDevices,
"keepAlive": keepAlive,
}
if err := local.AddUser(context.Background(), &model.Inbound{Tag: "wg", Protocol: model.WireGuard}, user); err != nil {
t.Fatal(err)
}
want := keepAlive
if keepAlive == "0" {
want = ""
}
assertWireguardDevicePeers(t, core, want)
if user["keepAlive"] != keepAlive {
t.Fatal("AddUser changed the caller's keepAlive value")
}
})
}
}