| // |
| 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 |
| 15 | package server |
| 16 |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "context" |
| 20 | "crypto/tls" |
| 21 | "encoding/json" |
| 22 | "fmt" |
| 23 | "io" |
| 24 | "net/http" |
| 25 | "net/url" |
| 26 | "reflect" |
| 27 | "slices" |
| 28 | "strings" |
| 29 |
| 30 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 31 | ) |
| 32 |
| 33 | type httpPlugin struct { |
| 34 | options v1.HTTPPluginOptions |
| 35 |
| 36 | url string |
| 37 | client *http.Client |
| 38 | } |
| 39 |
| 40 | func NewHTTPPluginOptions(options v1.HTTPPluginOptions) Plugin { |
| 41 | url := fmt.Sprintf("%s%s", options.Addr, options.Path) |
| 42 |
| 43 | var client *http.Client |
| 44 | if strings.HasPrefix(url, "https://") { |
| 45 | tr := &http.Transport{ |
| 46 | TLSClientConfig: &tls.Config{InsecureSkipVerify: !options.TLSVerify}, |
| 47 | } |
| 48 | client = &http.Client{Transport: tr} |
| 49 | } else { |
| 50 | client = &http.Client{} |
| 51 | } |
| 52 |
| 53 | if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") { |
| 54 | url = "http://" + url |
| 55 | } |
| 56 | return &httpPlugin{ |
| 57 | options: options, |
| 58 | url: url, |
| 59 | client: client, |
| 60 | } |
| 61 | } |
| 62 |
| 63 | func (p *httpPlugin) Name() string { |
| 64 | return p.options.Name |
| 65 | } |
| 66 |
| 67 | func (p *httpPlugin) IsSupport(op string) bool { |
| 68 | return slices.Contains(p.options.Ops, op) |
| 69 | } |
| 70 |
| 71 | func (p *httpPlugin) Handle(ctx context.Context, op string, content any) (*Response, any, error) { |
| 72 | r := &Request{ |
| 73 | Version: APIVersion, |
| 74 | Op: op, |
| 75 | Content: content, |
| 76 | } |
| 77 | var res Response |
| 78 | res.Content = reflect.New(reflect.TypeOf(content)).Interface() |
| 79 | if err := p.do(ctx, r, &res); err != nil { |
| 80 | return nil, nil, err |
| 81 | } |
| 82 | return &res, res.Content, nil |
| 83 | } |
| 84 |
| 85 | func (p *httpPlugin) do(ctx context.Context, r *Request, res *Response) error { |
| 86 | buf, err := json.Marshal(r) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | v := url.Values{} |
| 91 | v.Set("version", r.Version) |
| 92 | v.Set("op", r.Op) |
| 93 | req, err := http.NewRequest("POST", p.url+"?"+v.Encode(), bytes.NewReader(buf)) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | req = req.WithContext(ctx) |
| 98 | req.Header.Set("X-Frp-Reqid", GetReqidFromContext(ctx)) |
| 99 | req.Header.Set("Content-Type", "application/json") |
| 100 | resp, err := p.client.Do(req) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | defer resp.Body.Close() |
| 105 |
| 106 | if resp.StatusCode != http.StatusOK { |
| 107 | return fmt.Errorf("do http request error code: %d", resp.StatusCode) |
| 108 | } |
| 109 | buf, err = io.ReadAll(resp.Body) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | return json.Unmarshal(buf, res) |
| 114 | } |