diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-08-24 17:50:36 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-08-24 17:52:29 +0200 |
commit | aa59a979fd44fb6d3036150e386099470e576323 (patch) | |
tree | c8510abb2fb2b5d8ed7a470d74e38b2cb59055e3 /modules/proxy | |
parent | 3a7b9838798a8dfe1c5caee32322ea5ac0cf31da (diff) |
Add proxy module
Diffstat (limited to 'modules/proxy')
-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"); + } + }); +}; |