client.go 2.6 KB

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