1
0

plugin.go 3.8 KB

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