plugin.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. "github.com/samber/lo"
  22. "github.com/fatedier/frp/pkg/util/util"
  23. )
  24. type ClientPluginOptions interface {
  25. Complete()
  26. }
  27. type TypedClientPluginOptions struct {
  28. Type string `json:"type"`
  29. ClientPluginOptions
  30. }
  31. func (c *TypedClientPluginOptions) UnmarshalJSON(b []byte) error {
  32. if len(b) == 4 && string(b) == "null" {
  33. return nil
  34. }
  35. typeStruct := struct {
  36. Type string `json:"type"`
  37. }{}
  38. if err := json.Unmarshal(b, &typeStruct); err != nil {
  39. return err
  40. }
  41. c.Type = typeStruct.Type
  42. if c.Type == "" {
  43. return errors.New("plugin type is empty")
  44. }
  45. v, ok := clientPluginOptionsTypeMap[typeStruct.Type]
  46. if !ok {
  47. return fmt.Errorf("unknown plugin type: %s", typeStruct.Type)
  48. }
  49. options := reflect.New(v).Interface().(ClientPluginOptions)
  50. decoder := json.NewDecoder(bytes.NewBuffer(b))
  51. if DisallowUnknownFields {
  52. decoder.DisallowUnknownFields()
  53. }
  54. if err := decoder.Decode(options); err != nil {
  55. return fmt.Errorf("unmarshal ClientPluginOptions error: %v", err)
  56. }
  57. c.ClientPluginOptions = options
  58. return nil
  59. }
  60. func (c *TypedClientPluginOptions) MarshalJSON() ([]byte, error) {
  61. return json.Marshal(c.ClientPluginOptions)
  62. }
  63. const (
  64. PluginHTTP2HTTPS = "http2https"
  65. PluginHTTPProxy = "http_proxy"
  66. PluginHTTPS2HTTP = "https2http"
  67. PluginHTTPS2HTTPS = "https2https"
  68. PluginHTTP2HTTP = "http2http"
  69. PluginSocks5 = "socks5"
  70. PluginStaticFile = "static_file"
  71. PluginUnixDomainSocket = "unix_domain_socket"
  72. PluginTLS2Raw = "tls2raw"
  73. )
  74. var clientPluginOptionsTypeMap = map[string]reflect.Type{
  75. PluginHTTP2HTTPS: reflect.TypeOf(HTTP2HTTPSPluginOptions{}),
  76. PluginHTTPProxy: reflect.TypeOf(HTTPProxyPluginOptions{}),
  77. PluginHTTPS2HTTP: reflect.TypeOf(HTTPS2HTTPPluginOptions{}),
  78. PluginHTTPS2HTTPS: reflect.TypeOf(HTTPS2HTTPSPluginOptions{}),
  79. PluginHTTP2HTTP: reflect.TypeOf(HTTP2HTTPPluginOptions{}),
  80. PluginSocks5: reflect.TypeOf(Socks5PluginOptions{}),
  81. PluginStaticFile: reflect.TypeOf(StaticFilePluginOptions{}),
  82. PluginUnixDomainSocket: reflect.TypeOf(UnixDomainSocketPluginOptions{}),
  83. PluginTLS2Raw: reflect.TypeOf(TLS2RawPluginOptions{}),
  84. }
  85. type HTTP2HTTPSPluginOptions struct {
  86. Type string `json:"type,omitempty"`
  87. LocalAddr string `json:"localAddr,omitempty"`
  88. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  89. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  90. }
  91. func (o *HTTP2HTTPSPluginOptions) Complete() {}
  92. type HTTPProxyPluginOptions struct {
  93. Type string `json:"type,omitempty"`
  94. HTTPUser string `json:"httpUser,omitempty"`
  95. HTTPPassword string `json:"httpPassword,omitempty"`
  96. }
  97. func (o *HTTPProxyPluginOptions) Complete() {}
  98. type HTTPS2HTTPPluginOptions struct {
  99. Type string `json:"type,omitempty"`
  100. LocalAddr string `json:"localAddr,omitempty"`
  101. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  102. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  103. EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
  104. CrtPath string `json:"crtPath,omitempty"`
  105. KeyPath string `json:"keyPath,omitempty"`
  106. }
  107. func (o *HTTPS2HTTPPluginOptions) Complete() {
  108. o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true))
  109. }
  110. type HTTPS2HTTPSPluginOptions struct {
  111. Type string `json:"type,omitempty"`
  112. LocalAddr string `json:"localAddr,omitempty"`
  113. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  114. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  115. EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
  116. CrtPath string `json:"crtPath,omitempty"`
  117. KeyPath string `json:"keyPath,omitempty"`
  118. }
  119. func (o *HTTPS2HTTPSPluginOptions) Complete() {
  120. o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true))
  121. }
  122. type HTTP2HTTPPluginOptions struct {
  123. Type string `json:"type,omitempty"`
  124. LocalAddr string `json:"localAddr,omitempty"`
  125. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  126. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  127. }
  128. func (o *HTTP2HTTPPluginOptions) Complete() {}
  129. type Socks5PluginOptions struct {
  130. Type string `json:"type,omitempty"`
  131. Username string `json:"username,omitempty"`
  132. Password string `json:"password,omitempty"`
  133. }
  134. func (o *Socks5PluginOptions) Complete() {}
  135. type StaticFilePluginOptions struct {
  136. Type string `json:"type,omitempty"`
  137. LocalPath string `json:"localPath,omitempty"`
  138. StripPrefix string `json:"stripPrefix,omitempty"`
  139. HTTPUser string `json:"httpUser,omitempty"`
  140. HTTPPassword string `json:"httpPassword,omitempty"`
  141. }
  142. func (o *StaticFilePluginOptions) Complete() {}
  143. type UnixDomainSocketPluginOptions struct {
  144. Type string `json:"type,omitempty"`
  145. UnixPath string `json:"unixPath,omitempty"`
  146. }
  147. func (o *UnixDomainSocketPluginOptions) Complete() {}
  148. type TLS2RawPluginOptions struct {
  149. Type string `json:"type,omitempty"`
  150. LocalAddr string `json:"localAddr,omitempty"`
  151. CrtPath string `json:"crtPath,omitempty"`
  152. KeyPath string `json:"keyPath,omitempty"`
  153. }
  154. func (o *TLS2RawPluginOptions) Complete() {}