Files
platform/web/services/proxy.go

66 lines
1.4 KiB
Go

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"
)
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)),
q.Proxy.Status.Eq(int(m.ProxyStatusOnline)),
).Preload(
q.Proxy.Channels.On(q.Channel.ExpiredAt.Gte(time.Now())),
).Find()
if err != nil {
return nil, err
}
return proxies, nil
}
// RegisterBaiyin 注册新代理服务
func (s *proxyService) RegisterBaiyin(Mac string, IP netip.Addr, username, password string) error {
// 保存代理信息
proxy := &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,
}
if err := q.Proxy.Create(proxy); err != nil {
return core.NewServErr("保存通道数据失败")
}
// 添加可用通道到 redis
chans := make([]netip.AddrPort, 10000)
for i := range 10000 {
chans[i] = netip.AddrPortFrom(IP, uint16(i+10000))
}
err := regChans(proxy.ID, chans)
if err != nil {
return core.NewServErr("添加通道失败", err)
}
return nil
}
// UnregisterBaiyin 注销代理服务
func (s *proxyService) UnregisterBaiyin(id int) error {
return nil
}