From 68e96793fed1c1d4d74dc0e4e8ea8a0cf3f54afe Mon Sep 17 00:00:00 2001 From: luorijun Date: Wed, 2 Apr 2025 17:24:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=90=8C=E5=9C=B0=E5=8C=BA=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E5=90=88=E5=B9=B6=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/playground/main.go | 76 +++++++++++++++++++++++++++++++++++- web/services/channel.go | 50 ++++++++++++++++++------ web/services/channel_test.go | 10 +---- 3 files changed, 113 insertions(+), 23 deletions(-) diff --git a/cmd/playground/main.go b/cmd/playground/main.go index 0cedb3c..6b8edf0 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -1,6 +1,78 @@ package main +import ( + "encoding/json" + "platform/pkg/orm" + m "platform/web/models" + q "platform/web/queries" + "time" + + "github.com/glebarez/sqlite" + "gorm.io/gorm" +) + func main() { - println(rune('w')) - println(rune('m')) + open, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + panic(err) + } + + err = open.AutoMigrate(&m.Resource{}, &m.ResourcePss{}) + if err != nil { + panic(err) + } + + q.SetDefault(open) + + var r = &m.Resource{ + ID: 1, + UserID: 101, + Active: true, + } + open.Create(r) + var resourcePss = &m.ResourcePss{ + ID: 1, + ResourceID: 1, + Type: 1, + Live: 180, + Expire: time.Now().AddDate(1, 0, 0), + DailyLimit: 10000, + } + open.Create(resourcePss) + + var resource = new(ResourceInfo) + data := q.Resource.As("data") + pss := q.ResourcePss.As("pss") + err = data.Scopes(orm.Alias(data)). + Select( + data.ID, data.UserID, data.Active, + pss.Type, pss.Live, pss.DailyUsed, pss.DailyLimit, pss.DailyLast, pss.Quota, pss.Used, pss.Expire, + ). + LeftJoin(q.ResourcePss.As("pss"), pss.ResourceID.EqCol(data.ID)). + Where(data.ID.Eq(1)). + Scan(&resource) + if err != nil { + panic(err) + } + + bytes, err := json.MarshalIndent(resource, "", " ") + if err != nil { + panic(err) + } + + println(string(bytes)) +} + +type ResourceInfo struct { + Id int32 + UserId int32 + Active bool + Type int32 + Live int32 + DailyLimit int32 + DailyUsed int32 + DailyLast time.Time + Quota int32 + Used int32 + Expire time.Time } diff --git a/web/services/channel.go b/web/services/channel.go index 480b584..0bca190 100644 --- a/web/services/channel.go +++ b/web/services/channel.go @@ -308,6 +308,16 @@ func (s *channelService) CreateChannel( // 更新套餐使用记录 step = time.Now() + toUpdate := models.ResourcePss{ + Used: resource.Used + int32(count), + DailyLast: now, + } + last := resource.DailyLast + if now.Year() != last.Year() || now.Month() != last.Month() || now.Day() != last.Day() { + toUpdate.DailyUsed = int32(count) + } else { + toUpdate.DailyUsed = resource.DailyUsed + int32(count) + } _, err = q.ResourcePss. Where(q.ResourcePss.ResourceID.Eq(resourceId)). Select( @@ -315,11 +325,7 @@ func (s *channelService) CreateChannel( q.ResourcePss.DailyUsed, q.ResourcePss.DailyLast, ). - Updates(models.ResourcePss{ - Used: resource.Used + int32(count), - DailyUsed: resource.DailyUsed + int32(count), - DailyLast: now, - }) + Updates(toUpdate) if err != nil { return err } @@ -468,15 +474,33 @@ func assignEdge(count int, filter NodeFilterConfig) (*AssignEdgeResult, error) { step = time.Now() slog.Debug("新增新节点", "proxy", info.proxy.Name, "used", info.used, "count", info.count) + + rConfigs := rProxyConfigs[info.proxy.Name] + + var newConfig = remote.AutoConfig{ + Province: filter.Prov, + City: filter.City, + Isp: filter.Isp, + Count: int(math.Ceil(float64(info.used) * 11 / 10)), + } + var newConfigs []remote.AutoConfig + var update = false + for _, rConfig := range rConfigs { + if rConfig.Isp == filter.Isp && rConfig.City == filter.City && rConfig.Province == filter.Prov { + newConfigs = append(newConfigs, newConfig) + update = true + } else { + newConfigs = append(newConfigs, rConfig) + } + } + if !update { + newConfigs = append(newConfigs, newConfig) + } + err := remote.Cloud.CloudConnect(remote.CloudConnectReq{ - Uuid: info.proxy.Name, - Edge: nil, - AutoConfig: []remote.AutoConfig{{ - Province: filter.Prov, - City: filter.City, - Isp: filter.Isp, - Count: int(math.Ceil(float64(info.used) * 11 / 10)), - }}, + Uuid: info.proxy.Name, + Edge: nil, + AutoConfig: newConfigs, }) if err != nil { return nil, err diff --git a/web/services/channel_test.go b/web/services/channel_test.go index d743691..e3586b9 100644 --- a/web/services/channel_test.go +++ b/web/services/channel_test.go @@ -475,6 +475,8 @@ func Test_channelService_CreateChannel(t *testing.T) { wantErr: true, wantErrContains: "端口数量不足", }, + // todo 跨天用量更新 + // todo 多地区混杂条件提取 } for _, tt := range tests { @@ -510,14 +512,6 @@ func Test_channelService_CreateChannel(t *testing.T) { return } - // 检查返回地址格式 - for _, addr := range got { - protocol := string(tt.args.protocol) - if !strings.HasPrefix(addr, protocol+"://") { - t.Errorf("CreateChannel() 地址 %v 不是有效的 %s 地址", addr, protocol) - } - } - // 查询创建的通道 var channels []models.Channel db.Where(