-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxy.js
116 lines (100 loc) · 3.1 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var http = require("http"),
net = require("net"),
url = require("url"),
os = require("os"),
fs = require("fs"),
overwrideCors =true,
ethWifiKey ="sans fil",
port=3129;
// catch all uncaught exception and log them
process.on('uncaughtException', logError);
function logError(e){
console.warn('*** ' + e);
}
// read configuraiton file
var conf = JSON.parse(fs.readFileSync("./config.json"));
if(conf){
overwrideCors = conf.overwrideCors || true;
ethWifiKey = conf.ethWifiKey || "sans fil";
port = conf.port || 3129;
}
// search for wifi ip to redirect flows
var ethWifi = null,
localWifiAddress = null;
function getAvailableIntefaces(){
var IPs = os.networkInterfaces();
for(var networkType in IPs){
//console.info("ethWifiKey", ethWifiKey, networkType);
if(networkType.indexOf(ethWifiKey)!= -1){
ethWifi = IPs[networkType];
console.log("found wifi card");
}
}
if(ethWifi){
for(var ip in ethWifi){
var ipObj = ethWifi[ip];
if(!ipObj.internal && ipObj.family.toLowerCase() == "ipv4"){
localWifiAddress = ipObj.address;
console.info("ip to use :: ",localWifiAddress);
}
}
}else{
console.error("*** ERROR : No Wifi network available, please enalbe/connect wifi and restart proxy ***");
process.exit(0);
}
}
getAvailableIntefaces();
// create HTTP agent to forward request
var agent = new http.Agent({
maxSockets:Infinity
});
// server initailisation
var server = http.createServer(function(req,res){
console.info("HTTP : ",req.url);
var options = url.parse(req.url);
options.localAddress = localWifiAddress;
options.method = req.method;
options.headers = req.headers;
options.agent = agent;
var serverRequest = http.request(options);
req.pipe(serverRequest);
serverRequest.on("response",function(serverResponse){
console.log("response for",req.url);
if(overwrideCors){
for(var header in serverResponse.headers){
//console.info("header", header, serverResponse.headers[header]);
if(header.toLowerCase() != "access-control-allow-origin" || header.toLowerCase() != "access-control-allow-credential"){
res.setHeader(header,serverResponse.headers[header]);
}
}
res.setHeader("Access-Control-Allow-Origin","*");
res.setHeader("Access-Control-Allow-Credential","true");
}else{
res.writeHead(serverResponse.headers);
}
res.writeHead(serverResponse.statusCode);
return serverResponse.pipe(res);
}).on("error",function(error){
res.writeHead(502);
return res.end();
});
}).listen(port);
// enable HTTPS threw socket communication
server.addListener("connect",function(req,socket,head){
console.info("HTTPS : ",req.url);
var parts = req.url.split(':', 2);
// open a TCP connection to the remote host
var options = {
port:parts[1],
host:parts[0],
localAddress:localWifiAddress
};
var conn = net.connect(options, function() {
// respond to the client that the connection was made
socket.write("HTTP/1.1 200 OK\r\n\r\n");
// create a tunnel between the two hosts
socket.pipe(conn);
conn.pipe(socket);
});
});
console.info("Server Started on port :", port);