Add support: MacOS, Linux, Windows - Needs testing

This commit is contained in:
Raven Scott 2024-11-09 18:51:24 -05:00
parent 9d93a11327
commit a11b04ba43

22
p2ns.js
View File

@ -20,9 +20,10 @@ const dnsServer = dgram.createSocket('udp4');
const domainToIPMap = {}; const domainToIPMap = {};
let currentIP = 2; // Start assigning IP addresses from 192.168.100.2 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) { function removeExistingInterface(subnetName) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (os.platform() === 'darwin' || os.platform() === 'linux') {
exec(`sudo ifconfig ${subnetName} down`, (err) => { exec(`sudo ifconfig ${subnetName} down`, (err) => {
if (err) resolve(); if (err) resolve();
else { else {
@ -30,14 +31,21 @@ function removeExistingInterface(subnetName) {
resolve(); 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) { async function createVirtualInterface(subnetName, subnetCIDR) {
await removeExistingInterface(subnetName); await removeExistingInterface(subnetName);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`sudo ifconfig ${subnetName} alias ${subnetCIDR}`, (err, stdout, stderr) => { 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) { if (err) {
console.error(`Error creating virtual interface ${subnetName}:`, stderr); console.error(`Error creating virtual interface ${subnetName}:`, stderr);
reject(`Error creating virtual interface ${subnetName}: ${stderr}`); reject(`Error creating virtual interface ${subnetName}: ${stderr}`);
@ -46,13 +54,17 @@ async function createVirtualInterface(subnetName, subnetCIDR) {
resolve(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 // Updated function to create virtual interfaces dynamically, starting from 192.168.100.2
async function createInterfaceForDomain(domain) { async function createInterfaceForDomain(domain) {
const subnetID = currentIP; const subnetID = currentIP;
const subnetName = `lo0`; const subnetName = os.platform() === 'win32' ? '' : `lo0`; // No subnetName for Windows
const subnetCIDR = `192.168.100.${subnetID}/24`; const subnetCIDR = `192.168.100.${subnetID}/24`;
try { try {
@ -102,7 +114,7 @@ function checkPublicDNS(domain) {
questions: [{ type: 'A', name: 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) { if (err) {
logDebug(`Error forwarding DNS query to 1.1.1.1: ${err}`); logDebug(`Error forwarding DNS query to 1.1.1.1: ${err}`);
return resolve(null); return resolve(null);