Files
mirror-3x-ui/internal/sub/hwid_rejection.go
T
animesha3 77719154f6 feat(sub): optionally replace configs rejected by HWID limits (#7106)
When a device is removed and the device limit lowered, its next subscription refresh gets an empty 404, and most apps keep the working nodes they downloaded earlier.

Add a setting, off by default: a refresh the device limit rejects, or one that arrives with no HWID, gets a normal response holding a single dead node in the format that was asked for, so an app that accepts the update replaces its saved nodes. The dead node comes from the same status-node builder the expired and depleted cases use, with no subscriber credentials and no local fallback.
2026-11-24 21:15:23 +03:30

74 lines
2.0 KiB
Go

package sub
import (
"encoding/base64"
"net/http"
"github.com/gin-gonic/gin"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
)
type hwidResponseFormat int
const (
hwidLinks hwidResponseFormat = iota
hwidJSON
hwidJSONArray
hwidClash
)
// A parseable dummy node lets subscription apps replace previously downloaded nodes.
// Never reuse subscriber credentials or include a direct-connection fallback here.
func (a *SUBController) writeHwidRejection(c *gin.Context, result service.HwidGateResult, format hwidResponseFormat) {
enabled, err := a.settingService.GetSubHwidRejectWithConfig()
if err != nil {
writeSubError(c, err)
return
}
if !enabled {
applyHwidHeaders(c, result)
c.Status(http.StatusNotFound)
return
}
remark := "Device limit reached"
if result.NotSupported {
remark = "HWID required"
}
node := statusNode{remark: remark, blocked: true}
contentType := "text/plain; charset=utf-8"
var body string
switch format {
case hwidJSON, hwidJSONArray:
contentType = "application/json; charset=utf-8"
body = string(a.subJsonService.genStatusNodeConfig(node))
if format == hwidJSONArray || a.jsonAlwaysArray {
body = "[" + body + "]"
}
case hwidClash:
contentType = "application/yaml; charset=utf-8"
config := newClashConfig([]map[string]any{node.clashProxy()}, false)
yaml, err := marshalClashYAML(config)
if err != nil {
writeSubError(c, err)
return
}
body = string(yaml)
default:
body = node.link() + "\n"
if a.subEncrypt {
body = base64.StdEncoding.EncodeToString([]byte(body))
}
}
c.Header("Profile-Update-Interval", a.updateInterval)
// Rejection headers can make apps discard a successful replacement response.
applyHwidHeaders(c, service.HwidGateResult{Active: result.Active})
c.Header("Announce", "base64:"+base64.StdEncoding.EncodeToString([]byte(remark)))
c.Header("Content-Type", contentType)
if c.Request.Method == http.MethodHead {
c.Status(http.StatusOK)
return
}
c.Data(http.StatusOK, contentType, []byte(body))
}