Skip to content

Instantly share code, notes, and snippets.

@Adriem
Last active November 19, 2018 09:29
Show Gist options
  • Select an option

  • Save Adriem/336d0177ebaeb1565b95a02596b3c94c to your computer and use it in GitHub Desktop.

Select an option

Save Adriem/336d0177ebaeb1565b95a02596b3c94c to your computer and use it in GitHub Desktop.

Revisions

  1. Adriem revised this gist Nov 13, 2018. 1 changed file with 23 additions and 18 deletions.
    41 changes: 23 additions & 18 deletions update-hosts.js
    Original 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 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')
    }
    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)
  2. Adriem created this gist Nov 7, 2018.
    43 changes: 43 additions & 0 deletions update-hosts.js
    Original 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
    }