[PR #4083] [MERGED] Fix IP Limit continuous ban loop after a legitimate ban expires (#4077) #5595

Closed
opened 2026-10-31 20:48:03 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/4083
Author: @pwnnex
Created: 4/22/2026
Status: ✅ Merged
Merged: 4/22/2026
Merged by: @pwnnex

Base: main ← Head: fix/iplimit-stale-db-evict


📝 Commits (1)

  • eef2d31 Fix IP Limit continuous ban loop from stale DB entries (#4077)

📊 Changes

2 files changed (+124 additions, -10 deletions)

View changed files

📝 web/job/check_client_ip_job.go (+47 -10)
➕ web/job/check_client_ip_job_test.go (+77 -0)

📄 Description

Closes #4077.

What users see

  • Two IPs connect to the same config → fail2ban bans the excess one (correct).
  • The ban duration expires (e.g. 1800s).
  • The single legitimate IP tries to reconnect → gets banned again immediately.
  • Loop continues forever. /var/log/x-ui/3xipl.log keeps filling with [LIMIT_IP] … Disconnecting OLD IP = lines for the same honest IP.

Works fine on v2.8.11, starts looping on v2.9.1.

Root cause

web/job/check_client_ip_job.go → updateInboundClientIps merges two lists:

  1. The persisted inbound_client_ips.ips blob (what we've seen historically).
  2. Freshly observed lines from xray's access log.

Then it sorts ascending by timestamp and keeps the first limitIp entries. The intent (from 60abeaa) is "protect the original/current connections, ban new excess ones".

The hole: nothing ever evicts an IP that simply stopped connecting. Its timestamp from the last time it was logged stays there forever. On the next run that stale IP is still "the oldest", so it wins the protected slot, and whichever IP is actually active right now is classified as "new excess" and gets re-banned. Every. Single. Run.

Fix

Drop entries whose last-seen timestamp is older than ipStaleAfterSeconds (30 minutes) from the merge. An IP that is actually still active refreshes its timestamp any time xray emits a new accepted log line for a fresh TCP, which is well inside 30 minutes even for idle streaming. A client that has really gone away falls out of the table in bounded time and frees its slot.

The merge logic was extracted into a pure mergeClientIps(old, new, staleCutoff) → map helper so the policy can be exercised without the DB-backed job.

Tests

5 subtests in check_client_ip_job_test.go, all pass. go build ./... + go vet ./web/job/... + gofmt -l . clean.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/4083 **Author:** [@pwnnex](https://github.com/pwnnex) **Created:** 4/22/2026 **Status:** ✅ Merged **Merged:** 4/22/2026 **Merged by:** [@pwnnex](https://github.com/pwnnex) **Base:** `main` ← **Head:** `fix/iplimit-stale-db-evict` --- ### 📝 Commits (1) - [`eef2d31`](https://github.com/MHSanaei/3x-ui/commit/eef2d311f41df127f954e92447d3bb46436e8afa) Fix IP Limit continuous ban loop from stale DB entries (#4077) ### 📊 Changes **2 files changed** (+124 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `web/job/check_client_ip_job.go` (+47 -10) ➕ `web/job/check_client_ip_job_test.go` (+77 -0) </details> ### 📄 Description Closes #4077. ## What users see - Two IPs connect to the same config → fail2ban bans the excess one (correct). - The ban duration expires (e.g. 1800s). - The single legitimate IP tries to reconnect → gets banned again **immediately**. - Loop continues forever. `/var/log/x-ui/3xipl.log` keeps filling with `[LIMIT_IP] … Disconnecting OLD IP =` lines for the same honest IP. Works fine on v2.8.11, starts looping on v2.9.1. ## Root cause `web/job/check_client_ip_job.go → updateInboundClientIps` merges two lists: 1. The persisted `inbound_client_ips.ips` blob (what we've seen historically). 2. Freshly observed lines from xray's access log. Then it sorts **ascending by timestamp** and keeps the first `limitIp` entries. The intent (from 60abeaa) is *"protect the original/current connections, ban new excess ones"*. The hole: nothing ever evicts an IP that simply stopped connecting. Its timestamp from the last time it was logged stays there forever. On the next run that stale IP is still "the oldest", so it wins the protected slot, and whichever IP is **actually** active right now is classified as "new excess" and gets re-banned. Every. Single. Run. ## Fix Drop entries whose last-seen timestamp is older than `ipStaleAfterSeconds` (30 minutes) from the merge. An IP that is actually still active refreshes its timestamp any time xray emits a new `accepted` log line for a fresh TCP, which is well inside 30 minutes even for idle streaming. A client that has really gone away falls out of the table in bounded time and frees its slot. The merge logic was extracted into a pure `mergeClientIps(old, new, staleCutoff) → map` helper so the policy can be exercised without the DB-backed job. ## Tests 5 subtests in `check_client_ip_job_test.go`, all pass. `go build ./...` + `go vet ./web/job/...` + `gofmt -l .` clean. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-31 20:48:03 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#5595
No description provided.