1
0

remote_reset_test.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package runtime
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/http/httptest"
  7. "slices"
  8. "testing"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  10. )
  11. // The master replays a node's reset backlog through the node's bulk endpoint.
  12. func TestRemoteResetClientTrafficsPostsEmailsToBulkEndpoint(t *testing.T) {
  13. var path string
  14. var body struct {
  15. Emails []string `json:"emails"`
  16. }
  17. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. path = r.URL.Path
  19. _ = json.NewDecoder(r.Body).Decode(&body)
  20. w.Header().Set("Content-Type", "application/json")
  21. _, _ = w.Write([]byte(`{"success":true}`))
  22. }))
  23. t.Cleanup(srv.Close)
  24. r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
  25. if err := r.ResetClientTraffics(context.Background(), []string{"a@x", "b@x"}); err != nil {
  26. t.Fatalf("ResetClientTraffics: %v", err)
  27. }
  28. if path != "/panel/api/clients/bulkResetTraffic" || !slices.Equal(body.Emails, []string{"a@x", "b@x"}) {
  29. t.Fatalf("node got %s %v, want /panel/api/clients/bulkResetTraffic [a@x b@x]", path, body.Emails)
  30. }
  31. }
  32. // A central inbound id need not match the node's id, so the reset must target
  33. // the node-side id resolved from the tag, never ib.Id.
  34. func TestRemoteResetInboundTrafficUsesNodeInboundID(t *testing.T) {
  35. var method, path string
  36. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  37. method, path = req.Method, req.URL.Path
  38. w.Header().Set("Content-Type", "application/json")
  39. _, _ = w.Write([]byte(`{"success":true,"msg":"ok"}`))
  40. }))
  41. t.Cleanup(srv.Close)
  42. r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
  43. r.cacheSet("n1-in-443", 7)
  44. ib := &model.Inbound{Id: 42, Tag: "n1-in-443"}
  45. if err := r.ResetInboundTraffic(context.Background(), ib); err != nil {
  46. t.Fatalf("ResetInboundTraffic: %v", err)
  47. }
  48. if method != http.MethodPost || path != "/panel/api/inbounds/7/resetTraffic" {
  49. t.Fatalf("node got %s %s, want POST /panel/api/inbounds/7/resetTraffic", method, path)
  50. }
  51. }
  52. // An unresolvable tag must fail before posting, so a reset never lands on an
  53. // unrelated node inbound that happens to share the central id.
  54. func TestRemoteResetInboundTrafficUnknownTagErrors(t *testing.T) {
  55. var resetPosted bool
  56. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  57. w.Header().Set("Content-Type", "application/json")
  58. if req.URL.Path == "/panel/api/inbounds/list" {
  59. _, _ = w.Write([]byte(`{"success":true,"msg":"ok","obj":[]}`))
  60. return
  61. }
  62. resetPosted = true
  63. _, _ = w.Write([]byte(`{"success":true,"msg":"ok"}`))
  64. }))
  65. t.Cleanup(srv.Close)
  66. r := NewRemote(nodeForPlainServer(t, srv, "verify", "tok"), nil)
  67. ib := &model.Inbound{Id: 42, Tag: "n1-in-443"}
  68. if err := r.ResetInboundTraffic(context.Background(), ib); err == nil {
  69. t.Fatal("ResetInboundTraffic error = nil, want unknown-tag error")
  70. }
  71. if resetPosted {
  72. t.Fatal("reset request posted to node despite an unresolved tag")
  73. }
  74. }