Add support: MacOS, Linux, Windows - Needs testing
This commit is contained in:
parent
9d93a11327
commit
a11b04ba43
52
p2ns.js
52
p2ns.js
@ -20,39 +20,51 @@ 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) => {
|
||||||
exec(`sudo ifconfig ${subnetName} down`, (err) => {
|
if (os.platform() === 'darwin' || os.platform() === 'linux') {
|
||||||
if (err) resolve();
|
exec(`sudo ifconfig ${subnetName} down`, (err) => {
|
||||||
else {
|
if (err) resolve();
|
||||||
console.log(`Removed existing virtual interface: ${subnetName}`);
|
else {
|
||||||
resolve();
|
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) {
|
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') {
|
||||||
if (err) {
|
const command = os.platform() === 'darwin'
|
||||||
console.error(`Error creating virtual interface ${subnetName}:`, stderr);
|
? `sudo ifconfig ${subnetName} alias ${subnetCIDR}`
|
||||||
reject(`Error creating virtual interface ${subnetName}: ${stderr}`);
|
: `sudo ip addr add ${subnetCIDR} dev ${subnetName}`;
|
||||||
} else {
|
exec(command, (err, stdout, stderr) => {
|
||||||
console.log(`Virtual interface ${subnetName} created with CIDR ${subnetCIDR}.`);
|
if (err) {
|
||||||
resolve(subnetCIDR);
|
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
|
// 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);
|
||||||
|
Loading…
Reference in New Issue
Block a user