inbound_node_sync_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package controller
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "github.com/gin-gonic/gin"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database"
  12. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  13. "github.com/mhsanaei/3x-ui/v3/internal/util/crypto"
  14. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  15. )
  16. // A sub-node stores whatever the master pushes. A master row whose certificate
  17. // predates the TLS guard must still land, or the node silently falls out of sync.
  18. func TestNodeSyncPushSkipsOperatorTLSGuard(t *testing.T) {
  19. gin.SetMode(gin.TestMode)
  20. dbDir := t.TempDir()
  21. t.Setenv("XUI_DB_FOLDER", dbDir)
  22. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  23. t.Fatalf("InitDB: %v", err)
  24. }
  25. t.Cleanup(func() { _ = database.CloseDB() })
  26. prev := runtime.GetManager()
  27. runtime.SetManager(runtime.NewManager(runtime.LocalDeps{APIPort: func() int { return 0 }, SetNeedRestart: func() {}}))
  28. t.Cleanup(func() { runtime.SetManager(prev) })
  29. for name, scope := range map[string]string{"node-sync": model.ApiScopeNodeSync, "admin": model.ApiScopeAdmin} {
  30. row := &model.ApiToken{Name: name, Token: crypto.HashTokenSHA256(name + "-token"), Enabled: true, Scope: scope}
  31. if err := database.GetDB().Create(row).Error; err != nil {
  32. t.Fatalf("seed %s token: %v", name, err)
  33. }
  34. }
  35. engine := gin.New()
  36. a := &APIController{}
  37. api := engine.Group("/panel/api")
  38. api.Use(a.checkAPIAuth, a.enforceTokenScope)
  39. NewInboundController(api.Group("/inbounds"))
  40. const legacyStream = `{"network":"tcp","security":"tls","tlsSettings":{"certificates":[{"certificateFile":"","keyFile":"","certificate":[],"key":[]}]}}`
  41. add := func(t *testing.T, token string, port int) string {
  42. t.Helper()
  43. form := url.Values{
  44. "protocol": {"vless"},
  45. "port": {strconv.Itoa(port)},
  46. "tag": {"tls-legacy-" + strconv.Itoa(port)},
  47. "enable": {"true"},
  48. "settings": {`{"clients":[]}`},
  49. "streamSettings": {legacyStream},
  50. }
  51. req := httptest.NewRequest(http.MethodPost, "/panel/api/inbounds/add", strings.NewReader(form.Encode()))
  52. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  53. req.Header.Set("Authorization", "Bearer "+token)
  54. w := httptest.NewRecorder()
  55. engine.ServeHTTP(w, req)
  56. return w.Body.String()
  57. }
  58. rows := func(t *testing.T, tag string) int64 {
  59. t.Helper()
  60. var n int64
  61. if err := database.GetDB().Model(&model.Inbound{}).Where("tag = ?", tag).Count(&n).Error; err != nil {
  62. t.Fatalf("count %s: %v", tag, err)
  63. }
  64. return n
  65. }
  66. t.Run("a master push lands on the node", func(t *testing.T) {
  67. body := add(t, "node-sync-token", 45001)
  68. if !strings.Contains(body, `"success":true`) {
  69. t.Fatalf("node-sync add rejected: %s", body)
  70. }
  71. if got := rows(t, "tls-legacy-45001"); got != 1 {
  72. t.Fatalf("stored rows = %d, want 1", got)
  73. }
  74. })
  75. t.Run("an operator token is still held to the guard", func(t *testing.T) {
  76. body := add(t, "admin-token", 45002)
  77. if !strings.Contains(body, `"success":false`) || !strings.Contains(body, "TLS") {
  78. t.Fatalf("admin add should fail on TLS, got: %s", body)
  79. }
  80. if got := rows(t, "tls-legacy-45002"); got != 0 {
  81. t.Fatalf("stored rows = %d, want 0", got)
  82. }
  83. })
  84. }