|
|
|
@ -10,7 +10,6 @@ |
|
|
|
const { contextBridge, ipcRenderer } = require('electron') |
|
|
|
const { contextBridge, ipcRenderer } = require('electron') |
|
|
|
const nodeNet = require('net') |
|
|
|
const nodeNet = require('net') |
|
|
|
const dns = require('dns') |
|
|
|
const dns = require('dns') |
|
|
|
const { execFile } = require('child_process') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function tryConnect(host, port, timeout) { |
|
|
|
function tryConnect(host, port, timeout) { |
|
|
|
return new Promise((resolve) => { |
|
|
|
return new Promise((resolve) => { |
|
|
|
@ -23,41 +22,17 @@ 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 = { |
|
|
|
const net = { |
|
|
|
async ping(host, timeout) { |
|
|
|
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 { |
|
|
|
try { |
|
|
|
if (!isIp) await dns.promises.lookup(target) |
|
|
|
await dns.promises.resolve(host) |
|
|
|
} catch { |
|
|
|
} catch { |
|
|
|
if (!await trySystemPing(target, wait)) return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (const port of [443, 80]) { |
|
|
|
for (const port of [443, 80, 53]) { |
|
|
|
if (await tryConnect(host, port, timeout)) return true |
|
|
|
if (await tryConnect(target, port, wait)) return true |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return trySystemPing(target, wait) |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|