mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-08-29 14:37:13 +00:00
feat(hosts): bulk-add multiple hosts to multiple inbounds (#5677)
* feat(hosts): bulk-add multiple hosts to multiple inbounds Allow users to select multiple inbound IDs and enter multiple host addresses (with optional per-host port override) in a single form submission. - Add BulkAddHostReq entity and POST /panel/api/hosts/bulk/add endpoint - Add AddHostsBulk service with GORM transaction safety - Add parseHostAndPort helper (IPv4, bracketed/bracketless IPv6, port) - Update HostFormModal to multi-select inbounds and tag-input hosts - Wire bulkCreate mutation in HostsPage with existing-host suggestions - Register endpoint in api-docs/endpoints.ts and regenerate OpenAPI/Zod * feat(hosts): group override records by group_id and support group editing * fix: import Popover in HostList * fix: use messageApi in HostFormModal * fix(hosts): resolve 4 bugs found in host-group code review - fix(schema): allow empty hosts array in BulkAddHostSchema so users can save a host without an address (inherits inbound endpoint). The old .min(1) was never enforced at runtime since the schema is only used for type inference, but the type was incorrect. - fix(service): validate new inbound IDs in UpdateHostGroup before deleting old rows, matching the same check already present in AddHostGroup. Prevents orphaned host rows when an invalid inbound ID is supplied on edit. - fix(service): replace full-table scan in GetHostsByInbound with two targeted queries (DISTINCT group_id WHERE inbound_id=?, then WHERE group_id IN ?) to avoid loading every host in the DB. - fix(mutations): remove unused createMut / create export from useHostMutations. The /hosts/add endpoint is identical to /hosts/bulk/add; only bulkCreate is used by the UI. * fix(hosts): address code review feedback (optimize bulk inserts, add validation tests, and remove comments) * fix(fmt): apply gofumpt formatting to model.go and db.go The previous merge commit incorrectly applied gofmt (tab-aligned) to these files. The repository's golangci config requires gofumpt+goimports which produces space-aligned struct fields. This commit restores the correct gofumpt formatting that matches upstream/main. * chore(frontend): regenerate API schemas and update lockfile * fix * refactor(hosts): dedupe host-group service and tidy frontend AddHostGroup and UpdateHostGroup shared an identical ~35-field model.Host construction and hand-rolled transaction boilerplate (tx.Begin plus a committed flag plus a deferred recover/rollback). Extract buildHostRows, validateInboundsExist and formatHostAddr, and run every mutation through db.Transaction. groupHosts collapses its duplicated address/port formatting and create/append fork into one path using slices.Contains. Behavior-preserving: host.go drops ~90 lines with the existing service/controller tests green. Frontend: drop the Partial union and two as-casts in HostsPage.onSave (the modal always passes a full BulkAddHostValues), and remove the movable index map in HostList in favor of the table render index arg. --------- Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
This commit is contained in:
@@ -2,29 +2,18 @@ import { z } from 'zod';
|
||||
|
||||
import { AlpnSchema, UtlsFingerprintSchema } from '@/schemas/protocols/security/tls';
|
||||
|
||||
// A Host is a per-inbound override endpoint: at subscription time each enabled
|
||||
// host renders one extra share link/proxy with its own address/port/TLS, etc.,
|
||||
// superseding the legacy externalProxy array. The form schema mirrors the field
|
||||
// logic of schemas/protocols/stream/external-proxy.ts and reuses the shared
|
||||
// ALPN / uTLS primitives.
|
||||
|
||||
export const HostSecuritySchema = z.enum(['same', 'tls', 'none', 'reality']);
|
||||
export type HostSecurity = z.infer<typeof HostSecuritySchema>;
|
||||
|
||||
export const MihomoIpVersionSchema = z.enum(['dual', 'ipv4', 'ipv6', 'ipv4-prefer', 'ipv6-prefer']);
|
||||
export const SubTypeSchema = z.enum(['raw', 'json', 'clash']);
|
||||
|
||||
// Tags are short uppercase identifiers (≤10 tags, each ≤36 chars). Enforced on
|
||||
// the frontend; the backend stores them verbatim.
|
||||
const HostTagSchema = z.string().regex(/^[A-Z0-9_:]+$/, 'pages.hosts.toasts.badTag').max(36);
|
||||
|
||||
// HostFormValues is what the form edits and POSTs.
|
||||
export const HostFormSchema = z.object({
|
||||
id: z.number().optional(),
|
||||
inboundId: z.number().int().positive(),
|
||||
sortOrder: z.number().int().default(0),
|
||||
// Remark may contain {{VAR}} template tokens expanded per client at
|
||||
// subscription time, so the stored template gets a generous cap.
|
||||
remark: z.string().trim().min(1).max(256),
|
||||
serverDescription: z.string().max(64).default(''),
|
||||
isDisabled: z.boolean().default(false),
|
||||
@@ -46,8 +35,6 @@ export const HostFormSchema = z.object({
|
||||
overrideSniFromAddress: z.boolean().default(false),
|
||||
keepSniBlank: z.boolean().default(false),
|
||||
pinnedPeerCertSha256: z.array(z.string()).default([]),
|
||||
// Comma-separated cert names (xray `vcn`). Legacy rows stored a boolean here;
|
||||
// coerce any stray bool to '' so old data loads cleanly.
|
||||
verifyPeerCertByName: z.preprocess(
|
||||
(v) => (typeof v === 'boolean' ? '' : v),
|
||||
z.string().default(''),
|
||||
@@ -58,7 +45,6 @@ export const HostFormSchema = z.object({
|
||||
muxParams: z.string().default(''),
|
||||
sockoptParams: z.string().default(''),
|
||||
finalMask: z.string().default(''),
|
||||
// Single value 0-65535 baked into the subscription UUID's 3rd group. Empty = none.
|
||||
vlessRoute: z
|
||||
.string()
|
||||
.trim()
|
||||
@@ -69,8 +55,6 @@ export const HostFormSchema = z.object({
|
||||
|
||||
excludeFromSubTypes: z.array(SubTypeSchema).default([]),
|
||||
|
||||
// Visual-only assignment of nodes that resolve from this host (stored, not yet
|
||||
// wired into routing).
|
||||
nodeGuids: z.array(z.string()).default([]),
|
||||
|
||||
mihomoIpVersion: z.preprocess(
|
||||
@@ -82,18 +66,16 @@ export const HostFormSchema = z.object({
|
||||
});
|
||||
export type HostFormValues = z.infer<typeof HostFormSchema>;
|
||||
|
||||
// HostRecord is the loose list/read projection from /panel/api/hosts. Slice and
|
||||
// free-JSON fields tolerate the backend serializing nil as null.
|
||||
export const HostRecordSchema = z.object({
|
||||
id: z.number(),
|
||||
inboundId: z.number(),
|
||||
groupId: z.string(),
|
||||
inboundIds: z.array(z.number()),
|
||||
hosts: z.array(z.string()),
|
||||
sortOrder: z.number().optional(),
|
||||
remark: z.string().optional(),
|
||||
serverDescription: z.string().optional(),
|
||||
isDisabled: z.boolean().optional(),
|
||||
isHidden: z.boolean().optional(),
|
||||
tags: z.array(z.string()).nullish(),
|
||||
address: z.string().optional(),
|
||||
port: z.number().optional(),
|
||||
security: z.string().optional(),
|
||||
sni: z.string().optional(),
|
||||
@@ -123,3 +105,9 @@ export const HostRecordSchema = z.object({
|
||||
export type HostRecord = z.infer<typeof HostRecordSchema>;
|
||||
|
||||
export const HostListSchema = z.array(HostRecordSchema);
|
||||
|
||||
export const BulkAddHostSchema = HostFormSchema.omit({ inboundId: true, address: true }).extend({
|
||||
inboundIds: z.array(z.number().int().positive()).min(1),
|
||||
hosts: z.array(z.string()).default([]),
|
||||
});
|
||||
export type BulkAddHostValues = z.infer<typeof BulkAddHostSchema>;
|
||||
|
||||
Reference in New Issue
Block a user