Files
mirror-3x-ui/internal/xray/api_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

73 lines
2.3 KiB
Go

package xray
import (
"context"
"errors"
"testing"
"github.com/xtls/xray-core/app/proxyman/command"
"github.com/xtls/xray-core/common/protocol"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)
type wireguardUsersClient struct {
command.HandlerServiceClient
users []*protocol.User
removed []string
listErr error
removeErr error
}
func (c *wireguardUsersClient) GetInboundUsers(_ context.Context, request *command.GetInboundUserRequest, _ ...grpc.CallOption) (*command.GetInboundUserResponse, error) {
if request.Tag != "wg" || request.Email != "" {
return nil, errors.New("expected every user of the WireGuard inbound")
}
return &command.GetInboundUserResponse{Users: c.users}, c.listErr
}
func (c *wireguardUsersClient) AlterInbound(_ context.Context, request *command.AlterInboundRequest, _ ...grpc.CallOption) (*command.AlterInboundResponse, error) {
operation := new(command.RemoveUserOperation)
if err := proto.Unmarshal(request.Operation.Value, operation); err != nil {
return nil, err
}
c.removed = append(c.removed, operation.Email)
return &command.AlterInboundResponse{}, c.removeErr
}
func TestRemoveWireguardUsersRemovesEveryPeerOfClient(t *testing.T) {
core := &wireguardUsersClient{users: []*protocol.User{
{Email: "client"}, {Email: "other"}, {Email: "client"}, nil, {Email: "client"},
}}
var handler command.HandlerServiceClient = core
api := XrayAPI{HandlerServiceClient: &handler}
if err := api.RemoveWireguardUsers("wg", "client"); err != nil {
t.Fatal(err)
}
if len(core.removed) != 3 {
t.Fatalf("removed %v, want all three client peers", core.removed)
}
for _, email := range core.removed {
if email != "client" {
t.Fatalf("removed another client's peer: %q", email)
}
}
}
func TestRemoveWireguardUsersPropagatesCoreFailures(t *testing.T) {
for _, failList := range []bool{true, false} {
core := &wireguardUsersClient{users: []*protocol.User{{Email: "client"}}}
failure := errors.New("core unavailable")
if failList {
core.listErr = failure
} else {
core.removeErr = failure
}
var handler command.HandlerServiceClient = core
api := XrayAPI{HandlerServiceClient: &handler}
if err := api.RemoveWireguardUsers("wg", "client"); !errors.Is(err, failure) {
t.Fatalf("failure not propagated: %v", err)
}
}
}