2024-06-18 08:36:36 +03:00
|
|
|
package splithttp
|
|
|
|
|
|
|
|
import (
|
2024-07-27 15:52:36 +03:00
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
2024-06-18 08:36:36 +03:00
|
|
|
"net/http"
|
2024-07-29 07:35:17 +03:00
|
|
|
"strings"
|
2024-06-18 08:36:36 +03:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
)
|
|
|
|
|
2024-07-29 07:35:17 +03:00
|
|
|
func (c *Config) GetNormalizedPath(addPath string, addQuery bool) string {
|
|
|
|
pathAndQuery := strings.SplitN(c.Path, "?", 2)
|
|
|
|
path := pathAndQuery[0]
|
|
|
|
query := ""
|
|
|
|
if len(pathAndQuery) > 1 && addQuery {
|
|
|
|
query = "?" + pathAndQuery[1]
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
2024-07-29 07:35:17 +03:00
|
|
|
|
|
|
|
if path == "" || path[0] != '/' {
|
2024-06-18 08:36:36 +03:00
|
|
|
path = "/" + path
|
|
|
|
}
|
|
|
|
if path[len(path)-1] != '/' {
|
|
|
|
path = path + "/"
|
|
|
|
}
|
2024-07-29 07:35:17 +03:00
|
|
|
|
|
|
|
return path + addPath + query
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) GetRequestHeader() http.Header {
|
|
|
|
header := http.Header{}
|
|
|
|
for k, v := range c.Header {
|
|
|
|
header.Add(k, v)
|
|
|
|
}
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
2024-07-29 07:35:17 +03:00
|
|
|
func (c *Config) GetNormalizedMaxConcurrentUploads(isServer bool) RandRangeConfig {
|
|
|
|
if c.MaxConcurrentUploads == nil {
|
|
|
|
if isServer {
|
|
|
|
return RandRangeConfig{
|
|
|
|
From: 200,
|
|
|
|
To: 200,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return RandRangeConfig{
|
|
|
|
From: 100,
|
|
|
|
To: 100,
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
|
2024-07-29 07:35:17 +03:00
|
|
|
return *c.MaxConcurrentUploads
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
|
2024-07-29 07:35:17 +03:00
|
|
|
func (c *Config) GetNormalizedMaxUploadSize(isServer bool) RandRangeConfig {
|
|
|
|
if c.MaxUploadSize == nil {
|
|
|
|
if isServer {
|
|
|
|
return RandRangeConfig{
|
|
|
|
From: 2000000,
|
|
|
|
To: 2000000,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return RandRangeConfig{
|
|
|
|
From: 1000000,
|
|
|
|
To: 1000000,
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
|
2024-07-29 07:35:17 +03:00
|
|
|
return *c.MaxUploadSize
|
2024-06-18 08:36:36 +03:00
|
|
|
}
|
|
|
|
|
2024-07-27 15:52:36 +03:00
|
|
|
func (c *Config) GetNormalizedMinUploadInterval() RandRangeConfig {
|
2024-07-29 07:35:17 +03:00
|
|
|
if c.MinUploadIntervalMs == nil {
|
|
|
|
return RandRangeConfig{
|
2024-07-27 15:52:36 +03:00
|
|
|
From: 30,
|
|
|
|
To: 30,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 07:35:17 +03:00
|
|
|
return *c.MinUploadIntervalMs
|
2024-07-27 15:52:36 +03:00
|
|
|
}
|
|
|
|
|
2024-06-18 08:36:36 +03:00
|
|
|
func init() {
|
|
|
|
common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
|
|
|
|
return new(Config)
|
|
|
|
}))
|
|
|
|
}
|
2024-07-27 15:52:36 +03:00
|
|
|
|
|
|
|
func (c RandRangeConfig) roll() int32 {
|
|
|
|
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())
|
|
|
|
}
|