transport_authenticators.go 4.1 KB

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