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

52
p2ns.js
View File

@ -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);