transport_authenticators.go 4.4 KB

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