Skip to content

Commit 2d68d34

Browse files
committed
Custom sessionID
1 parent fdb9b61 commit 2d68d34

7 files changed

Lines changed: 120 additions & 43 deletions

File tree

infra/conf/transport_internet.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ type SplitHTTPConfig struct {
233233
ScMaxBufferedPosts int64 `json:"scMaxBufferedPosts"`
234234
ScStreamUpServerSecs Int32Range `json:"scStreamUpServerSecs"`
235235
ServerMaxHeaderBytes int32 `json:"serverMaxHeaderBytes"`
236+
SessionIDTable string `json:"sessionIDTable"`
237+
SessionIDLength Int32Range `json:"sessionIDLength"`
236238
Xmux XmuxConfig `json:"xmux"`
237239
DownloadSettings *StreamConfig `json:"downloadSettings"`
238240
Extra json.RawMessage `json:"extra"`
@@ -378,6 +380,17 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
378380
return nil, errors.New("invalid negative value of maxHeaderBytes")
379381
}
380382

383+
if c.SessionIDTable != "" {
384+
if c.SessionIDLength.From <= 0 {
385+
return nil, errors.New("sessionIDLength.from must be greater than 0")
386+
}
387+
for i := 0; i < len(c.SessionIDTable); i++ {
388+
if c.SessionIDTable[i] >= 0x80 {
389+
return nil, errors.New("sessionIDTable must contain only ASCII characters")
390+
}
391+
}
392+
}
393+
381394
if c.Xmux.MaxConnections.To > 0 && c.Xmux.MaxConcurrency.To > 0 {
382395
return nil, errors.New("maxConnections cannot be specified together with maxConcurrency")
383396
}
@@ -416,6 +429,8 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
416429
ScMaxBufferedPosts: c.ScMaxBufferedPosts,
417430
ScStreamUpServerSecs: newRangeConfig(c.ScStreamUpServerSecs),
418431
ServerMaxHeaderBytes: c.ServerMaxHeaderBytes,
432+
SessionIDTable: c.SessionIDTable,
433+
SessionIDLength: newRangeConfig(c.SessionIDLength),
419434
Xmux: &splithttp.XmuxConfig{
420435
MaxConcurrency: newRangeConfig(c.Xmux.MaxConcurrency),
421436
MaxConnections: newRangeConfig(c.Xmux.MaxConnections),

transport/internet/splithttp/config.go

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"encoding/base64"
55
"fmt"
66
"io"
7+
"math/rand/v2"
78
"net/http"
89
"strings"
910

1011
"github.com/xtls/xray-core/common"
1112
"github.com/xtls/xray-core/common/buf"
1213
"github.com/xtls/xray-core/common/crypto"
1314
"github.com/xtls/xray-core/common/utils"
15+
"github.com/xtls/xray-core/common/uuid"
1416
"github.com/xtls/xray-core/transport/internet"
1517
)
1618

@@ -131,26 +133,26 @@ func (c *Config) GetNormalizedUplinkHTTPMethod() string {
131133
return c.UplinkHTTPMethod
132134
}
133135

134-
func (c *Config) GetNormalizedScMaxEachPostBytes() RangeConfig {
136+
func (c *Config) GetNormalizedScMaxEachPostBytes() *RangeConfig {
135137
if c.ScMaxEachPostBytes == nil || c.ScMaxEachPostBytes.To == 0 {
136-
return RangeConfig{
138+
return &RangeConfig{
137139
From: 1000000,
138140
To: 1000000,
139141
}
140142
}
141143

142-
return *c.ScMaxEachPostBytes
144+
return c.ScMaxEachPostBytes
143145
}
144146

145-
func (c *Config) GetNormalizedScMinPostsIntervalMs() RangeConfig {
147+
func (c *Config) GetNormalizedScMinPostsIntervalMs() *RangeConfig {
146148
if c.ScMinPostsIntervalMs == nil || c.ScMinPostsIntervalMs.To == 0 {
147-
return RangeConfig{
149+
return &RangeConfig{
148150
From: 30,
149151
To: 30,
150152
}
151153
}
152154

153-
return *c.ScMinPostsIntervalMs
155+
return c.ScMinPostsIntervalMs
154156
}
155157

156158
func (c *Config) GetNormalizedScMaxBufferedPosts() int {
@@ -161,41 +163,41 @@ func (c *Config) GetNormalizedScMaxBufferedPosts() int {
161163
return int(c.ScMaxBufferedPosts)
162164
}
163165

164-
func (c *Config) GetNormalizedScStreamUpServerSecs() RangeConfig {
166+
func (c *Config) GetNormalizedScStreamUpServerSecs() *RangeConfig {
165167
if c.ScStreamUpServerSecs == nil || c.ScStreamUpServerSecs.To == 0 {
166-
return RangeConfig{
168+
return &RangeConfig{
167169
From: 20,
168170
To: 80,
169171
}
170172
}
171173

172-
return *c.ScStreamUpServerSecs
174+
return c.ScStreamUpServerSecs
173175
}
174176

175-
func (c *Config) GetNormalizedUplinkChunkSize() RangeConfig {
177+
func (c *Config) GetNormalizedUplinkChunkSize() *RangeConfig {
176178
if c.UplinkChunkSize == nil || c.UplinkChunkSize.To == 0 {
177179
switch c.UplinkDataPlacement {
178180
case PlacementCookie:
179-
return RangeConfig{
181+
return &RangeConfig{
180182
From: 2 * 1024, // 2 KiB
181183
To: 3 * 1024, // 3 KiB
182184
}
183185
case PlacementHeader:
184-
return RangeConfig{
186+
return &RangeConfig{
185187
From: 3 * 1000, // 3 KB
186188
To: 4 * 1000, // 4 KB
187189
}
188190
default:
189191
return c.GetNormalizedScMaxEachPostBytes()
190192
}
191193
} else if c.UplinkChunkSize.From < 64 {
192-
return RangeConfig{
194+
return &RangeConfig{
193195
From: 64,
194196
To: max(64, c.UplinkChunkSize.To),
195197
}
196198
}
197199

198-
return *c.UplinkChunkSize
200+
return c.UplinkChunkSize
199201
}
200202

201203
func (c *Config) GetNormalizedServerMaxHeaderBytes() int {
@@ -417,59 +419,59 @@ func (c *Config) ExtractMetaFromRequest(req *http.Request, path string) (session
417419
return sessionId, seqStr
418420
}
419421

420-
func (m *XmuxConfig) GetNormalizedMaxConcurrency() RangeConfig {
422+
func (m *XmuxConfig) GetNormalizedMaxConcurrency() *RangeConfig {
421423
if m.MaxConcurrency == nil {
422-
return RangeConfig{
424+
return &RangeConfig{
423425
From: 0,
424426
To: 0,
425427
}
426428
}
427429

428-
return *m.MaxConcurrency
430+
return m.MaxConcurrency
429431
}
430432

431-
func (m *XmuxConfig) GetNormalizedMaxConnections() RangeConfig {
433+
func (m *XmuxConfig) GetNormalizedMaxConnections() *RangeConfig {
432434
if m.MaxConnections == nil {
433-
return RangeConfig{
435+
return &RangeConfig{
434436
From: 0,
435437
To: 0,
436438
}
437439
}
438440

439-
return *m.MaxConnections
441+
return m.MaxConnections
440442
}
441443

442-
func (m *XmuxConfig) GetNormalizedCMaxReuseTimes() RangeConfig {
444+
func (m *XmuxConfig) GetNormalizedCMaxReuseTimes() *RangeConfig {
443445
if m.CMaxReuseTimes == nil {
444-
return RangeConfig{
446+
return &RangeConfig{
445447
From: 0,
446448
To: 0,
447449
}
448450
}
449451

450-
return *m.CMaxReuseTimes
452+
return m.CMaxReuseTimes
451453
}
452454

453-
func (m *XmuxConfig) GetNormalizedHMaxRequestTimes() RangeConfig {
455+
func (m *XmuxConfig) GetNormalizedHMaxRequestTimes() *RangeConfig {
454456
if m.HMaxRequestTimes == nil {
455-
return RangeConfig{
457+
return &RangeConfig{
456458
From: 0,
457459
To: 0,
458460
}
459461
}
460462

461-
return *m.HMaxRequestTimes
463+
return m.HMaxRequestTimes
462464
}
463465

464-
func (m *XmuxConfig) GetNormalizedHMaxReusableSecs() RangeConfig {
466+
func (m *XmuxConfig) GetNormalizedHMaxReusableSecs() *RangeConfig {
465467
if m.HMaxReusableSecs == nil {
466-
return RangeConfig{
468+
return &RangeConfig{
467469
From: 0,
468470
To: 0,
469471
}
470472
}
471473

472-
return *m.HMaxReusableSecs
474+
return m.HMaxReusableSecs
473475
}
474476

475477
func init() {
@@ -478,10 +480,40 @@ func init() {
478480
}))
479481
}
480482

481-
func (c RangeConfig) rand() int32 {
483+
func (c *RangeConfig) rand() int32 {
482484
return int32(crypto.RandBetween(int64(c.From), int64(c.To)))
483485
}
484486

487+
// predefined
488+
var (
489+
base62Table = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
490+
hexTable = "0123456789abcdef"
491+
hexTableUpper = "0123456789ABCDEF"
492+
)
493+
494+
func (c *Config) GenerateSessionID() string {
495+
length := c.SessionIDLength.rand()
496+
table := c.SessionIDTable
497+
switch table {
498+
case "base62":
499+
table = base62Table
500+
case "hex":
501+
table = hexTable
502+
case "HEX":
503+
table = hexTableUpper
504+
}
505+
if table != "" && length > 0 {
506+
id := make([]byte, length)
507+
for i := range id {
508+
id[i] = table[rand.N(len(table))]
509+
}
510+
return string(id)
511+
} else {
512+
uuid := uuid.New()
513+
return uuid.String()
514+
}
515+
}
516+
485517
func appendToPath(path, value string) string {
486518
if strings.HasSuffix(path, "/") {
487519
return path + value

transport/internet/splithttp/config.pb.go

Lines changed: 26 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transport/internet/splithttp/config.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ message Config {
5050
string uplinkDataKey = 25;
5151
RangeConfig uplinkChunkSize = 26;
5252
int32 serverMaxHeaderBytes = 27;
53+
string sessionIDTable = 28;
54+
RangeConfig sessionIDLength = 29;
5355
}

transport/internet/splithttp/dialer.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/xtls/xray-core/common/net"
2525
"github.com/xtls/xray-core/common/net/cnc"
2626
"github.com/xtls/xray-core/common/signal/done"
27-
"github.com/xtls/xray-core/common/uuid"
2827
"github.com/xtls/xray-core/transport/internet"
2928
"github.com/xtls/xray-core/transport/internet/browser_dialer"
3029
"github.com/xtls/xray-core/transport/internet/hysteria/congestion"
@@ -376,8 +375,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
376375

377376
sessionId := ""
378377
if mode != "stream-one" {
379-
sessionIdUuid := uuid.New()
380-
sessionId = sessionIdUuid.String()
378+
sessionId = transportConfiguration.GenerateSessionID()
381379
}
382380

383381
errors.LogInfo(ctx, fmt.Sprintf("XHTTP is dialing to %s, mode %s, HTTP version %s, host %s", dest, mode, httpVersion, requestURL.Host))

transport/internet/splithttp/splithttp_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/rand"
77
"fmt"
88
"io"
9+
mrand "math/rand/v2"
910
"net/http"
1011
"runtime"
1112
"testing"
@@ -462,3 +463,13 @@ func Test_maxUpload(t *testing.T) {
462463

463464
common.Must(listen.Close())
464465
}
466+
467+
func BenchmarkRandom(b *testing.B) {
468+
table := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
469+
k := make([]byte, 64)
470+
for range b.N {
471+
for i := range 64 {
472+
k[i] = table[mrand.N(len(table))]
473+
}
474+
}
475+
}

transport/internet/splithttp/xpadding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@ func ApplyPaddingToQuery(u *url.URL, key, value string) {
176176
u.RawQuery = q.Encode()
177177
}
178178

179-
func (c *Config) GetNormalizedXPaddingBytes() RangeConfig {
179+
func (c *Config) GetNormalizedXPaddingBytes() *RangeConfig {
180180
if c.XPaddingBytes == nil || c.XPaddingBytes.To == 0 {
181-
return RangeConfig{
181+
return &RangeConfig{
182182
From: 100,
183183
To: 1000,
184184
}
185185
}
186186

187-
return *c.XPaddingBytes
187+
return c.XPaddingBytes
188188
}
189189

190190
func (c *Config) ApplyXPaddingToHeader(h http.Header, config XPaddingConfig) {

0 commit comments

Comments
 (0)