feat(sub): add per-client subscription HWID limits (#5802)

* feat(sub): add per-client subscription HWID limits

* fix(sub): address HWID review on shared subId and bulk create

* fix(sub): store HWID devices by sub_id and drop anchor client workaround

* fix(sub): restore UA auto-detect and HTML page routing in subs()

The cherry-pick of the HWID gate onto main's refactored SUBController
had dropped main's UA-based format auto-detection and sub-page handling
from subs(). Restore those branches, slotting enforceHwid after the
HTML page and before format detection so the gate only applies to
machine-readable subscription bodies.

Also adapt tests to main's options-struct constructor and to the
ClientService.Update signature extended with limitHwid.

* fix(frontend): drop axios from HttpUtil.delete

The bulk-delete rework's committed version still referenced axios,
which this file no longer imports, breaking typecheck in CI. Use the
httpRequest wrapper like the other verbs.

---------

Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
Rouzbeh†
2026-08-15 18:20:20 +03:30
committed by GitHub
parent 1793a9b8b4
commit 694ad6deae
45 changed files with 1212 additions and 50 deletions
+45
View File
@@ -72,6 +72,7 @@ type SUBController struct {
subService *SubService
subJsonService *SubJsonService
subClashService *SubClashService
clientService service.ClientService
settingService service.SettingService
subTemplateMu sync.RWMutex
@@ -384,6 +385,9 @@ func (a *SUBController) subs(c *gin.Context) {
logSubscriptionRoute(userAgent, "html")
return
}
if !a.enforceHwid(c) {
return
}
if shouldAutoServeClash(a.subClashAutoDetect, a.clashEnabled, false, userAgent, a.clashUserAgent) && a.serveClashBody(c, false) {
a.recordSubscriptionFetch(c)
logSubscriptionRoute(userAgent, "clash")
@@ -605,6 +609,41 @@ func (a *SUBController) subPageContext(page PageData) map[string]any {
}
}
func (a *SUBController) enforceHwid(c *gin.Context) bool {
result, err := a.clientService.EnforceHwidForSubID(c.Param("subid"), service.HwidRequest{
Hwid: c.GetHeader("X-HWID"),
UserAgent: c.GetHeader("User-Agent"),
DeviceOS: c.GetHeader("X-Device-OS"),
OsVersion: c.GetHeader("X-Ver-OS"),
DeviceModel: c.GetHeader("X-Device-Model"),
})
if err != nil {
writeSubError(c, err)
return false
}
applyHwidHeaders(c, result)
if !result.Allowed {
c.Status(http.StatusNotFound)
return false
}
return true
}
func applyHwidHeaders(c *gin.Context, result service.HwidGateResult) {
if result.Active {
c.Header("X-Hwid-Active", "true")
}
if result.NotSupported {
c.Header("X-Hwid-Not-Supported", "true")
}
if result.LimitReached {
c.Header("X-Hwid-Limit", "true")
}
if result.MaxDevicesReached {
c.Header("X-Hwid-Max-Devices-Reached", "true")
}
}
// setNoCacheHeaders marks a subscription page response as non-cacheable so VPN
// clients and browsers always fetch fresh traffic/expiry data.
func setNoCacheHeaders(c *gin.Context) {
@@ -668,6 +707,9 @@ func (a *SUBController) subJsons(c *gin.Context) {
if a.maybeServeSubPage(c) {
return
}
if !a.enforceHwid(c) {
return
}
a.serveJson(c, a.jsonAlwaysArray, "text/plain; charset=utf-8")
}
@@ -713,6 +755,9 @@ func (a *SUBController) subClashs(c *gin.Context) {
if a.maybeServeSubPage(c) {
return
}
if !a.enforceHwid(c) {
return
}
if !a.serveClashBody(c, false) {
writeSubError(c, nil)
}