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) } }