2021-06-18 16:48:36 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-09-29 10:33:57 +08:00
|
|
|
"io"
|
2021-06-18 16:48:36 +08:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatedier/frp/client"
|
2023-09-15 10:33:32 +08:00
|
|
|
"github.com/fatedier/frp/pkg/util/util"
|
2021-06-18 16:48:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
address string
|
|
|
|
authUser string
|
|
|
|
authPwd string
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(host string, port int) *Client {
|
|
|
|
return &Client{
|
|
|
|
address: net.JoinHostPort(host, strconv.Itoa(port)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetAuth(user, pwd string) {
|
|
|
|
c.authUser = user
|
|
|
|
c.authPwd = pwd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetProxyStatus(name string) (*client.ProxyStatusResp, error) {
|
|
|
|
req, err := http.NewRequest("GET", "http://"+c.address+"/api/status", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
content, err := c.do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-26 02:54:53 +08:00
|
|
|
allStatus := make(client.StatusResp)
|
2021-06-18 16:48:36 +08:00
|
|
|
if err = json.Unmarshal([]byte(content), &allStatus); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(content))
|
|
|
|
}
|
2023-02-26 02:54:53 +08:00
|
|
|
for _, pss := range allStatus {
|
|
|
|
for _, ps := range pss {
|
|
|
|
if ps.Name == name {
|
|
|
|
return &ps, nil
|
|
|
|
}
|
2021-06-18 16:48:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no proxy status found")
|
|
|
|
}
|
|
|
|
|
2023-09-15 10:33:32 +08:00
|
|
|
func (c *Client) GetAllProxyStatus() (client.StatusResp, error) {
|
|
|
|
req, err := http.NewRequest("GET", "http://"+c.address+"/api/status", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
content, err := c.do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
allStatus := make(client.StatusResp)
|
|
|
|
if err = json.Unmarshal([]byte(content), &allStatus); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(content))
|
|
|
|
}
|
|
|
|
return allStatus, nil
|
|
|
|
}
|
|
|
|
|
2021-06-18 16:48:36 +08:00
|
|
|
func (c *Client) Reload() error {
|
|
|
|
req, err := http.NewRequest("GET", "http://"+c.address+"/api/reload", nil)
|
|
|
|
if err != nil {
|
2023-06-30 17:35:37 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = c.do(req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Stop() error {
|
|
|
|
req, err := http.NewRequest("POST", "http://"+c.address+"/api/stop", nil)
|
|
|
|
if err != nil {
|
2021-06-18 16:48:36 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = c.do(req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetConfig() (string, error) {
|
|
|
|
req, err := http.NewRequest("GET", "http://"+c.address+"/api/config", nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return c.do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) UpdateConfig(content string) error {
|
|
|
|
req, err := http.NewRequest("PUT", "http://"+c.address+"/api/config", strings.NewReader(content))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = c.do(req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) setAuthHeader(req *http.Request) {
|
|
|
|
if c.authUser != "" || c.authPwd != "" {
|
2023-09-15 10:33:32 +08:00
|
|
|
req.Header.Set("Authorization", util.BasicAuth(c.authUser, c.authPwd))
|
2021-06-18 16:48:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) do(req *http.Request) (string, error) {
|
|
|
|
c.setAuthHeader(req)
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return "", fmt.Errorf("api status code [%d]", resp.StatusCode)
|
|
|
|
}
|
2021-09-29 10:33:57 +08:00
|
|
|
buf, err := io.ReadAll(resp.Body)
|
2021-06-18 16:48:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
|
|
|
}
|