transport_authenticators.go 4.7 KB

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