重构代码结构,迁移Baiyin SDK相关逻辑至globals包,并添加支付宝客户端初始化
This commit is contained in:
32
web/globals/alipay.go
Normal file
32
web/globals/alipay.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
"platform/pkg/env"
|
||||
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
)
|
||||
|
||||
var Alipay *alipay.Client
|
||||
|
||||
func InitAlipay() {
|
||||
var client, err = alipay.New(
|
||||
env.AlipayAppId,
|
||||
env.AlipayAppPrivateKey,
|
||||
env.AlipayProduction,
|
||||
)
|
||||
if err != nil {
|
||||
panic("初始化支付宝客户端失败: " + err.Error())
|
||||
}
|
||||
|
||||
err = client.LoadAliPayPublicKey(env.AlipayPublicKey)
|
||||
if err != nil {
|
||||
panic("加载支付宝公钥失败: " + err.Error())
|
||||
}
|
||||
|
||||
err = client.SetEncryptKey(env.AlipayEncryptKey)
|
||||
if err != nil {
|
||||
panic("设置支付宝加密密钥失败: " + err.Error())
|
||||
}
|
||||
|
||||
Alipay = client
|
||||
}
|
||||
554
web/globals/baiyin.go
Normal file
554
web/globals/baiyin.go
Normal file
@@ -0,0 +1,554 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/rds"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CloudClient 定义云服务接口
|
||||
type CloudClient interface {
|
||||
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
|
||||
}
|
||||
|
||||
var Cloud CloudClient
|
||||
|
||||
func InitBaiyin() {
|
||||
Cloud = &cloud{
|
||||
url: env.BaiyinAddr,
|
||||
}
|
||||
}
|
||||
|
||||
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(), "")
|
||||
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 *cloud) CloudConnect(param CloudConnectReq) error {
|
||||
data, err := json.Marshal(param)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := c.requestCloud("POST", "/connect", string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("failed to connect")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result["status"] == "error" {
|
||||
return errors.New(result["details"].(string))
|
||||
}
|
||||
|
||||
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 Config 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) {
|
||||
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 *cloud) 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 *cloud) requestCloud(method string, url string, data string) (*http.Response, error) {
|
||||
|
||||
url = fmt.Sprintf("%s/api%s", 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")
|
||||
|
||||
var resp *http.Response
|
||||
for i := 0; i < 2; i++ {
|
||||
token, err := c.token(i == 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("token", 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))
|
||||
}
|
||||
|
||||
if resp.StatusCode != 401 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *cloud) token(refresh bool) (string, error) {
|
||||
// redis 获取令牌
|
||||
if !refresh {
|
||||
token, err := rds.Client.Get(context.Background(), "remote:token").Result()
|
||||
if err == nil && token != "" {
|
||||
return token, nil
|
||||
}
|
||||
}
|
||||
|
||||
// redis 获取失败,重新获取
|
||||
resp, err := http.Get(env.BaiyinTokenUrl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
dump, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Println(string(dump))
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", errors.New("failed to get token")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var result map[string]any
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if result["code"].(float64) != 1 {
|
||||
return "", errors.New("failed to get cloud token")
|
||||
}
|
||||
|
||||
// redis 设置令牌
|
||||
token := result["token"].(string)
|
||||
err = rds.Client.Set(context.Background(), "remote:token", token, 1*time.Hour).Err()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
type gateway struct {
|
||||
url string
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
var GatewayInitializer = func(url, username, password string) GatewayClient {
|
||||
return &gateway{
|
||||
url: url,
|
||||
username: username,
|
||||
password: password,
|
||||
}
|
||||
}
|
||||
|
||||
func NewGateway(url, username, password string) GatewayClient {
|
||||
return GatewayInitializer(url, username, password)
|
||||
}
|
||||
|
||||
// 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"`
|
||||
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"`
|
||||
PacketLoss int `json:"packet_loss,omitempty"`
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
if result["code"].(float64) != 0 {
|
||||
return errors.New("failed to configure port")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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) (map[string]PortData, error) {
|
||||
_param := PortActiveReq{}
|
||||
if len(param) != 0 {
|
||||
_param = param[0]
|
||||
}
|
||||
|
||||
path := strings.Builder{}
|
||||
path.WriteString("/port/active")
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
if result.Code != 0 {
|
||||
return nil, errors.New(result.Msg)
|
||||
}
|
||||
|
||||
return result.Data, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
func (c *gateway) requestGateway(method string, url string, data string) (*http.Response, error) {
|
||||
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
|
||||
}
|
||||
@@ -1,13 +1,16 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"platform/web/auth"
|
||||
g "platform/web/globals"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"platform/web/services"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/smartwalle/alipay/v3"
|
||||
)
|
||||
|
||||
// region CreateTrade
|
||||
@@ -83,4 +86,31 @@ func CreateTrade(c *fiber.Ctx) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func createTradeByAlipay() (*url.URL, error) {
|
||||
|
||||
target, err := g.Alipay.TradePagePay(alipay.TradePagePay{
|
||||
Trade: alipay.Trade{},
|
||||
AuthToken: "",
|
||||
QRPayMode: "",
|
||||
QRCodeWidth: "",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return target, nil
|
||||
}
|
||||
|
||||
func createTradeByWechat() error {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region TradeCallbackAlipay
|
||||
|
||||
// endregion
|
||||
|
||||
// region TradeCallbackWechat
|
||||
|
||||
// endregion
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
"platform/pkg/env"
|
||||
"platform/pkg/orm"
|
||||
"platform/pkg/rds"
|
||||
"platform/pkg/sdks/baiyin"
|
||||
"platform/pkg/u"
|
||||
"platform/web/common"
|
||||
g "platform/web/globals"
|
||||
"platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"strconv"
|
||||
@@ -132,18 +132,18 @@ func (s *channelService) RemoveChannels(ctx context.Context, auth *AuthContext,
|
||||
step = time.Now()
|
||||
|
||||
// 组织数据
|
||||
var configMap = make(map[int32][]baiyin.PortConfigsReq, len(proxies))
|
||||
var configMap = make(map[int32][]g.PortConfigsReq, len(proxies))
|
||||
var proxyMap = make(map[int32]*models.Proxy, len(proxies))
|
||||
for _, proxy := range proxies {
|
||||
configMap[proxy.ID] = make([]baiyin.PortConfigsReq, 0)
|
||||
configMap[proxy.ID] = make([]g.PortConfigsReq, 0)
|
||||
proxyMap[proxy.ID] = proxy
|
||||
}
|
||||
var portMap = make(map[uint64]struct{})
|
||||
for _, channel := range channels {
|
||||
var config = baiyin.PortConfigsReq{
|
||||
var config = g.PortConfigsReq{
|
||||
Port: int(channel.ProxyPort),
|
||||
Edge: &[]string{},
|
||||
AutoEdgeConfig: &baiyin.AutoEdgeConfig{
|
||||
AutoEdgeConfig: &g.AutoEdgeConfig{
|
||||
Count: u.P(0),
|
||||
},
|
||||
Status: false,
|
||||
@@ -167,7 +167,7 @@ func (s *channelService) RemoveChannels(ctx context.Context, auth *AuthContext,
|
||||
}
|
||||
|
||||
var secret = strings.Split(proxy.Secret, ":")
|
||||
gateway := baiyin.NewGateway(
|
||||
gateway := g.NewGateway(
|
||||
proxy.Host,
|
||||
secret[0],
|
||||
secret[1],
|
||||
@@ -208,7 +208,7 @@ func (s *channelService) RemoveChannels(ctx context.Context, auth *AuthContext,
|
||||
}
|
||||
}
|
||||
if len(edges) > 0 {
|
||||
_, err := baiyin.Cloud.CloudDisconnect(baiyin.CloudDisconnectReq{
|
||||
_, err := g.Cloud.CloudDisconnect(g.CloudDisconnectReq{
|
||||
Uuid: proxy.Name,
|
||||
Edge: edges,
|
||||
})
|
||||
@@ -406,7 +406,7 @@ func assignEdge(q *q.Query, count int, filter NodeFilterConfig) (*AssignEdgeResu
|
||||
// 查询已配置的节点
|
||||
step = time.Now()
|
||||
|
||||
rProxyConfigs, err := baiyin.Cloud.CloudAutoQuery()
|
||||
rProxyConfigs, err := g.Cloud.CloudAutoQuery()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -480,13 +480,13 @@ func assignEdge(q *q.Query, count int, filter NodeFilterConfig) (*AssignEdgeResu
|
||||
|
||||
rConfigs := rProxyConfigs[info.proxy.Name]
|
||||
|
||||
var newConfig = baiyin.AutoConfig{
|
||||
var newConfig = g.AutoConfig{
|
||||
Province: filter.Prov,
|
||||
City: filter.City,
|
||||
Isp: filter.Isp,
|
||||
Count: int(math.Ceil(float64(info.used) * 2)),
|
||||
}
|
||||
var newConfigs []baiyin.AutoConfig
|
||||
var newConfigs []g.AutoConfig
|
||||
var update = false
|
||||
for _, rConfig := range rConfigs {
|
||||
if rConfig.Isp == filter.Isp && rConfig.City == filter.City && rConfig.Province == filter.Prov {
|
||||
@@ -500,7 +500,7 @@ func assignEdge(q *q.Query, count int, filter NodeFilterConfig) (*AssignEdgeResu
|
||||
newConfigs = append(newConfigs, newConfig)
|
||||
}
|
||||
|
||||
err := baiyin.Cloud.CloudConnect(baiyin.CloudConnectReq{
|
||||
err := g.Cloud.CloudConnect(g.CloudConnectReq{
|
||||
Uuid: info.proxy.Name,
|
||||
Edge: nil,
|
||||
AutoConfig: newConfigs,
|
||||
@@ -587,7 +587,7 @@ func assignPort(
|
||||
var count = config.count
|
||||
|
||||
// 筛选可用端口
|
||||
var configs = make([]baiyin.PortConfigsReq, 0, count)
|
||||
var configs = make([]g.PortConfigsReq, 0, count)
|
||||
for port := 10000; port < 20000 && len(configs) < count; port++ {
|
||||
// 跳过存在的端口
|
||||
key := uint64(proxy.ID)<<32 | uint64(port)
|
||||
@@ -598,11 +598,11 @@ func assignPort(
|
||||
|
||||
// 配置新端口
|
||||
var i = len(configs)
|
||||
configs = append(configs, baiyin.PortConfigsReq{
|
||||
configs = append(configs, g.PortConfigsReq{
|
||||
Port: port,
|
||||
Edge: nil,
|
||||
Status: true,
|
||||
AutoEdgeConfig: &baiyin.AutoEdgeConfig{
|
||||
AutoEdgeConfig: &g.AutoEdgeConfig{
|
||||
Province: filter.Prov,
|
||||
City: filter.City,
|
||||
Isp: filter.Isp,
|
||||
@@ -682,7 +682,7 @@ func assignPort(
|
||||
step = time.Now()
|
||||
|
||||
var secret = strings.Split(proxy.Secret, ":")
|
||||
gateway := baiyin.NewGateway(
|
||||
gateway := g.NewGateway(
|
||||
proxy.Host,
|
||||
secret[0],
|
||||
secret[1],
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"platform/pkg/sdks/baiyin"
|
||||
"platform/pkg/testutil"
|
||||
"platform/web/common"
|
||||
g "platform/web/globals"
|
||||
"platform/web/models"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -288,9 +288,9 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), requestid.ConfigDefault.ContextKey, "test-request-id")
|
||||
var adminAuth = &AuthContext{Payload: Payload{Id: 100, Type: PayloadAdmin}}
|
||||
var userAuth = &AuthContext{Payload: Payload{Id: 101, Type: PayloadUser}}
|
||||
mc.AutoQueryMock = func() (baiyin.CloudConnectResp, error) {
|
||||
return baiyin.CloudConnectResp{
|
||||
"test-proxy": []baiyin.AutoConfig{
|
||||
mc.AutoQueryMock = func() (g.CloudConnectResp, error) {
|
||||
return g.CloudConnectResp{
|
||||
"test-proxy": []g.AutoConfig{
|
||||
{Province: "河南省", Count: 10},
|
||||
},
|
||||
}, nil
|
||||
@@ -373,14 +373,14 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
mr.FlushAll()
|
||||
resetDb()
|
||||
|
||||
mc.ConnectMock = func(param baiyin.CloudConnectReq) error {
|
||||
mc.ConnectMock = func(param g.CloudConnectReq) error {
|
||||
if param.Uuid != proxy.Name {
|
||||
return fmt.Errorf("代理名称不符合预期: %s", param.Uuid)
|
||||
}
|
||||
if len(param.Edge) != 0 {
|
||||
return fmt.Errorf("边缘节点不符合预期: %v", param.Edge)
|
||||
}
|
||||
if !reflect.DeepEqual(param.AutoConfig, []baiyin.AutoConfig{
|
||||
if !reflect.DeepEqual(param.AutoConfig, []g.AutoConfig{
|
||||
{Province: "河南省", Count: 10},
|
||||
{Province: "北京市", Count: 6},
|
||||
}) {
|
||||
@@ -389,7 +389,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
|
||||
mg.PortConfigsMock = func(c *testutil.MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
||||
mg.PortConfigsMock = func(c *testutil.MockGatewayClient, params []g.PortConfigsReq) error {
|
||||
if c.Host != proxy.Host {
|
||||
return fmt.Errorf("代理主机不符合预期: %s", c.Host)
|
||||
}
|
||||
@@ -523,14 +523,14 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
mr.FlushAll()
|
||||
resetDb()
|
||||
|
||||
mc.ConnectMock = func(param baiyin.CloudConnectReq) error {
|
||||
mc.ConnectMock = func(param g.CloudConnectReq) error {
|
||||
if param.Uuid != proxy.Name {
|
||||
return fmt.Errorf("代理名称不符合预期: %s", param.Uuid)
|
||||
}
|
||||
if len(param.Edge) != 0 {
|
||||
return fmt.Errorf("边缘节点不符合预期: %v", param.Edge)
|
||||
}
|
||||
if !reflect.DeepEqual(param.AutoConfig, []baiyin.AutoConfig{
|
||||
if !reflect.DeepEqual(param.AutoConfig, []g.AutoConfig{
|
||||
{Province: "河南省", Count: 10},
|
||||
{Province: "北京市", Count: 6},
|
||||
}) {
|
||||
@@ -539,7 +539,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
|
||||
mg.PortConfigsMock = func(c *testutil.MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
||||
mg.PortConfigsMock = func(c *testutil.MockGatewayClient, params []g.PortConfigsReq) error {
|
||||
if c.Host != proxy.Host {
|
||||
return fmt.Errorf("代理主机不符合预期: %s", c.Host)
|
||||
}
|
||||
@@ -667,14 +667,14 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
mr.FlushAll()
|
||||
resetDb()
|
||||
|
||||
mc.ConnectMock = func(param baiyin.CloudConnectReq) error {
|
||||
mc.ConnectMock = func(param g.CloudConnectReq) error {
|
||||
if param.Uuid != proxy.Name {
|
||||
return fmt.Errorf("代理名称不符合预期: %s", param.Uuid)
|
||||
}
|
||||
if len(param.Edge) != 0 {
|
||||
return fmt.Errorf("边缘节点不符合预期: %v", param.Edge)
|
||||
}
|
||||
if !reflect.DeepEqual(param.AutoConfig, []baiyin.AutoConfig{
|
||||
if !reflect.DeepEqual(param.AutoConfig, []g.AutoConfig{
|
||||
{Province: "河南省", Count: 10},
|
||||
{Province: "北京市", Count: 6},
|
||||
}) {
|
||||
@@ -683,7 +683,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
return nil
|
||||
}
|
||||
|
||||
mg.PortConfigsMock = func(c *testutil.MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
||||
mg.PortConfigsMock = func(c *testutil.MockGatewayClient, params []g.PortConfigsReq) error {
|
||||
if c.Host != proxy.Host {
|
||||
return fmt.Errorf("代理主机不符合预期: %s", c.Host)
|
||||
}
|
||||
@@ -899,9 +899,9 @@ func Test_channelService_CreateChannel(t *testing.T) {
|
||||
setup: func() {
|
||||
mr.FlushAll()
|
||||
resetDb()
|
||||
mc.AutoQueryMock = func() (baiyin.CloudConnectResp, error) {
|
||||
return baiyin.CloudConnectResp{
|
||||
"test-proxy": []baiyin.AutoConfig{
|
||||
mc.AutoQueryMock = func() (g.CloudConnectResp, error) {
|
||||
return g.CloudConnectResp{
|
||||
"test-proxy": []g.AutoConfig{
|
||||
{Count: 20000},
|
||||
},
|
||||
}, nil
|
||||
@@ -1054,21 +1054,21 @@ func Test_channelService_RemoveChannels(t *testing.T) {
|
||||
}
|
||||
|
||||
// 模拟网关客户端的响应
|
||||
mg.PortActiveMock = func(m *testutil.MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
|
||||
mg.PortActiveMock = func(m *testutil.MockGatewayClient, param ...g.PortActiveReq) (map[string]g.PortData, error) {
|
||||
switch {
|
||||
case m.Host == proxy.Host:
|
||||
return map[string]baiyin.PortData{
|
||||
return map[string]g.PortData{
|
||||
"10001": {Edge: []string{"edge1", "edge4"}},
|
||||
"10002": {Edge: []string{"edge2"}},
|
||||
}, nil
|
||||
case m.Host == proxy2.Host:
|
||||
return map[string]baiyin.PortData{
|
||||
return map[string]g.PortData{
|
||||
"10001": {Edge: []string{"edge3"}},
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("代理主机不符合预期: %s", m.Host)
|
||||
}
|
||||
mg.PortConfigsMock = func(m *testutil.MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
||||
mg.PortConfigsMock = func(m *testutil.MockGatewayClient, params []g.PortConfigsReq) error {
|
||||
switch {
|
||||
case m.Host == proxy.Host:
|
||||
for _, param := range params {
|
||||
@@ -1099,7 +1099,7 @@ func Test_channelService_RemoveChannels(t *testing.T) {
|
||||
}
|
||||
return fmt.Errorf("代理主机不符合预期: %s", m.Host)
|
||||
}
|
||||
mc.DisconnectMock = func(param baiyin.CloudDisconnectReq) (int, error) {
|
||||
mc.DisconnectMock = func(param g.CloudDisconnectReq) (int, error) {
|
||||
switch {
|
||||
case param.Uuid == proxy.Name:
|
||||
var edges = []string{"edge1", "edge2", "edge4"}
|
||||
@@ -1168,21 +1168,21 @@ func Test_channelService_RemoveChannels(t *testing.T) {
|
||||
}
|
||||
|
||||
// 模拟网关客户端的响应
|
||||
mg.PortActiveMock = func(m *testutil.MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
|
||||
mg.PortActiveMock = func(m *testutil.MockGatewayClient, param ...g.PortActiveReq) (map[string]g.PortData, error) {
|
||||
switch {
|
||||
case m.Host == proxy.Host:
|
||||
return map[string]baiyin.PortData{
|
||||
return map[string]g.PortData{
|
||||
"10001": {Edge: []string{"edge1", "edge4"}},
|
||||
"10002": {Edge: []string{"edge2"}},
|
||||
}, nil
|
||||
case m.Host == proxy2.Host:
|
||||
return map[string]baiyin.PortData{
|
||||
return map[string]g.PortData{
|
||||
"10001": {Edge: []string{"edge3"}},
|
||||
}, nil
|
||||
}
|
||||
return nil, fmt.Errorf("代理主机不符合预期: %s", m.Host)
|
||||
}
|
||||
mg.PortConfigsMock = func(m *testutil.MockGatewayClient, params []baiyin.PortConfigsReq) error {
|
||||
mg.PortConfigsMock = func(m *testutil.MockGatewayClient, params []g.PortConfigsReq) error {
|
||||
switch {
|
||||
case m.Host == proxy.Host:
|
||||
for _, param := range params {
|
||||
@@ -1213,7 +1213,7 @@ func Test_channelService_RemoveChannels(t *testing.T) {
|
||||
}
|
||||
return fmt.Errorf("代理主机不符合预期: %s", m.Host)
|
||||
}
|
||||
mc.DisconnectMock = func(param baiyin.CloudDisconnectReq) (int, error) {
|
||||
mc.DisconnectMock = func(param g.CloudDisconnectReq) (int, error) {
|
||||
switch {
|
||||
case param.Uuid == proxy.Name:
|
||||
var edges = []string{"edge1", "edge2", "edge4"}
|
||||
|
||||
@@ -3,6 +3,7 @@ package web
|
||||
import (
|
||||
"net/http"
|
||||
"platform/pkg/env"
|
||||
g "platform/web/globals"
|
||||
"runtime"
|
||||
|
||||
"log/slog"
|
||||
@@ -38,6 +39,10 @@ func New(config *Config) (*Server, error) {
|
||||
|
||||
func (s *Server) Run() error {
|
||||
|
||||
// inits
|
||||
g.InitBaiyin()
|
||||
g.InitAlipay()
|
||||
|
||||
// config
|
||||
s.fiber = fiber.New(fiber.Config{
|
||||
ErrorHandler: ErrorHandler,
|
||||
|
||||
Reference in New Issue
Block a user