2019-12-20 13:28:28 +01:00
|
|
|
// Copyright 2019 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// 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 plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-12-03 07:36:14 +01:00
|
|
|
"crypto/tls"
|
2019-12-20 13:28:28 +01:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-09-29 04:33:57 +02:00
|
|
|
"io"
|
2019-12-20 13:28:28 +01:00
|
|
|
"net/http"
|
2020-03-20 13:53:14 +01:00
|
|
|
"net/url"
|
2019-12-20 13:28:28 +01:00
|
|
|
"reflect"
|
2020-12-03 07:36:14 +01:00
|
|
|
"strings"
|
2019-12-20 13:28:28 +01:00
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
|
|
|
)
|
2019-12-20 13:28:28 +01:00
|
|
|
|
|
|
|
type httpPlugin struct {
|
2023-09-06 04:18:02 +02:00
|
|
|
options v1.HTTPPluginOptions
|
2019-12-20 13:28:28 +01:00
|
|
|
|
|
|
|
url string
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2023-09-06 04:18:02 +02:00
|
|
|
func NewHTTPPluginOptions(options v1.HTTPPluginOptions) Plugin {
|
2022-08-28 19:02:53 +02:00
|
|
|
url := fmt.Sprintf("%s%s", options.Addr, options.Path)
|
2020-12-03 07:36:14 +01:00
|
|
|
|
|
|
|
var client *http.Client
|
|
|
|
if strings.HasPrefix(url, "https://") {
|
|
|
|
tr := &http.Transport{
|
2022-08-28 19:02:53 +02:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: !options.TLSVerify},
|
2020-12-03 07:36:14 +01:00
|
|
|
}
|
|
|
|
client = &http.Client{Transport: tr}
|
|
|
|
} else {
|
|
|
|
client = &http.Client{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
|
|
|
|
url = "http://" + url
|
|
|
|
}
|
2019-12-20 13:28:28 +01:00
|
|
|
return &httpPlugin{
|
|
|
|
options: options,
|
2020-12-03 07:36:14 +01:00
|
|
|
url: url,
|
|
|
|
client: client,
|
2019-12-20 13:28:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *httpPlugin) Name() string {
|
|
|
|
return p.options.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *httpPlugin) IsSupport(op string) bool {
|
|
|
|
for _, v := range p.options.Ops {
|
|
|
|
if v == op {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *httpPlugin) Handle(ctx context.Context, op string, content interface{}) (*Response, interface{}, error) {
|
|
|
|
r := &Request{
|
|
|
|
Version: APIVersion,
|
|
|
|
Op: op,
|
|
|
|
Content: content,
|
|
|
|
}
|
|
|
|
var res Response
|
|
|
|
res.Content = reflect.New(reflect.TypeOf(content)).Interface()
|
|
|
|
if err := p.do(ctx, r, &res); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &res, res.Content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *httpPlugin) do(ctx context.Context, r *Request, res *Response) error {
|
|
|
|
buf, err := json.Marshal(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-20 13:53:14 +01:00
|
|
|
v := url.Values{}
|
|
|
|
v.Set("version", r.Version)
|
|
|
|
v.Set("op", r.Op)
|
|
|
|
req, err := http.NewRequest("POST", p.url+"?"+v.Encode(), bytes.NewReader(buf))
|
2019-12-20 13:28:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
req.Header.Set("X-Frp-Reqid", GetReqidFromContext(ctx))
|
2020-03-10 08:23:37 +01:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2019-12-20 13:28:28 +01:00
|
|
|
resp, err := p.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return fmt.Errorf("do http request error code: %d", resp.StatusCode)
|
|
|
|
}
|
2021-09-29 04:33:57 +02:00
|
|
|
buf, err = io.ReadAll(resp.Body)
|
2019-12-20 13:28:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-21 04:30:46 +02:00
|
|
|
return json.Unmarshal(buf, res)
|
2019-12-20 13:28:28 +01:00
|
|
|
}
|