http.go 2.9 KB

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