Custom sessionID

This commit is contained in:
Fangliding
2026-06-03 22:06:15 +08:00
parent fdb9b616fc
commit 2f121bd6a2
6 changed files with 109 additions and 43 deletions
+62 -30
View File
@@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"io"
"math/rand/v2"
"net/http"
"strings"
@@ -11,6 +12,7 @@ import (
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/crypto"
"github.com/xtls/xray-core/common/utils"
"github.com/xtls/xray-core/common/uuid"
"github.com/xtls/xray-core/transport/internet"
)
@@ -131,26 +133,26 @@ func (c *Config) GetNormalizedUplinkHTTPMethod() string {
return c.UplinkHTTPMethod
}
func (c *Config) GetNormalizedScMaxEachPostBytes() RangeConfig {
func (c *Config) GetNormalizedScMaxEachPostBytes() *RangeConfig {
if c.ScMaxEachPostBytes == nil || c.ScMaxEachPostBytes.To == 0 {
return RangeConfig{
return &RangeConfig{
From: 1000000,
To: 1000000,
}
}
return *c.ScMaxEachPostBytes
return c.ScMaxEachPostBytes
}
func (c *Config) GetNormalizedScMinPostsIntervalMs() RangeConfig {
func (c *Config) GetNormalizedScMinPostsIntervalMs() *RangeConfig {
if c.ScMinPostsIntervalMs == nil || c.ScMinPostsIntervalMs.To == 0 {
return RangeConfig{
return &RangeConfig{
From: 30,
To: 30,
}
}
return *c.ScMinPostsIntervalMs
return c.ScMinPostsIntervalMs
}
func (c *Config) GetNormalizedScMaxBufferedPosts() int {
@@ -161,27 +163,27 @@ func (c *Config) GetNormalizedScMaxBufferedPosts() int {
return int(c.ScMaxBufferedPosts)
}
func (c *Config) GetNormalizedScStreamUpServerSecs() RangeConfig {
func (c *Config) GetNormalizedScStreamUpServerSecs() *RangeConfig {
if c.ScStreamUpServerSecs == nil || c.ScStreamUpServerSecs.To == 0 {
return RangeConfig{
return &RangeConfig{
From: 20,
To: 80,
}
}
return *c.ScStreamUpServerSecs
return c.ScStreamUpServerSecs
}
func (c *Config) GetNormalizedUplinkChunkSize() RangeConfig {
func (c *Config) GetNormalizedUplinkChunkSize() *RangeConfig {
if c.UplinkChunkSize == nil || c.UplinkChunkSize.To == 0 {
switch c.UplinkDataPlacement {
case PlacementCookie:
return RangeConfig{
return &RangeConfig{
From: 2 * 1024, // 2 KiB
To: 3 * 1024, // 3 KiB
}
case PlacementHeader:
return RangeConfig{
return &RangeConfig{
From: 3 * 1000, // 3 KB
To: 4 * 1000, // 4 KB
}
@@ -189,13 +191,13 @@ func (c *Config) GetNormalizedUplinkChunkSize() RangeConfig {
return c.GetNormalizedScMaxEachPostBytes()
}
} else if c.UplinkChunkSize.From < 64 {
return RangeConfig{
return &RangeConfig{
From: 64,
To: max(64, c.UplinkChunkSize.To),
}
}
return *c.UplinkChunkSize
return c.UplinkChunkSize
}
func (c *Config) GetNormalizedServerMaxHeaderBytes() int {
@@ -417,59 +419,59 @@ func (c *Config) ExtractMetaFromRequest(req *http.Request, path string) (session
return sessionId, seqStr
}
func (m *XmuxConfig) GetNormalizedMaxConcurrency() RangeConfig {
func (m *XmuxConfig) GetNormalizedMaxConcurrency() *RangeConfig {
if m.MaxConcurrency == nil {
return RangeConfig{
return &RangeConfig{
From: 0,
To: 0,
}
}
return *m.MaxConcurrency
return m.MaxConcurrency
}
func (m *XmuxConfig) GetNormalizedMaxConnections() RangeConfig {
func (m *XmuxConfig) GetNormalizedMaxConnections() *RangeConfig {
if m.MaxConnections == nil {
return RangeConfig{
return &RangeConfig{
From: 0,
To: 0,
}
}
return *m.MaxConnections
return m.MaxConnections
}
func (m *XmuxConfig) GetNormalizedCMaxReuseTimes() RangeConfig {
func (m *XmuxConfig) GetNormalizedCMaxReuseTimes() *RangeConfig {
if m.CMaxReuseTimes == nil {
return RangeConfig{
return &RangeConfig{
From: 0,
To: 0,
}
}
return *m.CMaxReuseTimes
return m.CMaxReuseTimes
}
func (m *XmuxConfig) GetNormalizedHMaxRequestTimes() RangeConfig {
func (m *XmuxConfig) GetNormalizedHMaxRequestTimes() *RangeConfig {
if m.HMaxRequestTimes == nil {
return RangeConfig{
return &RangeConfig{
From: 0,
To: 0,
}
}
return *m.HMaxRequestTimes
return m.HMaxRequestTimes
}
func (m *XmuxConfig) GetNormalizedHMaxReusableSecs() RangeConfig {
func (m *XmuxConfig) GetNormalizedHMaxReusableSecs() *RangeConfig {
if m.HMaxReusableSecs == nil {
return RangeConfig{
return &RangeConfig{
From: 0,
To: 0,
}
}
return *m.HMaxReusableSecs
return m.HMaxReusableSecs
}
func init() {
@@ -478,10 +480,40 @@ func init() {
}))
}
func (c RangeConfig) rand() int32 {
func (c *RangeConfig) rand() int32 {
return int32(crypto.RandBetween(int64(c.From), int64(c.To)))
}
// predefined
var (
base62Table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
hexTable = "0123456789abcdef"
hexTableUpper = "0123456789ABCDEF"
)
func (c *Config) GenerateSessionID() string {
length := c.SessionIDLength.rand()
table := c.SessionIDTable
switch table {
case "base62":
table = base62Table
case "hex":
table = hexTable
case "HEX":
table = hexTableUpper
}
if table != "" && length > 0 {
id := make([]byte, length)
for i := range id {
id[i] = table[rand.N(len(table))]
}
return string(id)
} else {
uuid := uuid.New()
return uuid.String()
}
}
func appendToPath(path, value string) string {
if strings.HasSuffix(path, "/") {
return path + value