transport_authenticators.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package conf
  2. import (
  3. "v2ray.com/core/common/errors"
  4. "v2ray.com/core/common/loader"
  5. "v2ray.com/core/transport/internet/authenticators/http"
  6. "v2ray.com/core/transport/internet/authenticators/noop"
  7. "v2ray.com/core/transport/internet/authenticators/srtp"
  8. "v2ray.com/core/transport/internet/authenticators/utp"
  9. )
  10. type NoOpAuthenticator struct{}
  11. func (NoOpAuthenticator) Build() (*loader.TypedSettings, error) {
  12. return loader.NewTypedSettings(new(noop.Config)), nil
  13. }
  14. type NoOpConnectionAuthenticator struct{}
  15. func (NoOpConnectionAuthenticator) Build() (*loader.TypedSettings, error) {
  16. return loader.NewTypedSettings(new(noop.Config)), nil
  17. }
  18. type SRTPAuthenticator struct{}
  19. func (SRTPAuthenticator) Build() (*loader.TypedSettings, error) {
  20. return loader.NewTypedSettings(new(srtp.Config)), nil
  21. }
  22. type UTPAuthenticator struct{}
  23. func (UTPAuthenticator) Build() (*loader.TypedSettings, error) {
  24. return loader.NewTypedSettings(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. }
  111. if len(v.Version) > 0 {
  112. config.Version = &http.Version{Value: v.Version}
  113. }
  114. if len(v.Status) > 0 || len(v.Reason) > 0 {
  115. config.Status = &http.Status{
  116. Code: "200",
  117. Reason: "OK",
  118. }
  119. if len(v.Status) > 0 {
  120. config.Status.Code = v.Status
  121. }
  122. if len(v.Reason) > 0 {
  123. config.Status.Reason = v.Reason
  124. }
  125. }
  126. if len(v.Headers) > 0 {
  127. config.Header = make([]*http.Header, 0, len(v.Headers))
  128. for key, value := range v.Headers {
  129. if value == nil {
  130. return nil, errors.New("Empty HTTP header value: " + key)
  131. }
  132. config.Header = append(config.Header, &http.Header{
  133. Name: key,
  134. Value: append([]string(nil), (*value)...),
  135. })
  136. }
  137. }
  138. return config, nil
  139. }
  140. type HTTPAuthenticator struct {
  141. Request HTTPAuthenticatorRequest `json:"request"`
  142. Response HTTPAuthenticatorResponse `json:"response"`
  143. }
  144. func (v *HTTPAuthenticator) Build() (*loader.TypedSettings, error) {
  145. config := new(http.Config)
  146. requestConfig, err := v.Request.Build()
  147. if err != nil {
  148. return nil, err
  149. }
  150. config.Request = requestConfig
  151. responseConfig, err := v.Response.Build()
  152. if err != nil {
  153. return nil, err
  154. }
  155. config.Response = responseConfig
  156. return loader.NewTypedSettings(config), nil
  157. }