基本完成远端配置的通道创建

This commit is contained in:
2025-03-28 18:15:03 +08:00
parent edec734b71
commit 444b1a7b99
9 changed files with 295 additions and 158 deletions

View File

@@ -3,6 +3,7 @@ package remote
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
@@ -19,7 +20,8 @@ var Client client
func Init() {
// todo 从环境变量中获取参数
Client = client{
url: "http://103.139.212.110",
url: "http://103.139.212.110:9989",
token: "tHDarLc1ct6M9NMAxeO98lN2YsEadYSx.anVpcA==.MTc0MzA4MTAwMQ==",
}
}
@@ -106,15 +108,15 @@ type CloudConnectReq struct {
AutoConfig []AutoConfig `json:"auto_config,omitempty"`
}
func (c *client) CloudConnect(param CloudConnectReq) (int, error) {
func (c *client) CloudConnect(param CloudConnectReq) error {
data, err := json.Marshal(param)
if err != nil {
return 0, err
return err
}
resp, err := c.requestCloud("POST", "/connect", string(data))
if err != nil {
return 0, err
return err
}
defer func(Body io.ReadCloser) {
@@ -122,25 +124,25 @@ func (c *client) CloudConnect(param CloudConnectReq) (int, error) {
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return 0, errors.New("failed to connect")
return errors.New("failed to connect")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
return err
}
var result map[string]any
err = json.Unmarshal(body, &result)
if err != nil {
return 0, err
return err
}
if result["status"] == "error" {
return 0, errors.New(result["details"].(string))
return errors.New(result["details"].(string))
}
return int(result["connected_edges"].(float64)), nil
return nil
}
// endregion
@@ -202,7 +204,7 @@ func (c *client) CloudDisconnect(param CloudDisconnectReq) (int, error) {
// region cloud:/auto_query
type CloudConnectResp map[string]AutoConfig
type CloudConnectResp map[string][]AutoConfig
func (c *client) CloudAutoQuery() (CloudConnectResp, error) {
resp, err := c.requestCloud("GET", "/auto_query", "")
@@ -235,7 +237,8 @@ func (c *client) CloudAutoQuery() (CloudConnectResp, error) {
func (c *client) requestCloud(method string, url string, data string) (*http.Response, error) {
req, err := http.NewRequest(method, c.url+url, strings.NewReader(data))
url = fmt.Sprintf("%s/api%s", c.url, url)
req, err := http.NewRequest(method, url, strings.NewReader(data))
if err != nil {
return nil, err
}
@@ -264,7 +267,7 @@ func InitGateway(url, username, password string) *Gateway {
// region gateway:/port/configs
type PortConfigsReq struct {
Port string `json:"port"`
Port int `json:"port"`
Edge []string `json:"edge,omitempty"`
Type string `json:"type,omitempty"`
Time int `json:"time,omitempty"`
@@ -301,15 +304,15 @@ func (c *Gateway) GatewayPortConfigs(params []PortConfigsReq) error {
_ = Body.Close()
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return errors.New("failed to configure port")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New("failed to get port configs: " + string(body))
}
var result map[string]any
err = json.Unmarshal(body, &result)
if err != nil {
@@ -398,7 +401,8 @@ func (c *Gateway) requestGateway(method string, url string, data any) (*http.Res
return nil, err
}
req, err := http.NewRequest(method, c.username+":"+c.password+"@"+c.url+url, strings.NewReader(string(jsonData)))
url = fmt.Sprintf("http://%s:%s@%s:9990%s", c.username, c.password, c.url, url)
req, err := http.NewRequest(method, url, strings.NewReader(string(jsonData)))
if err != nil {
return nil, err
}