feat(sub): generate random IPs from CIDR host addresses (#6784)

Operators who front a server with an IP range (a CDN or anycast prefix) had to
list each address as its own host. A host address may now be a CIDR, and every
subscription fetch expands it into up to randomCount (1–100, default 1) unique
random host IPs from the range, IPv4 and IPv6 alike, skipping the network and
broadcast addresses. The panel's own previews, QR codes and tunnel config
exports (WireGuard, AmneziaWG, TUIC, MTProto) expand it the same way, and the
externalProxy importer writes the model's default count so the re-sync still
recognises untouched imported hosts.
This commit is contained in:
Pejman Yousefi
2026-10-24 06:43:44 +03:30
committed by GitHub
parent f6a233220a
commit 40cea0c2fe
33 changed files with 886 additions and 8 deletions
+1
View File
@@ -447,4 +447,5 @@ type HostGroup struct {
MihomoIpVersion string `json:"mihomoIpVersion" validate:"omitempty,oneof=dual ipv4 ipv6 ipv4-prefer ipv6-prefer"`
MihomoX25519 bool `json:"mihomoX25519"`
ShuffleHost bool `json:"shuffleHost"`
RandomCount int `json:"randomCount" validate:"omitempty,min=1,max=100"`
}
+15
View File
@@ -61,6 +61,7 @@ func newHostGroup(h *model.Host, groupId string) *entity.HostGroup {
MihomoIpVersion: h.MihomoIpVersion,
MihomoX25519: h.MihomoX25519,
ShuffleHost: h.ShuffleHost,
RandomCount: h.RandomCount,
}
}
@@ -91,6 +92,9 @@ func groupHosts(hosts []*model.Host) []*entity.HostGroup {
if h.SortOrder < g.SortOrder {
g.SortOrder = h.SortOrder
}
if h.RandomCount > g.RandomCount {
g.RandomCount = h.RandomCount
}
}
res := make([]*entity.HostGroup, 0, len(orderedGroupIds))
@@ -150,6 +154,7 @@ func buildHostRows(groupId string, req *entity.HostGroup) []*model.Host {
MihomoIpVersion: req.MihomoIpVersion,
MihomoX25519: req.MihomoX25519,
ShuffleHost: req.ShuffleHost,
RandomCount: clampRandomCount(req.RandomCount),
})
}
}
@@ -363,3 +368,13 @@ func parseHostAndPort(hostStr string, defaultPort int) (string, int) {
}
return addr, defaultPort
}
func clampRandomCount(n int) int {
if n <= 0 {
return 1
}
if n > 100 {
return 100
}
return n
}
+1
View File
@@ -2368,6 +2368,7 @@
"mihomoIpVersion": "إصدار IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "خلط المضيف",
"randomCount": "عدد عناوين IP العشوائية",
"tags": "وسوم",
"nodeGuids": "النودز",
"excludeFromSubTypes": "استبعاد من الصيغ",
+1
View File
@@ -1106,6 +1106,7 @@
"mihomoIpVersion": "IP version",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Shuffle host",
"randomCount": "Random IP count",
"tags": "Tags",
"nodeGuids": "Nodes",
"excludeFromSubTypes": "Exclude from formats",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "Versión de IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Barajar host",
"randomCount": "Cantidad de IPs aleatorias",
"tags": "Etiquetas",
"nodeGuids": "Nodos",
"excludeFromSubTypes": "Excluir de formatos",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "نسخه IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "درهم‌سازی میزبان",
"randomCount": "تعداد IP تصادفی",
"tags": "برچسب‌ها",
"nodeGuids": "نودها",
"excludeFromSubTypes": "حذف از فرمت‌ها",
+1
View File
@@ -2368,6 +2368,7 @@
"mihomoIpVersion": "Versi IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Acak host",
"randomCount": "Jumlah IP acak",
"tags": "Tag",
"nodeGuids": "Node",
"excludeFromSubTypes": "Kecualikan dari format",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "IP バージョン",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "ホストをシャッフル",
"randomCount": "ランダム IP 数",
"tags": "タグ",
"nodeGuids": "ノード",
"excludeFromSubTypes": "形式から除外",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "Versão de IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Embaralhar host",
"randomCount": "Contagem de IPs aleatórios",
"tags": "Tags",
"nodeGuids": "Nós",
"excludeFromSubTypes": "Excluir dos formatos",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "Версия IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Перемешивать хост",
"randomCount": "Количество случайных IP",
"tags": "Теги",
"nodeGuids": "Узлы",
"excludeFromSubTypes": "Исключить из форматов",
+1
View File
@@ -2368,6 +2368,7 @@
"mihomoIpVersion": "IP sürümü",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Host'u karıştır",
"randomCount": "Rastgele IP sayısı",
"tags": "Etiketler",
"nodeGuids": "Düğümler",
"excludeFromSubTypes": "Formatlardan hariç tut",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "Версія IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Перемішувати host",
"randomCount": "Кількість випадкових IP",
"tags": "Теги",
"nodeGuids": "Вузли",
"excludeFromSubTypes": "Виключити з форматів",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "Phiên bản IP",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "Xáo trộn host",
"randomCount": "Số lượng IP ngẫu nhiên",
"tags": "Tag",
"nodeGuids": "Nút",
"excludeFromSubTypes": "Loại trừ khỏi định dạng",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "IP 版本",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "随机打乱 Host",
"randomCount": "随机 IP 数量",
"tags": "标签",
"nodeGuids": "节点",
"excludeFromSubTypes": "从格式中排除",
+1
View File
@@ -2369,6 +2369,7 @@
"mihomoIpVersion": "IP 版本",
"mihomoX25519": "Mihomo X25519",
"shuffleHost": "隨機排序 Host",
"randomCount": "隨機 IP 數量",
"tags": "標籤",
"nodeGuids": "節點",
"excludeFromSubTypes": "從格式中排除",