forked from evervault/evervault-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsHelper.js
More file actions
151 lines (140 loc) · 3.94 KB
/
httpsHelper.js
File metadata and controls
151 lines (140 loc) · 3.94 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const https = require('https');
const tls = require('tls');
const Datatypes = require('./datatypes');
const certHelper = require('./certHelper');
const HttpsProxyAgent = require('./proxyAgent');
const {
http: { proxiedMarker },
} = require('../config');
const origCreateSecureContext = tls.createSecureContext;
const EVERVAULT_DOMAINS = ['evervault.com', 'evervault.io', 'evervault.dev'];
const certificateUtil = (evClient) => {
let x509 = null;
async function updateCertificate() {
const pem = await evClient.getCert();
let cert = pem.toString();
x509 = certHelper.parseX509(cert);
tls.createSecureContext = (options) => {
const context = origCreateSecureContext(options);
context.context.addCACert(pem);
return context;
};
}
function isCertificateInvalid() {
if (!Datatypes.isDefined(x509)) {
return true;
}
const epoch = new Date().valueOf();
return (
epoch > new Date(x509.validTo).valueOf() ||
epoch < new Date(x509.validFrom).valueOf()
);
}
return {
updateCertificate,
isCertificateInvalid,
};
};
/**
*
* @param {Parameters<typeof import('node:https').request>} args
* @returns {{ domain: string, path: string }}
*/
function getDomainAndPathFromArgs(args) {
if (typeof args[0] === 'string') {
const parsedUrl = new URL(args[0]);
return { domain: parsedUrl.host, path: parsedUrl.pathname };
}
if (args[0] instanceof URL) {
return { domain: args[0].host, path: args[0].pathname };
}
let domain, path;
for (const arg of args) {
if (arg instanceof Object) {
domain = domain ?? arg.hostname ?? arg.host;
path = path ?? arg.pathname ?? arg.path;
}
}
return {
domain,
path,
};
}
/**
* @param {string} apiKey
* @param {string} tunnelHostname
* @param {(domain: string, path: string) => boolean} domainFilter
* @param {boolean} debugRequests
* @param {ReturnType<typeof import('../core/http')>} evClient
* @param {typeof import('node:https').request} originalRequest
* @returns {void}
*/
const overloadHttpsModule = (
apiKey,
tunnelHostname,
domainFilter,
debugRequests = false,
evClient,
originalRequest
) => {
/**
* @param {Parameters<typeof import('node:https').request>} args
* @returns {ReturnType<typeof import('node:https').request>}
*/
function wrapMethodRequest(...args) {
const { domain, path } = getDomainAndPathFromArgs(args);
const shouldProxy = !!domain && domainFilter(domain, path);
if (
debugRequests &&
!EVERVAULT_DOMAINS.some((evervault_domain) =>
domain.endsWith(evervault_domain)
)
) {
console.log(
`EVERVAULT DEBUG :: Request to domain: ${domain}${path}, Outbound Proxy enabled: ${shouldProxy}`
);
}
args = args.map((arg) => {
if (shouldProxy && arg instanceof Object) {
const { updateCertificate, isCertificateInvalid } =
certificateUtil(evClient);
arg.agent = new HttpsProxyAgent(
tunnelHostname,
updateCertificate,
isCertificateInvalid
);
arg.headers = { ...arg.headers, 'Proxy-Authorization': apiKey };
}
return arg;
});
const request = originalRequest.apply(this, args);
request[proxiedMarker] = shouldProxy;
return request;
}
https.request = wrapMethodRequest;
};
const httpsRelayAgent = (
agentConfig = { port: 443, rejectUnauthorized: true, secureProxy: true },
evClient,
apiKey
) => {
const { updateCertificate, isCertificateInvalid } = certificateUtil(evClient);
const parsedUrl = new URL(agentConfig.hostname);
const agent = new HttpsProxyAgent(
{
host: parsedUrl.hostname,
port: parsedUrl.port || agentConfig.port,
secureProxy: true,
auth: apiKey,
rejectUnauthorized: agentConfig.rejectUnauthorized,
},
updateCertificate,
isCertificateInvalid
);
return agent;
};
module.exports = {
overloadHttpsModule,
httpsRelayAgent,
getDomainAndPathFromArgs,
};