-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
102 lines (90 loc) · 2.63 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
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
const debug = require("debug")("windows-pac-resolver");
let lib;
function _loadModules() {
if (lib) {
return;
}
lib = require("bindings")("binding");
}
function isFilePath(path) {
return (0 === path.indexOf("\\\\")) || (":" === path[1] && ("\\" === path[2] || "/" === path[2]));
}
function generatePacFile(proxyDetails = "") {
let data = `function FindProxyForURL(url, host) {`;
if (typeof proxyDetails === "string" && proxyDetails.length > 0) {
proxyDetails = `return "PROXY ${proxyDetails}";`;
} else if (typeof proxyDetails === "object") {
if (0 === Object.keys(proxyDetails).length) {
proxyDetails = "";
} else {
proxyDetails = Object.keys(proxyDetails)
.reduce((prev, curr) => {
return `${prev} if (url.substr(0,${curr.length + 1}) === "${curr}:") { return "PROXY ${proxyDetails[curr]}"; }`
}, "");
}
}
return `${data} ${proxyDetails} return "DIRECT"; }`;
}
function extractProxyAddress(proxyDetails = "") {
if (proxyDetails.indexOf("=") === -1) {
return proxyDetails;
}
return proxyDetails
.split(";")
.reduce((prev, proxyProtocolDetails) => {
const [protocol = "", url = ""] = proxyProtocolDetails.split("=", 2);
if (protocol && url) {
prev[protocol] = url;
}
return prev;
}, {});
}
function generateDataURI(data) {
return `data:text/plain;base64,${Buffer.from(data).toString("base64")}`;
}
function switchToURI(path) {
if (!isFilePath(path)) {
return path;
}
if (0 !== path.indexOf("\\\\")) {
path = `/${path}`;
}
return `file://${path}`;
}
function toPACUri(path) {
if (!path) {
return "";
}
path = path.replace(/\\/g, "/")
return `pac+${(switchToURI(path) || "").trim()}`;
}
function getPACUri() {
_loadModules();
let ieProxyDetails;
try {
const rawDetails = lib.getIEProxyDetails().replace(/\\/g, "\\\\");
ieProxyDetails = JSON.parse(rawDetails);
debug(`Successfully retrieved IE proxy details: ${JSON.stringify(ieProxyDetails)}`);
} catch (e) {
debug(`Failed retrieving IE proxy details: ${e.message}`);
return;
}
if (!ieProxyDetails) {
return;
}
if (ieProxyDetails.autoConfigurationFileEnabled) {
return toPACUri(ieProxyDetails.autoConfigurationFile);
}
if (ieProxyDetails.manualConfigurationFile) {
return toPACUri(ieProxyDetails.manualConfigurationFile);
}
if (ieProxyDetails.manualProxyAddress) {
const proxyDetails = extractProxyAddress(ieProxyDetails.manualProxyAddress);
const proxyPacData = generatePacFile(proxyDetails);
const proxyPacDataURI = generateDataURI(proxyPacData);
return toPACUri(proxyPacDataURI);
}
}
module.exports = {
getPACUri,
}