Skip to content

Instantly share code, notes, and snippets.

@addisaden
Created November 12, 2011 16:47
Show Gist options
  • Save addisaden/1360795 to your computer and use it in GitHub Desktop.
Save addisaden/1360795 to your computer and use it in GitHub Desktop.
node.js - Http Server for ip lookup from domain
var http = require("http");
var dns = require("dns");
var u = require("util");
var urls = { length: 0 }
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var url = req.url.replace(/^\/(https?:\/\/)?/i,"");
url = url.split("/")[0];
var writeIp = function(wurl, wip) {
res.write(String(wurl) + ": " + String(wip) + "\n");
};
var finished = function() {
if(urls[url] == "undefined") {
res.write("Die IP von '" + url + "' konnte nicht gefunden werden.\n");
} else {
res.write("Die IP von '" + url + "' ist " + String(urls[url]) + "\n");
}
if(url != "favicon.ico")
console.log("Die IP von '" + url + "' wurde nachgeschaut. Die IP ist " + urls[url]);
if(urls.length > 1 || ( urls.length > 0 && urls[url] == "undefined" ) ) {
res.write("\n\n\nWeiter IP Adressen:\n\n");
};
for( var key in urls ) {
if( key != "undefined" && key != url && key != "length" && urls[key] != "undefined") {
writeIp(key, urls[key]);
};
};
res.end("");
};
var cb = function(err, adress, family) {
if(url != "favicon.ico") {
if( urls[url] == undefined && String(adress) != "undefined") {
urls.length += 1;
};
urls[url] = String(adress);
};
finished();
};
dns.lookup(url, cb);
}).listen(3000);
@addisaden
Copy link
Author

http://localhost:3000/localhost
=> Die IP von 'localhost' ist 127.0.0.1

http://localhost:3000/google.de
=>
Die IP von 'google.de' ist 209.85.148.106
Weiter IP Adressen:
localhost: 127.0.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment