提供一个测试注册代理接口

This commit is contained in:
2025-12-01 12:42:51 +08:00
parent c0b3490d00
commit 299ce821d5
5 changed files with 86 additions and 15 deletions

View File

@@ -162,23 +162,18 @@ type ResourceView struct {
}
func lockChans(batch string, count int, expire time.Time) ([]netip.AddrPort, error) {
results, err := g.Redis.Eval(
chans, err := g.Redis.Eval(
context.Background(),
RedisScriptLockChans,
[]string{"channel"},
batch,
count,
expire.Unix(),
).Result()
).StringSlice()
if err != nil {
return nil, core.NewBizErr("获取通道失败", err)
}
chans, ok := results.([]string)
if !ok {
return nil, core.NewServErr("转换通道数据失败")
}
addrs := make([]netip.AddrPort, len(chans))
for i, ch := range chans {
addr, err := netip.ParseAddrPort(ch)
@@ -200,11 +195,11 @@ local expire = tonumber(ARGV[3])
local chans_key = key .. ":chans"
local lease_key = key .. ":lease:" .. batch
if redis.call("SCARD", key) < count then
if redis.call("SCARD", chans_key) < count then
return nil
end
local ports = redis.call("SPOP", key, count)
local ports = redis.call("SPOP", chans_key, count)
redis.call("SET", lease_key, cjson.encode({
p = ports,
@@ -244,6 +239,20 @@ redis.call("DEL", lease_key)
return chans
`
func registerChans(chans []netip.AddrPort) error {
strs := make([]string, len(chans))
for i, ch := range chans {
strs[i] = ch.String()
}
err := g.Redis.SAdd(context.Background(), "channel:chans", strs).Err()
if err != nil {
return core.NewBizErr("注册通道失败", err)
}
return nil
}
// 错误信息
var (
ErrResourceNotExist = core.NewBizErr("套餐不存在")

View File

@@ -1,6 +1,11 @@
package services
import (
"fmt"
"net/netip"
"platform/pkg/u"
"platform/web/core"
"platform/web/globals/orm"
m "platform/web/models"
q "platform/web/queries"
"time"
@@ -10,6 +15,7 @@ var Proxy = &proxyService{}
type proxyService struct{}
// AllProxies 获取所有代理
func (s *proxyService) AllProxies(proxyType m.ProxyType, channels bool) ([]*m.Proxy, error) {
proxies, err := q.Proxy.Where(
q.Proxy.Type.Eq(int(proxyType)),
@@ -23,3 +29,36 @@ func (s *proxyService) AllProxies(proxyType m.ProxyType, channels bool) ([]*m.Pr
return proxies, nil
}
// RegisterBaiyin 注册新代理服务
func (s *proxyService) RegisterBaiyin(Mac string, IP netip.Addr, username, password string) error {
// 添加可用通道到 redis
chans := make([]netip.AddrPort, 10000)
for i := range 10000 {
chans[i] = netip.AddrPortFrom(IP, uint16(i+10000))
}
err := registerChans(chans)
if err != nil {
return core.NewServErr("添加通道失败")
}
// 保存代理信息
if err := q.Proxy.Create(&m.Proxy{
Version: 0,
Mac: Mac,
IP: orm.Inet{Addr: IP},
Secret: u.P(fmt.Sprintf("%s:%s", username, password)),
Type: m.ProxyTypeBaiYin,
Status: m.ProxyStatusOnline,
}); err != nil {
return core.NewServErr("保存通道数据失败")
}
return nil
}
// UnregisterBaiyin 注销代理服务
func (s *proxyService) UnregisterBaiyin(id int) error {
return nil
}