Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for Linux. Add an "add" function that appends a host.

  • Loading branch information...
commit f2d54416c0615c68585b6cddae6916343130e991 1 parent a05383e
@squaretone squaretone authored
Showing with 108 additions and 28 deletions.
  1. +8 −4 README.md
  2. +97 −21 lib/localhose.js
  3. +3 −3 test.js
View
12 README.md
@@ -14,7 +14,7 @@ Localhose is a [node.js](http://nodejs.org/) module that provides a simple API f
## Install
$ npm install localhose
-
+
## Module API
### localhose = require( "localhose" )
@@ -34,6 +34,10 @@ Adds one or more hosts to be routed to your local machine. The hosts are stored
The path of the host file can be specified using the `hostsPath` property of the `localhose.constructor.prototype` object.
+### localhose.add( [host1], [host2], [etc] )
+
+Adds additional domain(s) the the <localhose> section of your hosts file but will not overwrite any existing hosts in <localhose> section.
+
### localhose.unset( [host1], [host2], [etc] )
Removes some or all of the hosts rerouted to your machine. If no arguments are specified, all currently hosts are unset. If no hosts remain after this is called, the `# <localhose> ... # </localhose>` section of the current `hosts` file is removed, leaving your file system as pristine as it was before.
@@ -58,13 +62,13 @@ For an example rerouting `nodejs.org` to your machine, see [test.js](/jed/localh
// sudo node ./someFile.js
localhose = require( "localhose" )
-
+
// resolve "google.com" and "google.org" to 127.0.0.1.
// note that you will be unable to use google while this is set.
localhose.set( "google.com", "google.org" )
-
+
localhose.domains() // [ "google.com", "google.org" ]
-
+
// remove all domains, and revert the `hosts` file to its original state
localhose.unset( "google.com" )
View
118 lib/localhose.js
@@ -3,11 +3,16 @@ var fs = require( "fs" )
, exec = require( "child_process" ).exec
, Localhoses = function(){}
, platform = process.platform
+ , platformPaths = {
+ "darwin": "/private/etc/hosts",
+ "win32": path.join(process.env["WinDir"], "System32", "drivers", "etc", "hosts"),
+ "linux": "/etc/hosts"
+ };
-if ( platform != "darwin" && platform != "win32" ) throw "Sorry, only tested on OS X and Windows."
+if (!platformPaths.hasOwnProperty(platform)) throw "Sorry, only tested on OS X, Linux and Windows. Feel free to submit pull requests to support other systems.";
Localhoses.prototype = {
- hostsPath: ( platform == "darwin" ) ? "/private/etc/hosts" : path.join(process.env["WinDir"], "System32", "drivers", "etc", "hosts"),
+ hostsPath: platformPaths[platform],
header:
"# <localhose>",
@@ -21,9 +26,35 @@ Localhoses.prototype = {
domains: function(){ return Object.keys( this ) },
+ // Returns and array of domains localhose is currently re-routing to 127.0.0.1
+ list: function() {
+ var end,
+ hose,
+ lineReg = /127\.0\.0\.1\t/ig,
+ hosts = this.getHostsFile().split( "\n" ),
+ start = hosts.indexOf( this.header );
+
+ if ( ~start ) {
+ end = hosts.indexOf( this.footer ) || hosts.length
+ hose = hosts.splice( start, 1 + end - start )
+
+ // TODO: use a more specific filter
+ hose = hose.filter(function(element, index, array) {
+ return (element.indexOf('#') != 0);
+ });
+
+ // TODO: make matching more fool-proof
+ hose = hose.map( function( line ){
+ return line.replace(lineReg, '');
+ });
+ }
+
+ return hose || [];
+ },
+
set: function() {
for ( var i = arguments.length; i--; ) this[ arguments[ i ] ] = true
-
+
return this.save()
},
@@ -31,7 +62,7 @@ Localhoses.prototype = {
var domains = 0 in arguments ? arguments : this.domains()
for ( var i = domains.length; i--; ) delete this[ domains[ i ] ]
-
+
return this.save()
},
@@ -41,31 +72,76 @@ Localhoses.prototype = {
.join( "\n" )
},
- save: function() {
+ // attempt to flush the DNS
+ flushDNS: function() {
+ var dnsHandlers = {
+ "darwin": function() {
+ exec("dscacheutil -flushcache");
+ },
+ "win32": function() {
+ exec("ipconfig /flushdns");
+ },
+ "linux": function() {
+ // TODO: check if nscd is installed
+ exec("/etc/rc.d/init.d/nscd restart");
+ }
+ };
+
+ if (typeof dnsHandlers[platform] ) {
+ dnsHandlers[platform]();
+ }
+ else {
+ throw "No DNS flush available for this platform.";
+ }
+ },
+
+ getHostsFile: function() {
var hostsPath = this.hostsPath
- , hosts, start, end
+ , hosts;
try {
- hosts = fs.readFileSync( hostsPath, "utf8" ).split( "\n" )
- start = hosts.indexOf( this.header )
-
- if ( ~start ) {
- end = hosts.indexOf( this.footer ) || hosts.length
- hosts.splice( start, 1 + end - start )
- }
+ hosts = fs.readFileSync( hostsPath, "utf8" );
+ return hosts;
+ }
+ catch ( e ) { throw hostsPath + " does not exist or cannot be read." };
+ },
+
+ // add additional passed domains without deleting existing ones
+ add: function() {
+ var currentDomains = this.list()
+ , allDomains;
+
+ // add any existing domains
+ for ( var i = currentDomains.length; i--; ) this[ currentDomains[ i ] ] = true
- this.domains().length && hosts.push(
- this.header, this.description, this.toString(), this.footer
- )
+ // add requested domains
+ for ( var ii = arguments.length; ii--; ) this[ arguments[ ii ] ] = true;
- try { fs.writeFileSync( hostsPath, hosts.join( "\n" ) ) }
- catch ( e ){ throw hostsPath + " is not writeable." }
+ this.save();
+ },
+
+ save: function() {
+ var hosts, start, end;
- exec( "dscacheutil -flushcache" )
+ hosts = this.getHostsFile().split( "\n" );
+ start = hosts.indexOf( this.header )
+
+ if ( ~start ) {
+ end = hosts.indexOf( this.footer ) || hosts.length
+ hosts.splice( start, 1 + end - start )
}
- catch ( e ) { throw hostsPath + " does not exist or cannot be read." }
-
+ this.domains().length && hosts.push(
+ this.header, this.description, this.toString(), this.footer
+ )
+
+ try { fs.writeFileSync( this.hostsPath, hosts.join( "\n" ) ) }
+ catch ( e ){ throw this.hostsPath + " is not writeable.\n" + e }
+
+ this.flushDNS();
+
+
+
return this
}
}
View
6 test.js
@@ -9,7 +9,7 @@
var assert = require( "assert" )
, http = require( "http" )
, localhose = require( "./lib/localhose" ).unset()
-
+
, url = require( "url" ).parse( "http://nodejs.org/" )
, sassyTitle = "rYah'S aweSOmE homEpagE!"
@@ -23,7 +23,7 @@ getTitle( url, function( title ) {
console.log( "- actual title: " + title )
assert.equal( title, "node.js" )
-
+
localhose.set( url.host )
getTitle( url, function( title ) {
@@ -39,7 +39,7 @@ getTitle( url, function( title ) {
console.log( "\nfetching title for " + url.host )
console.log( "- expected title: node.js" )
console.log( "- actual title: " + title )
-
+
assert.equal( title, "node.js" )
server.close()
Please sign in to comment.
Something went wrong with that request. Please try again.