Files
mirror-3x-ui/internal/sub/host_order.go
T
Nethmina Yasas e95d17a7db feat(groups): assign host rules from client groups (#7064)
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.
2026-11-25 15:51:43 +03:30

128 lines
3.7 KiB
Go

package sub
import (
"cmp"
"fmt"
"slices"
"sort"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
type hostEntryOrder struct {
sortOrder, inboundSort, inboundID, hostID int
remark, groupID string
}
type orderedSubEntry[T any] struct {
order *hostEntryOrder
value T
}
// Group ties match the Hosts page, then inbound order and host row ID.
// Stable sorting preserves emission order inside each host.
func hostOrderLess(a, b *hostEntryOrder) bool {
if a == nil || b == nil {
return a != nil && b == nil
}
for _, c := range []int{
cmp.Compare(a.sortOrder, b.sortOrder), cmp.Compare(a.remark, b.remark), cmp.Compare(a.groupID, b.groupID),
cmp.Compare(a.inboundSort, b.inboundSort), cmp.Compare(a.inboundID, b.inboundID), cmp.Compare(a.hostID, b.hostID),
} {
if c != 0 {
return c < 0
}
}
return false
}
func orderedSubValues[T any](entries []orderedSubEntry[T]) []T {
sort.SliceStable(entries, func(i, j int) bool { return hostOrderLess(entries[i].order, entries[j].order) })
var out []T
for _, entry := range entries {
out = append(out, entry.value)
}
return out
}
func (s *SubService) loadSubscriptionHosts(inbounds []*model.Inbound) error {
s.hostsByInbound = make(map[int][]*model.Host, len(inbounds))
s.hostsLoaded = make(map[int]bool, len(inbounds))
s.hostGroupOrders = make(map[string]hostEntryOrder)
for _, inbound := range inbounds {
s.hostsLoaded[inbound.Id] = true
}
ids := make([]int, len(inbounds))
for i, inbound := range inbounds {
ids[i] = inbound.Id
}
for lo := 0; lo < len(ids); lo += 400 {
var rows []*model.Host
if err := database.GetDB().Where("inbound_id IN ?", ids[lo:min(lo+400, len(ids))]).Order("inbound_id ASC, sort_order ASC, id ASC").Find(&rows).Error; err != nil {
return err
}
for _, row := range rows {
s.hostsByInbound[row.InboundId] = append(s.hostsByInbound[row.InboundId], row)
key := hostOrderGroupID(row)
if order, exists := s.hostGroupOrders[key]; exists {
order.sortOrder = min(order.sortOrder, row.SortOrder)
s.hostGroupOrders[key] = order
} else {
s.hostGroupOrders[key] = hostEntryOrder{sortOrder: row.SortOrder, remark: row.Remark, groupID: key}
}
}
}
return nil
}
type subscriptionHostVariant struct {
host *model.Host
order *hostEntryOrder
}
func hostOrderGroupID(h *model.Host) string {
if h.GroupId != "" {
return h.GroupId
}
return fmt.Sprintf("fallback_%d", h.Id)
}
// subscriptionHostVariants returns one variant per enabled host not excluded from format, a single
// host-less variant for an inbound with no enabled host, and none when all its hosts are excluded.
func (s *SubService) subscriptionHostVariants(inbound *model.Inbound, format string) []subscriptionHostVariant {
var variants []subscriptionHostVariant
enabled := 0
for _, h := range s.hostsForInbound(inbound) {
if h.IsDisabled {
continue
}
enabled++
if slices.Contains(h.ExcludeFromSubTypes, format) {
continue
}
order := s.hostGroupOrders[hostOrderGroupID(h)]
order.inboundSort, order.inboundID, order.hostID = inbound.SubSortIndex, inbound.Id, h.Id
variants = append(variants, subscriptionHostVariant{h, &order})
}
if enabled == 0 {
return []subscriptionHostVariant{{}}
}
return variants
}
func (v subscriptionHostVariant) endpoint(s *SubService, inbound *model.Inbound) []map[string]any {
if v.host == nil {
return nil
}
return s.hostEndpointMaps(v.host, s.resolveInboundAddress(inbound), inbound.Port)
}
func (v subscriptionHostVariant) inbound(s *SubService, inbound *model.Inbound) *model.Inbound {
clone := *inbound
if ep := v.endpoint(s, inbound); len(ep) > 0 {
injectExternalProxy(&clone, ep)
}
return &clone
}