fix(clients): backfill missing subId on startup and guard create/update

Legacy clients (and any API consumer that POSTs to AddInboundClient
without a subId) ended up with an empty SubID, which breaks the panel's
sub-link generation. Backfill them once at startup and stop the gap at
the write path so new clients can't reintroduce it.

- util/random: add NumLower(n) — 16-char [0-9a-z] generator that matches
  the frontend's RandomUtil.randomLowerAndNum convention.
- database/db.go: new InboundClientSubIdFix seeder, modeled on
  InboundClientTgIdFix. Loops every inbound, parses settings.clients,
  fills empty/missing subId with random.NumLower(16), persists via the
  same transaction-wrapped Update("settings", …) path, then records in
  HistoryOfSeeders so it runs at most once.
- web/service/client.go: defense-in-depth in AddInboundClient and
  UpdateInboundClient — fill subId on the persisted settings map when
  the payload omits it (Update prefers the previous value before
  generating a fresh one).
- database/db_seed_test: cover empty subId, missing-key subId, and
  preserved-existing subId; assert exactly one HistoryOfSeeders row.
This commit is contained in:
MHSanaei
2026-05-28 18:20:34 +02:00
parent 72b97efa8a
commit 99df5d70a8
4 changed files with 171 additions and 1 deletions
+13
View File
@@ -51,6 +51,19 @@ func Seq(n int) string {
return string(runes)
}
// NumLower generates a random string of length n containing digits and lowercase letters only.
func NumLower(n int) string {
runes := make([]rune, n)
for i := range n {
idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(numLowerSeq))))
if err != nil {
panic("crypto/rand failed: " + err.Error())
}
runes[i] = numLowerSeq[idx.Int64()]
}
return string(runes)
}
// Num generates a random integer between 0 and n-1.
func Num(n int) int {
bn := big.NewInt(int64(n))