mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-16 08:10:58 +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)
|
||||
}
|
||||
Reference in New Issue
Block a user