Files
3x-ui/internal/web/service/node_host_adopt_test.go
T
MHSanaei cbd2940a63 fix(node): adopt a node inbound's host overrides into the master
Per-inbound Host overrides (Security/SNI/Fingerprint/ALPN and friends)
are looked up by the local inbound id when subscriptions render, but
nothing in the node sync ever fetched the node's hosts table: an
inbound adopted from a managed node got zero Host rows on the master,
so its subscription configs fell back to a bare TLS block without the
fingerprint/SNI the node was configured with.

When a traffic snapshot carries a tag with no central row yet - the
only moment adoption can happen - the sync job now also pulls the
node's existing hosts/list endpoint (best-effort, so old nodes just
skip it) and the adoption branch materializes that inbound's groups
against the new central id inside the same transaction, reusing the
group-to-rows projection the hosts API already uses. Master stays
authoritative afterwards: this is a one-time import, not a continuous
sync, matching how the inbound's own settings are adopted.

Closes #5890
2026-07-11 23:17:57 +02:00

78 lines
2.1 KiB
Go

package service
import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/entity"
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
)
func TestSetRemoteTraffic_AdoptsNodeHostRows(t *testing.T) {
setupConflictDB(t)
db := database.GetDB()
const nodeID = 6
if err := db.Create(&model.Node{
Id: nodeID,
Name: "host-node",
Address: "10.0.0.6",
Port: 2053,
ApiToken: "t",
Guid: "host-node-guid",
}).Error; err != nil {
t.Fatalf("create node: %v", err)
}
snap := &runtime.TrafficSnapshot{
Inbounds: []*model.Inbound{{
Id: 77,
Tag: "host-adopt-443",
Enable: true,
Port: 443,
Protocol: model.VLESS,
Settings: `{"clients":[]}`,
}},
HostGroups: []*entity.HostGroup{{
GroupId: "g-node",
InboundIds: []int{77, 99},
Hosts: []string{"cdn.example.com:8443"},
Remark: "cdn",
Security: "tls",
Sni: "sni.example.com",
Fingerprint: "firefox",
}},
}
svc := InboundService{}
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
t.Fatalf("setRemoteTrafficLocked: %v", err)
}
var central model.Inbound
if err := db.Where("tag = ?", "host-adopt-443").First(&central).Error; err != nil {
t.Fatalf("load adopted inbound: %v", err)
}
var hosts []model.Host
if err := db.Where("inbound_id = ?", central.Id).Find(&hosts).Error; err != nil {
t.Fatalf("load adopted hosts: %v", err)
}
if len(hosts) != 1 {
t.Fatalf("adopted host rows = %d, want 1", len(hosts))
}
h := hosts[0]
if h.GroupId != "g-node" || h.Address != "cdn.example.com" || h.Port != 8443 ||
h.Security != "tls" || h.Sni != "sni.example.com" || h.Fingerprint != "firefox" || h.Remark != "cdn" {
t.Fatalf("adopted host mismatch: %+v", h)
}
var total int64
if err := db.Model(&model.Host{}).Count(&total).Error; err != nil {
t.Fatalf("count hosts: %v", err)
}
if total != 1 {
t.Fatalf("total host rows = %d, want 1 (group member for un-adopted node inbound 99 must not materialize)", total)
}
}