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