1
0

inbound_node_sync_test.go 3.1 KB

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