Add support: MacOS, Linux, Windows - Needs testing
This commit is contained in:
parent
9d93a11327
commit
a11b04ba43
22
p2ns.js
22
p2ns.js
@ -20,9 +20,10 @@ 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) => {
|
||||
if (os.platform() === 'darwin' || os.platform() === 'linux') {
|
||||
exec(`sudo ifconfig ${subnetName} down`, (err) => {
|
||||
if (err) resolve();
|
||||
else {
|
||||
@ -30,14 +31,21 @@ function removeExistingInterface(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 (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}`);
|
||||
@ -46,13 +54,17 @@ async function createVirtualInterface(subnetName, 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);
|
||||
|
Loading…
Reference in New Issue
Block a user