config.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package splithttp
  2. import (
  3. "crypto/rand"
  4. "math/big"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/transport/internet"
  10. )
  11. func (c *Config) GetNormalizedPath() string {
  12. pathAndQuery := strings.SplitN(c.Path, "?", 2)
  13. path := pathAndQuery[0]
  14. if path == "" || path[0] != '/' {
  15. path = "/" + path
  16. }
  17. if path[len(path)-1] != '/' {
  18. path = path + "/"
  19. }
  20. return path
  21. }
  22. func (c *Config) GetNormalizedQuery() string {
  23. pathAndQuery := strings.SplitN(c.Path, "?", 2)
  24. query := ""
  25. if len(pathAndQuery) > 1 {
  26. query = pathAndQuery[1]
  27. }
  28. if query != "" {
  29. query += "&"
  30. }
  31. // query += "x_version=" + core.Version()
  32. query += "x_padding=" + strings.Repeat("X", int(c.GetNormalizedXPaddingBytes().From))
  33. return query
  34. }
  35. func (c *Config) GetRequestHeader(rawURL string) http.Header {
  36. header := http.Header{}
  37. for k, v := range c.Headers {
  38. header.Add(k, v)
  39. }
  40. u, _ := url.Parse(rawURL)
  41. // https://www.rfc-editor.org/rfc/rfc7541.html#appendix-B
  42. // h2's HPACK Header Compression feature employs a huffman encoding using a static table.
  43. // 'X' is assigned an 8 bit code, so HPACK compression won't change actual padding length on the wire.
  44. // https://www.rfc-editor.org/rfc/rfc9204.html#section-4.1.2-2
  45. // h3's similar QPACK feature uses the same huffman table.
  46. u.RawQuery = "x_padding=" + strings.Repeat("X", int(c.GetNormalizedXPaddingBytes().rand()))
  47. header.Set("Referer", u.String())
  48. return header
  49. }
  50. func (c *Config) WriteResponseHeader(writer http.ResponseWriter) {
  51. // CORS headers for the browser dialer
  52. writer.Header().Set("Access-Control-Allow-Origin", "*")
  53. writer.Header().Set("Access-Control-Allow-Methods", "GET, POST")
  54. // writer.Header().Set("X-Version", core.Version())
  55. writer.Header().Set("X-Padding", strings.Repeat("X", int(c.GetNormalizedXPaddingBytes().rand())))
  56. }
  57. func (c *Config) GetNormalizedXPaddingBytes() RangeConfig {
  58. if c.XPaddingBytes == nil || c.XPaddingBytes.To == 0 {
  59. return RangeConfig{
  60. From: 100,
  61. To: 1000,
  62. }
  63. }
  64. return *c.XPaddingBytes
  65. }
  66. func (c *Config) GetNormalizedScMaxEachPostBytes() RangeConfig {
  67. if c.ScMaxEachPostBytes == nil || c.ScMaxEachPostBytes.To == 0 {
  68. return RangeConfig{
  69. From: 1000000,
  70. To: 1000000,
  71. }
  72. }
  73. return *c.ScMaxEachPostBytes
  74. }
  75. func (c *Config) GetNormalizedScMinPostsIntervalMs() RangeConfig {
  76. if c.ScMinPostsIntervalMs == nil || c.ScMinPostsIntervalMs.To == 0 {
  77. return RangeConfig{
  78. From: 30,
  79. To: 30,
  80. }
  81. }
  82. return *c.ScMinPostsIntervalMs
  83. }
  84. func (c *Config) GetNormalizedScMaxBufferedPosts() int {
  85. if c.ScMaxBufferedPosts == 0 {
  86. return 30
  87. }
  88. return int(c.ScMaxBufferedPosts)
  89. }
  90. func (c *Config) GetNormalizedScStreamUpServerSecs() RangeConfig {
  91. if c.ScStreamUpServerSecs == nil || c.ScStreamUpServerSecs.To == 0 {
  92. return RangeConfig{
  93. From: 20,
  94. To: 80,
  95. }
  96. }
  97. return *c.ScMinPostsIntervalMs
  98. }
  99. func (m *XmuxConfig) GetNormalizedMaxConcurrency() RangeConfig {
  100. if m.MaxConcurrency == nil {
  101. return RangeConfig{
  102. From: 0,
  103. To: 0,
  104. }
  105. }
  106. return *m.MaxConcurrency
  107. }
  108. func (m *XmuxConfig) GetNormalizedMaxConnections() RangeConfig {
  109. if m.MaxConnections == nil {
  110. return RangeConfig{
  111. From: 0,
  112. To: 0,
  113. }
  114. }
  115. return *m.MaxConnections
  116. }
  117. func (m *XmuxConfig) GetNormalizedCMaxReuseTimes() RangeConfig {
  118. if m.CMaxReuseTimes == nil {
  119. return RangeConfig{
  120. From: 0,
  121. To: 0,
  122. }
  123. }
  124. return *m.CMaxReuseTimes
  125. }
  126. func (m *XmuxConfig) GetNormalizedHMaxRequestTimes() RangeConfig {
  127. if m.HMaxRequestTimes == nil {
  128. return RangeConfig{
  129. From: 0,
  130. To: 0,
  131. }
  132. }
  133. return *m.HMaxRequestTimes
  134. }
  135. func (m *XmuxConfig) GetNormalizedHMaxReusableSecs() RangeConfig {
  136. if m.HMaxReusableSecs == nil {
  137. return RangeConfig{
  138. From: 0,
  139. To: 0,
  140. }
  141. }
  142. return *m.HMaxReusableSecs
  143. }
  144. func init() {
  145. common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
  146. return new(Config)
  147. }))
  148. }
  149. func (c RangeConfig) rand() int32 {
  150. if c.From == c.To {
  151. return c.From
  152. }
  153. bigInt, _ := rand.Int(rand.Reader, big.NewInt(int64(c.To-c.From)))
  154. return c.From + int32(bigInt.Int64())
  155. }