Files
mirror-3x-ui/internal/sub/hwid_rejection.go
T
animesha3 6bc7e81c90 feat(clients): add per-subscription HWID device bans (#7133)
An operator could remove a device from a subscription, but it simply registered again on its next refresh; there was no way to keep one device out.

Add Block and Unblock to the device list. A banned device is refused on every subscription route whether or not the client has a device limit, does not hold a registration slot, and survives trimming and clearing the list. Unblocking needs a free slot, so registered devices never exceed the limit, and a banned device whose configs are replaced is told it is blocked.
2026-11-24 21:26:44 +03:30

77 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"
switch {
case result.Blocked:
remark = "Device blocked"
case 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))
}