优化表结构,重构模型,重新实现基于白银网关的提取节点流程

This commit is contained in:
2025-11-24 18:44:06 +08:00
parent 9a574f55cb
commit cb2a963a37
142 changed files with 6528 additions and 5808 deletions

View File

@@ -1,11 +1,11 @@
package handlers
import (
"fmt"
"net/netip"
"platform/pkg/u"
"platform/web/auth"
"platform/web/core"
channel2 "platform/web/domains/channel"
"platform/web/globals/orm"
m "platform/web/models"
q "platform/web/queries"
s "platform/web/services"
"time"
@@ -40,18 +40,18 @@ func ListChannels(c *fiber.Ctx) error {
Where(q.Channel.UserID.Eq(authContext.User.ID))
switch req.AuthType {
case s.ChannelAuthTypeIp:
cond.Where(q.Channel.AuthIP.Is(true))
cond.Where(q.Channel.Whitelists.IsNotNull())
case s.ChannelAuthTypePass:
cond.Where(q.Channel.AuthPass.Is(true))
cond.Where(q.Channel.Username.IsNotNull(), q.Channel.Password.IsNotNull())
default:
break
}
if req.ExpireAfter != nil {
cond.Where(q.Channel.Expiration.Gte(orm.LocalDateTime(*req.ExpireAfter)))
cond.Where(q.Channel.ExpiredAt.Gte(*req.ExpireAfter))
}
if req.ExpireBefore != nil {
cond.Where(q.Channel.Expiration.Lte(orm.LocalDateTime(*req.ExpireBefore)))
cond.Where(q.Channel.ExpiredAt.Lte(*req.ExpireBefore))
}
// 查询数据
@@ -92,19 +92,19 @@ func ListChannels(c *fiber.Ctx) error {
type CreateChannelReq struct {
ResourceId int32 `json:"resource_id" validate:"required"`
AuthType s.ChannelAuthType `json:"auth_type" validate:"required"`
Protocol channel2.Protocol `json:"protocol" validate:"required"`
Protocol int `json:"protocol" validate:"required"`
Count int `json:"count" validate:"required"`
Prov string `json:"prov"`
City string `json:"city"`
Isp string `json:"isp"`
Prov *string `json:"prov"`
City *string `json:"city"`
Isp *int `json:"isp"`
}
type CreateChannelRespItem struct {
Proto channel2.Protocol `json:"-"`
Host string `json:"host"`
Port int32 `json:"port"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
Proto int `json:"-"`
Host string `json:"host"`
Port uint16 `json:"port"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
}
func CreateChannel(c *fiber.Ctx) error {
@@ -115,48 +115,32 @@ func CreateChannel(c *fiber.Ctx) error {
return err
}
// 检查用户其他权限
user := authCtx.User
if user.IDToken == nil || *user.IDToken == "" {
return fiber.NewError(fiber.StatusForbidden, "账号未实名")
}
count, err := q.Whitelist.Where(
q.Whitelist.UserID.Eq(user.ID),
q.Whitelist.Host.Eq(c.IP()),
).Count()
if err != nil {
return err
}
if count == 0 {
return fiber.NewError(fiber.StatusForbidden, fmt.Sprintf("非白名单IP %s", c.IP()))
}
// 解析参数
req := new(CreateChannelReq)
if err := c.BodyParser(req); err != nil {
return err
}
var isp string
switch req.Isp {
case "1":
isp = "电信"
case "2":
isp = "联通"
case "3":
isp = "移动"
ip, err := netip.ParseAddr(c.Get(core.HeaderUserIP))
if err != nil {
return core.NewBizErr("解析请求头客户端 IP 地址失败", err)
}
// 创建通道
result, err := s.Channel.CreateChannel(
c,
result, err := s.Channel.CreateChannels(
ip,
user.ID,
req.ResourceId,
req.Protocol,
req.AuthType,
req.AuthType == s.ChannelAuthTypeIp,
req.AuthType == s.ChannelAuthTypePass,
req.Count,
s.EdgeFilter{
Isp: isp,
Isp: u.ElseTo(req.Isp, m.ToEdgeISP),
Prov: req.Prov,
City: req.City,
},
@@ -170,8 +154,8 @@ func CreateChannel(c *fiber.Ctx) error {
for i, channel := range result {
resp[i] = &CreateChannelRespItem{
Proto: req.Protocol,
Host: channel.ProxyHost,
Port: channel.ProxyPort,
Host: channel.Proxy.IP.String(),
Port: channel.Port,
}
if req.AuthType == s.ChannelAuthTypePass {
resp[i].Username = channel.Username
@@ -188,12 +172,13 @@ type CreateChannelResultType string
// region RemoveChannels
type RemoveChannelsReq struct {
ByIds []int32 `json:"by_ids" validate:"required"`
Batch string `json:"batch" validate:"required"`
Ids []int32 `json:"ids" validate:"required"`
}
func RemoveChannels(c *fiber.Ctx) error {
// 检查权限
authCtx, err := auth.GetAuthCtx(c).PermitUser()
_, err := auth.GetAuthCtx(c).PermitOfficialClient()
if err != nil {
return err
}
@@ -205,7 +190,7 @@ func RemoveChannels(c *fiber.Ctx) error {
}
// 删除通道
err = s.Channel.RemoveChannels(req.ByIds, authCtx.User.ID)
err = s.Channel.RemoveChannels(req.Batch, req.Ids)
if err != nil {
return err
}