完善通道删除与定时失效功能
This commit is contained in:
@@ -6,6 +6,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"platform/pkg/env"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -18,10 +21,9 @@ type client struct {
|
||||
var Client client
|
||||
|
||||
func Init() {
|
||||
// todo 从环境变量中获取参数
|
||||
Client = client{
|
||||
url: "http://103.139.212.110:9989",
|
||||
token: "PhdnRF3z6VF2sPgygTSl1Xx6QJN0yFIK.anVpcA==.MTc0MzE2ODAwMQ==",
|
||||
url: env.RemoteAddr,
|
||||
token: env.RemoteToken,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,8 +153,8 @@ func (c *client) CloudConnect(param CloudConnectReq) error {
|
||||
|
||||
type CloudDisconnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge []string `json:"edge"`
|
||||
Config []Config `json:"auto_config"`
|
||||
Edge []string `json:"edge,omitempty"`
|
||||
Config []Config `json:"auto_config,omitempty"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -246,11 +248,29 @@ func (c *client) requestCloud(method string, url string, data string) (*http.Res
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("token", c.token)
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpRequest(req, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("==============================")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("==============================")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -267,22 +287,22 @@ func InitGateway(url, username, password string) *Gateway {
|
||||
// region gateway:/port/configs
|
||||
|
||||
type PortConfigsReq struct {
|
||||
Port int `json:"port"`
|
||||
Edge []string `json:"edge,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Time int `json:"time,omitempty"`
|
||||
Status bool `json:"status,omitempty"`
|
||||
Rate int `json:"rate,omitempty"`
|
||||
Whitelist []string `json:"whitelist,omitempty"`
|
||||
Userpass string `json:"userpass,omitempty"`
|
||||
AutoEdgeConfig AutoEdgeConfig `json:"auto_edge_config,omitempty"`
|
||||
Port int `json:"port"`
|
||||
Edge *[]string `json:"edge,omitempty"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Time int `json:"time,omitempty"`
|
||||
Status bool `json:"status"`
|
||||
Rate int `json:"rate,omitempty"`
|
||||
Whitelist *[]string `json:"whitelist,omitempty"`
|
||||
Userpass *string `json:"userpass,omitempty"`
|
||||
AutoEdgeConfig *AutoEdgeConfig `json:"auto_edge_config,omitempty"`
|
||||
}
|
||||
|
||||
type AutoEdgeConfig struct {
|
||||
Province string `json:"province,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
Isp string `json:"isp,omitempty"`
|
||||
Count int `json:"count,omitempty"`
|
||||
Count *int `json:"count,omitempty"`
|
||||
PacketLoss int `json:"packet_loss,omitempty"`
|
||||
}
|
||||
|
||||
@@ -332,8 +352,8 @@ func (c *Gateway) GatewayPortConfigs(params []PortConfigsReq) error {
|
||||
|
||||
type PortActiveReq struct {
|
||||
Port string `json:"port"`
|
||||
Active bool `json:"active"`
|
||||
Status bool `json:"status"`
|
||||
Active *bool `json:"active"`
|
||||
Status *bool `json:"status"`
|
||||
}
|
||||
|
||||
type PortActiveResp struct {
|
||||
@@ -352,22 +372,34 @@ type PortData struct {
|
||||
Userpass string `json:"userpass"`
|
||||
}
|
||||
|
||||
func (c *Gateway) GatewayPortActive(param PortActiveReq) (*PortActiveResp, error) {
|
||||
|
||||
url := strings.Builder{}
|
||||
url.WriteString("/port/active")
|
||||
|
||||
if param.Port != "" {
|
||||
url.WriteString("/")
|
||||
url.WriteString(param.Port)
|
||||
func (c *Gateway) GatewayPortActive(param ...PortActiveReq) (map[string]PortData, error) {
|
||||
_param := PortActiveReq{}
|
||||
if len(param) != 0 {
|
||||
_param = param[0]
|
||||
}
|
||||
|
||||
url.WriteString("?active=")
|
||||
url.WriteString(strconv.FormatBool(param.Active))
|
||||
url.WriteString("&status=")
|
||||
url.WriteString(strconv.FormatBool(param.Status))
|
||||
path := strings.Builder{}
|
||||
path.WriteString("/port/active")
|
||||
|
||||
resp, err := c.requestGateway("POST", url.String(), "")
|
||||
if _param.Port != "" {
|
||||
path.WriteString("/")
|
||||
path.WriteString(_param.Port)
|
||||
}
|
||||
|
||||
values := url.Values{}
|
||||
if _param.Active != nil {
|
||||
values.Set("active", strconv.FormatBool(*_param.Active))
|
||||
}
|
||||
if _param.Status != nil {
|
||||
values.Set("status", strconv.FormatBool(*_param.Status))
|
||||
}
|
||||
|
||||
if len(values) > 0 {
|
||||
path.WriteString("?")
|
||||
path.WriteString(values.Encode())
|
||||
}
|
||||
|
||||
resp, err := c.requestGateway("GET", path.String(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -390,7 +422,11 @@ func (c *Gateway) GatewayPortActive(param PortActiveReq) (*PortActiveResp, error
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
if result.Code != 0 {
|
||||
return nil, errors.New(result.Msg)
|
||||
}
|
||||
|
||||
return result.Data, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
@@ -404,10 +440,28 @@ func (c *Gateway) requestGateway(method string, url string, data string) (*http.
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpRequest(req, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("==============================")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if env.DebugHttpDump {
|
||||
str, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("==============================")
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user