From 284617f60a05606b6de529e7670dd6621a37c556 Mon Sep 17 00:00:00 2001 From: "WIN-87ES3P38OPV\\EDY" Date: Tue, 11 Aug 2026 16:17:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BD=91=E7=BB=9C=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 2 ++ preload.js | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index fad6344..fa17332 100644 --- a/main.js +++ b/main.js @@ -5,6 +5,8 @@ const { SensorController } = require('./src/business/sensor-controller') const { UsbSerialBridge } = require('./src/business/usb-serial-bridge') const pkg = require('./package.json') +if (require('electron-squirrel-startup')) app.quit() + const cacheDir = app.getPath('userData') const cacheFile = path.join(cacheDir, 'app-cache.json') const fileRoot = path.join(cacheDir, 'files') diff --git a/preload.js b/preload.js index 876cbc5..f4b1ac1 100644 --- a/preload.js +++ b/preload.js @@ -10,6 +10,7 @@ const { contextBridge, ipcRenderer } = require('electron') const nodeNet = require('net') const dns = require('dns') +const { execFile } = require('child_process') function tryConnect(host, port, timeout) { return new Promise((resolve) => { @@ -22,17 +23,41 @@ function tryConnect(host, port, timeout) { }) } +function normalizePingHost(input) { + const raw = String(input || '').trim() + if (!raw) return '' + try { + return new URL(raw).hostname + } catch {} + return raw.replace(/^https?:\/\//i, '').split('/')[0].split(':')[0] +} + +function trySystemPing(host, timeout) { + return new Promise((resolve) => { + const wait = Math.max(1000, Number(timeout) || 2000) + execFile('ping', ['-n', '1', '-w', String(wait), host], { windowsHide: true }, (err) => { + resolve(!err) + }) + }) +} + const net = { async ping(host, timeout) { + const target = normalizePingHost(host) + if (!target) return false + const wait = Math.max(1000, Number(timeout) || 2000) + const isIp = nodeNet.isIP(target) !== 0 + try { - await dns.promises.resolve(host) + if (!isIp) await dns.promises.lookup(target) } catch { - return false + if (!await trySystemPing(target, wait)) return false } - for (const port of [443, 80]) { - if (await tryConnect(host, port, timeout)) return true + + for (const port of [443, 80, 53]) { + if (await tryConnect(target, port, wait)) return true } - return false + return trySystemPing(target, wait) } }