mac端口快捷选择

This commit is contained in:
刘嘉伟 2024-08-11 18:33:30 +08:00
parent ef1b8c186e
commit 0fdfe4d169

View File

@ -12,7 +12,7 @@ type LocalPort = {
export const initLocalApi = () => { export const initLocalApi = () => {
const command = process.platform === 'win32' const command = process.platform === 'win32'
? 'netstat -a -n' ? 'netstat -a -n'
: 'netstat -tuln'; : 'netstat -an | grep LISTEN\n';
ipcMain.on("local.getLocalPorts", async (event, args) => { ipcMain.on("local.getLocalPorts", async (event, args) => {
log.info("开始获取本地端口") log.info("开始获取本地端口")
@ -30,37 +30,67 @@ export const initLocalApi = () => {
log.debug(`sc ${stdout}`) log.debug(`sc ${stdout}`)
let ports = []; let ports = [];
if (stdout) { if (stdout) {
ports = stdout.split('\r\n') if (process.platform === 'win32') {
.filter(f => f.indexOf('TCP') > 0 || f.indexOf('UDP') > 0) // window
.map(m => { ports = stdout.split('\r\n')
const cols = m.split(' ') .filter(f => f.indexOf('TCP') > 0 || f.indexOf('UDP') > 0)
.filter(f => f != '') .map(m => {
const local = cols[1] const cols = m.split(' ')
const s = local.lastIndexOf(":") .filter(f => f != '')
let localIP = local.slice(0, s); const local = cols[1]
let localPort = local.slice(s - local.length + 1); const s = local.lastIndexOf(":")
console.log(1) let localIP = local.slice(0, s);
// if (local.indexOf('[') == -1) { let localPort = local.slice(s - local.length + 1);
// // ipv4 console.log(1)
// const tmp = cols[1].split(":") // if (local.indexOf('[') == -1) {
// localIP = tmp[0] // // ipv4
// localPort = tmp[1] // const tmp = cols[1].split(":")
// } else { // localIP = tmp[0]
// // ipv6 // localPort = tmp[1]
// console.log(1) // } else {
// } // // ipv6
// console.log(1)
// }
const singe: LocalPort = { const singe: LocalPort = {
protocol: cols[0], protocol: cols[0],
ip: localIP, ip: localIP,
port: localPort port: localPort
} }
return singe;
})
} else if (process.platform === 'darwin') {
// mac
ports = stdout.split('\n')
.filter(m => {
const cols = m.split(' ')
.filter(f => f != '')
const local = cols[3]
return local
})
.map(m => {
const cols = m.split(' ')
.filter(f => f != '')
const local = cols[3]
console.log(local, '1')
const s = local.lastIndexOf(".")
let localIP = local.slice(0, s);
let localPort = local.slice(s - local.length + 1);
const singe: LocalPort = {
protocol: cols[0],
ip: localIP,
port: localPort
}
return singe;
})
// .filter(f => f.indexOf('TCP') > 0 || f.indexOf('UDP') > 0)
}
return singe;
})
} }
ports.sort((a, b) => a.port - b.port); ports.sort((a, b) => a.port - b.port);
event.reply("local.getLocalPorts.hook", { event.reply("local.getLocalPorts.hook", {
data: ports data: ports