- service/xray.go 1741->573行: 只剩 agent 配置构建(GetXrayConfigForAgent/buildAgentXrayConfig=模板+节点入站+egress+转发编译); 删进程生命周期/状态机/RestartXray/StopXray/SetToNeedRestart/GetXrayTraffic/GetOnlineUsers/balancer/TestRoute 与 injectPanelEgress/injectNodeEgresses/injectMtproto/injectAmneziawg/mergeSubscriptionOutbounds
- controller: 43处 SetToNeedRestart 与各 RestartXray 调用清除; 删 balancerStatus/balancerOverride/routeTest/getXrayResult 路由与handler(纯本地核心操作); 前端删 RouteTester 页签与组件
- server service: GetConfigJson 改模板直读; 状态Xray块恒 stopped/空版本; Stop/RestartXrayService 改 no-op(DB导入/恢复流程保留形状)
- setting service: PanelEgressProxyURL/NodeEgressProxyURL 恒直连(桥在本地核心内,已不存在); NodeService.withOutboundBridge 恒直连回退
- runtime: Local 改为全 no-op 桩(DB即真源); NewManager() 去 LocalDeps(APIPort/SetNeedRestart)
- job: 删 XrayTrafficJob/CheckXrayRunningJob/CheckClientIpJob 三文件(共享声明 EventBus/IPWithTimestamp/clientStatsSnapshotMaxClients 迁 shared.go); web.go 不再调度 MtprotoJob/AmneziaWGJob, 删 OnCrash 接线与 stop() 的 sidecar StopAll
- MTProto/AmneziaWG 下线: AddInbound/UpdateInbound 对两协议直接拒绝(存量行保留可删); TG bot 的 restart 命令与在线查询按无本地核心收口
- 测试: GetXrayConfig 系列移植到 agent 构建(finalmask/clients/wireguard/scale); 删退役行为测试(lifecycle/restart/mtproto本地应用/client-ip job/inject系列); 共享测试helper迁 mtproto_test_helpers_test.go 与 job/integration_helpers_test.go
- 既有红(非本轮引入,已在 f6d6520 复现): TestGetXrayConfig_EnabledClientsStillEmitted、TestRouteRegistryContract(P6删 endpoints.ts 遗留)
131 lines
3.7 KiB
Go
131 lines
3.7 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
|
|
// clientsTestNodeID returns the shared agent node these client-emission
|
|
// tests home their inbounds on, creating it on first use. The only config
|
|
// build left in the lite master is the per-agent one.
|
|
func clientsTestNodeID(t *testing.T, db *gorm.DB) int {
|
|
t.Helper()
|
|
var node model.Node
|
|
if err := db.Where("name = ?", "clients-test-node").First(&node).Error; err == nil {
|
|
return node.Id
|
|
}
|
|
node = model.Node{Name: "clients-test-node", Address: "10.0.0.8", Port: 2053, ApiToken: "t"}
|
|
if err := db.Create(&node).Error; err != nil {
|
|
t.Fatalf("create clients-test node: %v", err)
|
|
}
|
|
return node.Id
|
|
}
|
|
|
|
func seedVlessInbound(t *testing.T, tag string, port int, clients []model.Client) {
|
|
t.Helper()
|
|
setupSettingTestDB(t)
|
|
db := database.GetDB()
|
|
nodeID := clientsTestNodeID(t, db)
|
|
in := &model.Inbound{
|
|
Tag: tag,
|
|
Enable: true,
|
|
Port: port,
|
|
Protocol: model.VLESS,
|
|
Settings: `{"clients":[],"decryption":"none"}`,
|
|
NodeID: &nodeID,
|
|
}
|
|
if err := db.Create(in).Error; err != nil {
|
|
t.Fatalf("create vless inbound: %v", err)
|
|
}
|
|
svc := ClientService{}
|
|
if err := svc.SyncInbound(nil, in.Id, clients); err != nil {
|
|
t.Fatalf("SyncInbound: %v", err)
|
|
}
|
|
}
|
|
|
|
func disableClients(t *testing.T, emails ...string) {
|
|
t.Helper()
|
|
if err := database.GetDB().Model(&model.ClientRecord{}).
|
|
Where("email IN ?", emails).
|
|
Update("enable", false).Error; err != nil {
|
|
t.Fatalf("disable clients: %v", err)
|
|
}
|
|
}
|
|
|
|
func emittedClients(t *testing.T, tag string) (any, bool) {
|
|
t.Helper()
|
|
svc := &XrayService{}
|
|
cfg, err := svc.GetXrayConfigForAgent(clientsTestNodeID(t, database.GetDB()), "", "")
|
|
if err != nil {
|
|
t.Fatalf("GetXrayConfigForAgent: %v", err)
|
|
}
|
|
for i := range cfg.InboundConfigs {
|
|
if cfg.InboundConfigs[i].Tag != tag {
|
|
continue
|
|
}
|
|
var s map[string]any
|
|
if err := json.Unmarshal([]byte(cfg.InboundConfigs[i].Settings), &s); err != nil {
|
|
t.Fatalf("unmarshal emitted settings: %v", err)
|
|
}
|
|
v, ok := s["clients"]
|
|
return v, ok
|
|
}
|
|
t.Fatalf("inbound %q not found in generated config", tag)
|
|
return nil, false
|
|
}
|
|
|
|
func TestGetXrayConfig_EmptyClientListIsArrayNotNull(t *testing.T) {
|
|
seedVlessInbound(t, "vless-empty", 43101, []model.Client{
|
|
{Email: "gone@x", ID: "11111111-1111-1111-1111-111111111111", Enable: true},
|
|
})
|
|
disableClients(t, "gone@x")
|
|
|
|
value, present := emittedClients(t, "vless-empty")
|
|
if !present {
|
|
t.Fatal("the clients key must be present on a vless inbound")
|
|
}
|
|
if value == nil {
|
|
t.Fatal(`settings.clients must be [] when every client is filtered out, got null`)
|
|
}
|
|
list, ok := value.([]any)
|
|
if !ok {
|
|
t.Fatalf("settings.clients must be an array, got %T", value)
|
|
}
|
|
if len(list) != 0 {
|
|
t.Fatalf("a disabled client must not reach the config, got %d entries", len(list))
|
|
}
|
|
}
|
|
|
|
func TestGetXrayConfig_EnabledClientsStillEmitted(t *testing.T) {
|
|
seedVlessInbound(t, "vless-mixed", 43102, []model.Client{
|
|
{Email: "live@x", ID: "22222222-2222-2222-2222-222222222222", Enable: true},
|
|
{Email: "off@x", ID: "33333333-3333-3333-3333-333333333333", Enable: true},
|
|
})
|
|
disableClients(t, "off@x")
|
|
|
|
value, _ := emittedClients(t, "vless-mixed")
|
|
list, ok := value.([]any)
|
|
if !ok {
|
|
t.Fatalf("settings.clients must be an array, got %T", value)
|
|
}
|
|
if len(list) != 1 {
|
|
t.Fatalf("expected only the enabled client, got %d entries: %#v", len(list), list)
|
|
}
|
|
entry, ok := list[0].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("client entry must be an object, got %T", list[0])
|
|
}
|
|
if entry["email"] != "live@x" {
|
|
t.Errorf("wrong client emitted: %#v", entry)
|
|
}
|
|
if entry["id"] != "22222222-2222-2222-2222-222222222222" {
|
|
t.Errorf("client id not carried through: %#v", entry)
|
|
}
|
|
}
|