2023-09-06 10:18:02 +08:00
|
|
|
// Copyright 2023 The frp Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-11-28 18:43:33 +08:00
|
|
|
"bytes"
|
2023-09-06 10:18:02 +08:00
|
|
|
"encoding/json"
|
2024-05-23 14:52:12 +08:00
|
|
|
"errors"
|
2023-09-06 10:18:02 +08:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2024-07-08 17:50:36 +08:00
|
|
|
|
|
|
|
"github.com/samber/lo"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
2023-09-06 10:18:02 +08:00
|
|
|
)
|
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
type ClientPluginOptions interface {
|
|
|
|
Complete()
|
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
|
|
|
|
type TypedClientPluginOptions struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
ClientPluginOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TypedClientPluginOptions) UnmarshalJSON(b []byte) error {
|
|
|
|
if len(b) == 4 && string(b) == "null" {
|
2023-10-11 15:01:07 +08:00
|
|
|
return nil
|
2023-09-06 10:18:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typeStruct := struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(b, &typeStruct); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Type = typeStruct.Type
|
2023-10-11 15:01:07 +08:00
|
|
|
if c.Type == "" {
|
2024-05-23 14:52:12 +08:00
|
|
|
return errors.New("plugin type is empty")
|
2023-10-11 15:01:07 +08:00
|
|
|
}
|
2023-09-06 10:18:02 +08:00
|
|
|
|
|
|
|
v, ok := clientPluginOptionsTypeMap[typeStruct.Type]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("unknown plugin type: %s", typeStruct.Type)
|
|
|
|
}
|
2023-09-13 16:32:39 +08:00
|
|
|
options := reflect.New(v).Interface().(ClientPluginOptions)
|
2023-11-28 18:43:33 +08:00
|
|
|
|
|
|
|
decoder := json.NewDecoder(bytes.NewBuffer(b))
|
|
|
|
if DisallowUnknownFields {
|
|
|
|
decoder.DisallowUnknownFields()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(options); err != nil {
|
2024-03-12 15:14:13 +08:00
|
|
|
return fmt.Errorf("unmarshal ClientPluginOptions error: %v", err)
|
2023-09-06 10:18:02 +08:00
|
|
|
}
|
2023-09-13 16:32:39 +08:00
|
|
|
c.ClientPluginOptions = options
|
2023-09-06 10:18:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 14:52:12 +08:00
|
|
|
func (c *TypedClientPluginOptions) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(c.ClientPluginOptions)
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
const (
|
|
|
|
PluginHTTP2HTTPS = "http2https"
|
|
|
|
PluginHTTPProxy = "http_proxy"
|
|
|
|
PluginHTTPS2HTTP = "https2http"
|
|
|
|
PluginHTTPS2HTTPS = "https2https"
|
2024-07-08 17:50:36 +08:00
|
|
|
PluginHTTP2HTTP = "http2http"
|
2023-09-06 10:18:02 +08:00
|
|
|
PluginSocks5 = "socks5"
|
|
|
|
PluginStaticFile = "static_file"
|
|
|
|
PluginUnixDomainSocket = "unix_domain_socket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var clientPluginOptionsTypeMap = map[string]reflect.Type{
|
|
|
|
PluginHTTP2HTTPS: reflect.TypeOf(HTTP2HTTPSPluginOptions{}),
|
|
|
|
PluginHTTPProxy: reflect.TypeOf(HTTPProxyPluginOptions{}),
|
|
|
|
PluginHTTPS2HTTP: reflect.TypeOf(HTTPS2HTTPPluginOptions{}),
|
|
|
|
PluginHTTPS2HTTPS: reflect.TypeOf(HTTPS2HTTPSPluginOptions{}),
|
2024-07-08 17:50:36 +08:00
|
|
|
PluginHTTP2HTTP: reflect.TypeOf(HTTP2HTTPPluginOptions{}),
|
2023-09-06 10:18:02 +08:00
|
|
|
PluginSocks5: reflect.TypeOf(Socks5PluginOptions{}),
|
|
|
|
PluginStaticFile: reflect.TypeOf(StaticFilePluginOptions{}),
|
|
|
|
PluginUnixDomainSocket: reflect.TypeOf(UnixDomainSocketPluginOptions{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTP2HTTPSPluginOptions struct {
|
2023-11-28 18:43:33 +08:00
|
|
|
Type string `json:"type,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
LocalAddr string `json:"localAddr,omitempty"`
|
|
|
|
HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
|
|
|
|
RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
func (o *HTTP2HTTPSPluginOptions) Complete() {}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type HTTPProxyPluginOptions struct {
|
2023-11-28 18:43:33 +08:00
|
|
|
Type string `json:"type,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
HTTPUser string `json:"httpUser,omitempty"`
|
|
|
|
HTTPPassword string `json:"httpPassword,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
func (o *HTTPProxyPluginOptions) Complete() {}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type HTTPS2HTTPPluginOptions struct {
|
2023-11-28 18:43:33 +08:00
|
|
|
Type string `json:"type,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
LocalAddr string `json:"localAddr,omitempty"`
|
|
|
|
HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
|
|
|
|
RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
|
2024-07-08 17:50:36 +08:00
|
|
|
EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
CrtPath string `json:"crtPath,omitempty"`
|
|
|
|
KeyPath string `json:"keyPath,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
func (o *HTTPS2HTTPPluginOptions) Complete() {
|
|
|
|
o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true))
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type HTTPS2HTTPSPluginOptions struct {
|
2023-11-28 18:43:33 +08:00
|
|
|
Type string `json:"type,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
LocalAddr string `json:"localAddr,omitempty"`
|
|
|
|
HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
|
|
|
|
RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
|
2024-07-08 17:50:36 +08:00
|
|
|
EnableHTTP2 *bool `json:"enableHTTP2,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
CrtPath string `json:"crtPath,omitempty"`
|
|
|
|
KeyPath string `json:"keyPath,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
func (o *HTTPS2HTTPSPluginOptions) Complete() {
|
|
|
|
o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTP2HTTPPluginOptions struct {
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
LocalAddr string `json:"localAddr,omitempty"`
|
|
|
|
HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"`
|
|
|
|
RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *HTTP2HTTPPluginOptions) Complete() {}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type Socks5PluginOptions struct {
|
2023-11-28 18:43:33 +08:00
|
|
|
Type string `json:"type,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
func (o *Socks5PluginOptions) Complete() {}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type StaticFilePluginOptions struct {
|
2023-11-28 18:43:33 +08:00
|
|
|
Type string `json:"type,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
LocalPath string `json:"localPath,omitempty"`
|
|
|
|
StripPrefix string `json:"stripPrefix,omitempty"`
|
|
|
|
HTTPUser string `json:"httpUser,omitempty"`
|
|
|
|
HTTPPassword string `json:"httpPassword,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
func (o *StaticFilePluginOptions) Complete() {}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
type UnixDomainSocketPluginOptions struct {
|
2023-11-28 18:43:33 +08:00
|
|
|
Type string `json:"type,omitempty"`
|
2023-09-06 10:18:02 +08:00
|
|
|
UnixPath string `json:"unixPath,omitempty"`
|
|
|
|
}
|
2024-06-12 17:30:10 +08:00
|
|
|
|
2024-07-08 17:50:36 +08:00
|
|
|
func (o *UnixDomainSocketPluginOptions) Complete() {}
|