Last active
December 16, 2015 01:29
-
-
Save mdlawson/5355459 to your computer and use it in GitHub Desktop.
Windows host file rewriter
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
fs = require "fs" | |
os = require "os" | |
{exec} = require "child_process" | |
HOSTS = if os.platform() is "win32" then "C:\\Windows\\System32\\Drivers\\etc\\hosts" else "/etc/hosts" | |
hosts = (name,read,write) -> | |
entry = -1 | |
exit = -1 | |
BEGIN = "### BEGIN #{name} ###" | |
END = "### END #{name} ###" | |
env = | |
hosts: {} | |
add: (name,ip) -> @hosts[name] = ip | |
remove: (name) -> delete @hosts[name] | |
fs.readFile HOSTS, {encoding: "ascii"}, (err,data) -> | |
if err then throw err | |
data = data.split os.EOL | |
for line,i in data | |
if line.indexOf(BEGIN) > -1 then entry = i | |
if line.indexOf(END) > -1 then exit = i | |
if entry is -1 | |
entry = data.length | |
data.push BEGIN | |
if exit is -1 | |
data.splice entry+1,0,END | |
exit = entry+1 | |
current = data.slice entry+1,exit | |
for host in current | |
str = host.split " " | |
ip = str[0] | |
name = str[str.length-1] | |
env.hosts[name] = ip | |
if read then read.call(env) | |
newhosts = [] | |
for name,ip of env.hosts | |
newhosts.push "#{ip} #{name}" | |
data.splice.apply data,[entry+1,exit-entry-1].concat(newhosts) | |
fs.writeFile HOSTS,data.join(os.EOL),{encoding: "ascii"}, (err) -> | |
if err then throw err | |
if os.platform() is "win32" then exec "ipconfig /flushdns", (err) -> | |
if err then throw err | |
if write then write() | |
else | |
if write then write() | |
module.exports = hosts |
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
var HOSTS, exec, fs, hosts, os; | |
fs = require("fs"); | |
os = require("os"); | |
exec = require("child_process").exec; | |
HOSTS = os.platform() === "win32" ? "C:\\Windows\\System32\\Drivers\\etc\\hosts" : "/etc/hosts"; | |
hosts = function(name, read, write) { | |
var BEGIN, END, entry, env, exit; | |
entry = -1; | |
exit = -1; | |
BEGIN = "### BEGIN " + name + " ###"; | |
END = "### END " + name + " ###"; | |
env = { | |
hosts: {}, | |
add: function(name, ip) { | |
return this.hosts[name] = ip; | |
}, | |
remove: function(name) { | |
return delete this.hosts[name]; | |
} | |
}; | |
return fs.readFile(HOSTS, { | |
encoding: "ascii" | |
}, function(err, data) { | |
var current, host, i, ip, line, newhosts, str, _i, _j, _len, _len1, _ref; | |
if (err) { | |
throw err; | |
} | |
data = data.split(os.EOL); | |
for (i = _i = 0, _len = data.length; _i < _len; i = ++_i) { | |
line = data[i]; | |
if (line.indexOf(BEGIN) > -1) { | |
entry = i; | |
} | |
if (line.indexOf(END) > -1) { | |
exit = i; | |
} | |
} | |
if (entry === -1) { | |
entry = data.length; | |
data.push(BEGIN); | |
} | |
if (exit === -1) { | |
data.splice(entry + 1, 0, END); | |
exit = entry + 1; | |
} | |
current = data.slice(entry + 1, exit); | |
for (_j = 0, _len1 = current.length; _j < _len1; _j++) { | |
host = current[_j]; | |
str = host.split(" "); | |
ip = str[0]; | |
name = str[str.length - 1]; | |
env.hosts[name] = ip; | |
} | |
if (read) { | |
read.call(env); | |
} | |
newhosts = []; | |
_ref = env.hosts; | |
for (name in _ref) { | |
ip = _ref[name]; | |
newhosts.push("" + ip + " " + name); | |
} | |
data.splice.apply(data, [entry + 1, exit - entry - 1].concat(newhosts)); | |
return fs.writeFile(HOSTS, data.join(os.EOL), { | |
encoding: "ascii" | |
}, function(err) { | |
if (err) { | |
throw err; | |
} | |
if (os.platform() === "win32") { | |
return exec("ipconfig /flushdns", function(err) { | |
if (err) { | |
throw err; | |
} | |
if (write) { | |
return write(); | |
} | |
}); | |
} else { | |
if (write) { | |
return write(); | |
} | |
} | |
}); | |
}); | |
}; | |
module.exports = hosts; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment