transport_authenticators.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package conf
  2. import (
  3. "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 (this *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 this.Version != nil {
  62. config.Version = &http.Version{Value: *this.Version}
  63. }
  64. if this.Method != nil {
  65. config.Method = &http.Method{Value: *this.Method}
  66. }
  67. if this.Path != nil && this.Path.Len() > 0 {
  68. config.Uri = append([]string(nil), (*this.Path)...)
  69. }
  70. if len(this.Headers) > 0 {
  71. config.Header = make([]*http.Header, 0, len(this.Headers))
  72. for key, value := range this.Headers {
  73. config.Header = append(config.Header, &http.Header{
  74. Name: key,
  75. Value: append([]string(nil), (*value)...),
  76. })
  77. }
  78. }
  79. return config, nil
  80. }
  81. type HTTPAuthenticatorResponse struct {
  82. Version *string `json:"version"`
  83. Status *string `json:"status"`
  84. Reason *string `json:"reason"`
  85. Headers map[string]*StringList `json:"headers"`
  86. }
  87. func (this *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
  88. config := &http.ResponseConfig{
  89. Header: []*http.Header{
  90. {
  91. Name: "Content-Type",
  92. Value: []string{"application/octet-stream", "video/mpeg"},
  93. },
  94. {
  95. Name: "Transfer-Encoding",
  96. Value: []string{"chunked"},
  97. },
  98. {
  99. Name: "Connection",
  100. Value: []string{"keep-alive"},
  101. },
  102. {
  103. Name: "Pragma",
  104. Value: []string{"no-cache"},
  105. },
  106. },
  107. }
  108. if this.Version != nil {
  109. config.Version = &http.Version{Value: *this.Version}
  110. }
  111. if this.Status != nil || this.Reason != nil {
  112. config.Status = &http.Status{
  113. Code: "200",
  114. Reason: "OK",
  115. }
  116. if this.Status != nil {
  117. config.Status.Code = *this.Status
  118. }
  119. if this.Reason != nil {
  120. config.Status.Reason = *this.Reason
  121. }
  122. }
  123. if len(this.Headers) > 0 {
  124. config.Header = make([]*http.Header, 0, len(this.Headers))
  125. for key, value := range this.Headers {
  126. config.Header = append(config.Header, &http.Header{
  127. Name: key,
  128. Value: append([]string(nil), (*value)...),
  129. })
  130. }
  131. }
  132. return config, nil
  133. }
  134. type HTTPAuthenticator struct {
  135. Request *HTTPAuthenticatorRequest `json:"request"`
  136. Response *HTTPAuthenticatorResponse `json:"response"`
  137. }
  138. func (this *HTTPAuthenticator) Build() (*loader.TypedSettings, error) {
  139. config := new(http.Config)
  140. if this.Request == nil {
  141. return nil, errors.New("HTTP request settings not set.")
  142. }
  143. requestConfig, err := this.Request.Build()
  144. if err != nil {
  145. return nil, err
  146. }
  147. config.Request = requestConfig
  148. if this.Response != nil {
  149. responseConfig, err := this.Response.Build()
  150. if err != nil {
  151. return nil, err
  152. }
  153. config.Response = responseConfig
  154. }
  155. return loader.NewTypedSettings(config), nil
  156. }