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:
AmirRnz
2026-07-09 01:05:20 +03:30
committed by GitHub
parent f431e9cc03
commit 42690e1b8c
33 changed files with 1842 additions and 1108 deletions
+35 -25
View File
@@ -1029,26 +1029,26 @@ export const sections: readonly Section[] = [
method: 'GET',
path: '/panel/api/hosts/list',
summary: 'List every host across all inbounds, grouped by inbound then ordered by sort order.',
responseSchema: 'Host',
responseSchema: 'HostGroup',
responseSchemaArray: true,
},
{
method: 'GET',
path: '/panel/api/hosts/get/:id',
summary: 'Fetch a single host by ID.',
path: '/panel/api/hosts/get/:groupId',
summary: 'Fetch a single host group by Group ID.',
params: [
{ name: 'id', in: 'path', type: 'number', desc: 'Host ID.' },
{ name: 'groupId', in: 'path', type: 'string', desc: 'Host Group ID.' },
],
responseSchema: 'Host',
responseSchema: 'HostGroup',
},
{
method: 'GET',
path: '/panel/api/hosts/byInbound/:inboundId',
summary: "Fetch one inbound's hosts, ordered by sort order then id.",
summary: "Fetch one inbound's hosts, grouped by host group.",
params: [
{ name: 'inboundId', in: 'path', type: 'number', desc: 'Inbound ID.' },
],
responseSchema: 'Host',
responseSchema: 'HostGroup',
responseSchemaArray: true,
},
{
@@ -1060,54 +1060,64 @@ export const sections: readonly Section[] = [
{
method: 'POST',
path: '/panel/api/hosts/add',
summary: 'Create a host on an inbound. inboundId and remark are required; security defaults to "same" (inherit the inbound).',
body: '{\n "inboundId": 1,\n "remark": "cdn-front",\n "address": "cdn.example.com",\n "port": 8443,\n "security": "same",\n "sni": "",\n "tags": ["CDN"]\n}',
summary: 'Create a host group on inbounds.',
body: '{\n "inboundIds": [1],\n "remark": "cdn-front",\n "hosts": ["cdn.example.com"],\n "port": 8443,\n "security": "same",\n "tags": ["CDN"]\n}',
responseSchema: 'Host',
responseSchemaArray: true,
},
{
method: 'POST',
path: '/panel/api/hosts/update/:id',
summary: 'Replace a hosts content. The inbound and sort order are immutable here (use /reorder for ordering).',
path: '/panel/api/hosts/update/:groupId',
summary: 'Replace a host groups content.',
params: [
{ name: 'id', in: 'path', type: 'number', desc: 'Host ID.' },
{ name: 'groupId', in: 'path', type: 'string', desc: 'Host Group ID.' },
],
body: '{\n "inboundId": 1,\n "remark": "cdn-front",\n "address": "cdn.example.com",\n "port": 8443,\n "security": "same",\n "sni": "",\n "tags": ["CDN"]\n}',
body: '{\n "inboundIds": [1],\n "remark": "cdn-front",\n "hosts": ["cdn.example.com"],\n "port": 8443,\n "security": "same",\n "tags": ["CDN"]\n}',
responseSchema: 'Host',
responseSchemaArray: true,
},
{
method: 'POST',
path: '/panel/api/hosts/del/:id',
summary: 'Delete a host.',
path: '/panel/api/hosts/del/:groupId',
summary: 'Delete a host group.',
params: [
{ name: 'id', in: 'path', type: 'number', desc: 'Host ID.' },
{ name: 'groupId', in: 'path', type: 'string', desc: 'Host Group ID.' },
],
},
{
method: 'POST',
path: '/panel/api/hosts/setEnable/:id',
summary: 'Enable or disable a single host (disabled hosts are skipped in subscriptions).',
path: '/panel/api/hosts/setEnable/:groupId',
summary: 'Enable or disable a host group.',
params: [
{ name: 'id', in: 'path', type: 'number', desc: 'Host ID.' },
{ name: 'groupId', in: 'path', type: 'string', desc: 'Host Group ID.' },
],
body: '{\n "enable": true\n}',
},
{
method: 'POST',
path: '/panel/api/hosts/reorder',
summary: 'Set host sort order by the position of each id in the array.',
body: '{\n "ids": [3, 1, 2]\n}',
summary: 'Set host group sort order by the position of each groupId in the array.',
body: '{\n "ids": ["abc-123", "def-456"]\n}',
},
{
method: 'POST',
path: '/panel/api/hosts/bulk/add',
summary: 'Add a host group to inbounds (same as /add).',
body: '{\n "inboundIds": [1, 2],\n "hosts": ["cdn.example.com", "cdn2.example.com:443"],\n "remark": "Cloudflare CDN",\n "port": 0,\n "security": "same",\n "isDisabled": false\n}',
responseSchema: 'Host',
responseSchemaArray: true,
},
{
method: 'POST',
path: '/panel/api/hosts/bulk/setEnable',
summary: 'Enable or disable many hosts in one call.',
body: '{\n "ids": [1, 2, 3],\n "enable": false\n}',
summary: 'Enable or disable many host groups in one call.',
body: '{\n "ids": ["abc-123", "def-456"],\n "enable": false\n}',
},
{
method: 'POST',
path: '/panel/api/hosts/bulk/del',
summary: 'Delete many hosts in one call.',
body: '{\n "ids": [1, 2, 3]\n}',
summary: 'Delete many host groups in one call.',
body: '{\n "ids": ["abc-123", "def-456"]\n}',
},
],
},