transport_authenticators.go 4.6 KB

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