plugin.go 3.7 KB

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