完善通道创建与同步流程
This commit is contained in:
@@ -286,30 +286,38 @@ func (s *channelService) RemoteCreateChannel(
|
||||
}
|
||||
slog.Debug("检查用户权限完成")
|
||||
|
||||
// 申请节点
|
||||
edgeAssigns, err := assignEdge(count, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
debugAssigned := fmt.Sprintf("%+v", edgeAssigns)
|
||||
slog.Debug("申请节点完成", "edgeAssigns", debugAssigned)
|
||||
var postAssigns []AssignPortResult
|
||||
err = q.Q.Transaction(func(tx *q.Query) error {
|
||||
// 申请节点
|
||||
edgeAssigns, err := assignEdge(count, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
debugAssigned := fmt.Sprintf("%+v", edgeAssigns)
|
||||
slog.Debug("申请节点完成", "edgeAssigns", debugAssigned)
|
||||
|
||||
// 分配端口
|
||||
expiration := time.Now().Add(time.Duration(resource.Live) * time.Second)
|
||||
portAssigns, err := assignPort(edgeAssigns, auth.Payload.Id, protocol, authType, expiration, filter)
|
||||
// 分配端口
|
||||
expiration := time.Now().Add(time.Duration(resource.Live) * time.Second)
|
||||
postAssigns, err = assignPort(edgeAssigns, auth.Payload.Id, protocol, authType, expiration, filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
debugChannels := fmt.Sprintf("%+v", postAssigns)
|
||||
slog.Debug("分配端口完成", "portAssigns", debugChannels)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
debugChannels := fmt.Sprintf("%+v", portAssigns)
|
||||
slog.Debug("分配端口完成", "portAssigns", debugChannels)
|
||||
|
||||
// 缓存并关闭代理
|
||||
err = cache(ctx, portAssigns)
|
||||
err = cache(ctx, postAssigns)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return portAssigns, nil
|
||||
return postAssigns, nil
|
||||
}
|
||||
|
||||
// endregion
|
||||
@@ -349,13 +357,9 @@ func checkUser(auth *AuthContext, resource *ResourceInfo, count int) error {
|
||||
}
|
||||
|
||||
// assignEdge 分配边缘节点数量
|
||||
func assignEdge(count int, filter NodeFilterConfig) ([]*AssignEdgeResult, error) {
|
||||
// 查询现有节点连接情况
|
||||
edgeConfigs, err := remote.Client.CloudAutoQuery()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
func assignEdge(count int, filter NodeFilterConfig) (*AssignEdgeResult, error) {
|
||||
|
||||
// 查询可以使用的网关
|
||||
proxies, err := q.Proxy.
|
||||
Where(q.Proxy.Type.Eq(1)).
|
||||
Find()
|
||||
@@ -363,81 +367,18 @@ func assignEdge(count int, filter NodeFilterConfig) ([]*AssignEdgeResult, error)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 过滤需要变动的连接配置
|
||||
type ConfigInfo struct {
|
||||
proxy *models.Proxy
|
||||
config *remote.AutoConfig
|
||||
// 查询已配置的节点
|
||||
allConfigs, err := remote.Client.CloudAutoQuery()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var total = count
|
||||
var assigns = make([]*AssignEdgeResult, len(proxies), len(proxies))
|
||||
|
||||
// 查询已分配的节点
|
||||
var proxyIds = make([]int32, len(proxies))
|
||||
for i, proxy := range proxies {
|
||||
remoteConfigs := edgeConfigs[proxy.Name]
|
||||
for _, config := range remoteConfigs {
|
||||
if config.Isp == filter.Isp && config.City == filter.City && config.Province == filter.Prov {
|
||||
total += config.Count
|
||||
assigns[i] = &AssignEdgeResult{
|
||||
proxy: proxy,
|
||||
count: config.Count,
|
||||
}
|
||||
}
|
||||
}
|
||||
if assigns[i] == nil {
|
||||
assigns[i] = &AssignEdgeResult{
|
||||
proxy: proxy,
|
||||
count: 0,
|
||||
}
|
||||
}
|
||||
proxyIds[i] = proxy.ID
|
||||
}
|
||||
avg := int(math.Ceil(float64(total) / float64(len(proxies))))
|
||||
|
||||
for i, assign := range assigns {
|
||||
var prev = assign.count
|
||||
var next = assign.count
|
||||
if prev < avg && prev < total {
|
||||
next = int(math.Min(float64(avg), float64(total)))
|
||||
assigns[i].count = next - prev
|
||||
total -= next
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
// err := remote.Client.CloudConnect(remote.CloudConnectReq{
|
||||
// Uuid: assign.Proxy.Name,
|
||||
// Edge: nil,
|
||||
// AutoConfig: []remote.AutoConfig{{
|
||||
// Province: filter.Prov,
|
||||
// City: filter.City,
|
||||
// Isp: filter.Isp,
|
||||
// Count: next,
|
||||
// }},
|
||||
// })
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
}
|
||||
|
||||
return assigns, nil
|
||||
}
|
||||
|
||||
type AssignEdgeResult struct {
|
||||
proxy *models.Proxy
|
||||
count int
|
||||
}
|
||||
|
||||
// assignPort 分配指定数量的端口
|
||||
func assignPort(
|
||||
assigns []*AssignEdgeResult,
|
||||
userId int32,
|
||||
protocol ChannelProtocol,
|
||||
authType ChannelAuthType,
|
||||
expiration time.Time,
|
||||
filter NodeFilterConfig,
|
||||
) ([]AssignPortResult, error) {
|
||||
// 查询代理已配置端口
|
||||
var proxyIds = make([]int32, 0, len(assigns))
|
||||
for _, assigned := range assigns {
|
||||
proxyIds = append(proxyIds, assigned.proxy.ID)
|
||||
}
|
||||
channels, err := q.Channel.
|
||||
assigns, err := q.Channel.
|
||||
Select(
|
||||
q.Channel.ProxyID,
|
||||
q.Channel.ProxyPort).
|
||||
@@ -452,6 +393,99 @@ func assignPort(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 过滤需要变动的连接配置
|
||||
var current = 0
|
||||
var result = make([]ProxyConfig, len(proxies))
|
||||
for i, proxy := range proxies {
|
||||
remoteConfigs, ok := allConfigs[proxy.Name]
|
||||
if !ok {
|
||||
result[i] = ProxyConfig{
|
||||
proxy: proxy,
|
||||
config: &remote.AutoConfig{
|
||||
Province: filter.Prov,
|
||||
City: filter.City,
|
||||
Isp: filter.Isp,
|
||||
Count: 0,
|
||||
},
|
||||
}
|
||||
continue
|
||||
}
|
||||
for _, config := range remoteConfigs {
|
||||
if config.Isp == filter.Isp && config.City == filter.City && config.Province == filter.Prov {
|
||||
current += config.Count
|
||||
result[i] = ProxyConfig{
|
||||
proxy: proxy,
|
||||
config: &config,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果需要新增节点
|
||||
var needed = len(assigns) + count
|
||||
if needed-current > 0 {
|
||||
slog.Debug("新增新节点", "needed", needed, "current", current)
|
||||
avg := int(math.Ceil(float64(needed) / float64(len(proxies))))
|
||||
for i, assign := range result {
|
||||
var prev = assign.config.Count
|
||||
var next = assign.config.Count
|
||||
if prev >= avg || prev >= needed {
|
||||
continue
|
||||
}
|
||||
|
||||
next = int(math.Min(float64(avg), float64(needed)))
|
||||
result[i].config.Count = next - prev
|
||||
needed -= next
|
||||
err := remote.Client.CloudConnect(remote.CloudConnectReq{
|
||||
Uuid: assign.proxy.Name,
|
||||
Edge: nil,
|
||||
AutoConfig: []remote.AutoConfig{{
|
||||
Province: filter.Prov,
|
||||
City: filter.City,
|
||||
Isp: filter.Isp,
|
||||
Count: next,
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &AssignEdgeResult{
|
||||
configs: result,
|
||||
channels: assigns,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type AssignEdgeResult struct {
|
||||
configs []ProxyConfig
|
||||
channels []*models.Channel
|
||||
}
|
||||
|
||||
type ProxyConfig struct {
|
||||
proxy *models.Proxy
|
||||
config *remote.AutoConfig
|
||||
}
|
||||
|
||||
// assignPort 分配指定数量的端口
|
||||
func assignPort(
|
||||
proxies *AssignEdgeResult,
|
||||
userId int32,
|
||||
protocol ChannelProtocol,
|
||||
authType ChannelAuthType,
|
||||
expiration time.Time,
|
||||
filter NodeFilterConfig,
|
||||
) ([]AssignPortResult, error) {
|
||||
var assigns = proxies.configs
|
||||
var channels = proxies.channels
|
||||
|
||||
// 查询代理已配置端口
|
||||
var proxyIds = make([]int32, 0, len(assigns))
|
||||
for _, assigned := range assigns {
|
||||
proxyIds = append(proxyIds, assigned.proxy.ID)
|
||||
}
|
||||
|
||||
// 端口查找表
|
||||
var proxyPorts = make(map[uint64]struct{})
|
||||
for _, channel := range channels {
|
||||
@@ -462,8 +496,9 @@ func assignPort(
|
||||
// 配置启用代理
|
||||
var result = make([]AssignPortResult, len(assigns))
|
||||
for i, assign := range assigns {
|
||||
var err error
|
||||
var proxy = assign.proxy
|
||||
var count = assign.count
|
||||
var count = assign.config.Count
|
||||
|
||||
result[i] = AssignPortResult{
|
||||
Proxy: proxy,
|
||||
@@ -540,15 +575,15 @@ func assignPort(
|
||||
}
|
||||
|
||||
// 提交端口配置
|
||||
// gateway := remote.InitGateway(
|
||||
// proxy.Host,
|
||||
// "api",
|
||||
// "123456",
|
||||
// )
|
||||
// err = gateway.GatewayPortConfigs(configs)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
gateway := remote.InitGateway(
|
||||
proxy.Host,
|
||||
"api",
|
||||
"123456",
|
||||
)
|
||||
err = gateway.GatewayPortConfigs(configs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 保存到数据库
|
||||
err = q.Channel.
|
||||
|
||||
Reference in New Issue
Block a user