diff --git a/electron.vite.config.js b/electron.vite.config.js index 54f82adc..4d3363db 100644 --- a/electron.vite.config.js +++ b/electron.vite.config.js @@ -6,8 +6,8 @@ import Components from 'unplugin-vue-components/vite' import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers' import { builtinModules } from 'module' -const externals = ['fix-path', 'electron-store', '@electron/remote', 'extract-zip', 'got', 'hmc-win32', - ...builtinModules, ...builtinModules.map((m) => `node:${m}`)] +const externals = ['fix-path', 'electron-store', '@electron/remote', 'extract-zip', 'got', + 'hmc-win32', 'net-win32', ...builtinModules, ...builtinModules.map((m) => `node:${m}`)] export default defineConfig({ main: { diff --git a/package.json b/package.json index f2a1df5d..a85f8c58 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "eserver", "productName": "EServer", - "version": "3.8.1", + "version": "3.9.0", "description": "EServer", "main": "./out/main/index.js", "author": "xianyunleo", @@ -28,7 +28,8 @@ "extract-zip": "^2.0.1", "fix-path": "^3.0.0", "got": "^11.8.6", - "hmc-win32": "~1.4.6", + "hmc-win32": "~1.4.92", + "net-win32": "^1.0.1", "semver-diff": "3.1.1", "throttle-debounce": "^5.0.0" }, @@ -53,8 +54,8 @@ "unplugin-vue-components": "^0.25.2", "vite": "^5.2.7", "vite-plugin-commonjs-externals": "^0.1.4", - "vue-router": "^4.3.0", "vue": "~3.4.21", - "vue-i18n": "^9.5.0" + "vue-i18n": "^9.5.0", + "vue-router": "^4.3.0" } } diff --git a/src/main/utils/TcpProcess.js b/src/main/utils/TcpProcess.js index 39858599..2f94d593 100644 --- a/src/main/utils/TcpProcess.js +++ b/src/main/utils/TcpProcess.js @@ -9,11 +9,11 @@ export default class TcpProcess { static async getList() { if (isMacOS) { - return await this.getListForMacOS(); + return await this.getListForMacOS() } else if (isWindows) { - return await this.getListForWindows(); + return await this.getListForWindows() } - return []; + return [] } static async getListForMacOS() { @@ -44,7 +44,7 @@ export default class TcpProcess { } } - static async getListForWindows() { + static async getListForWindowsByShell() { let commandStr = ` Get-NetTCPConnection -State Listen|select-Object OwningProcess,LocalAddress,LocalPort`; commandStr += ', @{n="Name" ;e= {(Get-Process -Id $_.OwningProcess).Name } } , @{n="Path" ;e= {(Get-Process -Id $_.OwningProcess).Path } }'; commandStr += ' | fl | Out-String -Width 999'; @@ -76,65 +76,54 @@ export default class TcpProcess { } } + static async getListForWindows() { + const net = require('net-win32') + const hmc = require('hmc-win32') + try { + let list = await net.getConnectNetListAsync(true, false, true, false) + list = await list.filterAsync((item) => item.state === 'LISTEN') + //由于hmc的promise暂不支持并发,所以先用for of + const result = [] + for (const item of list) { + const { pid, ip, port } = item + const name = await hmc.getProcessName2(pid) + const path = await hmc.getProcessFilePath2(pid) + result.push({ pid, ip, port, name, path }) + } + + return result + } catch (e) { + return [] + } + } /** * - * @param port + * @param port {number} * @returns {Promise} */ static async getPidByPort(port) { try { - let commandStr, resStr, pid; - - if (isWindows) { - commandStr = `(Get-NetTCPConnection -LocalPort ${port} -State Listen).OwningProcess`; - resStr = await Shell.exec(commandStr, {shell: 'powershell'}); - } else { - commandStr = `lsof -t -sTCP:LISTEN -i:${port}`; - resStr = await Shell.exec(commandStr); - } - - if (!resStr) { - return null; - } - pid = resStr.trim().split("\n")[0]; - return parseInt(pid); + port = parseInt(port) + const net = require('net-win32') + return await net.getTCPv4PortProcessIDAsync(port) } catch { - return null; + return null } } /** - * @param port + * @param port {number} * @returns {Promise} */ static async getPathByPort(port) { try { - let commandStr, resStr, path; - - if (isWindows) { - commandStr = `(Get-Process -Id (Get-NetTCPConnection -LocalPort ${port} -State Listen).OwningProcess).Path"`; - resStr = await Shell.exec(commandStr, {shell: 'powershell'}); - } else { - commandStr = `lsof -t -sTCP:LISTEN -i:${port}|head -n 1|xargs lsof -a -w -d txt -Fn -p|awk 'NR==3{print}'|sed "s/n//"`; - resStr = await Shell.exec(commandStr); - } - - if (!resStr) { - return null; - } - path = resStr.trim().split("\n")[0]; - return path.trim(); + port = parseInt(port) + const pid = TcpProcess.getPidByPort(port) + const hmc = require('hmc-win32') + return await hmc.getProcessFilePath2(pid) } catch { - return null; - } - } - - - static async killByPort(port) { - let pid = await TcpProcess.getPidByPort(port); - if (pid) { - await ProcessExtend.kill(pid); + return null } } } diff --git a/src/renderer/views/Home.vue b/src/renderer/views/Home.vue index 5aa99658..5f9238d4 100644 --- a/src/renderer/views/Home.vue +++ b/src/renderer/views/Home.vue @@ -264,7 +264,8 @@ const startServerClick = async (item) => { if (item.ServerPort == 80) { await ProcessExtend.killWebServer() } else { - await TcpProcess.killByPort(item.ServerPort) + const pid = await TcpProcess.getPidByPort(item.ServerPort) + await ProcessExtend.kill(pid) } } diff --git a/src/renderer/views/Tool.vue b/src/renderer/views/Tool.vue index 1d5c1896..8aecdbd0 100644 --- a/src/renderer/views/Tool.vue +++ b/src/renderer/views/Tool.vue @@ -97,10 +97,6 @@ function mysqlResetPwd() { const tcpProcessListModalShow = ref(false) function tcpProcessList() { - if (isWindows && OS.getMajorVersion() <= 6.1) { - MessageBox.error(t('Your system version is too low, this function is not available!'), t('This feature is not available!')) - return - } tcpProcessListModalShow.value = true }