mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-19 03:25:01 +03:00
feat(limitip): let operators exempt trusted addresses from the IP limit (#6230)
* feat(limitip): let operators exempt trusted addresses from the IP limit Behind a shared address — an office gateway, a campus NAT, a residential carrier — every user looks like the same client. One of them trips the IP limit and the address is disconnected and handed to fail2ban, taking the others with it. Today the only way out is editing jail.d by hand, which an update overwrites. Add an allowlist setting of addresses and networks. A matching address is neither banned nor counted towards the limit: counting it would still cut the shared network the entry exists to protect. Entries are validated on save rather than skipped at scan time — a typo would otherwise leave the address unprotected until someone noticed the bans. * fix(limitip): keep each doc comment on its function and one grammar for the list Three review follow-ups. loadAllowlist landed between hasLimitIp's doc comment and hasLimitIp itself, so godoc showed one function's rationale above another's body; it now sits after that function with its own comment. The parser advertised semicolons and whitespace as separators while the settings validator accepts commas only, making those forms unreachable through the panel and the API — a promise the software never keeps. Both sides now read the same comma-separated grammar. The dist stub was a build artifact and does not belong in the tree. * chore: drop the accidentally committed dist build stub internal/web/dist/.gitkeep is what make dist-stub creates locally. Committing it changes fresh-clone behaviour for everyone: today a bare go build fails loudly on //go:embed all:dist, which is the documented signal to run the stub target; with the file present the build succeeds and the panel serves an empty dist instead. * chore(i18n): translate the IP limit allowlist strings into the remaining locales Ten locales carried the English source text verbatim; only ru-RU and uk-UA were translated. The i18n dead-key test only checks that a key exists in every file, so an untranslated value passes it silently. Wording follows each locale's existing terms: the ipLimit noun already in the file, and the comma-separated IP/CIDR phrasing from trustedProxyCidrsDesc. * refactor(limitip): share one IP/CIDR list validator and read the allowlist only when enforcing The allowlist check in CheckValid was a line-for-line copy of the trusted-proxy loop directly above it. Both now call one helper, each passing its own message, so the two lists cannot drift apart. Run() read the allowlist on every 10s scan, including the majority of panels where no client carries an IP limit and the value is discarded. It is now read only once enforcement is known to apply. CheckValid had no test for either list. The new one pins that a malformed entry is rejected and that each list still names itself in the error, which is what the shared helper could otherwise break. --------- Co-authored-by: n0ctal <n0ctal@users.noreply.github.com> Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
@@ -35,6 +35,7 @@ type CheckClientIpJob struct {
|
||||
disAllowedIps []string
|
||||
bannedSeen map[string]int64
|
||||
xrayService service.XrayService
|
||||
allowlist ipLimitAllowlist
|
||||
}
|
||||
|
||||
var job *CheckClientIpJob
|
||||
@@ -67,7 +68,13 @@ func (j *CheckClientIpJob) Run() {
|
||||
if hasLimit {
|
||||
f2bInstalled = j.checkFail2BanInstalled()
|
||||
}
|
||||
j.processObserved(observed, j.resolveEnforce(hasLimit, f2bInstalled), true)
|
||||
// Read only when the limit is actually applied: this runs every 10s and
|
||||
// most panels carry no IP limit at all.
|
||||
enforce := j.resolveEnforce(hasLimit, f2bInstalled)
|
||||
if enforce {
|
||||
j.allowlist = j.loadAllowlist()
|
||||
}
|
||||
j.processObserved(observed, enforce, true)
|
||||
}
|
||||
|
||||
// resolveEnforce decides whether limits can actually be enforced this run.
|
||||
@@ -126,6 +133,18 @@ func (j *CheckClientIpJob) hasLimitIp() bool {
|
||||
return err == nil && probe > 0
|
||||
}
|
||||
|
||||
// loadAllowlist reads the operator's trusted addresses once per scan; a bad
|
||||
// read leaves the list empty, which enforces the limit as before rather than
|
||||
// silently exempting everyone.
|
||||
func (j *CheckClientIpJob) loadAllowlist() ipLimitAllowlist {
|
||||
raw, err := (&service.SettingService{}).GetIpLimitAllowlist()
|
||||
if err != nil {
|
||||
logger.Warning("[LimitIP] could not read the allowlist, enforcing without it:", err)
|
||||
return ipLimitAllowlist{}
|
||||
}
|
||||
return parseIpLimitAllowlist(raw)
|
||||
}
|
||||
|
||||
const ipScanChunk = 400
|
||||
|
||||
func chunkEmails(s []string, size int) [][]string {
|
||||
@@ -510,7 +529,11 @@ func (j *CheckClientIpJob) updateInboundClientIps(tx *gorm.DB, inboundClientIps
|
||||
j.disAllowedIps = []string{}
|
||||
|
||||
// historical db-only ips are excluded from this count on purpose.
|
||||
keptLive, bannedLive := selectIpsToBan(liveIps, limitIp)
|
||||
limitedIps, allowedIps := j.allowlist.split(liveIps)
|
||||
keptLive, bannedLive := selectIpsToBan(limitedIps, limitIp)
|
||||
// Allowlisted addresses stay connected and out of the count: charging them
|
||||
// against the limit would still cut the shared network the entry protects.
|
||||
keptLive = append(keptLive, allowedIps...)
|
||||
actionable := j.filterAdvancedSinceLastBan(clientEmail, bannedLive)
|
||||
if len(actionable) > 0 {
|
||||
shouldCleanLog = true
|
||||
|
||||
Reference in New Issue
Block a user