mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-04 13:12:07 +03:00
15d82a5e47
Panels up to v2.x stored a client's tgId as a string, "" when unset. The startup migration heals copies already in the database, but an exported inbound imported into a current panel goes straight to AddInbound, whose typed client parse failed with "cannot unmarshal string into Go struct field .0.tgId of type int64", so every such import was refused. AddInbound now runs the legacy normalizer the clients-table seeder already used (moved from database to model so both share it) over the incoming settings before parsing them. String numbers become integers and empty ones are dropped, and the settings are stored in that shape. The same applies to /inbounds/add callers still sending the old types. The seeder change is a pure move; the PostgreSQL lane was not run (no Docker on this host) and is left to CI. Closes #6663
45 lines
1.6 KiB
Go
45 lines
1.6 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
// Exports from v2.x store tgId as a string ("" when unset); the stored copies
|
|
// are healed by a startup migration, but an imported export never passes it.
|
|
func TestAddInboundAcceptsLegacyStringClientFields(t *testing.T) {
|
|
setupConflictDB(t)
|
|
settings := `{"clients":[` +
|
|
`{"id":"11111111-1111-1111-1111-111111111111","email":"legacy-a","tgId":"","subId":"s-a","enable":true,"limitIp":0,"totalGB":0,"expiryTime":0,"reset":0},` +
|
|
`{"id":"22222222-2222-2222-2222-222222222222","email":"legacy-b","tgId":"123456","subId":"s-b","enable":true,"limitIp":0,"totalGB":0,"expiryTime":0,"reset":0}` +
|
|
`],"decryption":"none","fallbacks":[]}`
|
|
in := makeImportInbound("in-9201-tcp", 9201, settings, nil)
|
|
|
|
saved, _, err := (&InboundService{}).AddInbound(in)
|
|
if err != nil {
|
|
t.Fatalf("AddInbound: %v", err)
|
|
}
|
|
|
|
var rec model.ClientRecord
|
|
if err := database.GetDB().Where("email = ?", "legacy-b").First(&rec).Error; err != nil {
|
|
t.Fatalf("read client: %v", err)
|
|
}
|
|
if rec.TgID != 123456 {
|
|
t.Fatalf("client tgId = %d, want 123456 parsed from the string", rec.TgID)
|
|
}
|
|
var stored struct {
|
|
Clients []map[string]any `json:"clients"`
|
|
}
|
|
if err := json.Unmarshal([]byte(saved.Settings), &stored); err != nil {
|
|
t.Fatalf("stored settings: %v", err)
|
|
}
|
|
for _, c := range stored.Clients {
|
|
if _, isString := c["tgId"].(string); isString {
|
|
t.Fatalf("stored settings keep a string tgId for %v: %v", c["email"], c["tgId"])
|
|
}
|
|
}
|
|
}
|