mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-05 00:36:09 +03:00
1ac11c7783
Keep group client tables and bulk requests bounded with paged queries, frozen selections, and a streamed subscription export. Reuse existing bulk mutations and preserve the explicit-email contracts.
320 lines
11 KiB
Go
320 lines
11 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"hash"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
const attachCandidatePageSize = 100
|
|
|
|
type AttachCandidateParams struct {
|
|
SourceID int `form:"sourceId"`
|
|
Targets string `form:"inboundIds"`
|
|
Page int `form:"page"`
|
|
Search string `form:"search"`
|
|
Group string `form:"group"`
|
|
Freeze bool `form:"freeze"`
|
|
// The filter a frozen selection was taken under, which marks rows inScope.
|
|
ScopeSearch string `form:"scopeSearch"`
|
|
ScopeGroup string `form:"scopeGroup"`
|
|
}
|
|
|
|
type AttachCandidate struct {
|
|
ID int `json:"id" gorm:"column:id" example:"1"`
|
|
Email string `json:"email" example:"alice@example.test"`
|
|
Group string `json:"group" example:"staff"`
|
|
Comment string `json:"comment" example:"Example client"`
|
|
Enable bool `json:"enable" example:"true"`
|
|
AlreadyAttached bool `json:"alreadyAttached" example:"false"`
|
|
InScope bool `json:"inScope" example:"true"`
|
|
}
|
|
|
|
type AttachCandidatePage struct {
|
|
Items []AttachCandidate `json:"items"`
|
|
Total int64 `json:"total" example:"120"`
|
|
Eligible int64 `json:"eligible" example:"118"`
|
|
Groups []string `json:"groups" example:"[\"staff\"]"`
|
|
ScopeToken string `json:"scopeToken,omitempty"`
|
|
MaxID int `json:"maxId,omitempty"`
|
|
}
|
|
|
|
type AttachSelection struct {
|
|
Mode string `json:"mode"`
|
|
SourceID int `json:"sourceId"`
|
|
ClientIDs []int `json:"clientIds"`
|
|
ExcludedClientIDs []int `json:"excludedClientIds"`
|
|
ScopeToken string `json:"scopeToken"`
|
|
Search string `json:"search"`
|
|
Group string `json:"group"`
|
|
}
|
|
|
|
type AttachSelectionResult struct {
|
|
Attached int `json:"attached" example:"9997"`
|
|
Skipped int `json:"skipped" example:"0"`
|
|
Errors int `json:"errors" example:"0"`
|
|
}
|
|
|
|
func attachScope(db *gorm.DB, sourceID int, ids []int) ([]int, error) {
|
|
if len(ids) > 100 || sourceID < 0 || (sourceID == 0 && len(ids) == 0) {
|
|
return nil, fmt.Errorf("invalid client attachment scope")
|
|
}
|
|
targets := uniqueInts(ids)
|
|
slices.Sort(targets)
|
|
inbounds := slices.Clone(targets)
|
|
if sourceID > 0 {
|
|
if slices.Contains(targets, sourceID) {
|
|
return nil, fmt.Errorf("source inbound cannot be a target")
|
|
}
|
|
inbounds = append(inbounds, sourceID)
|
|
}
|
|
var rows []model.Inbound
|
|
if err := db.Select("id", "protocol").Where("id IN ?", inbounds).Find(&rows).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if len(rows) != len(inbounds) {
|
|
return nil, fmt.Errorf("an inbound in the attachment scope no longer exists")
|
|
}
|
|
for _, row := range rows {
|
|
switch row.Protocol {
|
|
case model.VMESS, model.VLESS, model.Trojan, model.Shadowsocks, model.Hysteria,
|
|
model.MTProto, model.WireGuard, model.AmneziaWG, model.TUIC, model.MASQUE:
|
|
default:
|
|
return nil, fmt.Errorf("inbound %d does not support attaching clients", row.Id)
|
|
}
|
|
}
|
|
return targets, nil
|
|
}
|
|
|
|
func attachCandidates(db *gorm.DB, sourceID int) *gorm.DB {
|
|
q := db.Table("clients AS c").Where("c.email <> ''")
|
|
if sourceID > 0 {
|
|
q = q.Where("EXISTS (SELECT 1 FROM client_inbounds AS src WHERE src.client_id = c.id AND src.inbound_id = ?)", sourceID)
|
|
}
|
|
return q
|
|
}
|
|
|
|
const attachedToTargetsSQL = "(SELECT COUNT(*) FROM client_inbounds AS dst WHERE dst.client_id = c.id AND dst.inbound_id IN ?) = ?"
|
|
|
|
func eligibleAttachCandidates(q *gorm.DB, targets []int) *gorm.DB {
|
|
if len(targets) > 0 {
|
|
return q.Where("NOT ("+attachedToTargetsSQL+")", targets, len(targets))
|
|
}
|
|
return q
|
|
}
|
|
|
|
// attachFilter is the search and group predicate of a candidate page or a frozen scope.
|
|
func attachFilter(sourceID int, search, group string) (string, []any) {
|
|
var clauses []string
|
|
var args []any
|
|
if sourceID == 0 && group != "" {
|
|
clauses = append(clauses, "c.group_name = ?")
|
|
args = append(args, group)
|
|
}
|
|
if search = strings.TrimSpace(search); search != "" {
|
|
col := "c.group_name"
|
|
if sourceID > 0 {
|
|
col = "c.comment"
|
|
}
|
|
const like = " LIKE ? ESCAPE '\\'"
|
|
predicates := []string{"LOWER(c.email)" + like, "LOWER(" + col + ")" + like}
|
|
pattern := "%" + escapeLikeLiteral(strings.ToLower(search)) + "%"
|
|
args = append(args, pattern, pattern)
|
|
for _, variant := range caseVariants(search) {
|
|
pattern := "%" + escapeLikeLiteral(variant) + "%"
|
|
predicates = append(predicates, "c.email"+like, col+like)
|
|
args = append(args, pattern, pattern)
|
|
}
|
|
clauses = append(clauses, "("+strings.Join(predicates, " OR ")+")")
|
|
}
|
|
return strings.Join(clauses, " AND "), args
|
|
}
|
|
|
|
func filteredAttachCandidates(q *gorm.DB, sourceID int, search, group string) *gorm.DB {
|
|
if filter, args := attachFilter(sourceID, search, group); filter != "" {
|
|
return q.Where(filter, args...)
|
|
}
|
|
return q
|
|
}
|
|
|
|
func writeClientScopeID(h hash.Hash, id int) {
|
|
var buf [8]byte
|
|
binary.BigEndian.PutUint64(buf[:], uint64(id))
|
|
_, _ = h.Write(buf[:])
|
|
}
|
|
|
|
func attachScopeHash(sourceID int, targets []int) hash.Hash {
|
|
h := sha256.New()
|
|
writeClientScopeID(h, sourceID)
|
|
writeClientScopeID(h, len(targets))
|
|
for _, id := range targets {
|
|
writeClientScopeID(h, id)
|
|
}
|
|
return h
|
|
}
|
|
|
|
func (s *ClientService) ListAttachCandidates(ctx context.Context, p AttachCandidateParams) (*AttachCandidatePage, error) {
|
|
if p.Page < 0 || p.Page > 1_000_000 || len(p.Search) > 256 || len(p.Group) > 256 ||
|
|
len(p.ScopeSearch) > 256 || len(p.ScopeGroup) > 256 {
|
|
return nil, fmt.Errorf("invalid client attachment page")
|
|
}
|
|
db := database.GetDB().WithContext(ctx)
|
|
var ids []int
|
|
if p.Targets != "" {
|
|
for part := range strings.SplitSeq(p.Targets, ",") {
|
|
id, err := strconv.Atoi(part)
|
|
if err != nil || id < 1 || len(ids) >= 100 {
|
|
return nil, fmt.Errorf("invalid attachment target ID")
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
targets, err := attachScope(db, p.SourceID, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp := &AttachCandidatePage{Items: []AttachCandidate{}, Groups: []string{}}
|
|
eligible := eligibleAttachCandidates(attachCandidates(db, p.SourceID), targets)
|
|
if p.Freeze {
|
|
if len(targets) == 0 {
|
|
return nil, fmt.Errorf("select attachment targets first")
|
|
}
|
|
h := attachScopeHash(p.SourceID, targets)
|
|
rows, err := filteredAttachCandidates(eligible, p.SourceID, p.Search, p.Group).Select("c.id").Order("c.id").Rows()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var id int
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
writeClientScopeID(h, id)
|
|
resp.Eligible++
|
|
resp.MaxID = id
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
writeClientScopeID(h, resp.MaxID)
|
|
resp.ScopeToken = strconv.Itoa(resp.MaxID) + "." + hex.EncodeToString(h.Sum(nil))
|
|
return resp, nil
|
|
}
|
|
if err := eligible.Count(&resp.Eligible).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if p.SourceID == 0 {
|
|
if err := attachCandidates(db, 0).Where("c.group_name <> ''").
|
|
Distinct("c.group_name").Order("c.group_name").Limit(100).Pluck("c.group_name", &resp.Groups).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
filtered := filteredAttachCandidates(attachCandidates(db, p.SourceID), p.SourceID, p.Search, p.Group)
|
|
if err := filtered.Count(&resp.Total).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
columns, args := "c.id, c.email, c.group_name AS \"group\", c.comment, c.enable, ", []any{}
|
|
if len(targets) == 0 {
|
|
columns += "false AS already_attached, "
|
|
} else {
|
|
columns += attachedToTargetsSQL + " AS already_attached, "
|
|
args = append(args, targets, len(targets))
|
|
}
|
|
if scope, scopeArgs := attachFilter(p.SourceID, p.ScopeSearch, p.ScopeGroup); scope == "" {
|
|
columns += "true AS in_scope"
|
|
} else {
|
|
columns += "(" + scope + ") AS in_scope"
|
|
args = append(args, scopeArgs...)
|
|
}
|
|
filtered = filtered.Select(columns, args...)
|
|
err = filtered.Order("c.id").Offset(max(0, p.Page-1) * attachCandidatePageSize).
|
|
Limit(attachCandidatePageSize).Scan(&resp.Items).Error
|
|
return resp, err
|
|
}
|
|
|
|
func (s *ClientService) BulkAttachSelection(ctx context.Context, inboundSvc *InboundService, targets []int, selection AttachSelection) (*AttachSelectionResult, bool, error) {
|
|
db := database.GetDB().WithContext(ctx)
|
|
targets, err := attachScope(db, selection.SourceID, targets)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
if len(targets) == 0 {
|
|
return nil, false, fmt.Errorf("select attachment targets first")
|
|
}
|
|
stale := fmt.Errorf("client attachment selection changed; clear the selection and select clients again")
|
|
q := eligibleAttachCandidates(attachCandidates(db, selection.SourceID), targets)
|
|
var records []*model.ClientRecord
|
|
switch selection.Mode {
|
|
case "explicit":
|
|
if len(selection.ClientIDs) == 0 || len(selection.ClientIDs) > 50_000 || selection.ScopeToken != "" ||
|
|
len(selection.ExcludedClientIDs) > 0 || selection.Search != "" || selection.Group != "" {
|
|
return nil, false, fmt.Errorf("invalid explicit client attachment selection")
|
|
}
|
|
ids := uniqueInts(selection.ClientIDs)
|
|
for _, chunk := range chunkInts(ids, 400) {
|
|
var part []*model.ClientRecord
|
|
if err := q.Session(&gorm.Session{}).Where("c.id IN ?", chunk).Find(&part).Error; err != nil {
|
|
return nil, false, err
|
|
}
|
|
if len(part) != len(chunk) {
|
|
return nil, false, stale
|
|
}
|
|
records = append(records, part...)
|
|
}
|
|
case "allMatching":
|
|
cutoff, digest, ok := strings.Cut(selection.ScopeToken, ".")
|
|
maxID, parseErr := strconv.Atoi(cutoff)
|
|
if !ok || parseErr != nil || maxID < 0 || len(digest) != sha256.Size*2 || len(selection.ClientIDs) > 0 ||
|
|
len(selection.ExcludedClientIDs) > 50_000 || len(selection.Search) > 256 || len(selection.Group) > 256 {
|
|
return nil, false, fmt.Errorf("invalid all-clients attachment selection")
|
|
}
|
|
excluded := make(map[int]struct{}, len(selection.ExcludedClientIDs))
|
|
for _, id := range selection.ExcludedClientIDs {
|
|
excluded[id] = struct{}{}
|
|
}
|
|
h := attachScopeHash(selection.SourceID, targets)
|
|
// Find runs the model's read hooks. The cutoff excludes newer clients.
|
|
var eligibleRecords []*model.ClientRecord
|
|
q = filteredAttachCandidates(q, selection.SourceID, selection.Search, selection.Group)
|
|
if err := q.Where("c.id <= ?", maxID).Order("c.id").Find(&eligibleRecords).Error; err != nil {
|
|
return nil, false, err
|
|
}
|
|
records = eligibleRecords[:0]
|
|
for _, record := range eligibleRecords {
|
|
writeClientScopeID(h, record.Id)
|
|
if _, skip := excluded[record.Id]; skip {
|
|
delete(excluded, record.Id)
|
|
continue
|
|
}
|
|
records = append(records, record)
|
|
}
|
|
writeClientScopeID(h, maxID)
|
|
if len(excluded) > 0 || hex.EncodeToString(h.Sum(nil)) != digest {
|
|
return nil, false, stale
|
|
}
|
|
default:
|
|
return nil, false, fmt.Errorf("invalid client attachment selection mode")
|
|
}
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, false, err
|
|
}
|
|
result, restart, err := s.bulkAttachRecords(inboundSvc, records, targets, &BulkAttachResult{})
|
|
if err != nil {
|
|
return nil, restart, err
|
|
}
|
|
return &AttachSelectionResult{Attached: len(result.Attached), Skipped: len(result.Skipped), Errors: len(result.Errors)}, restart, nil
|
|
}
|