Skip to content

Commit

Permalink
Merge pull request #118 from xianyunleo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
xianyunleo authored Jun 26, 2024
2 parents b98f678 + 0f38af4 commit 58c08e4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 57 deletions.
4 changes: 2 additions & 2 deletions electron.vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand All @@ -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"
}
}
81 changes: 35 additions & 46 deletions src/main/utils/TcpProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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<number|null>}
*/
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<null|string>}
*/
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
}
}
}
3 changes: 2 additions & 1 deletion src/renderer/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/renderer/views/Tool.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
</script>
Expand Down

0 comments on commit 58c08e4

Please sign in to comment.