-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
67 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,72 @@ | ||
const dns = require('dns').promises; | ||
|
||
async function dnsLookup(hostname) { | ||
const timeoutPromise = new Promise((resolve) => { | ||
setTimeout(resolve, 2000, []); | ||
}); | ||
const dnsPromise = dns.lookup(hostname, { all: true }).catch((error) => console.log(error)); // eg. [ { address: '65.21.189.1', family: 4 } ] | ||
const result = await Promise.race([dnsPromise, timeoutPromise]); | ||
return result || []; | ||
async function selectIPforG(ips, app) { | ||
// choose the ip address whose sum of digits is the lowest | ||
if (ips && ips.length) { | ||
let chosenIp = ips[0]; | ||
let chosenIpSum = ips[0].split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0); | ||
for (const ip of ips) { | ||
const sum = ip.split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0); | ||
if (sum < chosenIpSum) { | ||
chosenIp = ip; | ||
chosenIpSum = sum; | ||
} | ||
} | ||
const isOk = false; | ||
if (isOk) { | ||
return chosenIp; | ||
} | ||
console.log(ips); | ||
const newIps = ips.filter((ip) => ip !== chosenIp); | ||
if (newIps.length) { | ||
return selectIPforG(newIps, app); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
async function test() { | ||
try { | ||
const resp = await dnsLookup('www.astro-fun.com'); | ||
console.log(resp); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
const b = await selectIPforG(['123.234','2345.342','456.34234','342.434'], 'test'); | ||
console.log(b); | ||
} | ||
|
||
test(); | ||
|
||
const recentlyConfiguredApps = [{ | ||
appName: 'test1', | ||
ips: ['1'], | ||
}, | ||
{ | ||
appName: 'test2', | ||
ips: ['1'], | ||
}, | ||
{ | ||
appName: 'test', | ||
ips: ['1'], | ||
}, | ||
{ | ||
appName: 'test3', | ||
ips: ['1'], | ||
}]; | ||
let configuredApps = [{ | ||
appName: 'test', | ||
ips: ['2'], | ||
}, | ||
{ | ||
appName: 'test3', | ||
ips: ['2'], | ||
}]; | ||
const updatingConfig = JSON.parse(JSON.stringify(recentlyConfiguredApps)); | ||
// merge recentlyConfiguredApps with currently configuredApps | ||
for (const app of configuredApps) { | ||
let appExists = updatingConfig.find((a) => a.appName === app.appName); | ||
if (!appExists) { | ||
updatingConfig.push(app); | ||
} else { | ||
updatingConfig.splice(updatingConfig.indexOf(appExists), 1, app); | ||
// console.log(app); | ||
// appExists = app; // this is also updating element in updatingConfig | ||
} | ||
} | ||
console.log(updatingConfig); | ||
configuredApps = updatingConfig; | ||
|
||
console.log(configuredApps); |