check_client_ip_job_integration_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package job
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  12. xuilogger "github.com/mhsanaei/3x-ui/v3/internal/logger"
  13. "github.com/op/go-logging"
  14. )
  15. // 3x-ui logger must be initialised once before any code path that can
  16. // log a warning. otherwise log.Warningf panics on a nil logger.
  17. var loggerInitOnce sync.Once
  18. // setupIntegrationDB wires a temp sqlite db and log folder so
  19. // updateInboundClientIps can run end to end. closes the db before
  20. // TempDir cleanup so windows doesn't complain about the file being in
  21. // use.
  22. func setupIntegrationDB(t *testing.T) {
  23. t.Helper()
  24. loggerInitOnce.Do(func() {
  25. xuilogger.InitLogger(logging.ERROR)
  26. })
  27. dbDir := t.TempDir()
  28. logDir := t.TempDir()
  29. t.Setenv("XUI_DB_FOLDER", dbDir)
  30. t.Setenv("XUI_LOG_FOLDER", logDir)
  31. // updateInboundClientIps calls log.SetOutput on the package global,
  32. // which would leak to other tests in the same binary.
  33. origLogWriter := log.Writer()
  34. origLogFlags := log.Flags()
  35. t.Cleanup(func() {
  36. log.SetOutput(origLogWriter)
  37. log.SetFlags(origLogFlags)
  38. })
  39. if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
  40. t.Fatalf("database.InitDB failed: %v", err)
  41. }
  42. // LIFO cleanup order: this runs before t.TempDir's own cleanup.
  43. t.Cleanup(func() {
  44. if err := database.CloseDB(); err != nil {
  45. t.Logf("database.CloseDB warning: %v", err)
  46. }
  47. })
  48. }
  49. // seed an inbound whose settings json has a single client with the
  50. // given email and ip limit.
  51. func seedInboundWithClient(t *testing.T, tag, email string, limitIp int) {
  52. t.Helper()
  53. seedInboundOnlyWithClient(t, tag, email, limitIp)
  54. }
  55. func seedInboundOnlyWithClient(t *testing.T, tag, email string, limitIp int) *model.Inbound {
  56. t.Helper()
  57. settings := map[string]any{
  58. "clients": []map[string]any{
  59. {
  60. "email": email,
  61. "limitIp": limitIp,
  62. "enable": true,
  63. },
  64. },
  65. }
  66. settingsJSON, err := json.Marshal(settings)
  67. if err != nil {
  68. t.Fatalf("marshal settings: %v", err)
  69. }
  70. inbound := &model.Inbound{
  71. Tag: tag,
  72. Enable: true,
  73. Protocol: model.VLESS,
  74. Port: 4321,
  75. Settings: string(settingsJSON),
  76. }
  77. if err := database.GetDB().Create(inbound).Error; err != nil {
  78. t.Fatalf("seed inbound: %v", err)
  79. }
  80. return inbound
  81. }
  82. func seedLinkedInboundWithClient(t *testing.T, tag, email string, limitIp int) *model.Inbound {
  83. t.Helper()
  84. inbound := seedInboundOnlyWithClient(t, tag, email, limitIp)
  85. client := &model.ClientRecord{Email: email}
  86. if err := database.GetDB().Create(client).Error; err != nil {
  87. t.Fatalf("seed client record: %v", err)
  88. }
  89. link := &model.ClientInbound{ClientId: client.Id, InboundId: inbound.Id}
  90. if err := database.GetDB().Create(link).Error; err != nil {
  91. t.Fatalf("seed client inbound link: %v", err)
  92. }
  93. return inbound
  94. }
  95. // seed an InboundClientIps row with the given blob.
  96. func seedClientIps(t *testing.T, email string, ips []IPWithTimestamp) *model.InboundClientIps {
  97. t.Helper()
  98. blob, err := json.Marshal(ips)
  99. if err != nil {
  100. t.Fatalf("marshal ips: %v", err)
  101. }
  102. row := &model.InboundClientIps{
  103. ClientEmail: email,
  104. Ips: string(blob),
  105. }
  106. if err := database.GetDB().Create(row).Error; err != nil {
  107. t.Fatalf("seed InboundClientIps: %v", err)
  108. }
  109. return row
  110. }
  111. // read the persisted blob and parse it back.
  112. func readClientIps(t *testing.T, email string) []IPWithTimestamp {
  113. t.Helper()
  114. row := &model.InboundClientIps{}
  115. if err := database.GetDB().Where("client_email = ?", email).First(row).Error; err != nil {
  116. t.Fatalf("read InboundClientIps for %s: %v", email, err)
  117. }
  118. if row.Ips == "" {
  119. return nil
  120. }
  121. var out []IPWithTimestamp
  122. if err := json.Unmarshal([]byte(row.Ips), &out); err != nil {
  123. t.Fatalf("unmarshal Ips blob %q: %v", row.Ips, err)
  124. }
  125. return out
  126. }
  127. // make a lookup map so asserts don't depend on slice order.
  128. func ipSet(entries []IPWithTimestamp) map[string]int64 {
  129. out := make(map[string]int64, len(entries))
  130. for _, e := range entries {
  131. out[e.IP] = e.Timestamp
  132. }
  133. return out
  134. }
  135. // With the access-log fallback removed, an unavailable online-stats API (xray
  136. // down, as in this unit test) must make Run a clean no-op: no fail2ban probe, no
  137. // ban log, and no inbound_client_ips rows — never a crash or partial work.
  138. func TestRun_NoOpWhenOnlineApiUnavailable(t *testing.T) {
  139. setupIntegrationDB(t)
  140. t.Setenv("XUI_ENABLE_FAIL2BAN", "true")
  141. marker := fakeFail2BanClient(t)
  142. const email = "no-api-user"
  143. seedInboundWithClient(t, "inbound-no-api", email, 1)
  144. NewCheckClientIpJob().Run()
  145. if _, err := os.Stat(marker); !os.IsNotExist(err) {
  146. t.Fatalf("fail2ban-client should not have been probed when the online API is unavailable, stat error: %v", err)
  147. }
  148. if info, err := os.Stat(readIpLimitLogPath()); err == nil && info.Size() > 0 {
  149. body, _ := os.ReadFile(readIpLimitLogPath())
  150. t.Fatalf("3xipl.log should be empty when Run no-ops, got:\n%s", body)
  151. }
  152. var count int64
  153. if err := database.GetDB().Model(&model.InboundClientIps{}).Where("client_email = ?", email).Count(&count).Error; err != nil {
  154. t.Fatalf("count InboundClientIps: %v", err)
  155. }
  156. if count != 0 {
  157. t.Fatalf("no IP-limit rows should be persisted when Run no-ops, got %d", count)
  158. }
  159. }
  160. // #4091 repro: client has limit=3, db still holds 3 idle ips from a
  161. // few minutes ago, only one live ip is actually connecting. pre-fix:
  162. // live ip got banned every tick and never appeared in the panel.
  163. // post-fix: no ban, live ip persisted, historical ips still visible.
  164. func TestUpdateInboundClientIps_LiveIpNotBannedByStillFreshHistoricals(t *testing.T) {
  165. setupIntegrationDB(t)
  166. const email = "pr4091-repro"
  167. seedInboundWithClient(t, "inbound-pr4091", email, 3)
  168. now := time.Now().Unix()
  169. // idle but still within the 30min staleness window.
  170. row := seedClientIps(t, email, []IPWithTimestamp{
  171. {IP: "10.0.0.1", Timestamp: now - 20*60},
  172. {IP: "10.0.0.2", Timestamp: now - 15*60},
  173. {IP: "10.0.0.3", Timestamp: now - 10*60},
  174. })
  175. j := NewCheckClientIpJob()
  176. // the one that's actually connecting (user's 128.71.x.x).
  177. live := []IPWithTimestamp{
  178. {IP: "128.71.1.1", Timestamp: now},
  179. }
  180. inbound, err := j.getInboundByEmail(email)
  181. if err != nil {
  182. t.Fatalf("getInboundByEmail: %v", err)
  183. }
  184. shouldCleanLog := j.updateInboundClientIps(row, inbound, email, live, true, false)
  185. if shouldCleanLog {
  186. t.Fatalf("shouldCleanLog must be false, nothing should have been banned with 1 live ip under limit 3")
  187. }
  188. if len(j.disAllowedIps) != 0 {
  189. t.Fatalf("disAllowedIps must be empty, got %v", j.disAllowedIps)
  190. }
  191. persisted := ipSet(readClientIps(t, email))
  192. for _, want := range []string{"128.71.1.1", "10.0.0.1", "10.0.0.2", "10.0.0.3"} {
  193. if _, ok := persisted[want]; !ok {
  194. t.Errorf("expected %s to be persisted in inbound_client_ips.ips; got %v", want, persisted)
  195. }
  196. }
  197. if got := persisted["128.71.1.1"]; got != now {
  198. t.Errorf("live ip timestamp should match the scan timestamp %d, got %d", now, got)
  199. }
  200. // 3xipl.log must not contain a ban line.
  201. if info, err := os.Stat(readIpLimitLogPath()); err == nil && info.Size() > 0 {
  202. body, _ := os.ReadFile(readIpLimitLogPath())
  203. t.Fatalf("3xipl.log should be empty when no ips are banned, got:\n%s", body)
  204. }
  205. }
  206. // opposite invariant: when several ips are actually live and exceed
  207. // the limit, the oldest connection is dropped and the most recent one
  208. // keeps the slot (last-IP-wins policy from #3735, restored in #4699).
  209. func TestUpdateInboundClientIps_ExcessLiveIpIsStillBanned(t *testing.T) {
  210. setupIntegrationDB(t)
  211. const email = "pr4091-abuse"
  212. seedInboundWithClient(t, "inbound-pr4091-abuse", email, 1)
  213. now := time.Now().Unix()
  214. row := seedClientIps(t, email, []IPWithTimestamp{
  215. {IP: "10.1.0.1", Timestamp: now - 60}, // original connection
  216. })
  217. j := NewCheckClientIpJob()
  218. // both live, limit=1. use distinct timestamps so sort-by-timestamp
  219. // is deterministic: 10.1.0.1 is the original (older) and must get
  220. // banned; 192.0.2.9 joined later and keeps the slot (last IP wins).
  221. live := []IPWithTimestamp{
  222. {IP: "10.1.0.1", Timestamp: now - 5},
  223. {IP: "192.0.2.9", Timestamp: now},
  224. }
  225. inbound, err := j.getInboundByEmail(email)
  226. if err != nil {
  227. t.Fatalf("getInboundByEmail: %v", err)
  228. }
  229. shouldCleanLog := j.updateInboundClientIps(row, inbound, email, live, true, false)
  230. if !shouldCleanLog {
  231. t.Fatalf("shouldCleanLog must be true when the live set exceeds the limit")
  232. }
  233. if len(j.disAllowedIps) != 1 || j.disAllowedIps[0] != "10.1.0.1" {
  234. t.Fatalf("expected 10.1.0.1 to be banned; disAllowedIps = %v", j.disAllowedIps)
  235. }
  236. persisted := ipSet(readClientIps(t, email))
  237. if _, ok := persisted["192.0.2.9"]; !ok {
  238. t.Errorf("newest IP 192.0.2.9 must still be persisted; got %v", persisted)
  239. }
  240. if _, ok := persisted["10.1.0.1"]; ok {
  241. t.Errorf("banned IP 10.1.0.1 must NOT be persisted; got %v", persisted)
  242. }
  243. // 3xipl.log must contain the ban line in the exact fail2ban format.
  244. body, err := os.ReadFile(readIpLimitLogPath())
  245. if err != nil {
  246. t.Fatalf("read 3xipl.log: %v", err)
  247. }
  248. wantSubstr := "[LIMIT_IP] Email = pr4091-abuse || Disconnecting OLD IP = 10.1.0.1"
  249. if !contains(string(body), wantSubstr) {
  250. t.Fatalf("3xipl.log missing expected ban line %q\nfull log:\n%s", wantSubstr, body)
  251. }
  252. }
  253. // #4800: per-client IP tracking must populate even when no client has an IP
  254. // limit. processObserved records observed IPs for the panel regardless of any
  255. // limit; only enforcement is gated, so a limit-free install still shows IPs. No
  256. // ban may be written since there's no limit.
  257. func TestProcessObserved_CollectsIpsWithoutLimit(t *testing.T) {
  258. setupIntegrationDB(t)
  259. const email = "no-limit-user"
  260. seedInboundWithClient(t, "inbound-no-limit", email, 0) // limitIp = 0
  261. observed := map[string]map[string]int64{
  262. email: {"203.0.113.10": time.Now().Unix()},
  263. }
  264. NewCheckClientIpJob().processObserved(observed, true, true)
  265. ips := readClientIps(t, email)
  266. if len(ips) != 1 || ips[0].IP != "203.0.113.10" {
  267. t.Fatalf("expected the observed IP to be collected without a limit, got %v", ips)
  268. }
  269. if info, err := os.Stat(readIpLimitLogPath()); err == nil && info.Size() > 0 {
  270. body, _ := os.ReadFile(readIpLimitLogPath())
  271. t.Fatalf("3xipl.log should be empty with no limit set, got:\n%s", body)
  272. }
  273. }
  274. // #4963: an observed IP for a renamed/deleted client (its email no longer maps
  275. // to any inbound) must not create or resurrect an inbound_client_ips row, and
  276. // must drop any orphan left behind — instead of erroring every run.
  277. func TestProcessObserved_StaleEmailIsSkippedAndOrphanDropped(t *testing.T) {
  278. setupIntegrationDB(t)
  279. const staleEmail = "renamed-away"
  280. // No inbound references staleEmail. Pre-seed an orphan tracking row to
  281. // confirm the job removes it rather than leaving it to error forever.
  282. seedClientIps(t, staleEmail, []IPWithTimestamp{{IP: "203.0.113.5", Timestamp: time.Now().Unix()}})
  283. observed := map[string]map[string]int64{
  284. staleEmail: {"203.0.113.5": time.Now().Unix()},
  285. }
  286. NewCheckClientIpJob().processObserved(observed, true, true)
  287. var count int64
  288. if err := database.GetDB().Model(&model.InboundClientIps{}).Where("client_email = ?", staleEmail).Count(&count).Error; err != nil {
  289. t.Fatalf("count InboundClientIps: %v", err)
  290. }
  291. if count != 0 {
  292. t.Fatalf("stale-email orphan row should be deleted, got %d row(s)", count)
  293. }
  294. }
  295. // readIpLimitLogPath reads the 3xipl.log path the same way the job
  296. // does via xray.GetIPLimitLogPath but without importing xray here
  297. // just for the path helper (which would pull a lot more deps into the
  298. // test binary). The env-derived log folder is deterministic.
  299. func readIpLimitLogPath() string {
  300. folder := os.Getenv("XUI_LOG_FOLDER")
  301. if folder == "" {
  302. folder = filepath.Join(".", "log")
  303. }
  304. return filepath.Join(folder, "3xipl.log")
  305. }
  306. func contains(haystack, needle string) bool {
  307. for i := 0; i+len(needle) <= len(haystack); i++ {
  308. if haystack[i:i+len(needle)] == needle {
  309. return true
  310. }
  311. }
  312. return false
  313. }
  314. // the exact clients/client_inbounds relation must win over the substring scan,
  315. // so a client is resolved to its own inbound even when another inbound holds a
  316. // superstring email.
  317. func TestGetInboundByEmailUsesClientInboundLink(t *testing.T) {
  318. setupIntegrationDB(t)
  319. want := seedLinkedInboundWithClient(t, "linked-inbound", "[email protected]", 1)
  320. seedInboundOnlyWithClient(t, "other-inbound", "[email protected]", 1)
  321. got, err := (&CheckClientIpJob{}).getInboundByEmail("[email protected]")
  322. if err != nil {
  323. t.Fatalf("getInboundByEmail returned error: %v", err)
  324. }
  325. if got.Id != want.Id {
  326. t.Fatalf("getInboundByEmail returned inbound %d, want %d", got.Id, want.Id)
  327. }
  328. }
  329. // the substring fallback must still verify the exact email inside settings, so
  330. // "[email protected]" does not match an inbound holding "[email protected]".
  331. func TestGetInboundByEmailRejectsSubstringFallbackMatch(t *testing.T) {
  332. setupIntegrationDB(t)
  333. seedInboundOnlyWithClient(t, "substring-only", "[email protected]", 1)
  334. if got, err := (&CheckClientIpJob{}).getInboundByEmail("[email protected]"); err == nil {
  335. t.Fatalf("substring email matched inbound %d; want no exact match", got.Id)
  336. }
  337. }