Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ type SplitHTTPConfig struct {
ServerMaxHeaderBytes int32 `json:"serverMaxHeaderBytes"`
Xmux XmuxConfig `json:"xmux"`
DownloadSettings *StreamConfig `json:"downloadSettings"`
SessionGeneratorType string `json:"sessionGeneratorType"`
SessionGeneratorMinLength int32 `json:"sessionGeneratorMinLength"`
SessionGeneratorMaxLength int32 `json:"sessionGeneratorMaxLength"`
SessionGeneratorAlphabet string `json:"sessionGeneratorAlphabet"`
Extra json.RawMessage `json:"extra"`
}

Expand Down Expand Up @@ -391,6 +395,27 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
c.Xmux.HMaxReusableSecs.To = 3000
}

switch c.SessionGeneratorType {
case "":
c.SessionGeneratorType = "uuid"
case "uuid", "randstr":
default:
return nil, errors.New("unsupported sessionGeneratorType: ", c.SessionGeneratorType)
}

if c.SessionGeneratorMinLength < 0 {
return nil, errors.New("sessionGeneratorMinLength must be >= 0")
}
if c.SessionGeneratorMaxLength < 0 {
return nil, errors.New("sessionGeneratorMaxLength must be >= 0")
}
if c.SessionGeneratorMaxLength > 0 && c.SessionGeneratorMinLength > c.SessionGeneratorMaxLength {
return nil, errors.New("sessionGeneratorMaxLength must be >= sessionGeneratorMinLength")
}
if c.SessionGeneratorAlphabet != "" && strings.TrimSpace(c.SessionGeneratorAlphabet) == "" {
return nil, errors.New("sessionGeneratorAlphabet cannot be empty when provided")
}

config := &splithttp.Config{
Host: c.Host,
Path: c.Path,
Expand Down Expand Up @@ -425,6 +450,10 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) {
HMaxReusableSecs: newRangeConfig(c.Xmux.HMaxReusableSecs),
HKeepAlivePeriod: c.Xmux.HKeepAlivePeriod,
},
SessionGeneratorType: c.SessionGeneratorType,
SessionGeneratorMinLength: c.SessionGeneratorMinLength,
SessionGeneratorMaxLength: c.SessionGeneratorMaxLength,
SessionGeneratorAlphabet: c.SessionGeneratorAlphabet,
}

if c.DownloadSettings != nil {
Expand Down
84 changes: 84 additions & 0 deletions transport/internet/splithttp/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package splithttp

import (
cryptoRand "crypto/rand"
"encoding/base64"
"fmt"
"io"
"math/big"
"net/http"
"strings"

"github.com/xtls/xray-core/common"
"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"
)

Expand Down Expand Up @@ -206,6 +209,87 @@ func (c *Config) GetNormalizedServerMaxHeaderBytes() int {
}
}

func (c *Config) GetNormalizedSessionGeneratorType() string {
value := strings.ToLower(strings.TrimSpace(c.SessionGeneratorType))
if value == "" {
return "uuid"
}

switch value {
case "randstr", "uuid":
return value
default:
return "uuid"
}
}

func (c *Config) GenerateSessionID() string {
generatorType := c.GetNormalizedSessionGeneratorType()
switch generatorType {
case "uuid":
u := uuid.New()
return u.String()
case "randstr":
min, max, alphabet := c.getSessionGeneratorParams()
return generateRandomString(min, max, alphabet)
default:
u := uuid.New()
return u.String()
}
}

func (c *Config) getSessionGeneratorParams() (int, int, string) {
min := 16
max := 32
alphabet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

if v := c.GetSessionGeneratorMinLength(); v > 0 {
min = int(v)
}
if v := c.GetSessionGeneratorMaxLength(); v > 0 {
max = int(v)
}
if min <= 0 {
min = 1
}
if max < min {
max = min
}
if a := c.GetSessionGeneratorAlphabet(); a != "" {
alphabet = a
}
return min, max, alphabet
}

func generateRandomString(min, max int, alphabet string) string {
if min <= 0 {
min = 1
}
if max < min {
max = min
}
nRange := big.NewInt(int64(max - min + 1))
nBig, err := cryptoRand.Int(cryptoRand.Reader, nRange)
var length int
if err != nil {
length = min
} else {
length = min + int(nBig.Int64())
}

b := make([]byte, length)
alphaLen := big.NewInt(int64(len(alphabet)))
for i := 0; i < length; i++ {
idx, err := cryptoRand.Int(cryptoRand.Reader, alphaLen)
if err != nil {
b[i] = alphabet[0]
continue
}
b[i] = alphabet[idx.Int64()]
}
return string(b)
}

func (c *Config) GetNormalizedSessionPlacement() string {
if c.SessionPlacement == "" {
return PlacementPath
Expand Down
40 changes: 38 additions & 2 deletions transport/internet/splithttp/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions transport/internet/splithttp/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ message Config {
string uplinkDataKey = 25;
RangeConfig uplinkChunkSize = 26;
int32 serverMaxHeaderBytes = 27;
string sessionGeneratorType = 28;
int32 sessionGeneratorMinLength = 29;
int32 sessionGeneratorMaxLength = 30;
string sessionGeneratorAlphabet = 31;
}
4 changes: 1 addition & 3 deletions transport/internet/splithttp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net/cnc"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/common/uuid"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/browser_dialer"
"github.com/xtls/xray-core/transport/internet/hysteria/congestion"
Expand Down Expand Up @@ -376,8 +375,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me

sessionId := ""
if mode != "stream-one" {
sessionIdUuid := uuid.New()
sessionId = sessionIdUuid.String()
sessionId = transportConfiguration.GenerateSessionID()
}

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