client.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "github.com/fatedier/frp/client"
  12. httppkg "github.com/fatedier/frp/pkg/util/http"
  13. )
  14. type Client struct {
  15. address string
  16. authUser string
  17. authPwd string
  18. }
  19. func New(host string, port int) *Client {
  20. return &Client{
  21. address: net.JoinHostPort(host, strconv.Itoa(port)),
  22. }
  23. }
  24. func (c *Client) SetAuth(user, pwd string) {
  25. c.authUser = user
  26. c.authPwd = pwd
  27. }
  28. func (c *Client) GetProxyStatus(name string) (*client.ProxyStatusResp, error) {
  29. req, err := http.NewRequest("GET", "http://"+c.address+"/api/status", nil)
  30. if err != nil {
  31. return nil, err
  32. }
  33. content, err := c.do(req)
  34. if err != nil {
  35. return nil, err
  36. }
  37. allStatus := make(client.StatusResp)
  38. if err = json.Unmarshal([]byte(content), &allStatus); err != nil {
  39. return nil, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(content))
  40. }
  41. for _, pss := range allStatus {
  42. for _, ps := range pss {
  43. if ps.Name == name {
  44. return &ps, nil
  45. }
  46. }
  47. }
  48. return nil, fmt.Errorf("no proxy status found")
  49. }
  50. func (c *Client) GetAllProxyStatus() (client.StatusResp, error) {
  51. req, err := http.NewRequest("GET", "http://"+c.address+"/api/status", nil)
  52. if err != nil {
  53. return nil, err
  54. }
  55. content, err := c.do(req)
  56. if err != nil {
  57. return nil, err
  58. }
  59. allStatus := make(client.StatusResp)
  60. if err = json.Unmarshal([]byte(content), &allStatus); err != nil {
  61. return nil, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(content))
  62. }
  63. return allStatus, nil
  64. }
  65. func (c *Client) Reload(strictMode bool) error {
  66. v := url.Values{}
  67. if strictMode {
  68. v.Set("strictConfig", "true")
  69. }
  70. queryStr := ""
  71. if len(v) > 0 {
  72. queryStr = "?" + v.Encode()
  73. }
  74. req, err := http.NewRequest("GET", "http://"+c.address+"/api/reload"+queryStr, nil)
  75. if err != nil {
  76. return err
  77. }
  78. _, err = c.do(req)
  79. return err
  80. }
  81. func (c *Client) Stop() error {
  82. req, err := http.NewRequest("POST", "http://"+c.address+"/api/stop", nil)
  83. if err != nil {
  84. return err
  85. }
  86. _, err = c.do(req)
  87. return err
  88. }
  89. func (c *Client) GetConfig() (string, error) {
  90. req, err := http.NewRequest("GET", "http://"+c.address+"/api/config", nil)
  91. if err != nil {
  92. return "", err
  93. }
  94. return c.do(req)
  95. }
  96. func (c *Client) UpdateConfig(content string) error {
  97. req, err := http.NewRequest("PUT", "http://"+c.address+"/api/config", strings.NewReader(content))
  98. if err != nil {
  99. return err
  100. }
  101. _, err = c.do(req)
  102. return err
  103. }
  104. func (c *Client) setAuthHeader(req *http.Request) {
  105. if c.authUser != "" || c.authPwd != "" {
  106. req.Header.Set("Authorization", httppkg.BasicAuth(c.authUser, c.authPwd))
  107. }
  108. }
  109. func (c *Client) do(req *http.Request) (string, error) {
  110. c.setAuthHeader(req)
  111. resp, err := http.DefaultClient.Do(req)
  112. if err != nil {
  113. return "", err
  114. }
  115. defer resp.Body.Close()
  116. if resp.StatusCode != 200 {
  117. return "", fmt.Errorf("api status code [%d]", resp.StatusCode)
  118. }
  119. buf, err := io.ReadAll(resp.Body)
  120. if err != nil {
  121. return "", err
  122. }
  123. return string(buf), nil
  124. }