transport_authenticators.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package conf
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. "v2ray.com/core/common/loader"
  7. "v2ray.com/core/transport/internet/authenticators/http"
  8. "v2ray.com/core/transport/internet/authenticators/noop"
  9. "v2ray.com/core/transport/internet/authenticators/srtp"
  10. "v2ray.com/core/transport/internet/authenticators/utp"
  11. )
  12. type NoOpAuthenticator struct{}
  13. func (NoOpAuthenticator) Build() (*loader.TypedSettings, error) {
  14. return loader.NewTypedSettings(new(noop.Config)), nil
  15. }
  16. type NoOpConnectionAuthenticator struct{}
  17. func (NoOpConnectionAuthenticator) Build() (*loader.TypedSettings, error) {
  18. return loader.NewTypedSettings(new(noop.Config)), nil
  19. }
  20. type SRTPAuthenticator struct{}
  21. func (SRTPAuthenticator) Build() (*loader.TypedSettings, error) {
  22. return loader.NewTypedSettings(new(srtp.Config)), nil
  23. }
  24. type UTPAuthenticator struct{}
  25. func (UTPAuthenticator) Build() (*loader.TypedSettings, error) {
  26. return loader.NewTypedSettings(new(utp.Config)), nil
  27. }
  28. type HTTPAuthenticatorHeader struct {
  29. Name string `json:"name"`
  30. Value *StringList `json:"value"`
  31. }
  32. type HTTPAuthenticatorRequest struct {
  33. Version *string `json:"version"`
  34. Method *string `json:"method"`
  35. Path *StringList `json:"path"`
  36. Headers []HTTPAuthenticatorHeader `json:"headers"`
  37. }
  38. func (this *HTTPAuthenticatorRequest) Build() (*http.RequestConfig, error) {
  39. config := &http.RequestConfig{
  40. Uri: []string{"/"},
  41. Header: []*http.Header{
  42. {
  43. Name: "Host",
  44. Value: []string{"www.baidu.com", "www.bing.com"},
  45. },
  46. },
  47. }
  48. if this.Version != nil {
  49. config.Version = &http.Version{Value: *this.Version}
  50. }
  51. if this.Method != nil {
  52. config.Method = &http.Method{Value: *this.Method}
  53. }
  54. if this.Path != nil && this.Path.Len() > 0 {
  55. config.Uri = append([]string(nil), (*this.Path)...)
  56. }
  57. if len(this.Headers) > 0 {
  58. config.Header = make([]*http.Header, len(this.Headers))
  59. for idx, header := range this.Headers {
  60. config.Header[idx] = &http.Header{
  61. Name: header.Name,
  62. Value: append([]string(nil), (*header.Value)...),
  63. }
  64. }
  65. }
  66. return config, nil
  67. }
  68. type HTTPAuthenticatorResponse struct {
  69. Version *string `json:"version"`
  70. Status *string `json:"status"`
  71. Reason *string `json:"reason"`
  72. Headers []HTTPAuthenticatorHeader `json:"headers"`
  73. }
  74. func (this *HTTPAuthenticatorResponse) Build() (*http.ResponseConfig, error) {
  75. config := &http.ResponseConfig{
  76. Header: []*http.Header{
  77. {
  78. Name: "Content-Type",
  79. Value: []string{"application/octet-stream", "video/mpeg"},
  80. },
  81. {
  82. Name: "Transfer-Encoding",
  83. Value: []string{"chunked"},
  84. },
  85. },
  86. }
  87. if this.Version != nil {
  88. config.Version = &http.Version{Value: *this.Version}
  89. }
  90. if this.Status != nil || this.Reason != nil {
  91. config.Status = &http.Status{
  92. Code: "200",
  93. Reason: "OK",
  94. }
  95. if this.Status != nil {
  96. config.Status.Code = *this.Status
  97. }
  98. if this.Reason != nil {
  99. config.Status.Reason = *this.Reason
  100. }
  101. }
  102. if len(this.Headers) > 0 {
  103. config.Header = make([]*http.Header, len(this.Headers))
  104. for idx, header := range this.Headers {
  105. config.Header[idx] = &http.Header{
  106. Name: header.Name,
  107. Value: append([]string(nil), (*header.Value)...),
  108. }
  109. }
  110. }
  111. return config, nil
  112. }
  113. type HTTPAuthenticator struct {
  114. Request *HTTPAuthenticatorRequest `json:"request"`
  115. Response json.RawMessage `json:"response"`
  116. }
  117. func (this *HTTPAuthenticator) Build() (*loader.TypedSettings, error) {
  118. config := new(http.Config)
  119. if this.Request != nil {
  120. requestConfig, err := this.Request.Build()
  121. if err != nil {
  122. return nil, err
  123. }
  124. config.Request = requestConfig
  125. }
  126. if len(this.Response) > 0 {
  127. var text string
  128. parsed := false
  129. if err := json.Unmarshal(this.Response, &text); err == nil {
  130. if strings.ToLower(text) != "disabled" {
  131. return nil, errors.New("Unknown HTTP header settings: " + text)
  132. }
  133. parsed = true
  134. }
  135. if !parsed {
  136. var response HTTPAuthenticatorResponse
  137. if err := json.Unmarshal(this.Response, &response); err != nil {
  138. return nil, errors.New("Failed to parse HTTP header response.")
  139. }
  140. responseConfig, err := response.Build()
  141. if err != nil {
  142. return nil, err
  143. }
  144. config.Response = responseConfig
  145. }
  146. }
  147. return loader.NewTypedSettings(config), nil
  148. }