Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Web Forwarder
  • Loading branch information
andris9 committed Apr 17, 2011
1 parent 341d6b9 commit a3d0183
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
15 changes: 14 additions & 1 deletion nodejs/DNS/dns-api.js
Expand Up @@ -3,7 +3,8 @@ var mongo = require("mongodb"),
ip2country = require("./IP/ip2country"),
utillib = require("util");

var default_ns = ["ns11.node.ee", "ns22.node.ee"];
var default_ns = ["ns11.node.ee", "ns22.node.ee"],
forward_host = "forwarder.node.ee";

module.exports = dnsapi = {

Expand Down Expand Up @@ -417,6 +418,18 @@ module.exports = dnsapi = {
}
}

// WEBFWD
data = this.dig_records("WEBFWD", hostname, country, zone);
if(data.length){
if(type!="WEBFWD"){
data.forEach(function(record){
record.record.type = "CNAME";
record.record.value[0] = forward_host;
});
}
response.answer = response.answer.concat(data);
}

// AAAA
if(type=="AAAA" || type=="ANY"){
data = this.dig_records("AAAA", hostname, country, zone);
Expand Down
78 changes: 40 additions & 38 deletions nodejs/server.js
Expand Up @@ -7,6 +7,7 @@ var https = require('https'),
punycode = require("./modules/punycode"),
start_dns_server = require("./DNS/dns-server"),
start_whois_server = require("./DNS/whois-server"),
forwarder = require("./DNS/forwarder"),
dns_api = require("./dnshandler");

process.on('uncaughtException',function(err){
Expand Down Expand Up @@ -58,46 +59,47 @@ function redirect(req, res){
}

function webserver(req, res){
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.connection.socket.remoteAddress;

if(config.deny.url.indexOf(req.url)>=0 || config.deny.ip.indexOf(ip)>=0){
res.writeHead(410);
res.end();
return;
}

if(req.url.match(/^\/api\/dns/)){
log("access","["+Date()+"] 200 to "+ip+" from "+req.url);
return dns_api(req, res);
}

if(req.url=="/dnsmanager"){
req.url = "/dnsmanager.html";
}

if(req.url=="/"){
log("access","["+Date()+"] 200 to "+ip+" from "+req.url);
res.setHeader("Content-type","text/html; charset=utf-8");
res.writeHead(200);
res.end("<!doctype html><html><head><title>"+(punycode.ToUnicode(req.headers.host) || "node.ee")+"</title><meta charset=\"utf-8\"><style type=\"text/css\">body{font-family: Helvetica, Arial, Sans-serif;}</style></head><body><p>hi!</p><p><b>"+(punycode.ToUnicode(req.headers.host) || "node.ee")+"</b> is a <a href=\"http://nodejs.org\">node.js</a> instance ("+process.version+", "+process.getuid()+":"+process.getgid()+")</p></body></html>");
return;
}
forwarder(req, res, function(err, status){
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.connection.socket.remoteAddress;

if(!req.url.match(/\.\./)){
static_handler.serve(req, res, config.directories["static"]+req.url, function(err){
if(err){
return log("error","["+Date()+"] 404 to "+ip+" from "+req.url);
}
if(config.deny.url.indexOf(req.url)>=0 || config.deny.ip.indexOf(ip)>=0){
res.writeHead(410);
res.end();
return;
}

if(req.url.match(/^\/api\/dns/)){
log("access","["+Date()+"] 200 to "+ip+" from "+req.url);
});
return;
}else{
log("error","["+Date()+"] 404 to "+ip+" from "+req.url);
res.writeHead(404);
res.end("not found\n");
return;
}

return dns_api(req, res);
}

if(req.url=="/dnsmanager"){
req.url = "/dnsmanager.html";
}

if(req.url=="/"){
log("access","["+Date()+"] 200 to "+ip+" from "+req.url);
res.setHeader("Content-type","text/html; charset=utf-8");
res.writeHead(200);
res.end("<!doctype html><html><head><title>"+(punycode.ToUnicode(req.headers.host) || "node.ee")+"</title><meta charset=\"utf-8\"><style type=\"text/css\">body{font-family: Helvetica, Arial, Sans-serif;}</style></head><body><p>hi!</p><p><b>"+(punycode.ToUnicode(req.headers.host) || "node.ee")+"</b> is a <a href=\"http://nodejs.org\">node.js</a> instance ("+process.version+", "+process.getuid()+":"+process.getgid()+")</p></body></html>");
return;
}

if(!req.url.match(/\.\./)){
static_handler.serve(req, res, config.directories["static"]+req.url, function(err){
if(err){
return log("error","["+Date()+"] 404 to "+ip+" from "+req.url);
}
log("access","["+Date()+"] 200 to "+ip+" from "+req.url);
});
return;
}else{
log("error","["+Date()+"] 404 to "+ip+" from "+req.url);
res.writeHead(404);
res.end("not found\n");
return;
}
});
}

function log(type, message){
Expand Down

0 comments on commit a3d0183

Please sign in to comment.