-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.7 KB
/
index.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
var http = require('http');
var url = require('url');
var fs = require('fs');
var os = require('os');
var myLocalIP = localIP();
var port = 3000;
var proxyUrl = "http://" + myLocalIP + ":" + port + "/proxy.pac";
console.log("Set your device proxy to automatic and enter the URL");
console.log(proxyUrl);
http.createServer(function(req, res) {
if (req.method === 'GET' && req.url === '/proxy.pac') {
fs.readFile('./patterns.txt', 'utf8', function(err, data) {
if (err){
throw err;
}
var patterns = data.split("\n");
fs.readFile('./proxy.pac', 'utf8', function(err, data) {
if (err){
throw err;
}
res.writeHead(200, { 'Content-Type': 'application/x-javascript-config' });
data = data.replace(/\{LOCAL_IP\}/, myLocalIP);
data = data.replace(/\{PATTERNS\}/, patterns.map(pattern => 'shExpMatch(host, "' + pattern +'")').join(" || "));
res.end(data);
console.log("Returning Proxy Config for urls: " + patterns.join(", "));
return;
});
return;
});
}
else {
res.end("Set your device proxy to automatic and enter the URL: " + proxyUrl);
console.log("Set your device proxy to automatic and enter the URL: " + proxyUrl);
}
}).listen(port);
function localIP(){
var ifaces = os.networkInterfaces();
var ip;
Object.keys(ifaces).forEach(function (ifname) {
var alias = 0;
ifaces[ifname].forEach(function (iface) {
if ('IPv4' !== iface.family || iface.internal !== false) {
return;
}
ip = iface.address;
});
});
return ip;
}