mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-05 16:45:14 +03:00
145 lines
5.6 KiB
Go
145 lines
5.6 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/testpg"
|
|
)
|
|
|
|
func TestFrozenGroupBulkRejectsIdentityAndMembershipChanges(t *testing.T) {
|
|
for _, operation := range []string{"adjust", "delete"} {
|
|
for _, change := range []string{"replace", "move"} {
|
|
t.Run(operation+"/"+change, func(t *testing.T) {
|
|
cleanup, err := testpg.IsolatePackage("frozen_group_bulk")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(cleanup)
|
|
setupBulkDB(t)
|
|
db := database.GetDB()
|
|
svc, inboundSvc := &ClientService{}, &InboundService{}
|
|
client := model.Client{Email: "frozen@example.test", Group: "staff", ID: "11111111-1111-1111-1111-111111111111", TotalGB: 1024, Enable: true}
|
|
ib := mkInbound(t, 41001, model.VLESS, clientsSettings(t, []model.Client{client}))
|
|
if err := svc.SyncInbound(nil, ib.Id, []model.Client{client}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mkTraffic(t, ib.Id, client.Email, 1, 0, 1024, 0, true)
|
|
before, err := svc.GetRecordByEmail(nil, client.Email)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
page, err := svc.ListGroupClients(t.Context(), GroupClientPageParams{Group: "staff", Mode: "remove", Freeze: true})
|
|
if err != nil || page.Total != 1 {
|
|
t.Fatalf("freeze: %+v, %v", page, err)
|
|
}
|
|
scope, err := ResolveGroupBulkScope(t.Context(), "staff", page.ScopeToken)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// The operation can only proceed once the other writer has committed.
|
|
done := make(chan error, 1)
|
|
wantID := before.Id
|
|
go func() {
|
|
done <- runSerializedTx(func(tx *gorm.DB) error {
|
|
if change == "move" {
|
|
return tx.Model(before).Update("group_name", "outside").Error
|
|
}
|
|
if err := tx.Where("client_id = ?", before.Id).Delete(&model.ClientInbound{}).Error; err != nil {
|
|
return err
|
|
}
|
|
if err := tx.Delete(before).Error; err != nil {
|
|
return err
|
|
}
|
|
next := *before
|
|
next.Id = 0
|
|
if err := tx.Create(&next).Error; err != nil {
|
|
return err
|
|
}
|
|
if next.Id <= page.MaxID {
|
|
return fmt.Errorf("replacement %d is within cutoff %d", next.Id, page.MaxID)
|
|
}
|
|
wantID = next.Id
|
|
return tx.Create(&model.ClientInbound{ClientId: next.Id, InboundId: ib.Id}).Error
|
|
})
|
|
}()
|
|
if err := <-done; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if operation == "adjust" {
|
|
res, _, err := svc.BulkAdjustGroup(inboundSvc, scope, 0, 1024, "", nil, "")
|
|
if err != nil || res.Adjusted != 0 || len(res.Skipped) != 1 || res.Skipped[0].Reason != scope.skipReason() {
|
|
t.Fatalf("adjust: %+v, %v", res, err)
|
|
}
|
|
// Also exercise the guard with a plan made before the other writer.
|
|
plan := map[string]*bulkAdjustEntry{client.Email: {record: before, applyTotal: true, newTotal: 2048}}
|
|
part := svc.bulkAdjustInboundClients(inboundSvc, ib.Id, []string{client.Email}, plan, scope, "", "")
|
|
if part.perEmailSkipped[client.Email] != scope.skipReason() {
|
|
t.Fatalf("stale inbound adjust: %+v", part)
|
|
}
|
|
} else {
|
|
res, _, err := svc.BulkDeleteGroup(inboundSvc, scope, false)
|
|
if err != nil || res.Deleted != 0 || len(res.Skipped) != 1 || res.Skipped[0].Reason != scope.skipReason() {
|
|
t.Fatalf("delete: %+v, %v", res, err)
|
|
}
|
|
part := svc.bulkDelInboundClients(inboundSvc, ib.Id, []string{client.Email}, map[string]*model.ClientRecord{client.Email: before}, scope, false)
|
|
if part.perEmailSkipped[client.Email] != scope.skipReason() || isClientEmailTombstoned(client.Email) {
|
|
t.Fatalf("stale inbound delete: %+v", part)
|
|
}
|
|
}
|
|
got, err := svc.GetRecordByEmail(nil, client.Email)
|
|
if err != nil || got.Id != wantID || got.TotalGB != 1024 {
|
|
t.Fatalf("protected record: %+v, %v", got, err)
|
|
}
|
|
if got := trafficOf(t, client.Email); got.Total != 1024 || got.Up != 1 {
|
|
t.Fatalf("protected traffic: %+v", got)
|
|
}
|
|
var settings string
|
|
if err := db.Model(ib).Select("settings").Find(&settings).Error; err != nil || settings != ib.Settings {
|
|
t.Fatalf("protected settings changed: %s (%v)", settings, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFrozenGroupBulkAppliesLinkedTargets(t *testing.T) {
|
|
setupBulkDB(t)
|
|
svc, inboundSvc := &ClientService{}, &InboundService{}
|
|
client := model.Client{Email: "linked-group@example.test", Group: "staff", ID: "11111111-1111-1111-1111-111111111111", TotalGB: 1024, Enable: true}
|
|
ib := mkInbound(t, 41001, model.VLESS, clientsSettings(t, []model.Client{client}))
|
|
if err := svc.SyncInbound(nil, ib.Id, []model.Client{client}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mkTraffic(t, ib.Id, client.Email, 1, 0, 1024, 0, true)
|
|
page, err := svc.ListGroupClients(t.Context(), GroupClientPageParams{Group: "staff", Mode: "remove", Freeze: true})
|
|
if err != nil || page.Total != 1 {
|
|
t.Fatalf("freeze: %+v, %v", page, err)
|
|
}
|
|
scope, err := ResolveGroupBulkScope(t.Context(), "staff", page.ScopeToken)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
adjust, _, err := svc.BulkAdjustGroup(inboundSvc, scope, 0, 1024, "", nil, "")
|
|
if err != nil || adjust.Adjusted != 1 || len(adjust.Skipped) != 0 {
|
|
t.Fatalf("adjust: %+v, %v", adjust, err)
|
|
}
|
|
if got := trafficOf(t, client.Email); got.Total != 2048 {
|
|
t.Fatalf("adjusted traffic: %+v", got)
|
|
}
|
|
del, _, err := svc.BulkDeleteGroup(inboundSvc, scope, false)
|
|
if err != nil || del.Deleted != 1 || len(del.Skipped) != 0 {
|
|
t.Fatalf("delete: %+v, %v", del, err)
|
|
}
|
|
var rows int64
|
|
if err := database.GetDB().Model(&model.ClientRecord{}).Where("email = ?", client.Email).Count(&rows).Error; err != nil || rows != 0 {
|
|
t.Fatalf("deleted client still present: %d (%v)", rows, err)
|
|
}
|
|
}
|