transport_authenticators.go 4.1 KB

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