优化数据库结构与数据插入逻辑

This commit is contained in:
2025-04-12 18:03:44 +08:00
parent 438a55cc3e
commit 8c268fd7a9
28 changed files with 218 additions and 213 deletions

View File

@@ -12,7 +12,7 @@ import (
"platform/pkg/orm"
"platform/pkg/rds"
"platform/pkg/remote"
"platform/pkg/v"
"platform/pkg/u"
"platform/web/common"
"platform/web/models"
q "platform/web/queries"
@@ -37,12 +37,12 @@ const (
ChannelAuthTypePass
)
type ChannelProtocol string
type ChannelProtocol int32
const (
ProtocolSocks5 = ChannelProtocol("socks5")
ProtocolHTTP = ChannelProtocol("http")
ProtocolHttps = ChannelProtocol("https")
ProtocolHTTP = ChannelProtocol(1)
ProtocolHttps = ChannelProtocol(2)
ProtocolSocks5 = ChannelProtocol(3)
)
type ResourceInfo struct {
@@ -142,7 +142,7 @@ func (s *channelService) RemoveChannels(ctx context.Context, auth *AuthContext,
Port: int(channel.ProxyPort),
Edge: &[]string{},
AutoEdgeConfig: &remote.AutoEdgeConfig{
Count: v.P(0),
Count: u.P(0),
},
Status: false,
}
@@ -600,7 +600,7 @@ func assignPort(
Province: filter.Prov,
City: filter.City,
Isp: filter.Isp,
Count: v.P(1),
Count: u.P(1),
PacketLoss: 30,
},
})
@@ -608,7 +608,7 @@ func assignPort(
switch authType {
case ChannelAuthTypeIp:
configs[i].Whitelist = &whitelist
configs[i].Userpass = v.P("")
configs[i].Userpass = u.P("")
for _, item := range whitelist {
channels = append(channels, &models.Channel{
UserID: userId,
@@ -617,19 +617,19 @@ func assignPort(
ProxyPort: int32(port),
AuthIP: true,
AuthPass: false,
Protocol: string(protocol),
Protocol: int32(protocol),
Expiration: expiration,
})
}
result = append(result, &PortInfo{
Proto: string(protocol),
Proto: protocol,
Host: proxy.Host,
Port: port,
})
case ChannelAuthTypePass:
username, password := genPassPair()
configs[i].Whitelist = &[]string{}
configs[i].Userpass = v.P(fmt.Sprintf("%s:%s", username, password))
configs[i].Userpass = u.P(fmt.Sprintf("%s:%s", username, password))
channels = append(channels, &models.Channel{
UserID: userId,
ProxyID: proxy.ID,
@@ -638,11 +638,11 @@ func assignPort(
AuthPass: true,
Username: username,
Password: password,
Protocol: string(protocol),
Protocol: int32(protocol),
Expiration: expiration,
})
result = append(result, &PortInfo{
Proto: string(protocol),
Proto: protocol,
Host: proxy.Host,
Port: port,
Username: &username,
@@ -694,16 +694,17 @@ func assignPort(
}
type PortInfo struct {
Proto string `json:"-"`
Host string `json:"host"`
Port int `json:"port"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
Proto ChannelProtocol `json:"-"`
Host string `json:"host"`
Port int `json:"port"`
Username *string `json:"username,omitempty"`
Password *string `json:"password,omitempty"`
}
// endregion
func genPassPair() (string, string) {
//goland:noinspection SpellCheckingInspection
var alphabet = []rune("abcdefghjkmnpqrstuvwxyz")
var numbers = []rune("23456789")

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"platform/pkg/remote"
"platform/pkg/testutil"
"platform/web/common"
"platform/web/models"
"reflect"
"strings"
@@ -116,7 +117,7 @@ func Test_cache(t *testing.T) {
UserID: 100,
ProxyID: 10,
ProxyPort: 8080,
Protocol: "http",
Protocol: 1,
Expiration: expiration,
},
{
@@ -124,7 +125,7 @@ func Test_cache(t *testing.T) {
UserID: 101,
ProxyID: 11,
ProxyPort: 8081,
Protocol: "socks5",
Protocol: 3,
Expiration: expiration,
},
}
@@ -330,7 +331,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
ResourceID: 1,
Type: 1,
Live: 180,
Expire: time.Now().AddDate(1, 0, 0),
Expire: common.LocalDateTime(time.Now().AddDate(1, 0, 0)),
DailyLimit: 10000,
}
db.Create(resourcePss)
@@ -419,8 +420,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
// 验证结果
var gotMap = make(map[int]PortInfo)
for _, port := range got {
if port.Proto != "http" {
return fmt.Errorf("期望协议为 http得到 %s", port.Proto)
if port.Proto != 1 {
return fmt.Errorf("期望协议为 1(http),得到 %d", port.Proto)
}
if port.Host != proxy.Host {
return fmt.Errorf("期望主机为 %s得到 %s", proxy.Host, port.Host)
@@ -432,8 +433,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
var channels []*models.Channel
db.Where("user_id = ? and deleted_at is null", userAuth.Payload.Id).Find(&channels)
for _, ch := range channels {
if ch.Protocol != "http" {
return fmt.Errorf("通道协议不正确,期望 http得到 %s", ch.Protocol)
if ch.Protocol != 1 {
return fmt.Errorf("通道协议不正确,期望 1(http),得到 %d", ch.Protocol)
}
if ch.UserID != userAuth.Payload.Id {
return fmt.Errorf("通道用户ID不正确期望 %d得到 %d", userAuth.Payload.Id, ch.UserID)
@@ -449,8 +450,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
if ch.AuthPass != true && ch.AuthIP != false {
return fmt.Errorf("通道认证类型不正确,期望 Pass得到 %v", ch.AuthPass)
}
if ch.Protocol != info.Proto {
return fmt.Errorf("通道协议不正确,期望 %s,得到 %s", info.Proto, ch.Protocol)
if ch.Protocol != int32(info.Proto) {
return fmt.Errorf("通道协议不正确,期望 %d,得到 %d", info.Proto, ch.Protocol)
}
if ch.Username != *info.Username {
return fmt.Errorf("通道用户名不正确,期望 %s得到 %s", *info.Username, ch.Username)
@@ -485,7 +486,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
if pss.DailyUsed != 3 {
return fmt.Errorf("套餐每日用量不正确,期望 3得到 %d", pss.DailyUsed)
}
if pss.DailyLast.IsZero() {
if time.Time(pss.DailyLast).IsZero() {
return fmt.Errorf("套餐每日最后更新时间不应为空")
}
if pss.Used != 3 {
@@ -569,8 +570,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
// 验证结果
var gotMap = make(map[int]PortInfo)
for _, port := range got {
if port.Proto != "http" {
return fmt.Errorf("期望协议为 http得到 %s", port.Proto)
if port.Proto != 1 {
return fmt.Errorf("期望协议为 1(http),得到 %d", port.Proto)
}
if port.Host != proxy.Host {
return fmt.Errorf("期望主机为 %s得到 %s", proxy.Host, port.Host)
@@ -582,8 +583,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
var channels []*models.Channel
db.Where("user_id = ? and deleted_at is null", userAuth.Payload.Id).Find(&channels)
for _, ch := range channels {
if ch.Protocol != "http" {
return fmt.Errorf("通道协议不正确,期望 http得到 %s", ch.Protocol)
if ch.Protocol != 1 {
return fmt.Errorf("通道协议不正确,期望 1(http),得到 %d", ch.Protocol)
}
if ch.UserID != userAuth.Payload.Id {
return fmt.Errorf("通道用户ID不正确期望 %d得到 %d", userAuth.Payload.Id, ch.UserID)
@@ -599,8 +600,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
if ch.AuthPass != false && ch.AuthIP != true {
return fmt.Errorf("通道认证类型不正确,期望 Pass得到 %v", ch.AuthPass)
}
if ch.Protocol != info.Proto {
return fmt.Errorf("通道协议不正确,期望 %s,得到 %s", info.Proto, ch.Protocol)
if ch.Protocol != int32(info.Proto) {
return fmt.Errorf("通道协议不正确,期望 %d,得到 %d", info.Proto, ch.Protocol)
}
if ch.Expiration.IsZero() {
return fmt.Errorf("通道过期时间不应为空")
@@ -629,7 +630,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
if pss.DailyUsed != 3 {
return fmt.Errorf("套餐每日用量不正确,期望 3得到 %d", pss.DailyUsed)
}
if pss.DailyLast.IsZero() {
if time.Time(pss.DailyLast).IsZero() {
return fmt.Errorf("套餐每日最后更新时间不应为空")
}
if pss.Used != 3 {
@@ -713,8 +714,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
// 验证结果
var gotMap = make(map[int]PortInfo)
for _, port := range got {
if port.Proto != "socks5" {
return fmt.Errorf("期望协议为 http得到 %s", port.Proto)
if port.Proto != 3 {
return fmt.Errorf("期望协议为 1(http),得到 %d", port.Proto)
}
if port.Host != proxy.Host {
return fmt.Errorf("期望主机为 %s得到 %s", proxy.Host, port.Host)
@@ -726,8 +727,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
var channels []*models.Channel
db.Where("user_id = ? and deleted_at is null", userAuth.Payload.Id).Find(&channels)
for _, ch := range channels {
if ch.Protocol != "http" {
return fmt.Errorf("通道协议不正确,期望 http得到 %s", ch.Protocol)
if ch.Protocol != 1 {
return fmt.Errorf("通道协议不正确,期望 1(http),得到 %d", ch.Protocol)
}
if ch.UserID != userAuth.Payload.Id {
return fmt.Errorf("通道用户ID不正确期望 %d得到 %d", userAuth.Payload.Id, ch.UserID)
@@ -743,8 +744,8 @@ func Test_channelService_CreateChannel(t *testing.T) {
if ch.AuthPass != true && ch.AuthIP != false {
return fmt.Errorf("通道认证类型不正确,期望 Pass得到 %v", ch.AuthPass)
}
if ch.Protocol != info.Proto {
return fmt.Errorf("通道协议不正确,期望 %s,得到 %s", info.Proto, ch.Protocol)
if ch.Protocol != int32(info.Proto) {
return fmt.Errorf("通道协议不正确,期望 %d,得到 %d", info.Proto, ch.Protocol)
}
if ch.Username != *info.Username {
return fmt.Errorf("通道用户名不正确,期望 %s得到 %s", *info.Username, ch.Username)
@@ -779,7 +780,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
if pss.DailyUsed != 3 {
return fmt.Errorf("套餐每日用量不正确,期望 3得到 %d", pss.DailyUsed)
}
if pss.DailyLast.IsZero() {
if time.Time(pss.DailyLast).IsZero() {
return fmt.Errorf("套餐每日最后更新时间不应为空")
}
if pss.Used != 3 {
@@ -831,7 +832,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
ResourceID: 2,
Type: 1,
Live: 180,
Expire: time.Now().AddDate(1, 0, 0),
Expire: common.LocalDateTime(time.Now().AddDate(1, 0, 0)),
DailyLimit: 10000,
}
db.Create(resourcePss2)
@@ -1022,9 +1023,9 @@ func Test_channelService_RemoveChannels(t *testing.T) {
// 创建通道
channels := []models.Channel{
{ID: 1, UserID: 101, ProxyID: 1, ProxyPort: 10001, Protocol: "http", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 2, UserID: 101, ProxyID: 1, ProxyPort: 10002, Protocol: "http", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 3, UserID: 101, ProxyID: 2, ProxyPort: 10001, Protocol: "socks5", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 1, UserID: 101, ProxyID: 1, ProxyPort: 10001, Protocol: 1, Expiration: time.Now().Add(24 * time.Hour)},
{ID: 2, UserID: 101, ProxyID: 1, ProxyPort: 10002, Protocol: 1, Expiration: time.Now().Add(24 * time.Hour)},
{ID: 3, UserID: 101, ProxyID: 2, ProxyPort: 10001, Protocol: 3, Expiration: time.Now().Add(24 * time.Hour)},
}
// 保存预设数据
@@ -1136,9 +1137,9 @@ func Test_channelService_RemoveChannels(t *testing.T) {
// 创建通道
channels := []models.Channel{
{ID: 1, UserID: 101, ProxyID: 1, ProxyPort: 10001, Protocol: "http", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 2, UserID: 101, ProxyID: 1, ProxyPort: 10002, Protocol: "http", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 3, UserID: 101, ProxyID: 2, ProxyPort: 10001, Protocol: "socks5", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 1, UserID: 101, ProxyID: 1, ProxyPort: 10001, Protocol: 1, Expiration: time.Now().Add(24 * time.Hour)},
{ID: 2, UserID: 101, ProxyID: 1, ProxyPort: 10002, Protocol: 1, Expiration: time.Now().Add(24 * time.Hour)},
{ID: 3, UserID: 101, ProxyID: 2, ProxyPort: 10001, Protocol: 3, Expiration: time.Now().Add(24 * time.Hour)},
}
// 保存预设数据
@@ -1250,9 +1251,9 @@ func Test_channelService_RemoveChannels(t *testing.T) {
// 创建通道
channels := []models.Channel{
{ID: 1, UserID: 101, ProxyID: 1, ProxyPort: 10001, Protocol: "http", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 2, UserID: 101, ProxyID: 1, ProxyPort: 10002, Protocol: "http", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 3, UserID: 102, ProxyID: 2, ProxyPort: 10001, Protocol: "socks5", Expiration: time.Now().Add(24 * time.Hour)},
{ID: 1, UserID: 101, ProxyID: 1, ProxyPort: 10001, Protocol: 1, Expiration: time.Now().Add(24 * time.Hour)},
{ID: 2, UserID: 101, ProxyID: 1, ProxyPort: 10002, Protocol: 1, Expiration: time.Now().Add(24 * time.Hour)},
{ID: 3, UserID: 102, ProxyID: 2, ProxyPort: 10001, Protocol: 3, Expiration: time.Now().Add(24 * time.Hour)},
}
// 保存预设数据

View File

@@ -13,7 +13,7 @@ import (
"github.com/redis/go-redis/v9"
)
var ID IdService = IdService{}
var ID = IdService{}
type IdService struct {
}

View File

@@ -11,6 +11,7 @@ import (
// 创建测试用的认证上下文
func createTestAuthContext() AuthContext {
//goland:noinspection ALL
return AuthContext{
Payload: Payload{
Type: PayloadUser,

View File

@@ -2,6 +2,7 @@ package services
import (
"context"
"errors"
"platform/pkg/testutil"
"strconv"
"testing"
@@ -85,8 +86,10 @@ func Test_verifierService_SendSms(t *testing.T) {
// 验证错误类型
if tt.wantErr && tt.wantErrType != nil {
if _, isSendLimitErr := err.(VerifierServiceSendLimitErr); isSendLimitErr {
if _, wantSendLimitErr := tt.wantErrType.(VerifierServiceSendLimitErr); !wantSendLimitErr {
var verifierServiceSendLimitErr VerifierServiceSendLimitErr
if errors.As(err, &verifierServiceSendLimitErr) {
var verifierServiceSendLimitErr VerifierServiceSendLimitErr
if !errors.As(tt.wantErrType, &verifierServiceSendLimitErr) {
t.Errorf("SendSms() error type = %T, wantErrType %T", err, tt.wantErrType)
}
}
@@ -216,7 +219,7 @@ func Test_verifierService_VerifySms(t *testing.T) {
}
// 检查错误类型
if tt.wantErr && tt.wantErrType != nil && err != tt.wantErrType {
if tt.wantErr && tt.wantErrType != nil && !errors.Is(err, tt.wantErrType) {
t.Errorf("VerifySms() error = %v, wantErrType %v", err, tt.wantErrType)
return
}