实现云端控制的动态节点分配逻辑

This commit is contained in:
2025-03-28 10:03:29 +08:00
parent e337a9c08e
commit e61f0bef32
16 changed files with 1313 additions and 138 deletions

View File

@@ -1,16 +1,22 @@
package services
import (
"encoding/json"
"fmt"
"context"
"platform/pkg/orm"
"platform/web/models"
)
type NodeServiceErr string
func (e NodeServiceErr) Error() string {
return string(e)
}
var Node = &nodeService{}
type nodeService struct{}
func (s *nodeService) Filter(userId int32, count int, config ...NodeFilterConfig) ([]*FilteredNode, error) {
func (s *nodeService) Filter(ctx context.Context, userId int32, count int, config ...NodeFilterConfig) ([]*models.Node, error) {
_config := NodeFilterConfig{}
if len(config) > 0 {
_config = config[0]
@@ -24,24 +30,35 @@ func (s *nodeService) Filter(userId int32, count int, config ...NodeFilterConfig
Limit(count).
Find(&nodes)
rs, _ := json.Marshal(nodes)
fmt.Printf(string(rs))
// todo 异步任务关闭代理
// 返回节点列表
return nodes, nil
// todo 异步任务缩容
return nil, nil
}
type NodeFilterConfig struct {
Isp string
Prov string
City string
Isp string `json:"isp"`
Prov string `json:"prov"`
City string `json:"city"`
}
type NodeFilterAsyncTask struct {
Config NodeFilterConfig `json:"config"`
Count int `json:"count"`
}
// 筛选节点的SQL语句暂时用不到
// 筛选已连接的符合条件且未分配给用户过的节点
//
// 静态条件:省,市,运营商
// 排序方式1.分配给该用户的次数 2.分配给全部用户的次数
const filterSqlRaw = `
select
n.id as id,
n.name as name,
n.fwd_port as port,
n.host as host,
n.fwd_port as fwd_port,
count(c.*) as total,
count(c.*) filter ( where c.user_id = ? ) as assigned
from
@@ -61,7 +78,8 @@ order by
type FilteredNode struct {
Id int32 `json:"id"`
Name string `json:"name"`
Port int32 `json:"port"`
Host string `json:"host"`
FwdPort int32 `json:"fwd_port"`
Total int32 `json:"total"`
Assigned int32 `json:"assigned"`
}