inbound_fallback_runtime_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package service
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. // TestBuildRuntimeInboundForAPI_InjectsFallbacks is the #5963 regression: the
  10. // runtime inbound sent to nodes never carried the inbound_fallbacks rows, so
  11. // fallbacks configured on the master silently vanished from every node.
  12. func TestBuildRuntimeInboundForAPI_InjectsFallbacks(t *testing.T) {
  13. setupBulkDB(t)
  14. svc := &InboundService{}
  15. db := database.GetDB()
  16. clients := []model.Client{
  17. {Email: "fb@x", ID: "11111111-1111-1111-1111-111111111111", Enable: true},
  18. }
  19. master := mkInbound(t, 30201, model.VLESS, clientsSettings(t, clients))
  20. master.StreamSettings = `{"network":"tcp","security":"tls","tlsSettings":{"serverName":"s"}}`
  21. if err := db.Save(master).Error; err != nil {
  22. t.Fatalf("save master stream: %v", err)
  23. }
  24. fb := model.InboundFallback{MasterId: master.Id, ChildId: 0, Dest: "8081", Alpn: "h2", Path: "/fb", Xver: 1}
  25. if err := db.Create(&fb).Error; err != nil {
  26. t.Fatalf("seed fallback: %v", err)
  27. }
  28. runtimeIb, err := svc.buildRuntimeInboundForAPI(db, master)
  29. if err != nil {
  30. t.Fatalf("buildRuntimeInboundForAPI: %v", err)
  31. }
  32. var settings map[string]any
  33. if err := json.Unmarshal([]byte(runtimeIb.Settings), &settings); err != nil {
  34. t.Fatalf("runtime settings not valid json: %v", err)
  35. }
  36. fallbacks, ok := settings["fallbacks"].([]any)
  37. if !ok || len(fallbacks) != 1 {
  38. t.Fatalf("runtime settings must carry the fallback, got: %s", runtimeIb.Settings)
  39. }
  40. if !strings.Contains(runtimeIb.Settings, `"dest"`) || !strings.Contains(runtimeIb.Settings, "8081") {
  41. t.Fatalf("fallback dest missing from runtime settings: %s", runtimeIb.Settings)
  42. }
  43. }
  44. // A non-fallback-capable inbound (ws transport) must stay untouched.
  45. func TestBuildRuntimeInboundForAPI_NoFallbacksOnWsInbound(t *testing.T) {
  46. setupBulkDB(t)
  47. svc := &InboundService{}
  48. db := database.GetDB()
  49. clients := []model.Client{
  50. {Email: "ws@x", ID: "22222222-2222-2222-2222-222222222222", Enable: true},
  51. }
  52. ib := mkInbound(t, 30202, model.VLESS, clientsSettings(t, clients))
  53. ib.StreamSettings = `{"network":"ws","security":"none"}`
  54. if err := db.Save(ib).Error; err != nil {
  55. t.Fatalf("save stream: %v", err)
  56. }
  57. fb := model.InboundFallback{MasterId: ib.Id, Dest: "8082"}
  58. if err := db.Create(&fb).Error; err != nil {
  59. t.Fatalf("seed fallback: %v", err)
  60. }
  61. runtimeIb, err := svc.buildRuntimeInboundForAPI(db, ib)
  62. if err != nil {
  63. t.Fatalf("buildRuntimeInboundForAPI: %v", err)
  64. }
  65. if strings.Contains(runtimeIb.Settings, "fallbacks") {
  66. t.Fatalf("ws inbound must not receive fallbacks: %s", runtimeIb.Settings)
  67. }
  68. }