mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 20:15:01 +03:00
e95d17a7db
Host rules could only be given to clients one by one, so giving a set of customers their own endpoints meant editing every client. A client group can now carry host rules, chosen when the group is created, from its edit dialog or from a quick-assign action on the Groups page. A subscription resolves a client's hosts in this order: the client's own host rules, then the host rules of its group, then the inbound's default pool. A group whose bound rules are all disabled or excluded from the requested format renders nothing for that inbound instead of falling back to the default pool. Binding a rule to a group narrows what its members get; other clients still see that rule in the default pool. Group names are now matched case-insensitively. On upgrade, labels that differ only in case (VIP and vip) are merged into one group, the first by name order. Adds POST /panel/api/clients/groups/:name/hosts and an optional hostGroupIds on group create and rename.
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package database
|
|
|
|
import (
|
|
"errors"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// EnsureClientGroup returns the canonical group row for name, creating it if
|
|
// absent. Matching is case-insensitive so casing cannot create a second group.
|
|
func EnsureClientGroup(tx *gorm.DB, name string) (*model.ClientGroup, error) {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" {
|
|
return nil, nil
|
|
}
|
|
if tx == nil {
|
|
tx = db
|
|
}
|
|
var group model.ClientGroup
|
|
err := tx.Where("LOWER(name) = LOWER(?)", name).Order("id ASC").First(&group).Error
|
|
if err == nil {
|
|
return &group, nil
|
|
}
|
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, err
|
|
}
|
|
group = model.ClientGroup{Name: name}
|
|
if err := tx.Create(&group).Error; err != nil {
|
|
// A concurrent writer may have created it between the lookup and insert.
|
|
var existing model.ClientGroup
|
|
if e := tx.Where("LOWER(name) = LOWER(?)", name).Order("id ASC").First(&existing).Error; e == nil {
|
|
return &existing, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &group, nil
|
|
}
|
|
|
|
// PruneHostGroupClientGroups drops bindings whose host group no longer has any
|
|
// hosts row (e.g. the inbound was deleted). Idempotent; runs on every start.
|
|
func PruneHostGroupClientGroups() error {
|
|
return PruneHostGroupClientGroupsTx(db)
|
|
}
|
|
|
|
// PruneHostGroupClientGroupsTx drops bindings to rules whose hosts are gone, so
|
|
// deleting an inbound cannot leave a group bound to a rule nobody can select.
|
|
func PruneHostGroupClientGroupsTx(tx *gorm.DB) error {
|
|
return tx.Exec(`DELETE FROM host_group_client_groups
|
|
WHERE host_group_id NOT IN (SELECT DISTINCT group_id FROM hosts)
|
|
OR client_group_id NOT IN (SELECT id FROM client_groups)`).Error
|
|
}
|
|
|
|
// EnsureClientGroupRows repairs group labels that have no client_groups row and
|
|
// rewrites case variants to the canonical row's spelling. Idempotent.
|
|
func EnsureClientGroupRows() error {
|
|
var names []string
|
|
if err := db.Model(&model.ClientRecord{}).
|
|
Where("TRIM(COALESCE(group_name, '')) <> ''").
|
|
Distinct().Pluck("group_name", &names).Error; err != nil {
|
|
return err
|
|
}
|
|
if len(names) == 0 {
|
|
return nil
|
|
}
|
|
sort.Strings(names)
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
for _, raw := range names {
|
|
name := strings.TrimSpace(raw)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
group, err := EnsureClientGroup(tx, name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if group == nil || group.Name == raw {
|
|
continue
|
|
}
|
|
if err := tx.Model(&model.ClientRecord{}).
|
|
Where("group_name = ?", raw).
|
|
UpdateColumn("group_name", group.Name).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|