修复通道提取接口权限与数据映射问题
This commit is contained in:
@@ -17,7 +17,7 @@ var Channel ChannelService = &channelBaiyinService{}
|
||||
|
||||
// 通道服务
|
||||
type ChannelService interface {
|
||||
CreateChannels(source netip.Addr, userId int32, resourceId int32, authWhitelist bool, authPassword bool, count int, edgeFilter ...EdgeFilter) ([]*m.Channel, error)
|
||||
CreateChannels(source netip.Addr, resourceId int32, authWhitelist bool, authPassword bool, count int, edgeFilter ...EdgeFilter) ([]*m.Channel, error)
|
||||
RemoveChannels(batch string, ids []int32) error
|
||||
}
|
||||
|
||||
@@ -47,12 +47,11 @@ func genPassPair() (string, string) {
|
||||
return string(username), string(password)
|
||||
}
|
||||
|
||||
func findResource(q *q.Query, resourceId int32, userId int32, count int, now time.Time) (*ResourceView, error) {
|
||||
func findResource(q *q.Query, resourceId int32, count int, now time.Time) (*ResourceView, error) {
|
||||
resource, err := q.Resource.
|
||||
Preload(field.Associations).
|
||||
Where(
|
||||
q.Resource.ID.Eq(resourceId),
|
||||
q.Resource.UserID.Eq(userId),
|
||||
q.Resource.Active.Is(true),
|
||||
).
|
||||
Take()
|
||||
@@ -64,6 +63,7 @@ func findResource(q *q.Query, resourceId int32, userId int32, count int, now tim
|
||||
Id: resource.ID,
|
||||
Active: resource.Active,
|
||||
Type: resource.Type,
|
||||
User: resource.User,
|
||||
}
|
||||
|
||||
switch resource.Type {
|
||||
@@ -114,35 +114,6 @@ func findResource(q *q.Query, resourceId int32, userId int32, count int, now tim
|
||||
info.Used = sub.Used
|
||||
}
|
||||
|
||||
// 检查套餐使用情况
|
||||
switch info.Mode {
|
||||
default:
|
||||
return nil, core.NewBizErr("不支持的套餐模式")
|
||||
|
||||
// 包时
|
||||
case m.ResourceModeTime:
|
||||
// 检查过期时间
|
||||
if info.Expire.Before(now) {
|
||||
return nil, ErrResourceExpired
|
||||
}
|
||||
// 检查每日限额
|
||||
used := 0
|
||||
if now.Format("2006-01-02") == info.DailyLast.Format("2006-01-02") {
|
||||
used = int(info.DailyUsed)
|
||||
}
|
||||
excess := used+count > int(info.DailyLimit)
|
||||
if excess {
|
||||
return nil, ErrResourceDailyLimit
|
||||
}
|
||||
|
||||
// 包量
|
||||
case m.ResourceModeQuota:
|
||||
// 检查可用配额
|
||||
if int(info.Quota)-int(info.Used) < count {
|
||||
return nil, ErrResourceExhausted
|
||||
}
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
|
||||
@@ -159,14 +130,17 @@ type ResourceView struct {
|
||||
Quota int32
|
||||
Used int32
|
||||
Expire time.Time
|
||||
User m.User
|
||||
}
|
||||
|
||||
func lockChans(batch string, count int, expire time.Time) ([]netip.AddrPort, error) {
|
||||
chans, err := g.Redis.Eval(
|
||||
context.Background(),
|
||||
RedisScriptLockChans,
|
||||
[]string{"channel"},
|
||||
batch,
|
||||
[]string{
|
||||
"channel:chans",
|
||||
"channel:lease:" + batch,
|
||||
},
|
||||
count,
|
||||
expire.Unix(),
|
||||
).StringSlice()
|
||||
@@ -187,13 +161,10 @@ func lockChans(batch string, count int, expire time.Time) ([]netip.AddrPort, err
|
||||
}
|
||||
|
||||
var RedisScriptLockChans = `
|
||||
local key = KEYS[1]
|
||||
local batch = ARGV[1]
|
||||
local count = tonumber(ARGV[2])
|
||||
local expire = tonumber(ARGV[3])
|
||||
|
||||
local chans_key = key .. ":chans"
|
||||
local lease_key = key .. ":lease:" .. batch
|
||||
local chans_key = KEYS[1]
|
||||
local lease_key = KEYS[2]
|
||||
local count = tonumber(ARGV[1])
|
||||
local expire = tonumber(ARGV[2])
|
||||
|
||||
if redis.call("SCARD", chans_key) < count then
|
||||
return nil
|
||||
@@ -210,12 +181,19 @@ return ports
|
||||
`
|
||||
|
||||
func freeChans(batch string, chans []string) error {
|
||||
values := make([]any, len(chans))
|
||||
for i, ch := range chans {
|
||||
values[i] = ch
|
||||
}
|
||||
|
||||
err := g.Redis.Eval(
|
||||
context.Background(),
|
||||
RedisScriptFreeChans,
|
||||
[]string{"channel"},
|
||||
batch,
|
||||
chans,
|
||||
[]string{
|
||||
"channel:chans",
|
||||
"channel:lease:" + batch,
|
||||
},
|
||||
values...,
|
||||
).Err()
|
||||
if err != nil {
|
||||
return core.NewBizErr("释放通道失败", err)
|
||||
@@ -225,15 +203,11 @@ func freeChans(batch string, chans []string) error {
|
||||
}
|
||||
|
||||
var RedisScriptFreeChans = `
|
||||
local key = KEYS[1]
|
||||
local batch = ARGV[1]
|
||||
local chans = ARGV[2]
|
||||
|
||||
local chans_key = key .. ":chans"
|
||||
local lease_key = key .. ":lease:" .. batch
|
||||
local chans_key = KEYS[1]
|
||||
local lease_key = KEYS[2]
|
||||
local chans = ARGV
|
||||
|
||||
redis.call("SADD", chans_key, unpack(chans))
|
||||
|
||||
redis.call("DEL", lease_key)
|
||||
|
||||
return chans
|
||||
|
||||
Reference in New Issue
Block a user