outbound_hwid_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package service
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. )
  9. // #6574: an outbound subscription fetch must send the id client external links
  10. // already send (#6559), or an HWID-limited provider counts the panel twice.
  11. func TestOutboundFetchSendsExternalSubscriptionHwid(t *testing.T) {
  12. setupSettingTestDB(t)
  13. var gotHwid string
  14. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  15. gotHwid = r.Header.Get("X-HWID")
  16. _, _ = w.Write([]byte("outbounds:\n"))
  17. }))
  18. defer srv.Close()
  19. svc := NewOutboundSubscriptionService()
  20. sub, err := svc.Create("hwid-test", srv.URL, "", "", true, 3600, true, false, false)
  21. if err != nil {
  22. t.Fatalf("create: %v", err)
  23. }
  24. t.Cleanup(func() { _ = svc.Delete(sub.Id) })
  25. if _, err := svc.Refresh(sub.Id); err != nil {
  26. t.Fatalf("refresh: %v", err)
  27. }
  28. var row model.Setting
  29. if err := database.GetDB().Where("key = ?", "externalSubHwid").First(&row).Error; err != nil {
  30. t.Fatalf("no persisted externalSubHwid after the fetch: %v", err)
  31. }
  32. if gotHwid == "" || gotHwid != row.Value {
  33. t.Fatalf("X-HWID = %q, want the persisted externalSubHwid %q", gotHwid, row.Value)
  34. }
  35. }
  36. func TestExternalSubscriptionHwidIsStableAndPersisted(t *testing.T) {
  37. setupSettingTestDB(t)
  38. first := ExternalSubscriptionHwid()
  39. if first == "" {
  40. t.Fatal("ExternalSubscriptionHwid returned empty")
  41. }
  42. if second := ExternalSubscriptionHwid(); second != first {
  43. t.Fatalf("hwid not stable: %q vs %q", first, second)
  44. }
  45. var row model.Setting
  46. if err := database.GetDB().Where("key = ?", "externalSubHwid").First(&row).Error; err != nil {
  47. t.Fatalf("hwid not persisted: %v", err)
  48. }
  49. if row.Value != first {
  50. t.Fatalf("persisted hwid %q != returned %q", row.Value, first)
  51. }
  52. }