1 | var url = require('url');
|
2 | var path = require('path');
|
3 |
|
4 | exports = module.exports = function () {
|
5 | return function (req, res, next) {
|
6 | var urlInfo = url.parse(req.url, true);
|
7 | var extname = path.extname(urlInfo.pathname);
|
8 | if (extname === '.pac') {
|
9 | var ipAddress = JSON.stringify(exports.localAddresses);
|
10 | var localAddress = req.connection.localAddress.split(':').pop();
|
11 | var pac = [
|
12 | 'var ipAddress = ' + ipAddress + ';',
|
13 | 'function FindProxyForURL (url, host) {',
|
14 | 'var host = host.split(":")[0].toLowerCase();',
|
15 | 'var url = url.toLowerCase();',
|
16 | 'if (ipAddress.indexOf(host) != -1 || url.substring(0, 6) === "https:") {',
|
17 | 'return "DIRECT;";',
|
18 | '}',
|
19 | 'return "PROXY ' + localAddress +':9000; DIRECT;";',
|
20 | '}'
|
21 | ].join('\n\r');
|
22 | res.writeHeader(200, {
|
23 | "Content-Type": "application/octet-stream"
|
24 | });
|
25 | res.end(pac);
|
26 | return ;
|
27 | }
|
28 | next();
|
29 | };
|
30 | };
|
31 | exports.localAddresses = [];
|
32 | var _ = require('underscore');
|
33 | _.each(require('os').networkInterfaces(), function (networkInterface) {
|
34 | _.each(networkInterface, function (item) {
|
35 | exports.localAddresses.push(item.address);
|
36 | });
|
37 | }); |
\ | No newline at end of file |