mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6966] feat(sub): opt-in per-client rate limit for the subscription server #6549
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6549
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/MHSanaei/3x-ui/pull/6966
Author: @amirim1
Created: 10/26/2026
Status: 🔄 Open
Base:
main← Head:feat/sub-rate-limit📝 Commits (7)
99d23ccfeat(sub): add an opt-in per-client rate limit to the subscription servera32d087fix(sub): gofmt controller; frontend settings model fieldsc1d0c51fix(sub): address adversarial review of the rate limit branchd028b0ctest(sub): prime the limiter before asserting 429 in the controller teste6a3639test(sub): init the DB and assert exact codes in the controller rate limit test5cdc9ecdocs(sub): note why static assets stay outside the rate limit guard250ff8efix(sub): address the review bot's low findings📊 Changes
29 files changed (+851 additions, -0 deletions)
View changed files
📝
docs/public/openapi.json(+20 -0)📝
frontend/public/openapi.json(+20 -0)📝
frontend/src/generated/examples.ts(+4 -0)📝
frontend/src/generated/schemas.ts(+20 -0)📝
frontend/src/generated/types.ts(+4 -0)📝
frontend/src/generated/zod.ts(+4 -0)📝
frontend/src/models/setting.ts(+2 -0)📝
frontend/src/pages/settings/SubscriptionGeneralTab.tsx(+28 -0)📝
frontend/src/schemas/setting.ts(+2 -0)📝
internal/sub/controller.go(+20 -0)➕
internal/sub/rate_limiter.go(+301 -0)➕
internal/sub/rate_limiter_test.go(+340 -0)📝
internal/sub/sub.go(+18 -0)📝
internal/web/entity/entity.go(+2 -0)📝
internal/web/service/setting.go(+10 -0)📝
internal/web/translation/ar-EG.json(+4 -0)📝
internal/web/translation/en-US.json(+4 -0)📝
internal/web/translation/es-ES.json(+4 -0)📝
internal/web/translation/fa-IR.json(+4 -0)📝
internal/web/translation/id-ID.json(+4 -0)...and 9 more files
📄 Description
Summary
Adds an opt-in per-client rate limit to the subscription server (
subRateLimitEnable,subRateLimitrequests/minute, default 60). When enabled, every subscription route (/sub/:id,/:id/hwid-status, JSON, Clash/Mihomo formats, the HTML sub page) passes through a bounded sliding-window limiter keyed by client identity. Over-limit requests get429 Too Many Requestswith aRetry-Afterheader pointing at when the oldest hit in the window ages out.Motivation: subscription bodies are cheap to serve but their reads are not free (inbounds join, per-email traffic loads, host rows), and
subIdis the only gate. A probe loop over guessed subscription IDs currently turns into a database load generator; this caps it at the configured rate.Design notes:
limittimestamps, so the worst case stays proportional to the configured limit.internal/web/controller/util.go getRemoteIp): the peer address, unless the peer is a trusted proxy (sharedtrustedProxyCIDRssetting), in which caseX-Forwarded-Foris walked from the proxy end and a client-suppliedX-Real-IPis only believed when it agrees with the forwarded chain. One deliberate deviation, documented in code: a partially invalid CIDR list fails closed to loopback instead of trusting the remaining entries, because a silently narrowed identity would lump every proxied client into one bucket.subRateLimitis clamped to 1–1000 on the read path too, not only on panel writes.AllSettingform fields →UpdateAllSettingreflection →defaultValueMapdefaults); the subscription server builds the limiter and resolver once atinitRouter, consistent with every other sub setting.frontend/src/generated/and bothopenapi.jsonfiles regenerated vianpm run gen./assetsare intentionally outside the guard (mounted on the engine before the controller; no database reads).Why
No rate limiting exists on the subscription server today (checked against
mainand the issue tracker; the panel's login limiter covers only login). Large installations can see thousands of subscription fetches per minute from client apps plus scanners probing randomsubIds; both share the same database. A per-client ceiling with trusted-proxy awareness is the cheap first layer — an exact continuation of the subscription-body caching shipped in #6758.Type
Areas affected
(Settings UI + i18n touched, hence Panel UI.)
How tested
Retry-Afterarithmetic, LRU + idle eviction, concurrent hammering under-race(injected clock, no sleeps).X-Real-IPfalls back to the proxy address, malformed headers, unparseable peer →unknown, wildcard CIDR refused.NewSUBController: with the limit on, every subscription format route returns 429 +Retry-Afterafter the first request; with it off, handlers answer 404 (exact codes, so a vacuous pass is impossible). Runs in isolation and under-shuffle=on.go build ./...,go vet,gofmt, fullinternal/subandinternal/web/servicesuites,-race, two shuffled seeds ofinternal/sub; frontendtsc --noEmit, oxlint;npm run genproduces a clean diff.Breaking changes
None. The limiter is disabled by default and adds two settings keys with shipped defaults.
Checklist
<area>: summaryconventionnpm run genrun, no generated-file drift🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.