transport_authenticators.go 3.7 KB

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