More table

This commit is contained in:
Fangliding
2026-06-06 06:11:54 +08:00
parent 0e6ab63b8a
commit 87c08d0d7a
2 changed files with 33 additions and 12 deletions
+20
View File
@@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"math" "math"
"math/big"
"net/netip" "net/netip"
"net/url" "net/url"
"os" "os"
@@ -381,6 +382,14 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
} }
if c.SessionIDTable != "" { if c.SessionIDTable != "" {
if predefined, ok := splithttp.PredefinedTable[c.SessionIDTable]; ok {
c.SessionIDTable = predefined
}
room := roomSize(len(c.SessionIDTable), c.SessionIDLength.From, c.SessionIDLength.To)
// 2.1B possiblities should be enough
if room.Cmp(big.NewInt(2<<30)) < 0 {
return nil, errors.New("sessionIDTable or sessionIDLength is too small")
}
if c.SessionIDLength.From <= 0 { if c.SessionIDLength.From <= 0 {
return nil, errors.New("sessionIDLength.from must be greater than 0") return nil, errors.New("sessionIDLength.from must be greater than 0")
} }
@@ -454,6 +463,17 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
return config, nil return config, nil
} }
func roomSize(tableSize int, min, max int32) *big.Int {
base := big.NewInt(int64(tableSize))
sum := new(big.Int)
term := new(big.Int)
for k := min; k <= max; k++ {
term.Exp(base, big.NewInt(int64(k)), nil)
sum.Add(sum, term)
}
return sum
}
const ( const (
Byte = 1 Byte = 1
Kilobyte = 1024 * Byte Kilobyte = 1024 * Byte
+13 -12
View File
@@ -488,22 +488,23 @@ func (c *RangeConfig) rand() int32 {
} }
// predefined // predefined
var ( var PredefinedTable = map[string]string{
base62Table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" "number": "0123456789",
hexTable = "0123456789abcdef" "base62": "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
hexTableUpper = "0123456789ABCDEF" "base36": "0123456789abcdefghijklmnopqrstuvwxyz",
) "BASE36": "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"hex": "0123456789abcdef",
"HEX": "0123456789ABCDEF",
"Alphabet": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
"ALPHABET": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"alphabet": "abcdefghijklmnopqrstuvwxyz",
}
func (c *Config) GenerateSessionID() string { func (c *Config) GenerateSessionID() string {
length := c.SessionIDLength.rand() length := c.SessionIDLength.rand()
table := c.SessionIDTable table := c.SessionIDTable
switch table { if predefined, ok := PredefinedTable[table]; ok {
case "base62": table = predefined
table = base62Table
case "hex":
table = hexTable
case "HEX":
table = hexTableUpper
} }
if table != "" && length > 0 { if table != "" && length > 0 {
id := make([]byte, length) id := make([]byte, length)