通道接口预实现

This commit is contained in:
2025-03-25 09:49:56 +08:00
parent c3abb42bce
commit 727297f4ee
5 changed files with 193 additions and 6 deletions

58
web/services/channel.go Normal file
View File

@@ -0,0 +1,58 @@
package services
import (
"errors"
"log/slog"
"platform/web/models"
q "platform/web/queries"
)
var Channel = &channelService{}
type channelService struct {
}
func (s *channelService) CreateChannel(
userID int32,
region string,
provider string,
protocol Protocol,
resourceId int,
count int,
) ([]*models.Channel, error) {
// 检查并扣减套餐余额
var resourceInfo = struct {
models.Resource
models.ResourcePss
}{}
err := q.Resource.
Where(q.Resource.UserID.Eq(userID)).
LeftJoin(q.ResourcePss, q.ResourcePss.ResourceID.EqCol(q.Resource.ID)).
Scan(&resourceInfo)
if err != nil {
return nil, err
}
slog.Debug("查询资源", slog.Any("info", resourceInfo))
// 创建连接通道
// 保存到数据库与缓存,以及计时关闭
// 组织请求数据
// 发送请求到远端配置服务
// 返回连接通道列表
return nil, errors.New("not implemented")
}
type Protocol string
const (
ProtocolSocks5 = Protocol("socks5")
ProtocolHTTP = Protocol("http")
ProtocolHttps = Protocol("https")
)

View File

@@ -0,0 +1,37 @@
package services
import (
"testing"
)
func Test_channelService_CreateChannel(t *testing.T) {
// type args struct {
// userID int32
// region string
// provider string
// protocol Protocol
// resourceId int
// count int
// }
// tests := []struct {
// name string
// args args
// want []*models.Channel
// wantErr bool
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// s := &channelService{}
// got, err := s.CreateChannel(tt.args.userID, tt.args.region, tt.args.provider, tt.args.protocol, tt.args.resourceId, tt.args.count)
// if (err != nil) != tt.wantErr {
// t.Errorf("CreateChannel() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
// if !reflect.DeepEqual(got, tt.want) {
// t.Errorf("CreateChannel() got = %v, want %v", got, tt.want)
// }
// })
// }
}

View File

@@ -185,6 +185,11 @@ func (s *sessionService) Remove(ctx context.Context, accessToken, refreshToken s
return nil
}
// 生成一个新的令牌
func genToken() string {
return uuid.NewString()
}
// 令牌键的格式为 "session:<token>"
func accessKey(token string) string {
return fmt.Sprintf("session:%s", token)
@@ -195,11 +200,6 @@ func refreshKey(token string) string {
return fmt.Sprintf("session:refresh:%s", token)
}
// 生成一个新的令牌
func genToken() string {
return uuid.NewString()
}
// endregion
// region SessionConfig