diff --git a/electron/api/local.ts b/electron/api/local.ts new file mode 100644 index 0000000..4658e7f --- /dev/null +++ b/electron/api/local.ts @@ -0,0 +1,70 @@ +import {ipcMain} from "electron"; +import log from "electron-log"; + +const {exec, spawn} = require("child_process"); + +type LocalPort = { + protocol: string; + ip: string; + port: number; +} + +export const initLocalApi = () => { + const command = process.platform === 'win32' + ? 'netstat -a -n' + : 'netstat -tuln'; + + ipcMain.on("local.getLocalPorts", async (event, args) => { + log.info("开始获取本地端口") + // 执行命令 + exec(command, (error, stdout, stderr) => { + if (error) { + log.error(`getLocalPorts - error ${error.message}`) + return; + } + if (stderr) { + log.error(`getLocalPorts - stderr ${stderr}`) + return; + } + + log.debug(`sc ${stdout}`) + let ports = []; + if (stdout) { + ports = stdout.split('\r\n') + .filter(f => f.indexOf('TCP') > 0 || f.indexOf('UDP') > 0) + .map(m => { + const cols = m.split(' ') + .filter(f => f != '') + const local = cols[1] + const s = local.lastIndexOf(":") + let localIP = local.slice(0, s); + let localPort = local.slice(s - local.length + 1); + console.log(1) + // if (local.indexOf('[') == -1) { + // // ipv4 + // const tmp = cols[1].split(":") + // localIP = tmp[0] + // localPort = tmp[1] + // } else { + // // ipv6 + // console.log(1) + // } + + const singe: LocalPort = { + protocol: cols[0], + ip: localIP, + port: localPort + } + + return singe; + }) + } + + ports.sort((a, b) => a.port - b.port); + + event.reply("local.getLocalPorts.hook", { + data: ports + }); + }); + }); +} diff --git a/electron/main/index.ts b/electron/main/index.ts index 04c5c33..f042efe 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -7,10 +7,10 @@ import {initProxyApi} from "../api/proxy"; import {initFrpcApi, startFrpWorkerProcess, stopFrpcProcess} from "../api/frpc"; import {initLoggerApi} from "../api/logger"; import {initFileApi} from "../api/file"; -import {initUpdaterApi} from "../api/update"; import {getConfig} from "../storage/config"; import log from "electron-log"; import {initCommonApi} from "../api/common"; +import {initLocalApi} from "../api/local"; // The built directory structure // // ├─┬ dist-electron @@ -172,6 +172,7 @@ app.whenReady().then(() => { initLoggerApi(); initFileApi(); initCommonApi(); + initLocalApi(); // initUpdaterApi(win); }) }); diff --git a/src/views/config/index.vue b/src/views/config/index.vue index ba0b9a6..529cdd4 100644 --- a/src/views/config/index.vue +++ b/src/views/config/index.vue @@ -814,20 +814,22 @@ onUnmounted(() => { - + - + diff --git a/src/views/proxy/index.vue b/src/views/proxy/index.vue index 35d24cf..8a29380 100644 --- a/src/views/proxy/index.vue +++ b/src/views/proxy/index.vue @@ -5,6 +5,7 @@ import Breadcrumb from "@/layout/compoenets/Breadcrumb.vue"; import {ElMessage, FormInstance, FormRules} from "element-plus"; import {ipcRenderer} from "electron"; import {clone} from "@/utils/clone"; +import {useDebounceFn} from "@vueuse/core"; defineComponent({ name: "Proxy" @@ -20,6 +21,12 @@ type Proxy = { customDomains: string[]; }; +type LocalPort = { + protocol: string; + ip: string; + port: number; +} + /** * 代理列表 */ @@ -29,9 +36,13 @@ const proxys = ref>([]); */ const loading = ref({ list: 1, - form: 0 + form: 0, + localPorts: 1 }); +const localPorts = ref>([]); +const listPortsVisible = ref(false); + /** * 弹出层属性 */ @@ -170,6 +181,12 @@ const handleInitHook = () => { ipcRenderer.on("Proxy.updateProxy.hook", (event, args) => { InsertOrUpdateHook("修改成功", args); }); + + ipcRenderer.on("local.getLocalPorts.hook", (event, args) => { + loading.value.localPorts--; + localPorts.value = args.data; + console.log('本地端口', localPorts.value) + }) // ipcRenderer.on("Proxy.updateProxy.hook", (event, args) => { // loading.value.form--; // const { err } = args; @@ -216,6 +233,26 @@ const handleOpenUpdate = (proxy: Proxy) => { }; }; +const handleLoadLocalPorts = () => { + loading.value.localPorts = 1; + ipcRenderer.send("local.getLocalPorts") +} + + +const handleSelectLocalPort = useDebounceFn((port: number) => { + editForm.value.localPort = port; + handleCloseLocalPortDialog(); +}) + +const handleCloseLocalPortDialog = () => { + listPortsVisible.value = false; +} + +const handleOpenLocalPortDialog = () => { + listPortsVisible.value = true; + handleLoadLocalPorts(); +}; + onMounted(() => { handleInitHook(); handleLoadProxys(); @@ -226,6 +263,7 @@ onUnmounted(() => { ipcRenderer.removeAllListeners("Proxy.updateProxy.hook"); ipcRenderer.removeAllListeners("Proxy.deleteProxyById.hook"); ipcRenderer.removeAllListeners("Proxy.getProxys.hook"); + ipcRenderer.removeAllListeners("local.getLocalPorts.hook"); });