network-performance-monitor
Version:
A comprehensive network performance monitoring tool that continuously tests and tracks your network's performance over time
246 lines • 9.53 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetworkDetector = void 0;
const child_process_1 = require("child_process");
const util_1 = require("util");
const os = __importStar(require("os"));
const logger_1 = require("./logger");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
class NetworkDetector {
static instance;
currentNetwork = null;
lastCheck = 0;
CACHE_DURATION = 60000; // 1 minute cache
static getInstance() {
if (!NetworkDetector.instance) {
NetworkDetector.instance = new NetworkDetector();
}
return NetworkDetector.instance;
}
async getCurrentNetwork(forceRefresh = false) {
const now = Date.now();
if (!forceRefresh && this.currentNetwork && (now - this.lastCheck) < this.CACHE_DURATION) {
return this.currentNetwork;
}
this.currentNetwork = await this.detectNetwork();
this.lastCheck = now;
return this.currentNetwork;
}
async detectNetwork() {
const platform = os.platform();
if (platform === 'darwin') {
return this.detectNetworkMacOS();
}
else if (platform === 'linux') {
return this.detectNetworkLinux();
}
else if (platform === 'win32') {
return this.detectNetworkWindows();
}
else {
return {
name: 'Unknown Network',
isWifi: false,
interface: 'unknown',
gateway: 'unknown'
};
}
}
async detectNetworkMacOS() {
try {
// Get default route - this is the authoritative source
const { stdout: routeOutput } = await execAsync('route -n get default');
const gatewayMatch = routeOutput.match(/gateway:\s+(\S+)/);
const interfaceMatch = routeOutput.match(/interface:\s+(\S+)/);
if (!gatewayMatch || !interfaceMatch) {
throw new Error('Could not determine default route');
}
const gateway = gatewayMatch[1];
const iface = interfaceMatch[1];
logger_1.logger.debug(`Detected default route: interface=${iface}, gateway=${gateway}`);
// Use ipconfig getsummary to get interface details
try {
const { stdout: ipconfigOutput } = await execAsync(`ipconfig getsummary ${iface}`);
// Check if it's a WiFi interface
const interfaceTypeMatch = ipconfigOutput.match(/InterfaceType\s*:\s*(\S+)/);
const isWifi = interfaceTypeMatch && interfaceTypeMatch[1] === 'WiFi';
if (isWifi) {
// Extract SSID (not BSSID)
const ssidMatch = ipconfigOutput.match(/^\s*SSID\s*:\s*(.+)$/m);
const ssid = ssidMatch ? ssidMatch[1].trim() : null;
if (ssid) {
logger_1.logger.debug(`Detected WiFi network: ${ssid} on ${iface}`);
return {
name: ssid,
isWifi: true,
interface: iface,
gateway: gateway
};
}
else {
// WiFi but no SSID (disconnected?)
logger_1.logger.debug(`WiFi interface ${iface} but no SSID detected`);
return {
name: `WiFi (${iface})`,
isWifi: true,
interface: iface,
gateway: gateway
};
}
}
}
catch (error) {
// ipconfig command failed, interface might not exist or not be active
logger_1.logger.debug(`ipconfig getsummary failed for ${iface}:`, error);
}
// Not WiFi (Ethernet, VPN, etc.)
logger_1.logger.debug(`Non-WiFi network on ${iface}`);
return {
name: `${iface} (${gateway})`,
isWifi: false,
interface: iface,
gateway: gateway
};
}
catch (error) {
console.error('Error detecting network on macOS:', error);
return {
name: 'Unknown Network',
isWifi: false,
interface: 'unknown',
gateway: 'unknown'
};
}
}
async detectNetworkLinux() {
try {
// Get default route
const { stdout: routeOutput } = await execAsync('ip route show default');
const routeMatch = routeOutput.match(/default via (\S+) dev (\S+)/);
if (!routeMatch) {
throw new Error('Could not determine default route');
}
const gateway = routeMatch[1];
const iface = routeMatch[2];
// Check if it's Wi-Fi
try {
const { stdout: iwOutput } = await execAsync(`iwconfig ${iface} 2>/dev/null`);
const ssidMatch = iwOutput.match(/ESSID:"([^"]+)"/);
if (ssidMatch && ssidMatch[1]) {
// It's Wi-Fi, use SSID as network name
return {
name: ssidMatch[1],
isWifi: true,
interface: iface,
gateway: gateway
};
}
}
catch {
// Not Wi-Fi or iwconfig not available
}
// Not Wi-Fi, use interface and gateway
return {
name: `${iface} (${gateway})`,
isWifi: false,
interface: iface,
gateway: gateway
};
}
catch (error) {
console.error('Error detecting network on Linux:', error);
return {
name: 'Unknown Network',
isWifi: false,
interface: 'unknown',
gateway: 'unknown'
};
}
}
async detectNetworkWindows() {
try {
// Get default gateway
const { stdout: routeOutput } = await execAsync('route print 0.0.0.0');
const lines = routeOutput.split('\n');
let gateway = '';
let iface = '';
for (const line of lines) {
if (line.includes('0.0.0.0') && !line.includes('On-link')) {
const parts = line.trim().split(/\s+/);
if (parts.length >= 3) {
gateway = parts[2];
break;
}
}
}
// Get Wi-Fi info
try {
const { stdout: wifiOutput } = await execAsync('netsh wlan show interfaces');
const ssidMatch = wifiOutput.match(/\s+SSID\s+:\s+(.+)/);
if (ssidMatch && ssidMatch[1]) {
// It's Wi-Fi, use SSID as network name
return {
name: ssidMatch[1].trim(),
isWifi: true,
interface: 'Wi-Fi',
gateway: gateway || 'unknown'
};
}
}
catch {
// Not Wi-Fi or command failed
}
// Not Wi-Fi
return {
name: `Ethernet (${gateway || 'unknown'})`,
isWifi: false,
interface: 'Ethernet',
gateway: gateway || 'unknown'
};
}
catch (error) {
console.error('Error detecting network on Windows:', error);
return {
name: 'Unknown Network',
isWifi: false,
interface: 'unknown',
gateway: 'unknown'
};
}
}
}
exports.NetworkDetector = NetworkDetector;
//# sourceMappingURL=networkInfo.js.map