Skip to content

Commit 51252a1

Browse files
authored
feat: support port usage check for firewall port range rules (#12950)
1 parent 375824f commit 51252a1

1 file changed

Lines changed: 43 additions & 29 deletions

File tree

  • frontend/src/views/host/firewall/port

frontend/src/views/host/firewall/port/index.vue

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,17 @@
8282
<el-table-column :label="$t('commons.table.port')" :min-width="70" prop="port" />
8383
<el-table-column :label="$t('commons.table.status')" :min-width="120">
8484
<template #default="{ row }">
85-
<div v-if="isSinglePort(row.port)">
86-
<el-tag type="success" v-if="row.usedStatus">
87-
{{ $t('firewall.used') + ' (' + row.usedStatus + ')' }}
88-
<el-icon
89-
v-if="row.processInfo"
90-
@click="showProcessDetail(row.processInfo.PID)"
91-
style="margin-left: 4px; cursor: pointer; vertical-align: middle"
92-
>
93-
<Expand />
94-
</el-icon>
95-
</el-tag>
96-
<el-tag type="info" v-else>{{ $t('firewall.unUsed') }}</el-tag>
97-
</div>
98-
<span v-else>-</span>
85+
<el-tag type="success" v-if="row.usedStatus">
86+
{{ $t('firewall.used') + ' (' + row.usedStatus + ')' }}
87+
<el-icon
88+
v-if="row.processInfo"
89+
@click="showProcessDetail(row.processInfo.PID)"
90+
style="margin-left: 4px; cursor: pointer; vertical-align: middle"
91+
>
92+
<Expand />
93+
</el-icon>
94+
</el-tag>
95+
<el-tag type="info" v-else>{{ $t('firewall.unUsed') }}</el-tag>
9996
</template>
10097
</el-table-column>
10198
<el-table-column :min-width="80" :label="$t('firewall.strategy')" prop="strategy">
@@ -211,8 +208,28 @@ const extractPortsFromObject = (portObj: { [key: string]: {} }): number[] => {
211208
.filter((port) => !isNaN(port));
212209
};
213210
214-
const isSinglePort = (portStr: string): boolean => {
215-
return portStr.indexOf('-') === -1 && portStr.indexOf(':') === -1 && portStr.indexOf(',') === -1;
211+
const isPortInRule = (rulePort: string, port: number): boolean => {
212+
const segments = rulePort.split(',');
213+
for (const segment of segments) {
214+
const portSegment = segment.trim();
215+
if (!portSegment) {
216+
continue;
217+
}
218+
219+
const rangeDelimiter = portSegment.includes('-') && !portSegment.startsWith('-') ? '-' : ':';
220+
if (portSegment.includes(rangeDelimiter) && !portSegment.startsWith(rangeDelimiter)) {
221+
const [startPort, endPort] = portSegment.split(rangeDelimiter).map((item) => parseInt(item.trim()));
222+
if (!isNaN(startPort) && !isNaN(endPort) && port >= startPort && port <= endPort) {
223+
return true;
224+
}
225+
continue;
226+
}
227+
228+
if (parseInt(portSegment) === port) {
229+
return true;
230+
}
231+
}
232+
return false;
216233
};
217234
218235
const loadListeningProcesses = async () => {
@@ -221,20 +238,17 @@ const loadListeningProcesses = async () => {
221238
listeningProcesses.value = res.data || [];
222239
223240
for (const item of data.value) {
224-
if (!item.usedStatus && isSinglePort(item.port)) {
225-
const portNum = parseInt(item.port.trim());
226-
if (!isNaN(portNum)) {
227-
const protocolNum =
228-
item.protocol.toLowerCase() === 'tcp' ? 1 : item.protocol.toLowerCase() === 'udp' ? 2 : 0;
241+
if (!item.processInfo) {
242+
const protocolNum =
243+
item.protocol.toLowerCase() === 'tcp' ? 1 : item.protocol.toLowerCase() === 'udp' ? 2 : 0;
229244
230-
for (const proc of listeningProcesses.value) {
231-
if (proc.Protocol === protocolNum) {
232-
const procPorts = extractPortsFromObject(proc.Port);
233-
if (procPorts.includes(portNum)) {
234-
item.usedStatus = proc.Name;
235-
item.processInfo = proc;
236-
break;
237-
}
245+
for (const proc of listeningProcesses.value) {
246+
if (proc.Protocol === protocolNum) {
247+
const procPorts = extractPortsFromObject(proc.Port);
248+
if (procPorts.some((port) => isPortInRule(item.port, port))) {
249+
item.usedStatus = proc.Name;
250+
item.processInfo = proc;
251+
break;
238252
}
239253
}
240254
}

0 commit comments

Comments
 (0)