mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-02 03:05:02 +03:00
7907843941
A node snapshot was merged into the master's client records by email, and the only ownership check filtered one derived copy of it: the node's settings were still stored and re-synced later, its stats still merged, and a record linked to the master as well as the node took every value the node reported, including credentials rendered into the master's own Xray config. Each snapshot now passes one gate before anything is written. A record also linked to the master or another node keeps the master's values apart from per-link fields and the lifecycle the merge already settles; a record linked only elsewhere, a detached record, a padded or case-variant email and a new client holding another client's subId are dropped from the stored settings as well. A soft-orphaned record can be taken back only by the node whose merge orphaned it, which the new clients.sync_orphaned_by column records. A node tunnel no longer owns the peer keys of a client a master-local tunnel also serves.
86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package service
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/logger"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// How long a client stays recoverable after a node merge concluded it is gone.
|
|
// Any merge that sees it attached again inside this window clears the mark.
|
|
const syncOrphanReapGrace = 15 * time.Minute
|
|
|
|
// A traffic row with no clients row behind it is drift the orphan sweep will
|
|
// never mark, so it stays on the old delete-immediately path.
|
|
func clientRecordExists(tx *gorm.DB, email string) bool {
|
|
var n int64
|
|
if err := tx.Model(&model.ClientRecord{}).Where("email = ?", email).Count(&n).Error; err != nil {
|
|
return false
|
|
}
|
|
return n > 0
|
|
}
|
|
|
|
func markSyncOrphan(tx *gorm.DB, email string, nodeID int, nowMs int64) error {
|
|
if email == "" {
|
|
return nil
|
|
}
|
|
return tx.Model(&model.ClientRecord{}).
|
|
Where("email = ? AND sync_orphaned_at = 0", email).
|
|
Updates(map[string]any{"sync_orphaned_at": nowMs, "sync_orphaned_by": nodeID}).Error
|
|
}
|
|
|
|
// A client that is attached again is not orphaned, whatever an earlier merge
|
|
// concluded — this is what makes a bad merge recoverable instead of fatal.
|
|
func clearSyncOrphanMarks(tx *gorm.DB) error {
|
|
return tx.Model(&model.ClientRecord{}).
|
|
Where("sync_orphaned_at > 0 AND EXISTS (SELECT 1 FROM client_inbounds WHERE client_inbounds.client_id = clients.id)").
|
|
Updates(map[string]any{"sync_orphaned_at": 0, "sync_orphaned_by": 0}).Error
|
|
}
|
|
|
|
// ReapSyncOrphans deletes the clients the node-snapshot sweep marked and no
|
|
// later merge reclaimed. It is the only path that hard-deletes for that sweep.
|
|
func (s *ClientService) ReapSyncOrphans() (int, error) {
|
|
db := database.GetDB()
|
|
cutoff := time.Now().Add(-syncOrphanReapGrace).UnixMilli()
|
|
|
|
var emails []string
|
|
if err := db.Model(&model.ClientRecord{}).
|
|
Where("sync_orphaned_at > 0 AND sync_orphaned_at <= ?", cutoff).
|
|
Where("NOT EXISTS (SELECT 1 FROM client_inbounds WHERE client_inbounds.client_id = clients.id)").
|
|
Pluck("email", &emails).Error; err != nil {
|
|
return 0, err
|
|
}
|
|
if len(emails) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
reaped := 0
|
|
for _, batch := range chunkStrings(emails, sqlInChunk) {
|
|
if err := runSerializedTx(func(tx *gorm.DB) error {
|
|
if err := adjustGroupBaselinesForRemovedTraffic(tx, batch); err != nil {
|
|
return err
|
|
}
|
|
if err := tx.Where("email IN ?", batch).Delete(&model.ClientRecord{}).Error; err != nil {
|
|
return err
|
|
}
|
|
if err := tx.Where("email IN ?", batch).Delete(&xray.ClientTraffic{}).Error; err != nil {
|
|
return err
|
|
}
|
|
if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
|
|
return err
|
|
}
|
|
return tx.Where("client_email IN ?", batch).Delete(&model.InboundClientIps{}).Error
|
|
}); err != nil {
|
|
return reaped, err
|
|
}
|
|
reaped += len(batch)
|
|
logger.Infof("reaped %d client(s) confirmed removed on their node", len(batch))
|
|
}
|
|
return reaped, nil
|
|
}
|