transport_authenticators.go 4.1 KB

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