limit_ip_disconnect_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package job
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. )
  8. // A reverse client has to be left alone: the cycle drops its reverse outbound
  9. // handler and re-adds an account without a reverse, which no retry restores.
  10. func TestDisconnectClientTemporarilySkipsReverseClient(t *testing.T) {
  11. setupIntegrationDB(t)
  12. const email = "rev-limit-probe"
  13. inbound := &model.Inbound{
  14. Id: 1,
  15. Protocol: model.VLESS,
  16. Tag: "rev-limit-probe-tag",
  17. Settings: `{"clients":[]}`,
  18. }
  19. clients := []model.Client{{Email: email, ID: "11111111-1111-1111-1111-111111111111", Reverse: &model.ClientReverse{Tag: "rev-out"}}}
  20. (&CheckClientIpJob{}).disconnectClientTemporarily(inbound, email, clients)
  21. var skipped, attempted bool
  22. for _, line := range logger.GetLogs(500, "warning") {
  23. if strings.Contains(line, "Not disconnecting "+email) {
  24. skipped = true
  25. }
  26. if strings.Contains(line, "Failed to remove user "+email) {
  27. attempted = true
  28. }
  29. }
  30. if attempted {
  31. t.Fatal("a reverse client must not be removed and re-added")
  32. }
  33. if !skipped {
  34. t.Fatal("the skip must be reported, not silent")
  35. }
  36. }
  37. // The protocol gate must let hysteria through: XrayAPI supports it, and the
  38. // skip left over-limit Hysteria2 sessions alive until the fail2ban ban caught up.
  39. func TestDisconnectClientTemporarilyAllowsHysteria(t *testing.T) {
  40. setupIntegrationDB(t)
  41. const email = "hy2-limit-probe"
  42. inbound := &model.Inbound{
  43. Id: 1,
  44. Protocol: model.Hysteria,
  45. Tag: "hy2-limit-probe-tag",
  46. Settings: `{"clients":[]}`,
  47. }
  48. clients := []model.Client{{Email: email, Auth: "secret"}}
  49. (&CheckClientIpJob{}).disconnectClientTemporarily(inbound, email, clients)
  50. var unsupported, attempted bool
  51. for _, line := range logger.GetLogs(500, "warning") {
  52. if strings.Contains(line, "Temporary disconnect is not supported for protocol hysteria") {
  53. unsupported = true
  54. }
  55. if strings.Contains(line, "Failed to remove user "+email) {
  56. attempted = true
  57. }
  58. }
  59. if unsupported {
  60. t.Fatal("hysteria was rejected by the protocol gate")
  61. }
  62. if !attempted {
  63. t.Fatal("expected a remove attempt against the Xray API for hysteria")
  64. }
  65. }