2024-06-18 05:36:36 +00:00
|
|
|
package splithttp
|
|
|
|
|
|
|
|
import (
|
2024-07-27 12:52:36 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
2024-06-18 05:36:36 +00:00
|
|
|
"net/http"
|
2024-07-29 04:35:17 +00:00
|
|
|
"strings"
|
2024-06-18 05:36:36 +00:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
)
|
|
|
|
|
2024-08-10 21:47:42 +00:00
|
|
|
func (c *Config) GetNormalizedPath() string {
|
2024-07-29 04:35:17 +00:00
|
|
|
pathAndQuery := strings.SplitN(c.Path, "?", 2)
|
|
|
|
path := pathAndQuery[0]
|
|
|
|
|
|
|
|
if path == "" || path[0] != '/' {
|
2024-06-18 05:36:36 +00:00
|
|
|
path = "/" + path
|
|
|
|
}
|
2024-08-10 21:47:42 +00:00
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
if path[len(path)-1] != '/' {
|
|
|
|
path = path + "/"
|
|
|
|
}
|
2024-07-29 04:35:17 +00:00
|
|
|
|
2024-08-10 21:47:42 +00:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) GetNormalizedQuery() string {
|
|
|
|
pathAndQuery := strings.SplitN(c.Path, "?", 2)
|
|
|
|
query := ""
|
|
|
|
|
|
|
|
if len(pathAndQuery) > 1 {
|
|
|
|
query = pathAndQuery[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if query != "" {
|
|
|
|
query += "&"
|
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
paddingLen := c.GetNormalizedXPaddingBytes().rand()
|
2024-08-10 21:47:42 +00:00
|
|
|
if paddingLen > 0 {
|
|
|
|
query += "x_padding=" + strings.Repeat("0", int(paddingLen))
|
|
|
|
}
|
|
|
|
|
|
|
|
return query
|
2024-06-18 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) GetRequestHeader() http.Header {
|
|
|
|
header := http.Header{}
|
2024-12-11 14:05:39 +00:00
|
|
|
for k, v := range c.Headers {
|
2024-06-18 05:36:36 +00:00
|
|
|
header.Add(k, v)
|
|
|
|
}
|
2024-08-10 21:47:42 +00:00
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2024-08-10 21:47:42 +00:00
|
|
|
func (c *Config) WriteResponseHeader(writer http.ResponseWriter) {
|
2024-09-20 07:55:54 +00:00
|
|
|
// CORS headers for the browser dialer
|
|
|
|
writer.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
writer.Header().Set("Access-Control-Allow-Methods", "GET, POST")
|
2024-12-15 05:43:10 +00:00
|
|
|
paddingLen := c.GetNormalizedXPaddingBytes().rand()
|
2024-08-10 21:47:42 +00:00
|
|
|
if paddingLen > 0 {
|
|
|
|
writer.Header().Set("X-Padding", strings.Repeat("0", int(paddingLen)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-11 14:05:39 +00:00
|
|
|
func (c *Config) GetNormalizedScMaxBufferedPosts() int {
|
|
|
|
if c.ScMaxBufferedPosts == 0 {
|
|
|
|
return 30
|
2024-06-18 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-12-11 14:05:39 +00:00
|
|
|
return int(c.ScMaxBufferedPosts)
|
2024-06-18 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (c *Config) GetNormalizedScMaxEachPostBytes() RangeConfig {
|
2024-07-29 10:10:29 +00:00
|
|
|
if c.ScMaxEachPostBytes == nil || c.ScMaxEachPostBytes.To == 0 {
|
2024-12-15 05:43:10 +00:00
|
|
|
return RangeConfig{
|
2024-07-29 10:10:29 +00:00
|
|
|
From: 1000000,
|
|
|
|
To: 1000000,
|
2024-07-29 04:35:17 +00:00
|
|
|
}
|
2024-06-18 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 10:10:29 +00:00
|
|
|
return *c.ScMaxEachPostBytes
|
2024-06-18 05:36:36 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (c *Config) GetNormalizedScMinPostsIntervalMs() RangeConfig {
|
2024-07-29 10:10:29 +00:00
|
|
|
if c.ScMinPostsIntervalMs == nil || c.ScMinPostsIntervalMs.To == 0 {
|
2024-12-15 05:43:10 +00:00
|
|
|
return RangeConfig{
|
2024-07-27 12:52:36 +00:00
|
|
|
From: 30,
|
|
|
|
To: 30,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 10:10:29 +00:00
|
|
|
return *c.ScMinPostsIntervalMs
|
2024-07-27 12:52:36 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (c *Config) GetNormalizedXPaddingBytes() RangeConfig {
|
2024-08-10 21:47:42 +00:00
|
|
|
if c.XPaddingBytes == nil || c.XPaddingBytes.To == 0 {
|
2024-12-15 05:43:10 +00:00
|
|
|
return RangeConfig{
|
2024-08-02 01:16:19 +00:00
|
|
|
From: 100,
|
|
|
|
To: 1000,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 21:47:42 +00:00
|
|
|
return *c.XPaddingBytes
|
2024-08-02 01:16:19 +00:00
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (m *XmuxConfig) GetNormalizedCMaxRequestTimes() RangeConfig {
|
|
|
|
if m.HMaxRequestTimes == nil {
|
|
|
|
return RangeConfig{
|
|
|
|
From: 0,
|
|
|
|
To: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *m.HMaxRequestTimes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *XmuxConfig) GetNormalizedCMaxReuseTimes() RangeConfig {
|
2024-09-16 12:42:01 +00:00
|
|
|
if m.CMaxReuseTimes == nil {
|
2024-12-15 05:43:10 +00:00
|
|
|
return RangeConfig{
|
2024-09-16 12:42:01 +00:00
|
|
|
From: 0,
|
|
|
|
To: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *m.CMaxReuseTimes
|
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (m *XmuxConfig) GetNormalizedCMaxLifetimeMs() RangeConfig {
|
|
|
|
if m.CMaxLifetimeMs == nil {
|
|
|
|
return RangeConfig{
|
2024-09-16 12:42:01 +00:00
|
|
|
From: 0,
|
|
|
|
To: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *m.CMaxLifetimeMs
|
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (m *XmuxConfig) GetNormalizedMaxConnections() RangeConfig {
|
2024-09-16 12:42:01 +00:00
|
|
|
if m.MaxConnections == nil {
|
2024-12-15 05:43:10 +00:00
|
|
|
return RangeConfig{
|
2024-09-16 12:42:01 +00:00
|
|
|
From: 0,
|
|
|
|
To: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *m.MaxConnections
|
|
|
|
}
|
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (m *XmuxConfig) GetNormalizedMaxConcurrency() RangeConfig {
|
2024-09-16 12:42:01 +00:00
|
|
|
if m.MaxConcurrency == nil {
|
2024-12-15 05:43:10 +00:00
|
|
|
return RangeConfig{
|
2024-09-16 12:42:01 +00:00
|
|
|
From: 0,
|
|
|
|
To: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *m.MaxConcurrency
|
|
|
|
}
|
|
|
|
|
2024-06-18 05:36:36 +00:00
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
|
|
|
|
return new(Config)
|
|
|
|
}))
|
|
|
|
}
|
2024-07-27 12:52:36 +00:00
|
|
|
|
2024-12-15 05:43:10 +00:00
|
|
|
func (c RangeConfig) rand() int32 {
|
2024-07-27 12:52:36 +00:00
|
|
|
if c.From == c.To {
|
|
|
|
return c.From
|
|
|
|
}
|
|
|
|
bigInt, _ := rand.Int(rand.Reader, big.NewInt(int64(c.To-c.From)))
|
|
|
|
return c.From + int32(bigInt.Int64())
|
|
|
|
}
|