http.go 2.7 KB

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