1
0

external_hwid_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package sub
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "path/filepath"
  6. "testing"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database"
  8. "github.com/mhsanaei/3x-ui/v3/internal/web/service"
  9. )
  10. // #6559: the Master panel must send a stable X-HWID when fetching external
  11. // subscriptions, otherwise an HWID-limited donor answers 404.
  12. func TestFetchSendsStableHwid(t *testing.T) {
  13. if err := database.InitDB(filepath.Join(t.TempDir(), "x-ui.db")); err != nil {
  14. t.Fatalf("InitDB: %v", err)
  15. }
  16. t.Cleanup(func() { _ = database.CloseDB() })
  17. var gotHwid string
  18. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. gotHwid = r.Header.Get("X-HWID")
  20. _, _ = w.Write([]byte("vless://uuid@host:443?security=none#x"))
  21. }))
  22. defer srv.Close()
  23. res := fetchSubscriptionLinks(srv.URL)
  24. if res.err != nil {
  25. t.Fatalf("fetch: %v", res.err)
  26. }
  27. if len(res.links) != 1 {
  28. t.Fatalf("links = %v", res.links)
  29. }
  30. if want := service.ExternalSubscriptionHwid(); gotHwid == "" || gotHwid != want {
  31. t.Fatalf("X-HWID = %q, want the panel's stable %q", gotHwid, want)
  32. }
  33. }