Files
3x-ui/util/random/random.go
MHSanaei 99df5d70a8 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.
2026-05-28 18:20:34 +02:00

87 lines
2.3 KiB
Go

// Package random provides utilities for generating random strings and numbers.
package random
import (
"crypto/rand"
"encoding/base64"
"math/big"
)
var (
numSeq [10]rune
lowerSeq [26]rune
upperSeq [26]rune
numLowerSeq [36]rune
numUpperSeq [36]rune
allSeq [62]rune
)
// init initializes the character sequences used for random string generation.
// It sets up arrays for numbers, lowercase letters, uppercase letters, and combinations.
func init() {
for i := range 10 {
numSeq[i] = rune('0' + i)
}
for i := range 26 {
lowerSeq[i] = rune('a' + i)
upperSeq[i] = rune('A' + i)
}
copy(numLowerSeq[:], numSeq[:])
copy(numLowerSeq[len(numSeq):], lowerSeq[:])
copy(numUpperSeq[:], numSeq[:])
copy(numUpperSeq[len(numSeq):], upperSeq[:])
copy(allSeq[:], numSeq[:])
copy(allSeq[len(numSeq):], lowerSeq[:])
copy(allSeq[len(numSeq)+len(lowerSeq):], upperSeq[:])
}
// Seq generates a random string of length n containing alphanumeric characters (numbers, lowercase and uppercase letters).
func Seq(n int) string {
runes := make([]rune, n)
for i := range n {
idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(allSeq))))
if err != nil {
panic("crypto/rand failed: " + err.Error())
}
runes[i] = allSeq[idx.Int64()]
}
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))
r, err := rand.Int(rand.Reader, bn)
if err != nil {
panic("crypto/rand failed: " + err.Error())
}
return int(r.Int64())
}
// Base64Bytes returns n cryptographically-random bytes encoded as standard
// base64 (with padding). Used for ss2022 keys, which xray expects as a
// base64-encoded key of a specific byte length per cipher.
func Base64Bytes(n int) string {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
panic("crypto/rand failed: " + err.Error())
}
return base64.StdEncoding.EncodeToString(b)
}