diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/proxy/.gitignore | 1 | ||||
-rw-r--r-- | modules/proxy/proxy.js | 50 |
2 files changed, 51 insertions, 0 deletions
diff --git a/modules/proxy/.gitignore b/modules/proxy/.gitignore new file mode 100644 index 0000000..f4a3cc2 --- /dev/null +++ b/modules/proxy/.gitignore @@ -0,0 +1 @@ +idfile.txt diff --git a/modules/proxy/proxy.js b/modules/proxy/proxy.js new file mode 100644 index 0000000..66c50df --- /dev/null +++ b/modules/proxy/proxy.js @@ -0,0 +1,50 @@ +var cmn=require("../$common.js"), + fs=require("fs"), + http=require("http"), + https=require("https"), + URL=require("url"); + + +function fetch(method,url,cb){ + var module; + if(url.slice(0,7)=="http://")module=http; + else if(url.slice(0,8)=="https://")module=https; + var obj=URL.parse(url); + obj.method=method; + module.request(obj,function(res){ + var body=""; + res.on("data",function(data){body+=data;}); + res.on("end",function(){ + cb(res.statusCode,body); + }); + }).on("error",function(){ + cb(null,null); + }).end(); +} + +function objectFromList(l){ + var obj={}; + for(var i=0;i<l.length;i++)obj[l[i][0]]=l[i][1]; + return obj; +} + +module.exports=function(app,io,moddir){ + var iddict=objectFromList( + fs.readFileSync(moddir+"/idfile.txt").toString() + .split("\n").map(function(r){return r.split(" ");})); + + app.all("/proxy/:id/*",function(req,res){ + var id=req.params.id; + var path="/"+req.path.split("/").slice(3).join("/"); + if(iddict[id]){ + fetch(req.method,iddict[id]+path,function(status,body){ + if(status==null)res.status(500).send("An error occurred...\n"); + else res.status(status).send(body); + }); + } else { + res.status(404).send( + "That ID is incorrect.\n"+ + "Please contact the person you got this link from.\n"); + } + }); +}; |