diff --git a/.gitignore b/.gitignore index b31681c..8f5fe8d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,6 @@ .env.* !.env.example -build/ +bin/ *.http \ No newline at end of file diff --git a/build.ps1 b/build.ps1 new file mode 100644 index 0000000..ebccf69 --- /dev/null +++ b/build.ps1 @@ -0,0 +1,36 @@ +Remove-Job * + +$tasks = 0 +$start = Get-Date + +$tasks++ +Write-Output "building platform for linux amd64..." +Start-Job -ScriptBlock { + $env:GOOS = "linux"; $env:GOARCH = "amd64"; go build -ldflags '-w -s' -o bin/platform_linux_amd64 cmd/main/main.go +} | Out-Null + +$tasks++ +Write-Output "building tasks for linux amd64..." +Start-Job -ScriptBlock { + $env:GOOS = "linux"; $env:GOARCH = "amd64"; go build -ldflags '-w -s' -o bin/tasks_linux_amd64 cmd/tasks/main.go +} | Out-Null + +# Wait for all jobs to complete +while ($tasks -gt 0) +{ + foreach ($job in Get-Job) + { + if ($job.State -eq "Completed") + { + $tasks-- + $job | Receive-Job + $job | Remove-Job + } + } +} + +$end = Get-Date + +Write-Output "build completed" +Write-Output "time taken: $( ($end - $start).TotalSeconds ) seconds" +Write-Output "output files are in ./bin/" \ No newline at end of file diff --git a/pkg/testutil/db.go b/pkg/testutil/db.go index 05c098c..9623e3d 100644 --- a/pkg/testutil/db.go +++ b/pkg/testutil/db.go @@ -13,7 +13,7 @@ import ( // SetupDBTest 创建一个基于 SQLite 内存数据库的 GORM 连接 func SetupDBTest(t *testing.T) *gorm.DB { // 使用 SQLite 内存数据库 - gormDB, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{}) + gormDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) if err != nil { t.Fatalf("gorm 打开 SQLite 内存数据库失败: %v", err) } diff --git a/web/services/channel.go b/web/services/channel.go index 0bca190..129172b 100644 --- a/web/services/channel.go +++ b/web/services/channel.go @@ -251,7 +251,7 @@ func (s *channelService) CreateChannel( } var addr []*PortInfo - err := q.Q.Transaction(func(tx *q.Query) error { + err := q.Q.Transaction(func(q *q.Query) error { // 查找套餐 step = time.Now() @@ -285,7 +285,7 @@ func (s *channelService) CreateChannel( // 申请节点 step = time.Now() - edgeAssigns, err := assignEdge(count, filter) + edgeAssigns, err := assignEdge(q, count, filter) if err != nil { return err } @@ -297,7 +297,7 @@ func (s *channelService) CreateChannel( now := time.Now() expiration := now.Add(time.Duration(resource.Live) * time.Second) - _addr, channels, err := assignPort(edgeAssigns, auth.Payload.Id, protocol, authType, expiration, filter) + _addr, channels, err := assignPort(q, edgeAssigns, auth.Payload.Id, protocol, authType, expiration, filter) if err != nil { return err } @@ -386,7 +386,7 @@ func checkUser(auth *AuthContext, resource *ResourceInfo, count int) error { } // assignEdge 分配边缘节点数量 -func assignEdge(count int, filter NodeFilterConfig) (*AssignEdgeResult, error) { +func assignEdge(q *q.Query, count int, filter NodeFilterConfig) (*AssignEdgeResult, error) { // 查询可以使用的网关 var step = time.Now() @@ -539,6 +539,7 @@ type ProxyConfig struct { // assignPort 分配指定数量的端口 func assignPort( + q *q.Query, proxies *AssignEdgeResult, userId int32, protocol ChannelProtocol, @@ -718,8 +719,7 @@ func genPassPair() (string, string) { password[i] = letters[rand.N(len(letters))] } - // return string(username), string(password) - return "123123", "123123" + return string(username), string(password) } func chKey(channel *models.Channel) string { diff --git a/web/services/channel_test.go b/web/services/channel_test.go index e3586b9..0dd086c 100644 --- a/web/services/channel_test.go +++ b/web/services/channel_test.go @@ -8,6 +8,7 @@ import ( "platform/pkg/remote" "platform/pkg/testutil" "platform/web/models" + "reflect" "strings" "testing" "time" @@ -337,7 +338,7 @@ func Test_channelService_CreateChannel(t *testing.T) { name string args args setup func() - want []string + want []*PortInfo wantErr bool wantErrContains string checkCache func(channels []models.Channel) error @@ -353,10 +354,12 @@ func Test_channelService_CreateChannel(t *testing.T) { count: 3, nodeFilter: []NodeFilterConfig{{Prov: "河南", City: "郑州", Isp: "电信"}}, }, - want: []string{ - "http://111.111.111.111:10000", - "http://111.111.111.111:10001", - "http://111.111.111.111:10002", + want: []*PortInfo{ + { + Proto: "http", + Host: proxy.Host, + Port: 10000, + }, }, }, { @@ -369,10 +372,7 @@ func Test_channelService_CreateChannel(t *testing.T) { authType: ChannelAuthTypeIp, count: 2, }, - want: []string{ - "http://111.111.111.111:10000", - "http://111.111.111.111:10001", - }, + want: []*PortInfo{}, }, { name: "管理员创建SOCKS5密码通道", @@ -384,10 +384,7 @@ func Test_channelService_CreateChannel(t *testing.T) { authType: ChannelAuthTypePass, count: 2, }, - want: []string{ - "socks5://111.111.111.111:10000", - "socks5://111.111.111.111:10001", - }, + want: []*PortInfo{}, }, { name: "套餐不存在", @@ -507,9 +504,9 @@ func Test_channelService_CreateChannel(t *testing.T) { return } - if len(got) != len(tt.want) { - t.Errorf("CreateChannel() 返回长度 = %v, want %v", len(got), len(tt.want)) - return + // 检查返回值 + if reflect.DeepEqual(got, tt.want) { + t.Errorf("CreateChannel() 返回值 = %v, 期望 %v", got, tt.want) } // 查询创建的通道