From a11b04ba43f0905d324e36da7bf0dfd0776864e1 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sat, 9 Nov 2024 18:51:24 -0500 Subject: [PATCH] Add support: MacOS, Linux, Windows - Needs testing --- p2ns.js | 52 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/p2ns.js b/p2ns.js index fefb34e..62d2926 100644 --- a/p2ns.js +++ b/p2ns.js @@ -20,39 +20,51 @@ const dnsServer = dgram.createSocket('udp4'); const domainToIPMap = {}; let currentIP = 2; // Start assigning IP addresses from 192.168.100.2 -// Helper function to remove existing virtual interface if it already exists (for macOS) +// Helper function to remove existing virtual interface if it already exists (for macOS and Linux) function removeExistingInterface(subnetName) { return new Promise((resolve) => { - exec(`sudo ifconfig ${subnetName} down`, (err) => { - if (err) resolve(); - else { - console.log(`Removed existing virtual interface: ${subnetName}`); - resolve(); - } - }); + if (os.platform() === 'darwin' || os.platform() === 'linux') { + exec(`sudo ifconfig ${subnetName} down`, (err) => { + if (err) resolve(); + else { + console.log(`Removed existing virtual interface: ${subnetName}`); + resolve(); + } + }); + } else { + resolve(); // No virtual interface removal needed for Windows + } }); } -// Helper function to create virtual interfaces (for macOS) +// Helper function to create virtual interfaces (for macOS and Linux) async function createVirtualInterface(subnetName, subnetCIDR) { await removeExistingInterface(subnetName); return new Promise((resolve, reject) => { - exec(`sudo ifconfig ${subnetName} alias ${subnetCIDR}`, (err, stdout, stderr) => { - if (err) { - console.error(`Error creating virtual interface ${subnetName}:`, stderr); - reject(`Error creating virtual interface ${subnetName}: ${stderr}`); - } else { - console.log(`Virtual interface ${subnetName} created with CIDR ${subnetCIDR}.`); - resolve(subnetCIDR); - } - }); + if (os.platform() === 'darwin' || os.platform() === 'linux') { + const command = os.platform() === 'darwin' + ? `sudo ifconfig ${subnetName} alias ${subnetCIDR}` + : `sudo ip addr add ${subnetCIDR} dev ${subnetName}`; + exec(command, (err, stdout, stderr) => { + if (err) { + console.error(`Error creating virtual interface ${subnetName}:`, stderr); + reject(`Error creating virtual interface ${subnetName}: ${stderr}`); + } else { + console.log(`Virtual interface ${subnetName} created with CIDR ${subnetCIDR}.`); + resolve(subnetCIDR); + } + }); + } else { + console.warn('Virtual interfaces are not supported on Windows. Skipping interface creation.'); + resolve(subnetCIDR); // Proceed without creating a virtual interface + } }); } // Updated function to create virtual interfaces dynamically, starting from 192.168.100.2 async function createInterfaceForDomain(domain) { const subnetID = currentIP; - const subnetName = `lo0`; + const subnetName = os.platform() === 'win32' ? '' : `lo0`; // No subnetName for Windows const subnetCIDR = `192.168.100.${subnetID}/24`; try { @@ -102,7 +114,7 @@ function checkPublicDNS(domain) { questions: [{ type: 'A', name: domain }] }); - resolver.send(query, 53, '192.168.0.16', (err) => { + resolver.send(query, 53, '1.1.1.1', (err) => { if (err) { logDebug(`Error forwarding DNS query to 1.1.1.1: ${err}`); return resolve(null);