修复通道提取接口权限与数据映射问题

This commit is contained in:
2025-12-01 12:43:29 +08:00
parent 299ce821d5
commit a024c38375
11 changed files with 124 additions and 198 deletions

View File

@@ -18,11 +18,12 @@ import (
"time"
"github.com/hibiken/asynq"
"gorm.io/gen/field"
)
type channelBaiyinService struct{}
func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, resourceId int32, authWhitelist bool, authPassword bool, count int, edgeFilter ...EdgeFilter) ([]*m.Channel, error) {
func (s *channelBaiyinService) CreateChannels(source netip.Addr, resourceId int32, authWhitelist bool, authPassword bool, count int, edgeFilter ...EdgeFilter) ([]*m.Channel, error) {
if count > 400 {
return nil, core.NewBizErr("单次最多提取 400 个")
}
@@ -35,9 +36,21 @@ func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, r
now := time.Now()
batch := ID.GenReadable("bat")
// 获取用户套餐
resource, err := findResource(q.Q, resourceId, count, now)
if err != nil {
return nil, err
}
// 检查用户
user := resource.User
if user.IDToken == nil || *user.IDToken == "" {
return nil, core.NewBizErr("账号未实名")
}
// 获取用户白名单并检查用户 ip 地址
whitelists, err := q.Whitelist.Where(
q.Whitelist.UserID.Eq(userId),
q.Whitelist.UserID.Eq(user.ID),
).Find()
if err != nil {
return nil, err
@@ -55,11 +68,35 @@ func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, r
return nil, core.NewBizErr(fmt.Sprintf("IP 地址 %s 不在白名单内", source.String()))
}
// 获取用户套餐并检查
resource, err := findResource(q.Q, resourceId, userId, count, now)
if err != nil {
return nil, err
// 检查套餐使用情况
switch resource.Mode {
default:
return nil, core.NewBizErr("不支持的套餐模式")
// 包时
case m.ResourceModeTime:
// 检查过期时间
if resource.Expire.Before(now) {
return nil, ErrResourceExpired
}
// 检查每日限额
used := 0
if now.Format("2006-01-02") == resource.DailyLast.Format("2006-01-02") {
used = int(resource.DailyUsed)
}
excess := used+count > int(resource.DailyLimit)
if excess {
return nil, ErrResourceDailyLimit
}
// 包量
case m.ResourceModeQuota:
// 检查可用配额
if int(resource.Quota)-int(resource.Used) < count {
return nil, ErrResourceExhausted
}
}
expire := now.Add(resource.Live)
// 获取可用通道
@@ -104,18 +141,19 @@ func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, r
// 使用记录
actions[i] = &m.LogsUserUsage{
UserID: userId,
UserID: user.ID,
ResourceID: resourceId,
Count: int32(count),
ISP: u.P(filter.Isp.String()),
Prov: filter.Prov,
City: filter.City,
IP: orm.Inet{Addr: source},
}
// 通道数据
inet := orm.Inet{Addr: ch.Addr()}
channels[i] = &m.Channel{
UserID: userId,
UserID: user.ID,
ResourceID: resourceId,
BatchNo: batch,
ProxyID: findProxy[inet].ID,
@@ -124,9 +162,10 @@ func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, r
FilterProv: filter.Prov,
FilterCity: filter.City,
ExpiredAt: expire,
Proxy: *findProxy[inet],
}
if authWhitelist {
channels[i].Whitelists = &orm.Slice[string]{Arr: whitelistIPs}
channels[i].Whitelists = u.P(strings.Join(whitelistIPs, ","))
}
if authPassword {
username, password := genPassPair()
@@ -181,7 +220,9 @@ func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, r
}
// 保存通道和分配记录
err = q.Channel.Create(channels...)
err = q.Channel.
Omit(field.AssociationFields).
Create(channels...)
if err != nil {
return core.NewServErr("保存通道失败", err)
}
@@ -226,7 +267,7 @@ func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, r
},
}
if authWhitelist {
configs[i].Whitelist = &channel.Whitelists.Arr
configs[i].Whitelist = &whitelistIPs
}
if authPassword {
configs[i].Userpass = u.P(fmt.Sprintf("%s:%s", *channel.Username, *channel.Password))
@@ -248,9 +289,10 @@ func (s *channelBaiyinService) CreateChannels(source netip.Addr, userId int32, r
}
func (s *channelBaiyinService) RemoveChannels(batch string, ids []int32) error {
start := time.Now()
// 获取连接数据
channels, err := q.Channel.Debug().
channels, err := q.Channel.
Preload(q.Channel.Proxy).
Where(q.Channel.ID.In(ids...)).
Find()
@@ -279,7 +321,7 @@ func (s *channelBaiyinService) RemoveChannels(batch string, ids []int32) error {
// 释放端口
err = freeChans(batch, chans)
if err != nil {
return core.NewServErr("释放端口失败", err)
return err
}
// 清空配置
@@ -304,9 +346,10 @@ func (s *channelBaiyinService) RemoveChannels(batch string, ids []int32) error {
}
} else {
bytes, _ := json.Marshal(configs)
slog.Debug("清除代理端口配置", "config", bytes)
slog.Debug("清除代理端口配置", "config", string(bytes))
}
}
slog.Debug("清除代理端口配置", "time", time.Since(start).String())
return nil
}