实现云端控制的动态节点分配逻辑
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"platform/pkg/env"
|
||||
"platform/web/queries"
|
||||
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gen/field"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
@@ -44,3 +46,13 @@ func Init() {
|
||||
|
||||
DB = db
|
||||
}
|
||||
|
||||
type WithAlias interface {
|
||||
Alias() string
|
||||
}
|
||||
|
||||
func Alias(model WithAlias) func(db gen.Dao) gen.Dao {
|
||||
return func(db gen.Dao) gen.Dao {
|
||||
return db.Unscoped().Where(field.NewBool(model.Alias(), "deleted_at").IsNull())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,13 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
gatewayUrl string
|
||||
username string
|
||||
password string
|
||||
cloudUrl string
|
||||
token string
|
||||
url string
|
||||
token string
|
||||
}
|
||||
|
||||
var Client client
|
||||
@@ -21,23 +19,260 @@ var Client client
|
||||
func Init() {
|
||||
// todo 从环境变量中获取参数
|
||||
Client = client{
|
||||
gatewayUrl: "http://110.40.82.248:9990",
|
||||
username: "api",
|
||||
password: "123456",
|
||||
cloudUrl: "http://103.139.212.110",
|
||||
url: "http://103.139.212.110",
|
||||
}
|
||||
}
|
||||
|
||||
type PortConfig struct {
|
||||
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 *client) 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(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to get edges")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result CloudEdgesResp
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
func (c *client) CloudConnect(param CloudConnectReq) (int, error) {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
resp, err := c.requestCloud("POST", "/connect", string(data))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, errors.New("failed to connect")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if result["status"] == "error" {
|
||||
return 0, errors.New(result["details"].(string))
|
||||
}
|
||||
|
||||
return int(result["connected_edges"].(float64)), nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cloud:/disconnect
|
||||
|
||||
type CloudDisconnectReq struct {
|
||||
Uuid string `json:"uuid"`
|
||||
Edge []string `json:"edge"`
|
||||
Config []Config `json:"auto_config"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Isp string `json:"isp"`
|
||||
Count int `json:"count"`
|
||||
Online bool `json:"online"`
|
||||
}
|
||||
|
||||
func (c *client) CloudDisconnect(param CloudDisconnectReq) (int, error) {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
resp, err := c.requestCloud("POST", "/disconnect", string(data))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return 0, errors.New("failed to disconnect")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if result["status"] == "error" {
|
||||
return 0, errors.New(result["details"].(string))
|
||||
}
|
||||
|
||||
return int(result["disconnected_edges"].(float64)), nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region cloud:/auto_query
|
||||
|
||||
type CloudConnectResp map[string]AutoConfig
|
||||
|
||||
func (c *client) CloudAutoQuery() (CloudConnectResp, error) {
|
||||
resp, err := c.requestCloud("GET", "/auto_query", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to get auto_query")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result CloudConnectResp
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
func (c *client) requestCloud(method string, url string, data string) (*http.Response, error) {
|
||||
|
||||
req, err := http.NewRequest(method, c.url+url, strings.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("token", c.token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
type Gateway struct {
|
||||
url string
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
func InitGateway(url, username, password string) *Gateway {
|
||||
return &Gateway{url, username, password}
|
||||
}
|
||||
|
||||
// region gateway:/port/configs
|
||||
|
||||
type PortConfigsReq struct {
|
||||
Port string `json:"port"`
|
||||
Edge []string `json:"edge"`
|
||||
Type string `json:"type"`
|
||||
Time int `json:"time"`
|
||||
Status bool `json:"status"`
|
||||
Rate int `json:"rate"`
|
||||
Whitelist []string `json:"whitelist"`
|
||||
Userpass string `json:"userpass"`
|
||||
AutoEdgeConfig AutoEdgeConfig `json:"auto_edge_config"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type AutoEdgeConfig struct {
|
||||
@@ -48,8 +283,17 @@ type AutoEdgeConfig struct {
|
||||
PacketLoss int `json:"packet_loss"`
|
||||
}
|
||||
|
||||
func (c *client) PortConfigs(params ...PortConfig) error {
|
||||
resp, err := c.requestCloud("/port/configs", params)
|
||||
func (c *Gateway) GatewayPortConfigs(params []PortConfigsReq) error {
|
||||
if len(params) == 0 {
|
||||
return errors.New("params is empty")
|
||||
}
|
||||
|
||||
data, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := c.requestGateway("POST", "/port/configs", string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -79,41 +323,87 @@ func (c *client) PortConfigs(params ...PortConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *client) requestGateway(url string, data any) (*http.Response, error) {
|
||||
// endregion
|
||||
|
||||
// region gateway:/port/active
|
||||
|
||||
type PortActiveReq struct {
|
||||
Port string `json:"port"`
|
||||
Active bool `json:"active"`
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
|
||||
type PortActiveResp struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data map[string]PortData `json:"data"`
|
||||
}
|
||||
|
||||
type PortData struct {
|
||||
Edge []string `json:"edge"`
|
||||
Type string `json:"type"`
|
||||
Status bool `json:"status"`
|
||||
Active bool `json:"active"`
|
||||
Time int `json:"time"`
|
||||
Whitelist []string `json:"whitelist"`
|
||||
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)
|
||||
}
|
||||
|
||||
url.WriteString("?active=")
|
||||
url.WriteString(strconv.FormatBool(param.Active))
|
||||
url.WriteString("&status=")
|
||||
url.WriteString(strconv.FormatBool(param.Status))
|
||||
|
||||
resp, err := c.requestGateway("POST", url.String(), "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("failed to get port active")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result PortActiveResp
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
func (c *Gateway) requestGateway(method string, url string, data any) (*http.Response, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", c.gatewayUrl+url, strings.NewReader(string(jsonData)))
|
||||
req, err := http.NewRequest(method, c.username+":"+c.password+"@"+c.url+url, strings.NewReader(string(jsonData)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.SetBasicAuth(c.username, c.password)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *client) requestCloud(url string, data any) (*http.Response, error) {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", c.cloudUrl+url, strings.NewReader(string(jsonData)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("token", c.token)
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user