plugin.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "encoding/json"
  17. "fmt"
  18. "reflect"
  19. )
  20. type ClientPluginOptions interface{}
  21. type TypedClientPluginOptions struct {
  22. Type string `json:"type"`
  23. ClientPluginOptions
  24. }
  25. func (c *TypedClientPluginOptions) UnmarshalJSON(b []byte) error {
  26. if len(b) == 4 && string(b) == "null" {
  27. return nil
  28. }
  29. typeStruct := struct {
  30. Type string `json:"type"`
  31. }{}
  32. if err := json.Unmarshal(b, &typeStruct); err != nil {
  33. return err
  34. }
  35. c.Type = typeStruct.Type
  36. if c.Type == "" {
  37. return nil
  38. }
  39. v, ok := clientPluginOptionsTypeMap[typeStruct.Type]
  40. if !ok {
  41. return fmt.Errorf("unknown plugin type: %s", typeStruct.Type)
  42. }
  43. options := reflect.New(v).Interface().(ClientPluginOptions)
  44. if err := json.Unmarshal(b, options); err != nil {
  45. return err
  46. }
  47. c.ClientPluginOptions = options
  48. return nil
  49. }
  50. const (
  51. PluginHTTP2HTTPS = "http2https"
  52. PluginHTTPProxy = "http_proxy"
  53. PluginHTTPS2HTTP = "https2http"
  54. PluginHTTPS2HTTPS = "https2https"
  55. PluginSocks5 = "socks5"
  56. PluginStaticFile = "static_file"
  57. PluginUnixDomainSocket = "unix_domain_socket"
  58. )
  59. var clientPluginOptionsTypeMap = map[string]reflect.Type{
  60. PluginHTTP2HTTPS: reflect.TypeOf(HTTP2HTTPSPluginOptions{}),
  61. PluginHTTPProxy: reflect.TypeOf(HTTPProxyPluginOptions{}),
  62. PluginHTTPS2HTTP: reflect.TypeOf(HTTPS2HTTPPluginOptions{}),
  63. PluginHTTPS2HTTPS: reflect.TypeOf(HTTPS2HTTPSPluginOptions{}),
  64. PluginSocks5: reflect.TypeOf(Socks5PluginOptions{}),
  65. PluginStaticFile: reflect.TypeOf(StaticFilePluginOptions{}),
  66. PluginUnixDomainSocket: reflect.TypeOf(UnixDomainSocketPluginOptions{}),
  67. }
  68. type HTTP2HTTPSPluginOptions struct {
  69. LocalAddr string `json:"localAddr,omitempty"`
  70. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  71. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  72. }
  73. type HTTPProxyPluginOptions struct {
  74. HTTPUser string `json:"httpUser,omitempty"`
  75. HTTPPassword string `json:"httpPassword,omitempty"`
  76. }
  77. type HTTPS2HTTPPluginOptions struct {
  78. LocalAddr string `json:"localAddr,omitempty"`
  79. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  80. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  81. CrtPath string `json:"crtPath,omitempty"`
  82. KeyPath string `json:"keyPath,omitempty"`
  83. }
  84. type HTTPS2HTTPSPluginOptions struct {
  85. LocalAddr string `json:"localAddr,omitempty"`
  86. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  87. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  88. CrtPath string `json:"crtPath,omitempty"`
  89. KeyPath string `json:"keyPath,omitempty"`
  90. }
  91. type Socks5PluginOptions struct {
  92. Username string `json:"username,omitempty"`
  93. Password string `json:"password,omitempty"`
  94. }
  95. type StaticFilePluginOptions struct {
  96. LocalPath string `json:"localPath,omitempty"`
  97. StripPrefix string `json:"stripPrefix,omitempty"`
  98. HTTPUser string `json:"httpUser,omitempty"`
  99. HTTPPassword string `json:"httpPassword,omitempty"`
  100. }
  101. type UnixDomainSocketPluginOptions struct {
  102. UnixPath string `json:"unixPath,omitempty"`
  103. }