65 lines
1.4 KiB
Go
65 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 {
|
|
|
|
// 添加可用通道到 redis
|
|
chans := make([]netip.AddrPort, 10000)
|
|
for i := range 10000 {
|
|
chans[i] = netip.AddrPortFrom(IP, uint16(i+10000))
|
|
}
|
|
err := addChans(chans)
|
|
if err != nil {
|
|
return core.NewServErr("添加通道失败", err)
|
|
}
|
|
|
|
// 保存代理信息
|
|
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
|
|
}
|