1
0

plugin.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. )
  73. var clientPluginOptionsTypeMap = map[string]reflect.Type{
  74. PluginHTTP2HTTPS: reflect.TypeOf(HTTP2HTTPSPluginOptions{}),
  75. PluginHTTPProxy: reflect.TypeOf(HTTPProxyPluginOptions{}),
  76. PluginHTTPS2HTTP: reflect.TypeOf(HTTPS2HTTPPluginOptions{}),
  77. PluginHTTPS2HTTPS: reflect.TypeOf(HTTPS2HTTPSPluginOptions{}),
  78. PluginHTTP2HTTP: reflect.TypeOf(HTTP2HTTPPluginOptions{}),
  79. PluginSocks5: reflect.TypeOf(Socks5PluginOptions{}),
  80. PluginStaticFile: reflect.TypeOf(StaticFilePluginOptions{}),
  81. PluginUnixDomainSocket: reflect.TypeOf(UnixDomainSocketPluginOptions{}),
  82. }
  83. type HTTP2HTTPSPluginOptions struct {
  84. Type string `json:"type,omitempty"`
  85. LocalAddr string `json:"localAddr,omitempty"`
  86. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  87. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  88. }
  89. func (o *HTTP2HTTPSPluginOptions) Complete() {}
  90. type HTTPProxyPluginOptions struct {
  91. Type string `json:"type,omitempty"`
  92. HTTPUser string `json:"httpUser,omitempty"`
  93. HTTPPassword string `json:"httpPassword,omitempty"`
  94. }
  95. func (o *HTTPProxyPluginOptions) Complete() {}
  96. type HTTPS2HTTPPluginOptions 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. EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
  102. CrtPath string `json:"crtPath,omitempty"`
  103. KeyPath string `json:"keyPath,omitempty"`
  104. }
  105. func (o *HTTPS2HTTPPluginOptions) Complete() {
  106. o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true))
  107. }
  108. type HTTPS2HTTPSPluginOptions struct {
  109. Type string `json:"type,omitempty"`
  110. LocalAddr string `json:"localAddr,omitempty"`
  111. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  112. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  113. EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
  114. CrtPath string `json:"crtPath,omitempty"`
  115. KeyPath string `json:"keyPath,omitempty"`
  116. }
  117. func (o *HTTPS2HTTPSPluginOptions) Complete() {
  118. o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true))
  119. }
  120. type HTTP2HTTPPluginOptions struct {
  121. Type string `json:"type,omitempty"`
  122. LocalAddr string `json:"localAddr,omitempty"`
  123. HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
  124. RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
  125. }
  126. func (o *HTTP2HTTPPluginOptions) Complete() {}
  127. type Socks5PluginOptions struct {
  128. Type string `json:"type,omitempty"`
  129. Username string `json:"username,omitempty"`
  130. Password string `json:"password,omitempty"`
  131. }
  132. func (o *Socks5PluginOptions) Complete() {}
  133. type StaticFilePluginOptions struct {
  134. Type string `json:"type,omitempty"`
  135. LocalPath string `json:"localPath,omitempty"`
  136. StripPrefix string `json:"stripPrefix,omitempty"`
  137. HTTPUser string `json:"httpUser,omitempty"`
  138. HTTPPassword string `json:"httpPassword,omitempty"`
  139. }
  140. func (o *StaticFilePluginOptions) Complete() {}
  141. type UnixDomainSocketPluginOptions struct {
  142. Type string `json:"type,omitempty"`
  143. UnixPath string `json:"unixPath,omitempty"`
  144. }
  145. func (o *UnixDomainSocketPluginOptions) Complete() {}