Auto Assign IPs when domains are resolved
This commit is contained in:
@@ -109,10 +109,9 @@ async function handleDomainsRoutes(req, res) {
|
||||
domains.push({ domain: validation.domain, hash: validation.hash, ssl: ssl });
|
||||
}
|
||||
await fs.writeFile(domainsFile, JSON.stringify(domains, null, 2));
|
||||
if (!state.domainToIPMap.has(validation.domain)) {
|
||||
await createInterfaceForDomain(validation.domain);
|
||||
logDebug('Admin', `Assigned IP to ${validation.domain}: ${state.domainToIPMap.get(validation.domain)}`);
|
||||
}
|
||||
// Note: IP assignment now happens automatically after consensus resolution
|
||||
// See assignAllIPs() function for automatic IP assignment logic
|
||||
logDebug('Admin', `Domain ${validation.domain} added - IP will be assigned automatically after consensus resolution`);
|
||||
broadcast({ type: 'update-database' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
|
||||
@@ -109,10 +109,9 @@ async function handleDomainsRoutes(req, res) {
|
||||
domains.push({ domain: validation.domain, hash: validation.hash, ssl: ssl });
|
||||
}
|
||||
await fs.writeFile(domainsFile, JSON.stringify(domains, null, 2));
|
||||
if (!state.domainToIPMap.has(validation.domain)) {
|
||||
await createInterfaceForDomain(validation.domain);
|
||||
logDebug('Admin', `Assigned IP to ${validation.domain}: ${state.domainToIPMap.get(validation.domain)}`);
|
||||
}
|
||||
// Note: IP assignment now happens automatically after consensus resolution
|
||||
// See assignAllIPs() function for automatic IP assignment logic
|
||||
logDebug('Admin', `Domain ${validation.domain} added - IP will be assigned automatically after consensus resolution`);
|
||||
broadcast({ type: 'update-database' });
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('OK');
|
||||
|
||||
@@ -1283,7 +1283,8 @@ async function main() {
|
||||
|
||||
await listDomains();
|
||||
broadcast({ type: 'update-database' });
|
||||
await assignAllIPs();
|
||||
await assignIPsToResolvedDomains(); // Assign IPs to newly resolved domains
|
||||
await assignAllIPs(); // Ensure all resolved domains have IPs
|
||||
} catch (err) {
|
||||
logError('Swarm', `Error in update handler: ${err.message}`);
|
||||
}
|
||||
@@ -1352,7 +1353,8 @@ async function main() {
|
||||
|
||||
await listDomains();
|
||||
broadcast({ type: 'update-database' });
|
||||
await assignAllIPs();
|
||||
await assignIPsToResolvedDomains(); // Assign IPs to newly resolved domains
|
||||
await assignAllIPs(); // Ensure all resolved domains have IPs
|
||||
} catch (err) {
|
||||
logError('Swarm', `Error in append handler: ${err.message}`);
|
||||
}
|
||||
@@ -2834,7 +2836,7 @@ async function main() {
|
||||
// Function to assign IPs to all domains
|
||||
async function assignAllIPs() {
|
||||
try {
|
||||
logInfo('Main', 'Assigning IPs to all domains...');
|
||||
logInfo('Main', 'Assigning IPs to resolved domains...');
|
||||
let internalDomains = ['p2ns.admin'];
|
||||
try {
|
||||
const { getInternalDomains } = require('./includes/plugins/plugin-handler');
|
||||
@@ -2862,18 +2864,32 @@ async function assignAllIPs() {
|
||||
} else {
|
||||
logWarn('Main', 'dnsPass not initialized, only assigning IPs to internal domains');
|
||||
}
|
||||
// Assign IPs to all domains
|
||||
|
||||
// Import getConsensusState for checking resolution status
|
||||
const { getConsensusState } = require('./includes/core/core');
|
||||
|
||||
// Assign IPs only to resolved domains
|
||||
for (const domain of domains) {
|
||||
if (domain === "p2ns.admin" || domain === "peer.directory") continue;
|
||||
if (!state.domainToIPMap.has(domain)) {
|
||||
try {
|
||||
await createInterfaceForDomain(domain);
|
||||
logDebug('Main', `Assigned IP to domain: ${domain} (${state.domainToIPMap.get(domain)})`);
|
||||
} catch (err) {
|
||||
logError('Main', `Failed to assign IP to ${domain}: ${err.message}`);
|
||||
}
|
||||
} else {
|
||||
|
||||
// Check if domain already has IP
|
||||
if (state.domainToIPMap.has(domain)) {
|
||||
logDebug('Main', `IP already assigned for ${domain}: ${state.domainToIPMap.get(domain)}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check consensus state - only assign if resolved
|
||||
try {
|
||||
const consensusState = await getConsensusState(domain);
|
||||
|
||||
if (consensusState.status === 'resolved') {
|
||||
await createInterfaceForDomain(domain);
|
||||
logInfo('Main', `Assigned IP to resolved domain: ${domain} (${state.domainToIPMap.get(domain)})`);
|
||||
} else {
|
||||
logDebug('Main', `Skipping ${domain} - not resolved yet (status: ${consensusState.status})`);
|
||||
}
|
||||
} catch (err) {
|
||||
logError('Main', `Failed to check consensus or assign IP to ${domain}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
logInfo('Main', `IP assignment completed for ${domains.size} domains`);
|
||||
@@ -2882,6 +2898,63 @@ async function assignAllIPs() {
|
||||
}
|
||||
}
|
||||
|
||||
// Assign IPs to domains that have just become resolved (called after consensus updates)
|
||||
async function assignIPsToResolvedDomains() {
|
||||
try {
|
||||
logDebug('Main', 'Checking for newly resolved domains...');
|
||||
|
||||
// Fetch domains from Autopass entries if dnsPass is initialized
|
||||
if (!state.dnsPass) {
|
||||
logDebug('Main', 'dnsPass not initialized, skipping IP assignment check');
|
||||
return;
|
||||
}
|
||||
|
||||
const { getAllEntries, getConsensusState } = require('./includes/core/core');
|
||||
const allEntries = await getAllEntries();
|
||||
const domains = new Set();
|
||||
|
||||
// Collect all domains with claims
|
||||
for (const entry of allEntries) {
|
||||
if (entry.key.startsWith('claim:')) {
|
||||
const parts = entry.key.split(':');
|
||||
if (parts.length === 3) {
|
||||
domains.add(parts[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let assignedCount = 0;
|
||||
|
||||
// Check each domain for resolution status
|
||||
for (const domain of domains) {
|
||||
// Skip internal domains and domains that already have IPs
|
||||
if (domain === "p2ns.admin" || domain === "peer.directory" || state.domainToIPMap.has(domain)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const consensusState = await getConsensusState(domain);
|
||||
|
||||
if (consensusState.status === 'resolved') {
|
||||
await createInterfaceForDomain(domain);
|
||||
logInfo('Main', `Assigned IP to newly resolved domain: ${domain} (${state.domainToIPMap.get(domain)})`);
|
||||
assignedCount++;
|
||||
}
|
||||
} catch (err) {
|
||||
logError('Main', `Failed to check consensus for ${domain}: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (assignedCount > 0) {
|
||||
logInfo('Main', `Assigned IPs to ${assignedCount} newly resolved domains`);
|
||||
} else {
|
||||
logDebug('Main', 'No newly resolved domains found');
|
||||
}
|
||||
} catch (err) {
|
||||
logError('Main', `Error in assignIPsToResolvedDomains: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
// Usage:
|
||||
// Master: node p2ns.js --master
|
||||
|
||||
Reference in New Issue
Block a user