config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package splithttp
  2. import (
  3. "crypto/rand"
  4. "math/big"
  5. "net/http"
  6. "strings"
  7. "github.com/xtls/xray-core/common"
  8. "github.com/xtls/xray-core/transport/internet"
  9. )
  10. func (c *Config) GetNormalizedPath() string {
  11. pathAndQuery := strings.SplitN(c.Path, "?", 2)
  12. path := pathAndQuery[0]
  13. if path == "" || path[0] != '/' {
  14. path = "/" + path
  15. }
  16. if path[len(path)-1] != '/' {
  17. path = path + "/"
  18. }
  19. return path
  20. }
  21. func (c *Config) GetNormalizedQuery() string {
  22. pathAndQuery := strings.SplitN(c.Path, "?", 2)
  23. query := ""
  24. if len(pathAndQuery) > 1 {
  25. query = pathAndQuery[1]
  26. }
  27. if query != "" {
  28. query += "&"
  29. }
  30. paddingLen := c.GetNormalizedXPaddingBytes().roll()
  31. if paddingLen > 0 {
  32. query += "x_padding=" + strings.Repeat("0", int(paddingLen))
  33. }
  34. return query
  35. }
  36. func (c *Config) GetRequestHeader() http.Header {
  37. header := http.Header{}
  38. for k, v := range c.Header {
  39. header.Add(k, v)
  40. }
  41. return header
  42. }
  43. func (c *Config) WriteResponseHeader(writer http.ResponseWriter) {
  44. // CORS headers for the browser dialer
  45. writer.Header().Set("Access-Control-Allow-Origin", "*")
  46. writer.Header().Set("Access-Control-Allow-Methods", "GET, POST")
  47. paddingLen := c.GetNormalizedXPaddingBytes().roll()
  48. if paddingLen > 0 {
  49. writer.Header().Set("X-Padding", strings.Repeat("0", int(paddingLen)))
  50. }
  51. }
  52. func (c *Config) GetNormalizedScMaxConcurrentPosts() RandRangeConfig {
  53. if c.ScMaxConcurrentPosts == nil || c.ScMaxConcurrentPosts.To == 0 {
  54. return RandRangeConfig{
  55. From: 100,
  56. To: 100,
  57. }
  58. }
  59. return *c.ScMaxConcurrentPosts
  60. }
  61. func (c *Config) GetNormalizedScMaxEachPostBytes() RandRangeConfig {
  62. if c.ScMaxEachPostBytes == nil || c.ScMaxEachPostBytes.To == 0 {
  63. return RandRangeConfig{
  64. From: 1000000,
  65. To: 1000000,
  66. }
  67. }
  68. return *c.ScMaxEachPostBytes
  69. }
  70. func (c *Config) GetNormalizedScMinPostsIntervalMs() RandRangeConfig {
  71. if c.ScMinPostsIntervalMs == nil || c.ScMinPostsIntervalMs.To == 0 {
  72. return RandRangeConfig{
  73. From: 30,
  74. To: 30,
  75. }
  76. }
  77. return *c.ScMinPostsIntervalMs
  78. }
  79. func (c *Config) GetNormalizedXPaddingBytes() RandRangeConfig {
  80. if c.XPaddingBytes == nil || c.XPaddingBytes.To == 0 {
  81. return RandRangeConfig{
  82. From: 100,
  83. To: 1000,
  84. }
  85. }
  86. return *c.XPaddingBytes
  87. }
  88. func (m *Multiplexing) GetNormalizedCMaxReuseTimes() RandRangeConfig {
  89. if m.CMaxReuseTimes == nil {
  90. return RandRangeConfig{
  91. From: 0,
  92. To: 0,
  93. }
  94. }
  95. return *m.CMaxReuseTimes
  96. }
  97. func (m *Multiplexing) GetNormalizedCMaxLifetimeMs() RandRangeConfig {
  98. if m.CMaxLifetimeMs == nil || m.CMaxLifetimeMs.To == 0 {
  99. return RandRangeConfig{
  100. From: 0,
  101. To: 0,
  102. }
  103. }
  104. return *m.CMaxLifetimeMs
  105. }
  106. func (m *Multiplexing) GetNormalizedMaxConnections() RandRangeConfig {
  107. if m.MaxConnections == nil {
  108. return RandRangeConfig{
  109. From: 0,
  110. To: 0,
  111. }
  112. }
  113. return *m.MaxConnections
  114. }
  115. func (m *Multiplexing) GetNormalizedMaxConcurrency() RandRangeConfig {
  116. if m.MaxConcurrency == nil {
  117. return RandRangeConfig{
  118. From: 0,
  119. To: 0,
  120. }
  121. }
  122. return *m.MaxConcurrency
  123. }
  124. func init() {
  125. common.Must(internet.RegisterProtocolConfigCreator(protocolName, func() interface{} {
  126. return new(Config)
  127. }))
  128. }
  129. func (c RandRangeConfig) roll() int32 {
  130. if c.From == c.To {
  131. return c.From
  132. }
  133. bigInt, _ := rand.Int(rand.Reader, big.NewInt(int64(c.To-c.From)))
  134. return c.From + int32(bigInt.Int64())
  135. }