node_host_adopt_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package service
  2. import (
  3. "testing"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  7. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  8. )
  9. func TestSetRemoteTraffic_AdoptsNodeHostRows(t *testing.T) {
  10. setupConflictDB(t)
  11. db := database.GetDB()
  12. const nodeID = 6
  13. if err := db.Create(&model.Node{
  14. Id: nodeID,
  15. Name: "host-node",
  16. Address: "10.0.0.6",
  17. Port: 2053,
  18. ApiToken: "t",
  19. Guid: "host-node-guid",
  20. }).Error; err != nil {
  21. t.Fatalf("create node: %v", err)
  22. }
  23. snap := &runtime.TrafficSnapshot{
  24. Inbounds: []*model.Inbound{{
  25. Id: 77,
  26. Tag: "host-adopt-443",
  27. Enable: true,
  28. Port: 443,
  29. Protocol: model.VLESS,
  30. Settings: `{"clients":[]}`,
  31. }},
  32. HostGroups: []*entity.HostGroup{{
  33. GroupId: "g-node",
  34. InboundIds: []int{77, 99},
  35. Hosts: []string{"cdn.example.com:8443"},
  36. Remark: "cdn",
  37. Security: "tls",
  38. Sni: "sni.example.com",
  39. Fingerprint: "firefox",
  40. }},
  41. }
  42. svc := InboundService{}
  43. if _, err := svc.setRemoteTrafficLocked(nodeID, snap, false); err != nil {
  44. t.Fatalf("setRemoteTrafficLocked: %v", err)
  45. }
  46. var central model.Inbound
  47. if err := db.Where("tag = ?", "host-adopt-443").First(&central).Error; err != nil {
  48. t.Fatalf("load adopted inbound: %v", err)
  49. }
  50. var hosts []model.Host
  51. if err := db.Where("inbound_id = ?", central.Id).Find(&hosts).Error; err != nil {
  52. t.Fatalf("load adopted hosts: %v", err)
  53. }
  54. if len(hosts) != 1 {
  55. t.Fatalf("adopted host rows = %d, want 1", len(hosts))
  56. }
  57. h := hosts[0]
  58. if h.GroupId != "g-node" || h.Address != "cdn.example.com" || h.Port != 8443 ||
  59. h.Security != "tls" || h.Sni != "sni.example.com" || h.Fingerprint != "firefox" || h.Remark != "cdn" {
  60. t.Fatalf("adopted host mismatch: %+v", h)
  61. }
  62. var total int64
  63. if err := db.Model(&model.Host{}).Count(&total).Error; err != nil {
  64. t.Fatalf("count hosts: %v", err)
  65. }
  66. if total != 1 {
  67. t.Fatalf("total host rows = %d, want 1 (group member for un-adopted node inbound 99 must not materialize)", total)
  68. }
  69. }