Last active
November 19, 2018 09:29
-
-
Save Adriem/336d0177ebaeb1565b95a02596b3c94c to your computer and use it in GitHub Desktop.
Revisions
-
Adriem revised this gist
Nov 13, 2018 . 1 changed file with 23 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,24 +8,9 @@ const fs = require('fs') const urls = process.argv.slice(2).filter(arg => arg !== '-v' && arg !== '--verbose') const verbose = process.argv.includes('-v') || process.argv.includes('--verbose') const defaultUrls = [ // PUT YOUR DEFAULT URLS HERE ] async function fetchIp(url) { const dnsData = await new Promise((resolve, reject) => { @@ -41,3 +26,23 @@ async function fetchIp(url) { return dnsData.find((it) => it.type == 1).data } async function updateHostsFile(urls) { let hostsFileContent = fs.readFileSync('/etc/hosts', 'utf8') await Promise.all(urls.map(async url => { const entryRegex = new RegExp(`[\\.\\d]*[ \\t]+${url}`, 'ig') const newHostEntry = `${await fetchIp(url)}\t${url}` const isExistingEntry = entryRegex.test(hostsFileContent) hostsFileContent = isExistingEntry ? hostsFileContent.replace(entryRegex, newHostEntry) : `${hostsFileContent}${newHostEntry}\n` if (verbose) console.log(`ENTRY UPDATED: ${newHostEntry}`) })) fs.writeFileSync('/etc/hosts', hostsFileContent, 'utf8') } updateHostsFile(urls.length > 0 ? urls : defaultUrls) -
Adriem created this gist
Nov 7, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ #!/usr/bin/env node /** @author Adrian Moreno (https://github.com/adriem) */ const https = require('https') const fs = require('fs') const urls = process.argv.slice(2).filter(arg => arg !== '-v' && arg !== '--verbose') const verbose = process.argv.includes('-v') || process.argv.includes('--verbose') const defaultUrls = [] // PUT ANY DEFAULT URLS HERE updateHostsFile(urls.length > 0 ? urls : defaultUrls) async function updateHostsFile(urls) { let hostsFileContent = fs.readFileSync('/etc/hosts', 'utf8') await Promise.all(urls.map(async url => { const entryRegex = new RegExp(`[\\.\\d]*[ \\t]+${url}`, 'ig') const newHostEntry = `${await fetchIp(url)}\t${url}` if (verbose) console.log(`ENTRY UPDATED: ${newHostEntry}`) hostsFileContent = hostsFileContent.replace(entryRegex, newHostEntry) })) fs.writeFileSync('/etc/hosts', hostsFileContent, 'utf8') } async function fetchIp(url) { const dnsData = await new Promise((resolve, reject) => { let rData = '' https .get(`https://dns.google.com/resolve?name=${url}`, (response) => { response.on('data', (chunk) => rData += chunk) response.on('end', () => resolve(JSON.parse(rData)['Answer'])) }) .on('error', reject) }) return dnsData.find((it) => it.type == 1).data }