Files
3x-ui/internal/web/service/client_hwid.go
T
Namso9 89ee1242bd feat(sub): add read-only HWID device-slot status endpoint (#6380)
* feat(sub): add read-only HWID device-slot status endpoint

Closes #6357

A client with an HWID limit had no way to tell a subscriber how many device
slots were left: /{subPath}/{subId} only exposes the gate as a boolean through
X-Hwid-* headers on a 404, and ?format=info carries no limitHwid or registered
count. Every "why can't I connect on my new phone" case therefore had to be
answered by the operator by hand.

GET /{subPath}/{subId}/hwid-status now returns the aggregate counters:

  {"active":true,"limit":2,"registered":1,"remaining":1,"full":false}

- SELECT-only. It never registers an hwid, never touches last_seen and never
  calls the enforcement path, so asking about a slot cannot spend one.
- Counters only: no hwid value or hash, no email, no device metadata, no IP,
  no User-Agent, and none of the X-Hwid-* gate headers.
- The subscription id is already the bearer secret for /{subPath}/{subId}, so
  no admin token and no new auth mechanism.
- Unknown and disabled subscriptions both answer a bare 404, with identical
  status, headers and body, so the route cannot be used to probe which
  subscription ids exist.
- No HWID limit configured returns {"active":false,"limit":0,...}.
- No schema change and no migration.

Scoped to enabled clients exactly like effectiveHwidLimitForSubID, so the
reported limit is always the limit the gate enforces on a shared sub_id, and
remaining clamps at zero when the effective limit drops below the number of
registered devices. A separate route leaves /{subPath}/{subId}, ?format=info
and the JSON/Clash routes byte-for-byte unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(sub): document hwid-status as the bare object it returns

The OpenAPI operation for GET /{subPath}/{subId}/hwid-status inherited the
{success,msg,obj} panel envelope from build-openapi.mjs's default 200
response, while the handler writes the HwidSlotStatus struct bare. A client
generated from the spec would read `obj` and never find the counters, and
the description prose contradicted the schema with a hand-written example.

HwidSlotStatus now sits in openapigen's StructAllow with example: tags, the
entry references the generated schema through a `responses` block, and
build-openapi.mjs attaches the generated example to any `responses` entry
that $refs a generated schema, so no example is hand-written. The HEAD
variant the controller registers is documented like its siblings, and the
summary follows the "path prefix is configured by subPath" wording now that
fresh panels randomise the prefix.

Regenerated frontend/public/openapi.json, docs/public/openapi.json and the
subscription-server MDX. openapi-runtime-contracts.test.ts pins the bare
schema, the generated example and the HEAD operation.

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
2026-09-11 11:59:35 +02:00

342 lines
8.8 KiB
Go

package service
import (
"crypto/sha256"
"encoding/hex"
"errors"
"strings"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"gorm.io/gorm"
)
type HwidRequest struct {
Hwid string
UserAgent string
DeviceOS string
OsVersion string
DeviceModel string
}
type HwidGateResult struct {
Allowed bool
Active bool
NotSupported bool
MaxDevicesReached bool
LimitReached bool
Limit int
Registered int
}
// HwidSlotStatus is the aggregate device-slot view exposed to subscribers:
// counters only, no hwid value or hash, no email, no device metadata.
type HwidSlotStatus struct {
Active bool `json:"active" example:"true"`
Limit int `json:"limit" example:"2"`
Registered int `json:"registered" example:"1"`
Remaining int `json:"remaining" example:"1"`
Full bool `json:"full" example:"false"`
}
const minHwidLength = 6
type ClientHwidInfo struct {
Id int `json:"id"`
FirstSeen int64 `json:"firstSeen"`
LastSeen int64 `json:"lastSeen"`
UserAgent string `json:"userAgent"`
DeviceOS string `json:"deviceOs"`
OsVersion string `json:"osVersion"`
DeviceModel string `json:"deviceModel"`
}
func hashHwid(raw string) string {
sum := sha256.Sum256([]byte(raw))
return hex.EncodeToString(sum[:])
}
func trimHwidMeta(s string) string {
s = strings.TrimSpace(s)
r := []rune(s)
if len(r) > 512 {
return string(r[:512])
}
return s
}
func normalizeHwidRequest(req HwidRequest) HwidRequest {
return HwidRequest{
Hwid: strings.TrimSpace(req.Hwid),
UserAgent: trimHwidMeta(req.UserAgent),
DeviceOS: trimHwidMeta(req.DeviceOS),
OsVersion: trimHwidMeta(req.OsVersion),
DeviceModel: trimHwidMeta(req.DeviceModel),
}
}
func effectiveHwidLimitForSubID(tx *gorm.DB, subID string) (int, error) {
var limit int
err := tx.Model(&model.ClientRecord{}).
Where("sub_id = ? AND enable = ?", subID, true).
Select("COALESCE(MAX(limit_hwid), 0)").
Scan(&limit).Error
return limit, err
}
func (s *ClientService) EnforceHwidForSubID(subID string, req HwidRequest) (HwidGateResult, error) {
var res HwidGateResult
subID = strings.TrimSpace(subID)
if subID == "" {
res.Allowed = true
return res, nil
}
db := database.GetDB()
limit, err := effectiveHwidLimitForSubID(db, subID)
if err != nil {
return res, err
}
if limit <= 0 {
res.Allowed = true
return res, nil
}
req = normalizeHwidRequest(req)
res.Active = true
res.Limit = limit
if len(req.Hwid) < minHwidLength {
res.NotSupported = true
return res, nil
}
hwidHash := hashHwid(req.Hwid)
err = db.Transaction(func(tx *gorm.DB) error {
limit, err := effectiveHwidLimitForSubID(tx, subID)
if err != nil {
return err
}
if limit <= 0 {
res = HwidGateResult{Allowed: true}
return nil
}
res.Active = true
res.Limit = limit
now := time.Now().UnixMilli()
var existing model.ClientHwid
err = tx.Where("sub_id = ? AND hwid_hash = ?", subID, hwidHash).First(&existing).Error
if err == nil {
if err := tx.Model(&model.ClientHwid{}).Where("id = ?", existing.Id).Updates(map[string]any{
"last_seen": now, "user_agent": req.UserAgent, "device_os": req.DeviceOS, "os_version": req.OsVersion, "device_model": req.DeviceModel,
}).Error; err != nil {
return err
}
var count int64
if err := tx.Model(&model.ClientHwid{}).Where("sub_id = ?", subID).Count(&count).Error; err != nil {
return err
}
res.Allowed = true
res.Registered = int(count)
res.LimitReached = count >= int64(limit)
return nil
}
if !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
var count int64
if err := tx.Model(&model.ClientHwid{}).Where("sub_id = ?", subID).Count(&count).Error; err != nil {
return err
}
res.Registered = int(count)
if count >= int64(limit) {
res.MaxDevicesReached = true
res.LimitReached = true
return nil
}
if err := tx.Create(&model.ClientHwid{SubID: subID, HwidHash: hwidHash, FirstSeen: now, LastSeen: now, UserAgent: req.UserAgent, DeviceOS: req.DeviceOS, OsVersion: req.OsVersion, DeviceModel: req.DeviceModel}).Error; err != nil {
return err
}
res.Allowed = true
res.Registered = int(count) + 1
res.LimitReached = res.Registered >= limit
return nil
})
return res, err
}
// HwidSlotStatusForSubID is SELECT-only: it must never write client_hwids or
// last_seen. Enabled-clients scope mirrors the gate, so limit == limit enforced.
func (s *ClientService) HwidSlotStatusForSubID(subID string) (status HwidSlotStatus, found bool, err error) {
subID = strings.TrimSpace(subID)
if subID == "" {
return status, false, nil
}
db := database.GetDB()
var enabled int64
if err := db.Model(&model.ClientRecord{}).
Where("sub_id = ? AND enable = ?", subID, true).
Count(&enabled).Error; err != nil {
return status, false, err
}
if enabled == 0 {
return status, false, nil
}
limit, err := effectiveHwidLimitForSubID(db, subID)
if err != nil {
return status, false, err
}
if limit <= 0 {
return status, true, nil
}
var registered int64
if err := db.Model(&model.ClientHwid{}).Where("sub_id = ?", subID).Count(&registered).Error; err != nil {
return status, false, err
}
status.Active = true
status.Limit = limit
status.Registered = int(registered)
status.Remaining = max(limit-status.Registered, 0)
status.Full = status.Registered >= limit
return status, true, nil
}
func (s *ClientService) ListClientHwids(email string) ([]ClientHwidInfo, error) {
rec, err := s.GetRecordByEmail(nil, email)
if err != nil {
return nil, err
}
subID := strings.TrimSpace(rec.SubID)
if subID == "" {
return nil, nil
}
var rows []model.ClientHwid
if err := database.GetDB().
Where("sub_id = ?", subID).
Order("last_seen DESC").
Order("id DESC").
Find(&rows).Error; err != nil {
return nil, err
}
out := make([]ClientHwidInfo, 0, len(rows))
for _, r := range rows {
out = append(out, ClientHwidInfo{
Id: r.Id,
FirstSeen: r.FirstSeen,
LastSeen: r.LastSeen,
UserAgent: r.UserAgent,
DeviceOS: r.DeviceOS,
OsVersion: r.OsVersion,
DeviceModel: r.DeviceModel,
})
}
return out, nil
}
func (s *ClientService) ClearClientHwids(email string) error {
rec, err := s.GetRecordByEmail(nil, email)
if err != nil {
return err
}
subID := strings.TrimSpace(rec.SubID)
if subID == "" {
return nil
}
return database.GetDB().Where("sub_id = ?", subID).Delete(&model.ClientHwid{}).Error
}
// DeleteClientHwid removes one device, scoped to the client's sub_id: ids
// are a global auto-increment, so an id outside this subscription won't match.
func (s *ClientService) DeleteClientHwid(email string, id int) error {
rec, err := s.GetRecordByEmail(nil, email)
if err != nil {
return err
}
subID := strings.TrimSpace(rec.SubID)
if subID == "" {
return errors.New("client has no subscription id")
}
res := database.GetDB().Where("sub_id = ? AND id = ?", subID, id).Delete(&model.ClientHwid{})
if res.Error != nil {
return res.Error
}
if res.RowsAffected == 0 {
return errors.New("device not found")
}
return nil
}
func (s *ClientService) setClientLimitHwidByEmail(tx *gorm.DB, email string, limit int) error {
if tx == nil {
tx = database.GetDB()
}
if limit < 0 {
limit = 0
}
var rec model.ClientRecord
if err := tx.Where("email = ?", email).First(&rec).Error; err != nil {
return err
}
if err := tx.Model(&model.ClientRecord{}).Where("id = ?", rec.Id).UpdateColumn("limit_hwid", limit).Error; err != nil {
return err
}
subID := strings.TrimSpace(rec.SubID)
if subID == "" {
return nil
}
effective, err := effectiveHwidLimitForSubID(tx, subID)
if err != nil {
return err
}
return trimClientHwidsForSubID(tx, subID, effective)
}
func trimClientHwidsForSubID(tx *gorm.DB, subID string, limit int) error {
subID = strings.TrimSpace(subID)
if subID == "" || limit <= 0 {
return nil
}
var keep []int
if err := tx.Model(&model.ClientHwid{}).
Where("sub_id = ?", subID).
Order("last_seen DESC").
Order("id DESC").
Limit(limit).
Pluck("id", &keep).Error; err != nil {
return err
}
if len(keep) == 0 {
return tx.Where("sub_id = ?", subID).Delete(&model.ClientHwid{}).Error
}
return tx.Where("sub_id = ? AND id NOT IN ?", subID, keep).Delete(&model.ClientHwid{}).Error
}
func clearClientHwidsBySubIDTx(tx *gorm.DB, subIDs ...string) error {
if tx == nil {
tx = database.GetDB()
}
clean := make([]string, 0, len(subIDs))
seen := map[string]struct{}{}
for _, subID := range subIDs {
subID = strings.TrimSpace(subID)
if subID == "" {
continue
}
if _, ok := seen[subID]; ok {
continue
}
seen[subID] = struct{}{}
clean = append(clean, subID)
}
for _, batch := range chunkStrings(clean, sqlInChunk) {
if err := tx.Where("sub_id IN ?", batch).Delete(&model.ClientHwid{}).Error; err != nil {
return err
}
}
return nil
}