1
0

transport_authenticators.go 5.5 KB

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