plugin.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2023 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v1
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "reflect"
  20. )
  21. type ClientPluginOptions interface{}
  22. type TypedClientPluginOptions struct {
  23. Type string `json:"type"`
  24. ClientPluginOptions
  25. }
  26. func (c *TypedClientPluginOptions) UnmarshalJSON(b []byte) error {
  27. if len(b) == 4 && string(b) == "null" {
  28. return nil
  29. }
  30. typeStruct := struct {
  31. Type string `json:"type"`
  32. }{}
  33. if err := json.Unmarshal(b, &typeStruct); err != nil {
  34. return err
  35. }
  36. c.Type = typeStruct.Type
  37. if c.Type == "" {
  38. return nil
  39. }
  40. v, ok := clientPluginOptionsTypeMap[typeStruct.Type]
  41. if !ok {
  42. return fmt.Errorf("unknown plugin type: %s", typeStruct.Type)
  43. }
  44. options := reflect.New(v).Interface().(ClientPluginOptions)
  45. decoder := json.NewDecoder(bytes.NewBuffer(b))
  46. if DisallowUnknownFields {
  47. decoder.DisallowUnknownFields()
  48. }
  49. if err := decoder.Decode(options); err != nil {
  50. return fmt.Errorf("unmarshal ClientPluginOptions error: %v", err)
  51. }
  52. c.ClientPluginOptions = options
  53. return nil
  54. }
  55. const (
  56. PluginHTTP2HTTPS = "http2https"
  57. PluginHTTPProxy = "http_proxy"
  58. PluginHTTPS2HTTP = "https2http"
  59. PluginHTTPS2HTTPS = "https2https"
  60. PluginSocks5 = "socks5"
  61. PluginStaticFile = "static_file"
  62. PluginUnixDomainSocket = "unix_domain_socket"
  63. )
  64. var clientPluginOptionsTypeMap = map[string]reflect.Type{
  65. PluginHTTP2HTTPS: reflect.TypeOf(HTTP2HTTPSPluginOptions{}),
  66. PluginHTTPProxy: reflect.TypeOf(HTTPProxyPluginOptions{}),
  67. PluginHTTPS2HTTP: reflect.TypeOf(HTTPS2HTTPPluginOptions{}),
  68. PluginHTTPS2HTTPS: reflect.TypeOf(HTTPS2HTTPSPluginOptions{}),
  69. PluginSocks5: reflect.TypeOf(Socks5PluginOptions{}),
  70. PluginStaticFile: reflect.TypeOf(StaticFilePluginOptions{}),
  71. PluginUnixDomainSocket: reflect.TypeOf(UnixDomainSocketPluginOptions{}),
  72. }
  73. type HTTP2HTTPSPluginOptions struct {
  74. Type string `json:"type,omitempty"`
  75. LocalAddr string `json:"localAddr,omitempty"`
  76. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  77. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  78. }
  79. type HTTPProxyPluginOptions struct {
  80. Type string `json:"type,omitempty"`
  81. HTTPUser string `json:"httpUser,omitempty"`
  82. HTTPPassword string `json:"httpPassword,omitempty"`
  83. }
  84. type HTTPS2HTTPPluginOptions struct {
  85. Type string `json:"type,omitempty"`
  86. LocalAddr string `json:"localAddr,omitempty"`
  87. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  88. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  89. CrtPath string `json:"crtPath,omitempty"`
  90. KeyPath string `json:"keyPath,omitempty"`
  91. }
  92. type HTTPS2HTTPSPluginOptions struct {
  93. Type string `json:"type,omitempty"`
  94. LocalAddr string `json:"localAddr,omitempty"`
  95. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  96. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  97. CrtPath string `json:"crtPath,omitempty"`
  98. KeyPath string `json:"keyPath,omitempty"`
  99. }
  100. type Socks5PluginOptions struct {
  101. Type string `json:"type,omitempty"`
  102. Username string `json:"username,omitempty"`
  103. Password string `json:"password,omitempty"`
  104. }
  105. type StaticFilePluginOptions struct {
  106. Type string `json:"type,omitempty"`
  107. LocalPath string `json:"localPath,omitempty"`
  108. StripPrefix string `json:"stripPrefix,omitempty"`
  109. HTTPUser string `json:"httpUser,omitempty"`
  110. HTTPPassword string `json:"httpPassword,omitempty"`
  111. }
  112. type UnixDomainSocketPluginOptions struct {
  113. Type string `json:"type,omitempty"`
  114. UnixPath string `json:"unixPath,omitempty"`
  115. }