hwid_controller_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package sub
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/gin-gonic/gin"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  11. )
  12. func initHwidSubRouter(t *testing.T, limit int) (*gin.Engine, string) {
  13. t.Helper()
  14. tmp := t.TempDir()
  15. t.Chdir(tmp)
  16. if err := os.MkdirAll("internal/web/dist", 0o755); err != nil {
  17. t.Fatalf("mkdir dist: %v", err)
  18. }
  19. if err := os.WriteFile("internal/web/dist/subpage.html", []byte("<html><head></head><body></body></html>"), 0o644); err != nil {
  20. t.Fatalf("write subpage: %v", err)
  21. }
  22. t.Setenv("XUI_DB_FOLDER", tmp)
  23. if err := database.InitDB(filepath.Join(tmp, "x-ui.db")); err != nil {
  24. t.Fatalf("InitDB: %v", err)
  25. }
  26. t.Cleanup(func() { _ = database.CloseDB() })
  27. const subID = "sub-hwid-route"
  28. const email = "[email protected]"
  29. const uuid = "11111111-2222-4333-8444-555555555555"
  30. db := database.GetDB()
  31. ib := &model.Inbound{
  32. UserId: 1,
  33. Tag: "hwid-sub",
  34. Enable: true,
  35. Port: 443,
  36. Protocol: model.VLESS,
  37. Settings: `{"clients":[]}`,
  38. StreamSettings: `{"network":"tcp","security":"none"}`,
  39. }
  40. if err := db.Create(ib).Error; err != nil {
  41. t.Fatalf("seed inbound: %v", err)
  42. }
  43. client := &model.ClientRecord{Email: email, SubID: subID, UUID: uuid, Enable: true, LimitHwid: limit}
  44. if err := db.Create(client).Error; err != nil {
  45. t.Fatalf("seed client: %v", err)
  46. }
  47. if err := db.Create(&model.ClientInbound{ClientId: client.Id, InboundId: ib.Id}).Error; err != nil {
  48. t.Fatalf("seed client inbound: %v", err)
  49. }
  50. gin.SetMode(gin.TestMode)
  51. router := gin.New()
  52. NewSUBController(
  53. router.Group("/"),
  54. WithSUBPath("/sub/"),
  55. WithSUBJsonPath("/json/"),
  56. WithSUBClashPath("/clash/"),
  57. WithSUBClashAutoDetect(true),
  58. WithSUBJsonAutoDetect(true),
  59. WithSUBJsonEnabled(true),
  60. WithSUBClashEnabled(true),
  61. )
  62. return router, subID
  63. }
  64. func requestSub(t *testing.T, router *gin.Engine, method string, path string, hwid string, accept string) *httptest.ResponseRecorder {
  65. t.Helper()
  66. req := httptest.NewRequest(method, path, nil)
  67. req.Host = "sub.example.com"
  68. if hwid != "" {
  69. req.Header.Set("X-HWID", hwid)
  70. }
  71. if accept != "" {
  72. req.Header.Set("Accept", accept)
  73. }
  74. rec := httptest.NewRecorder()
  75. router.ServeHTTP(rec, req)
  76. return rec
  77. }
  78. func TestSubscriptionHwidGateAcrossBodyRoutes(t *testing.T) {
  79. router, subID := initHwidSubRouter(t, 1)
  80. for _, path := range []string{"/sub/" + subID, "/json/" + subID, "/clash/" + subID} {
  81. rec := requestSub(t, router, http.MethodGet, path, "", "")
  82. if rec.Code != http.StatusNotFound {
  83. t.Fatalf("%s missing HWID status = %d, want 404", path, rec.Code)
  84. }
  85. if rec.Header().Get("X-Hwid-Active") != "true" || rec.Header().Get("X-Hwid-Not-Supported") != "true" {
  86. t.Fatalf("%s missing HWID headers = %#v", path, rec.Header())
  87. }
  88. }
  89. rec := requestSub(t, router, http.MethodHead, "/sub/"+subID, "", "")
  90. if rec.Code != http.StatusNotFound || rec.Header().Get("X-Hwid-Not-Supported") != "true" {
  91. t.Fatalf("HEAD missing HWID = %d %#v", rec.Code, rec.Header())
  92. }
  93. for _, path := range []string{"/sub/" + subID, "/json/" + subID, "/clash/" + subID} {
  94. rec = requestSub(t, router, http.MethodGet, path, "device-one", "")
  95. if rec.Code != http.StatusOK {
  96. t.Fatalf("%s registered HWID status = %d, body=%q", path, rec.Code, rec.Body.String())
  97. }
  98. if rec.Header().Get("X-Hwid-Active") != "true" {
  99. t.Fatalf("%s allowed response missing active HWID header", path)
  100. }
  101. }
  102. rec = requestSub(t, router, http.MethodGet, "/json/"+subID, "device-two", "")
  103. if rec.Code != http.StatusNotFound {
  104. t.Fatalf("new HWID after limit status = %d, want 404", rec.Code)
  105. }
  106. if rec.Header().Get("X-Hwid-Max-Devices-Reached") != "true" || rec.Header().Get("X-Hwid-Limit") != "true" {
  107. t.Fatalf("limit headers missing: %#v", rec.Header())
  108. }
  109. }
  110. func TestSubscriptionHwidGateSkipsHtmlInfoPage(t *testing.T) {
  111. router, subID := initHwidSubRouter(t, 1)
  112. rec := requestSub(t, router, http.MethodGet, "/sub/"+subID, "", "text/html")
  113. if rec.Code != http.StatusOK {
  114. t.Fatalf("HTML sub page status = %d, want 200, body=%q", rec.Code, rec.Body.String())
  115. }
  116. if rec.Header().Get("X-Hwid-Not-Supported") != "" {
  117. t.Fatalf("HTML sub page should not be HWID-gated: %#v", rec.Header())
  118. }
  119. }