1
0

plugin.go 4.4 KB

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