-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.js
67 lines (62 loc) · 2 KB
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var http = require('http'),
httpProxy = require('http-proxy');
url = require('url');
request = require('request');
function processRequest(req, res, proxy, new_req){
req.url = new_req.path
req.headers.host = new_req.host
res.oldWriteHead = res.writeHead;
res.writeHead = function(statusCode, headers) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Credentials', true)
res.oldWriteHead(statusCode, headers);
}
try{
proxy.proxyRequest(req, res, {
host: req.headers.host.split(":")[0],
port: parseInt(req.headers.host.split(":")[1]) || 80
});
}catch(err){
console.log(err);
}
}
httpProxy.createServer(function (req, res, proxy) {
var url_path = req.url.slice(1)
if(url_path == "crossdomain.xml"){
proxy.proxyRequest(req, res, {
host: '127.0.0.1',
port: 9000
});
}
else{
new_req = url.parse(url_path)
if(new_req.host == "avatars.io"){
request({
url: new_req.href,
followRedirect:false
}, function(error, response, body){
var final_req = url.parse(response.headers.location);
processRequest(req, res, proxy, final_req);
});
}
else{
if(new_req.host && new_req.host.match(/dgm59|cloudinary|twimg|instagram|facebook|fbcdn|distillery|qrserver|igcdn|naver/)){
processRequest(req, res, proxy, new_req);
console.log("allowed " + new_req.host)
}else{
console.log("disallowed " + new_req.host)
res.writeHead(400);
res.write('Not Allowed');
res.end();
}
}
}
}).listen(8000);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.write('<?xml version="1.0"?>\n<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">\n<cross-domain-policy>\n<allow-access-from domain="*" />\n</cross-domain-policy>\n');
res.end();
}).listen(9000);
process.on('uncaughtException', function(e){
console.log(e)
});