重构白银节点分配方式,使用手动接口精确配置节点
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"platform/pkg/env"
|
||||
"platform/web/core"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -17,18 +18,12 @@ import (
|
||||
|
||||
// CloudClient 定义云服务接口
|
||||
type CloudClient interface {
|
||||
CloudEdges(param CloudEdgesReq) (*CloudEdgesResp, error)
|
||||
CloudConnect(param CloudConnectReq) error
|
||||
CloudDisconnect(param CloudDisconnectReq) (int, error)
|
||||
CloudEdges(param *CloudEdgesReq) (*CloudEdgesResp, error)
|
||||
CloudConnect(param *CloudConnectReq) error
|
||||
CloudDisconnect(param *CloudDisconnectReq) (int, error)
|
||||
CloudAutoQuery() (CloudConnectResp, error)
|
||||
}
|
||||
|
||||
// GatewayClient 定义网关接口
|
||||
type GatewayClient interface {
|
||||
GatewayPortConfigs(params []PortConfigsReq) error
|
||||
GatewayPortActive(param ...PortActiveReq) (map[string]PortData, error)
|
||||
}
|
||||
|
||||
type cloud struct {
|
||||
url string
|
||||
}
|
||||
@@ -37,59 +32,14 @@ var Cloud CloudClient
|
||||
|
||||
func initBaiyin() error {
|
||||
Cloud = &cloud{
|
||||
url: env.BaiyinAddr,
|
||||
url: env.BaiyinCloudUrl,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type AutoConfig struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
// region cloud:/edges
|
||||
|
||||
type CloudEdgesReq struct {
|
||||
Province string
|
||||
City string
|
||||
Isp string
|
||||
Offset int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type CloudEdgesResp struct {
|
||||
Edges []Edge `json:"edges"`
|
||||
Total int `json:"total"`
|
||||
Offset int `json:"offset"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
type Edge struct {
|
||||
EdgesId int `json:"edges_id"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Ip string `json:"ip"`
|
||||
Rtt int `json:"rtt"`
|
||||
PacketLoss int `json:"packet_loss"`
|
||||
}
|
||||
|
||||
func (c *cloud) CloudEdges(param CloudEdgesReq) (*CloudEdgesResp, error) {
|
||||
data := strings.Builder{}
|
||||
data.WriteString("province=")
|
||||
data.WriteString(param.Province)
|
||||
data.WriteString("&city=")
|
||||
data.WriteString(param.City)
|
||||
data.WriteString("&isp=")
|
||||
data.WriteString(param.Isp)
|
||||
data.WriteString("&offset=")
|
||||
data.WriteString(strconv.Itoa(param.Offset))
|
||||
data.WriteString("&limit=")
|
||||
data.WriteString(strconv.Itoa(param.Limit))
|
||||
|
||||
resp, err := c.requestCloud("GET", "/edges?"+data.String(), "")
|
||||
// cloud:/edges 筛选查询边缘节点
|
||||
func (c *cloud) CloudEdges(param *CloudEdgesReq) (*CloudEdgesResp, error) {
|
||||
resp, err := c.requestCloud("GET", "/edges?"+core.Query(param).Encode(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -115,17 +65,46 @@ func (c *cloud) CloudEdges(param CloudEdgesReq) (*CloudEdgesResp, error) {
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cloud:/connect
|
||||
|
||||
type CloudConnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge []string `json:"edge,omitempty"`
|
||||
AutoConfig []AutoConfig `json:"auto_config,omitempty"`
|
||||
type CloudEdgesReq struct {
|
||||
Province *string `query:"province"`
|
||||
City *string `query:"city"`
|
||||
Isp *string `query:"isp"`
|
||||
Offset *int `query:"offset"`
|
||||
Limit *int `query:"limit"`
|
||||
NoRepeat *bool `query:"norepeat,b2i"`
|
||||
NoDayRepeat *bool `query:"nodayrepeat,b2i"`
|
||||
IpUnchangedTime *int `query:"ip_unchanged_time"` // 单位秒
|
||||
ActiveTime *int `query:"active_time"` // 单位秒
|
||||
// 排序方式,可选值:
|
||||
// - create_time_asc 设备创建时间顺序
|
||||
// - create_time_desc 设备创建时间倒序
|
||||
// - ip_unchanged_time_asc ip持续没变化时间顺序
|
||||
// - ip_unchanged_time_desc ip持续没变化时间倒序
|
||||
// - active_time_asc 连续活跃时间顺序
|
||||
// - active_time_desc 连续活跃时间倒序
|
||||
// - rand 随机排序 (默认)
|
||||
Sort *string `query:"sort"`
|
||||
}
|
||||
|
||||
func (c *cloud) CloudConnect(param CloudConnectReq) error {
|
||||
type CloudEdgesResp struct {
|
||||
Edges []Edge `json:"edges"`
|
||||
Total int `json:"total"`
|
||||
Offset int `json:"offset"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
type Edge struct {
|
||||
EdgeID string `json:"edge_id"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Ip string `json:"ip"`
|
||||
Rtt int `json:"rtt"`
|
||||
PacketLoss int `json:"packet_loss"`
|
||||
}
|
||||
|
||||
// cloud:/connect 连接边缘节点到网关
|
||||
func (c *cloud) CloudConnect(param *CloudConnectReq) error {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -162,25 +141,21 @@ func (c *cloud) CloudConnect(param CloudConnectReq) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cloud:/disconnect
|
||||
|
||||
type CloudDisconnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge []string `json:"edge,omitempty"`
|
||||
Config []Config `json:"auto_config,omitempty"`
|
||||
type CloudConnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge *[]string `json:"edge,omitempty"`
|
||||
AutoConfig *[]AutoConfig `json:"auto_config,omitempty"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
type AutoConfig struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Count int `json:"count"`
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
|
||||
func (c *cloud) CloudDisconnect(param CloudDisconnectReq) (int, error) {
|
||||
// cloud:/disconnect 解除连接边缘节点到网关
|
||||
func (c *cloud) CloudDisconnect(param *CloudDisconnectReq) (int, error) {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -217,12 +192,21 @@ func (c *cloud) CloudDisconnect(param CloudDisconnectReq) (int, error) {
|
||||
return int(result["disconnected_edges"].(float64)), nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
type CloudDisconnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge *[]string `json:"edge,omitempty"`
|
||||
Config *[]Config `json:"auto_config,omitempty"`
|
||||
}
|
||||
|
||||
// region cloud:/auto_query
|
||||
|
||||
type CloudConnectResp map[string][]AutoConfig
|
||||
type Config struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Count int `json:"count"`
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
|
||||
// cloud:/auto_query 自动连接配置查询
|
||||
func (c *cloud) CloudAutoQuery() (CloudConnectResp, error) {
|
||||
resp, err := c.requestCloud("GET", "/auto_query", "")
|
||||
if err != nil {
|
||||
@@ -250,7 +234,7 @@ func (c *cloud) CloudAutoQuery() (CloudConnectResp, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
type CloudConnectResp map[string][]AutoConfig
|
||||
|
||||
func (c *cloud) requestCloud(method string, url string, data string) (*http.Response, error) {
|
||||
|
||||
@@ -263,7 +247,7 @@ func (c *cloud) requestCloud(method string, url string, data string) (*http.Resp
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
var resp *http.Response
|
||||
for i := 0; i < 2; i++ {
|
||||
for i := range 2 {
|
||||
token, err := c.token(i == 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -304,7 +288,7 @@ func (c *cloud) requestCloud(method string, url string, data string) (*http.Resp
|
||||
func (c *cloud) token(refresh bool) (string, error) {
|
||||
// redis 获取令牌
|
||||
if !refresh {
|
||||
token, err := Redis.Get(context.Background(), "remote:token").Result()
|
||||
token, err := Redis.Get(context.Background(), BaiyinToken).Result()
|
||||
if err == nil && token != "" {
|
||||
return token, nil
|
||||
}
|
||||
@@ -338,7 +322,7 @@ func (c *cloud) token(refresh bool) (string, error) {
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("解析响应 [%s] 失败: %w", string(body), err)
|
||||
}
|
||||
|
||||
if result["code"].(float64) != 1 {
|
||||
@@ -347,7 +331,7 @@ func (c *cloud) token(refresh bool) (string, error) {
|
||||
|
||||
// redis 设置令牌
|
||||
token := result["token"].(string)
|
||||
err = Redis.Set(context.Background(), "remote:token", token, 1*time.Hour).Err()
|
||||
err = Redis.Set(context.Background(), BaiyinToken, token, 1*time.Hour).Err()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -355,6 +339,15 @@ func (c *cloud) token(refresh bool) (string, error) {
|
||||
return token, nil
|
||||
}
|
||||
|
||||
const BaiyinToken = "clients:baiyin:token"
|
||||
|
||||
// GatewayClient 定义网关接口
|
||||
type GatewayClient interface {
|
||||
GatewayPortConfigs(params []*PortConfigsReq) error
|
||||
GatewayPortActive(param ...*PortActiveReq) (map[string]PortData, error)
|
||||
GatewayEdge(params *GatewayEdgeReq) (map[string]GatewayEdgeInfo, error)
|
||||
}
|
||||
|
||||
type gateway struct {
|
||||
url string
|
||||
username string
|
||||
@@ -373,6 +366,68 @@ func NewGateway(url, username, password string) GatewayClient {
|
||||
return GatewayInitializer(url, username, password)
|
||||
}
|
||||
|
||||
type GatewayEdgeReq struct {
|
||||
EdgeID *string `query:"edge_id"`
|
||||
Province *string `query:"province"`
|
||||
City *string `query:"city"`
|
||||
Isp *string `query:"isp"`
|
||||
Connected *bool `query:"connected"`
|
||||
Assigned *bool `query:"assigned"`
|
||||
GetRand *int `query:"getRand"`
|
||||
IpUnchangedTimeStart *int `query:"ip_unchanged_time_start"`
|
||||
IpUnchangedTimeEnd *int `query:"ip_unchanged_time_end"`
|
||||
OnlineTimeStart *int `query:"online_time_start"`
|
||||
OnlineTimeEnd *int `query:"online_time_end"`
|
||||
Rtt *int `query:"rtt"`
|
||||
MinRtt *int `query:"min_rtt"`
|
||||
RttBaidu *int `query:"rtt_baidu"`
|
||||
PacketLoss *int `query:"packet_loss"`
|
||||
PacketLossBaidu *int `query:"packet_loss_baidu"`
|
||||
IP *string `query:"ip"`
|
||||
Limit *int `query:"limit"`
|
||||
Offset *int `query:"offset"`
|
||||
}
|
||||
|
||||
type GatewayEdgeResp struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data map[string]GatewayEdgeInfo `json:"data"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
type GatewayEdgeInfo struct {
|
||||
IP string `json:"ip"`
|
||||
Connected bool `json:"connected"`
|
||||
Assigned bool `json:"assigned"`
|
||||
AssignedTo string `json:"assignedto"`
|
||||
PacketLoss int `json:"packet_loss"`
|
||||
PacketLossBaidu int `json:"packet_loss_baidu"`
|
||||
Rtt int `json:"rtt"`
|
||||
RttBaidu int `json:"rtt_baidu"`
|
||||
OfflineTime int `json:"offline_time"`
|
||||
OnlineTime int `json:"online_time"`
|
||||
IpUnchangedTime int `json:"ip_unchanged_time"`
|
||||
}
|
||||
|
||||
func (c *gateway) GatewayEdge(req *GatewayEdgeReq) (map[string]GatewayEdgeInfo, error) {
|
||||
resp, err := c.get("/edge", core.Query(req))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询可用节点失败:%w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body := new(GatewayEdgeResp)
|
||||
if err = json.NewDecoder(resp.Body).Decode(body); err != nil {
|
||||
return nil, fmt.Errorf("解析响应内容失败:%w", err)
|
||||
}
|
||||
|
||||
if body.Code != 0 {
|
||||
return nil, fmt.Errorf("接口业务响应异常: %d %s", body.Code, body.Msg)
|
||||
}
|
||||
|
||||
return body.Data, nil
|
||||
}
|
||||
|
||||
// region gateway:/port/configs
|
||||
|
||||
type PortConfigsReq struct {
|
||||
@@ -395,7 +450,7 @@ type AutoEdgeConfig struct {
|
||||
PacketLoss int `json:"packet_loss,omitempty"`
|
||||
}
|
||||
|
||||
func (c *gateway) GatewayPortConfigs(params []PortConfigsReq) error {
|
||||
func (c *gateway) GatewayPortConfigs(params []*PortConfigsReq) error {
|
||||
if len(params) == 0 {
|
||||
return errors.New("params is empty")
|
||||
}
|
||||
@@ -461,10 +516,10 @@ type PortData struct {
|
||||
Userpass string `json:"userpass"`
|
||||
}
|
||||
|
||||
func (c *gateway) GatewayPortActive(param ...PortActiveReq) (map[string]PortData, error) {
|
||||
func (c *gateway) GatewayPortActive(param ...*PortActiveReq) (map[string]PortData, error) {
|
||||
_param := PortActiveReq{}
|
||||
if len(param) != 0 {
|
||||
_param = param[0]
|
||||
if len(param) != 0 && param[0] != nil {
|
||||
_param = *param[0]
|
||||
}
|
||||
|
||||
path := strings.Builder{}
|
||||
@@ -520,38 +575,33 @@ func (c *gateway) GatewayPortActive(param ...PortActiveReq) (map[string]PortData
|
||||
|
||||
// endregion
|
||||
|
||||
func (c *gateway) get(url string, params url.Values) (*http.Response, error) {
|
||||
url = fmt.Sprintf("http://%s:%s@%s:9990%s?%s", c.username, c.password, c.url, url, params.Encode())
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建请求失败:%w", err)
|
||||
}
|
||||
|
||||
res, err := core.Fetch(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取数据失败:%w", err)
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
bytes, _ := io.ReadAll(res.Body)
|
||||
return nil, fmt.Errorf("接口响应异常: %d %s", res.StatusCode, string(bytes))
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (c *gateway) requestGateway(method string, url string, data string) (*http.Response, error) {
|
||||
//goland:noinspection ALL
|
||||
url = fmt.Sprintf("http://%s:%s@%s:9990%s", c.username, c.password, c.url, url)
|
||||
req, err := http.NewRequest(method, url, strings.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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
|
||||
return core.Fetch(req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user