mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-16 00:01:02 +00:00
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:
@@ -0,0 +1,62 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func assertClientHwidSchema(t *testing.T, db *gorm.DB) {
|
||||
t.Helper()
|
||||
if !db.Migrator().HasColumn(&model.ClientRecord{}, "limit_hwid") {
|
||||
t.Fatalf("clients.limit_hwid missing")
|
||||
}
|
||||
if !db.Migrator().HasTable(&model.ClientHwid{}) {
|
||||
t.Fatalf("client_hwids table missing")
|
||||
}
|
||||
for _, col := range []string{"sub_id", "hwid_hash", "first_seen", "last_seen", "user_agent", "device_os", "os_version", "device_model"} {
|
||||
if !db.Migrator().HasColumn(&model.ClientHwid{}, col) {
|
||||
t.Fatalf("client_hwids.%s missing", col)
|
||||
}
|
||||
}
|
||||
if !db.Migrator().HasIndex(&model.ClientHwid{}, "idx_client_hwids_sub_hash") {
|
||||
t.Fatalf("client_hwids unique hash index missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientHwidSchemaSQLite(t *testing.T) {
|
||||
dbDir := t.TempDir()
|
||||
t.Setenv("XUI_DB_FOLDER", dbDir)
|
||||
if err := InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
|
||||
t.Fatalf("InitDB: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = CloseDB() })
|
||||
assertClientHwidSchema(t, GetDB())
|
||||
}
|
||||
|
||||
func TestClientHwidSchemaPostgres(t *testing.T) {
|
||||
dsn := strings.TrimSpace(os.Getenv("XUI_TEST_PG_DSN"))
|
||||
if dsn == "" {
|
||||
t.Skip("set XUI_TEST_PG_DSN to a reachable Postgres to run this test")
|
||||
}
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.Discard})
|
||||
if err != nil {
|
||||
t.Fatalf("open postgres: %v", err)
|
||||
}
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
t.Fatalf("postgres db handle: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = sqlDB.Close() })
|
||||
if err := db.AutoMigrate(&model.ClientRecord{}, &model.ClientHwid{}); err != nil {
|
||||
t.Fatalf("automigrate postgres: %v", err)
|
||||
}
|
||||
assertClientHwidSchema(t, db)
|
||||
}
|
||||
@@ -75,6 +75,7 @@ func allModels() []any {
|
||||
&model.ApiToken{},
|
||||
&model.ClientRecord{},
|
||||
&model.ClientInbound{},
|
||||
&model.ClientHwid{},
|
||||
&model.ClientExternalLink{},
|
||||
&model.ClientGroup{},
|
||||
&model.InboundFallback{},
|
||||
|
||||
@@ -48,6 +48,7 @@ func migrationModels() []any {
|
||||
&model.InboundClientIps{},
|
||||
&model.ClientRecord{},
|
||||
&model.ClientInbound{},
|
||||
&model.ClientHwid{},
|
||||
&model.ClientExternalLink{},
|
||||
&model.ClientGroup{},
|
||||
&model.InboundFallback{},
|
||||
|
||||
@@ -914,6 +914,7 @@ type ClientRecord struct {
|
||||
Secret string `json:"secret" gorm:"column:secret"`
|
||||
AdTag string `json:"adTag" gorm:"column:ad_tag;default:''"`
|
||||
LimitIP int `json:"limitIp" gorm:"column:limit_ip"`
|
||||
LimitHwid int `json:"limitHwid" gorm:"column:limit_hwid;default:0"`
|
||||
TotalGB int64 `json:"totalGB" gorm:"column:total_gb"`
|
||||
ExpiryTime int64 `json:"expiryTime" gorm:"column:expiry_time"`
|
||||
Enable bool `json:"enable" gorm:"default:true"`
|
||||
@@ -981,6 +982,20 @@ type ClientInbound struct {
|
||||
|
||||
func (ClientInbound) TableName() string { return "client_inbounds" }
|
||||
|
||||
type ClientHwid struct {
|
||||
Id int `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||
SubID string `json:"subId" gorm:"column:sub_id;not null;index;uniqueIndex:idx_client_hwids_sub_hash,priority:1"`
|
||||
HwidHash string `json:"-" gorm:"column:hwid_hash;size:64;not null;uniqueIndex:idx_client_hwids_sub_hash,priority:2"`
|
||||
FirstSeen int64 `json:"firstSeen" gorm:"column:first_seen;not null"`
|
||||
LastSeen int64 `json:"lastSeen" gorm:"column:last_seen;not null;index"`
|
||||
UserAgent string `json:"userAgent" gorm:"column:user_agent"`
|
||||
DeviceOS string `json:"deviceOs" gorm:"column:device_os"`
|
||||
OsVersion string `json:"osVersion" gorm:"column:os_version"`
|
||||
DeviceModel string `json:"deviceModel" gorm:"column:device_model"`
|
||||
}
|
||||
|
||||
func (ClientHwid) TableName() string { return "client_hwids" }
|
||||
|
||||
// ClientExternalLink is a per-client entry surfaced in the client's
|
||||
// subscription. Two kinds:
|
||||
// - "link": a single third-party share link (vless://, vmess://, trojan://,
|
||||
@@ -1267,6 +1282,16 @@ func MergeClientRecord(existing *ClientRecord, incoming *ClientRecord) []ClientM
|
||||
existing.LimitIP = picked
|
||||
}
|
||||
}
|
||||
if existing.LimitHwid != incoming.LimitHwid && incoming.LimitHwid != 0 {
|
||||
picked := existing.LimitHwid
|
||||
if existing.LimitHwid == 0 || incoming.LimitHwid > existing.LimitHwid {
|
||||
picked = incoming.LimitHwid
|
||||
}
|
||||
if picked != existing.LimitHwid {
|
||||
keep("limitHwid", existing.LimitHwid, incoming.LimitHwid, picked)
|
||||
existing.LimitHwid = picked
|
||||
}
|
||||
}
|
||||
if existing.TgID != incoming.TgID && incoming.TgID != 0 {
|
||||
if incomingNewer || existing.TgID == 0 {
|
||||
keep("tgId", existing.TgID, incoming.TgID, incoming.TgID)
|
||||
|
||||
Reference in New Issue
Block a user