transport_authenticators.go 5.3 KB

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