transport_authenticators.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package conf
  2. import (
  3. "sort"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/xtls/xray-core/transport/internet/headers/http"
  6. "github.com/xtls/xray-core/transport/internet/headers/noop"
  7. "github.com/xtls/xray-core/transport/internet/headers/srtp"
  8. "github.com/xtls/xray-core/transport/internet/headers/tls"
  9. "github.com/xtls/xray-core/transport/internet/headers/utp"
  10. "github.com/xtls/xray-core/transport/internet/headers/wechat"
  11. "github.com/xtls/xray-core/transport/internet/headers/wireguard"
  12. )
  13. type NoOpAuthenticator struct{}
  14. func (NoOpAuthenticator) Build() (proto.Message, error) {
  15. return new(noop.Config), nil
  16. }
  17. type NoOpConnectionAuthenticator struct{}
  18. func (NoOpConnectionAuthenticator) Build() (proto.Message, error) {
  19. return new(noop.ConnectionConfig), nil
  20. }
  21. type SRTPAuthenticator struct{}
  22. func (SRTPAuthenticator) Build() (proto.Message, error) {
  23. return new(srtp.Config), nil
  24. }
  25. type UTPAuthenticator struct{}
  26. func (UTPAuthenticator) Build() (proto.Message, error) {
  27. return new(utp.Config), nil
  28. }
  29. type WechatVideoAuthenticator struct{}
  30. func (WechatVideoAuthenticator) Build() (proto.Message, error) {
  31. return new(wechat.VideoConfig), nil
  32. }
  33. type WireguardAuthenticator struct{}
  34. func (WireguardAuthenticator) Build() (proto.Message, error) {
  35. return new(wireguard.WireguardConfig), nil
  36. }
  37. type DTLSAuthenticator struct{}
  38. func (DTLSAuthenticator) Build() (proto.Message, error) {
  39. return new(tls.PacketConfig), nil
  40. }
  41. type AuthenticatorRequest struct {
  42. Version string `json:"version"`
  43. Method string `json:"method"`
  44. Path StringList `json:"path"`
  45. Headers map[string]*StringList `json:"headers"`
  46. }
  47. func sortMapKeys(m map[string]*StringList) []string {
  48. var keys []string
  49. for key := range m {
  50. keys = append(keys, key)
  51. }
  52. sort.Strings(keys)
  53. return keys
  54. }
  55. func (v *AuthenticatorRequest) Build() (*http.RequestConfig, error) {
  56. config := &http.RequestConfig{
  57. Uri: []string{"/"},
  58. Header: []*http.Header{
  59. {
  60. Name: "Host",
  61. Value: []string{"www.baidu.com", "www.bing.com"},
  62. },
  63. {
  64. Name: "User-Agent",
  65. Value: []string{
  66. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36",
  67. "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.109 Mobile/14A456 Safari/601.1.46",
  68. },
  69. },
  70. {
  71. Name: "Accept-Encoding",
  72. Value: []string{"gzip, deflate"},
  73. },
  74. {
  75. Name: "Connection",
  76. Value: []string{"keep-alive"},
  77. },
  78. {
  79. Name: "Pragma",
  80. Value: []string{"no-cache"},
  81. },
  82. },
  83. }
  84. if len(v.Version) > 0 {
  85. config.Version = &http.Version{Value: v.Version}
  86. }
  87. if len(v.Method) > 0 {
  88. config.Method = &http.Method{Value: v.Method}
  89. }
  90. if len(v.Path) > 0 {
  91. config.Uri = append([]string(nil), (v.Path)...)
  92. }
  93. if len(v.Headers) > 0 {
  94. config.Header = make([]*http.Header, 0, len(v.Headers))
  95. headerNames := sortMapKeys(v.Headers)
  96. for _, key := range headerNames {
  97. value := v.Headers[key]
  98. if value == nil {
  99. return nil, newError("empty HTTP header value: " + key).AtError()
  100. }
  101. config.Header = append(config.Header, &http.Header{
  102. Name: key,
  103. Value: append([]string(nil), (*value)...),
  104. })
  105. }
  106. }
  107. return config, nil
  108. }
  109. type AuthenticatorResponse struct {
  110. Version string `json:"version"`
  111. Status string `json:"status"`
  112. Reason string `json:"reason"`
  113. Headers map[string]*StringList `json:"headers"`
  114. }
  115. func (v *AuthenticatorResponse) Build() (*http.ResponseConfig, error) {
  116. config := &http.ResponseConfig{
  117. Header: []*http.Header{
  118. {
  119. Name: "Content-Type",
  120. Value: []string{"application/octet-stream", "video/mpeg"},
  121. },
  122. {
  123. Name: "Transfer-Encoding",
  124. Value: []string{"chunked"},
  125. },
  126. {
  127. Name: "Connection",
  128. Value: []string{"keep-alive"},
  129. },
  130. {
  131. Name: "Pragma",
  132. Value: []string{"no-cache"},
  133. },
  134. {
  135. Name: "Cache-Control",
  136. Value: []string{"private", "no-cache"},
  137. },
  138. },
  139. }
  140. if len(v.Version) > 0 {
  141. config.Version = &http.Version{Value: v.Version}
  142. }
  143. if len(v.Status) > 0 || len(v.Reason) > 0 {
  144. config.Status = &http.Status{
  145. Code: "200",
  146. Reason: "OK",
  147. }
  148. if len(v.Status) > 0 {
  149. config.Status.Code = v.Status
  150. }
  151. if len(v.Reason) > 0 {
  152. config.Status.Reason = v.Reason
  153. }
  154. }
  155. if len(v.Headers) > 0 {
  156. config.Header = make([]*http.Header, 0, len(v.Headers))
  157. headerNames := sortMapKeys(v.Headers)
  158. for _, key := range headerNames {
  159. value := v.Headers[key]
  160. if value == nil {
  161. return nil, newError("empty HTTP header value: " + key).AtError()
  162. }
  163. config.Header = append(config.Header, &http.Header{
  164. Name: key,
  165. Value: append([]string(nil), (*value)...),
  166. })
  167. }
  168. }
  169. return config, nil
  170. }
  171. type Authenticator struct {
  172. Request AuthenticatorRequest `json:"request"`
  173. Response AuthenticatorResponse `json:"response"`
  174. }
  175. func (v *Authenticator) Build() (proto.Message, error) {
  176. config := new(http.Config)
  177. requestConfig, err := v.Request.Build()
  178. if err != nil {
  179. return nil, err
  180. }
  181. config.Request = requestConfig
  182. responseConfig, err := v.Response.Build()
  183. if err != nil {
  184. return nil, err
  185. }
  186. config.Response = responseConfig
  187. return config, nil
  188. }