@memberjunction/react-test-harness 6.3.0-edge.1 → 6.3.0-edge.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/browser-context.d.ts +70 -0
- package/dist/lib/browser-context.d.ts.map +1 -1
- package/dist/lib/browser-context.js +135 -7
- package/dist/lib/browser-context.js.map +1 -1
- package/dist/lib/component-runner.d.ts +30 -0
- package/dist/lib/component-runner.d.ts.map +1 -1
- package/dist/lib/component-runner.js +49 -1
- package/dist/lib/component-runner.js.map +1 -1
- package/dist/lib/network-policy.d.ts +67 -0
- package/dist/lib/network-policy.d.ts.map +1 -0
- package/dist/lib/network-policy.js +150 -0
- package/dist/lib/network-policy.js.map +1 -0
- package/dist/lib/network-proxy.d.ts +33 -0
- package/dist/lib/network-proxy.d.ts.map +1 -0
- package/dist/lib/network-proxy.js +126 -0
- package/dist/lib/network-proxy.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -15
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network policy for the test page.
|
|
3
|
+
*
|
|
4
|
+
* The component under test is generated code, and it runs with real data reached through the
|
|
5
|
+
* harness's exposed functions (`__mjRunView`, `__mjRunQuery`, ...). Those functions are Playwright
|
|
6
|
+
* bindings, not HTTP, so the page itself only needs the network to load the runtime and the
|
|
7
|
+
* registered component libraries from their CDNs. Requests to any other origin are blocked, so a
|
|
8
|
+
* component cannot send the rows it was given to an outside host, nor reach hosts on the network
|
|
9
|
+
* the harness runs in.
|
|
10
|
+
*
|
|
11
|
+
* Two layers enforce it. Request routing sees the page's HTTP requests and WebSockets, and records
|
|
12
|
+
* what it blocks for the page that made it. Some traffic never reaches routing: Speculation Rules
|
|
13
|
+
* prefetches, and WebRTC's connections to STUN and TURN servers. So every context the harness
|
|
14
|
+
* creates also sends its traffic through an allowlisting proxy (`AllowlistProxy`), and a browser
|
|
15
|
+
* the harness launches gets flags ({@link BROWSER_LAUNCH_ARGS}) that send WebRTC's TCP through that
|
|
16
|
+
* proxy and turn off its UDP. WebRTC is also removed from every frame
|
|
17
|
+
* ({@link DISABLE_WEBRTC_SCRIPT}), but only as a first line: a script can be raced.
|
|
18
|
+
*
|
|
19
|
+
* What is not covered: a browser the harness attaches to keeps its own launch flags, so WebRTC
|
|
20
|
+
* over UDP is not stopped there; a reused external context gets neither the proxy nor the
|
|
21
|
+
* context-wide route; an allowed origin is allowed as a whole; a redirect from an allowed origin
|
|
22
|
+
* is not re-checked by routing (the proxy does check it). This is a first layer, not a sandbox.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Chromium flags for a browser the harness launches with the filter on.
|
|
26
|
+
* - WebRTC may use only the proxy: its TCP goes through the proxy, which refuses hosts the
|
|
27
|
+
* allowlist does not allow, and its UDP, which no HTTP proxy can carry, is turned off.
|
|
28
|
+
* - DNS prefetching is off: a lookup of `<data>.attacker.example` carries data on its own.
|
|
29
|
+
*/
|
|
30
|
+
export const BROWSER_LAUNCH_ARGS = [
|
|
31
|
+
'--force-webrtc-ip-handling-policy=disable_non_proxied_udp',
|
|
32
|
+
'--webrtc-ip-handling-policy=disable_non_proxied_udp',
|
|
33
|
+
'--dns-prefetch-disable',
|
|
34
|
+
];
|
|
35
|
+
/**
|
|
36
|
+
* Init script for every filtered context (or page, in a reused context), run in each frame.
|
|
37
|
+
* Components have no use for WebRTC, so its constructors are removed. This is a first line only:
|
|
38
|
+
* Chrome can hand a sandboxed iframe a fresh `RTCPeerConnection` before the script runs, which
|
|
39
|
+
* is why the proxy and {@link BROWSER_LAUNCH_ARGS} back it at the network level.
|
|
40
|
+
*/
|
|
41
|
+
export const DISABLE_WEBRTC_SCRIPT = `for (const name of ['RTCPeerConnection', 'webkitRTCPeerConnection', 'RTCDataChannel', 'RTCIceCandidate', 'RTCSessionDescription', 'RTCRtpSender', 'RTCRtpReceiver', 'RTCRtpTransceiver']) {
|
|
42
|
+
try { delete globalThis[name]; } catch { /* not configurable in this browser: leave it */ }
|
|
43
|
+
}`;
|
|
44
|
+
/** Schemes that never leave the browser and are always allowed. */
|
|
45
|
+
const LOCAL_SCHEMES = new Set(['about:', 'data:', 'blob:']);
|
|
46
|
+
/** The host a URL names, lowercase, or null when the URL does not parse or has no host. */
|
|
47
|
+
export function HostOfUrl(url) {
|
|
48
|
+
try {
|
|
49
|
+
const host = new URL(url).hostname.toLowerCase();
|
|
50
|
+
return host.length > 0 ? host : null;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The port a parsed http(s) URL connects to, filling in the scheme's default. */
|
|
57
|
+
function effectivePort(url) {
|
|
58
|
+
return url.port || (url.protocol === 'http:' ? '80' : '443');
|
|
59
|
+
}
|
|
60
|
+
/** The entry `value` names, or null when it names no host. */
|
|
61
|
+
function parseEntry(value) {
|
|
62
|
+
const trimmed = value.trim();
|
|
63
|
+
if (trimmed.includes('://')) {
|
|
64
|
+
try {
|
|
65
|
+
const url = new URL(trimmed);
|
|
66
|
+
if ((url.protocol !== 'http:' && url.protocol !== 'https:') || !url.hostname) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
return { scheme: url.protocol, host: url.hostname.toLowerCase(), port: effectivePort(url) };
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const url = new URL(`https://${trimmed}`);
|
|
77
|
+
return url.hostname ? { scheme: null, host: url.hostname.toLowerCase(), port: url.port || null } : null;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function entryKey(entry) {
|
|
84
|
+
return `${entry.scheme ?? '*:'}//${entry.host}:${entry.port ?? '*'}`;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The origins and hosts the allowlist is given. One instance is shared by all pages of a
|
|
88
|
+
* {@link BrowserManager}; each page may also get its own, for a URL a caller navigates it to.
|
|
89
|
+
*/
|
|
90
|
+
export class NetworkAllowlist {
|
|
91
|
+
constructor(hostsOrUrls = []) {
|
|
92
|
+
this.entries = new Map();
|
|
93
|
+
this.AllowUrls(hostsOrUrls);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Allow each value. A URL ('https://cdn.example.com/lib.js') allows its origin only; a bare
|
|
97
|
+
* host name ('cdn.example.com') allows that host on any port, and 'host:port' that port only.
|
|
98
|
+
*/
|
|
99
|
+
AllowUrls(hostsOrUrls) {
|
|
100
|
+
for (const value of hostsOrUrls) {
|
|
101
|
+
const entry = parseEntry(value);
|
|
102
|
+
if (entry) {
|
|
103
|
+
this.entries.set(entryKey(entry), entry);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** The allowed entries, sorted, for logging: `scheme://host:port`, with `*` for any. */
|
|
108
|
+
get AllowedEntries() {
|
|
109
|
+
return [...this.entries.keys()].sort();
|
|
110
|
+
}
|
|
111
|
+
/** True when the page may make a request to `url`. */
|
|
112
|
+
IsAllowed(url) {
|
|
113
|
+
let parsed;
|
|
114
|
+
try {
|
|
115
|
+
parsed = new URL(url);
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
if (LOCAL_SCHEMES.has(parsed.protocol)) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
const host = parsed.hostname.toLowerCase();
|
|
127
|
+
const port = effectivePort(parsed);
|
|
128
|
+
for (const entry of this.entries.values()) {
|
|
129
|
+
if (entry.host === host && (entry.port === null || entry.port === port)
|
|
130
|
+
&& (entry.scheme === null || entry.scheme === parsed.protocol)) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The URL as it is recorded in a blocked-request report: origin and path only. The query string and
|
|
139
|
+
* fragment are dropped, because they are where an exfiltration attempt would carry the data.
|
|
140
|
+
*/
|
|
141
|
+
export function RedactUrl(url) {
|
|
142
|
+
try {
|
|
143
|
+
const parsed = new URL(url);
|
|
144
|
+
return parsed.origin === 'null' ? parsed.protocol : `${parsed.origin}${parsed.pathname}`;
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return '(unparseable URL)';
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=network-policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network-policy.js","sourceRoot":"","sources":["../../src/lib/network-policy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAsB;IACpD,2DAA2D;IAC3D,qDAAqD;IACrD,wBAAwB;CACzB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;;EAEnC,CAAC;AAEH,mEAAmE;AACnE,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;AAQ5D,2FAA2F;AAC3F,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;QACjD,OAAO,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IACvC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KAAK,CAAC;QACf,CAAC;QACD,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;QAC3C,MAAM,IAAI,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;QACnC,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;YAC1C,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;mBAClE,CAAC,KAAK,CAAC,MAAM,KAAK,IAAI,IAAI,KAAK,CAAC,MAAM,KAAK,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACjE,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW;IACnC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QAC5B,OAAO,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC3F,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,mBAAmB,CAAC;IAC7B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The network-level half of the test page's allowlist: an HTTP proxy on the loopback interface
|
|
3
|
+
* that every context the harness creates sends its traffic through.
|
|
4
|
+
*
|
|
5
|
+
* Request routing only sees what the page's network stack hands it. The browser also makes
|
|
6
|
+
* requests routing never sees: Speculation Rules prefetches, and WebRTC's connections to STUN and
|
|
7
|
+
* TURN servers. The proxy refuses any of them whose origin the allowlist does not allow, so they
|
|
8
|
+
* fail closed even though no route saw them. Plain-HTTP requests arrive in absolute form and are
|
|
9
|
+
* forwarded; HTTPS, WSS and TURN-over-TCP arrive as `CONNECT host:port` and are tunnelled.
|
|
10
|
+
*/
|
|
11
|
+
export declare class AllowlistProxy {
|
|
12
|
+
private readonly isAllowed;
|
|
13
|
+
private server;
|
|
14
|
+
private readonly sockets;
|
|
15
|
+
private readonly blocked;
|
|
16
|
+
/** @param isAllowed decides each request, given its URL (a `CONNECT` is checked as `https://host:port/`). */
|
|
17
|
+
constructor(isAllowed: (url: string) => boolean);
|
|
18
|
+
/** The proxy's address for Playwright's `proxy.server`. Throws before {@link Start}. */
|
|
19
|
+
get Server(): string;
|
|
20
|
+
/** What the proxy refused, oldest first, as origin + path (query strings dropped). */
|
|
21
|
+
get BlockedRequests(): string[];
|
|
22
|
+
/** Listens on a free loopback port. The server is unref'd, so it never keeps the process alive. */
|
|
23
|
+
Start(): Promise<void>;
|
|
24
|
+
/** Closes the listener and every connection still open through it. */
|
|
25
|
+
Stop(): Promise<void>;
|
|
26
|
+
/** Forwards an absolute-form plain-HTTP request, or refuses it with 403. */
|
|
27
|
+
private forward;
|
|
28
|
+
/** Opens a `CONNECT` tunnel to an allowed host and port, or refuses it with 403. */
|
|
29
|
+
private tunnel;
|
|
30
|
+
private refuse;
|
|
31
|
+
private track;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=network-proxy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network-proxy.d.ts","sourceRoot":"","sources":["../../src/lib/network-proxy.ts"],"names":[],"mappings":"AAIA;;;;;;;;;GASG;AACH,qBAAa,cAAc;IAMb,OAAO,CAAC,QAAQ,CAAC,SAAS;IALtC,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAC7C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAgB;IAExC,6GAA6G;gBAChF,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO;IAEhE,wFAAwF;IACxF,IAAI,MAAM,IAAI,MAAM,CAMnB;IAED,sFAAsF;IACtF,IAAI,eAAe,IAAI,MAAM,EAAE,CAE9B;IAED,mGAAmG;IAC7F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAe5B,sEAAsE;IAChE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAW3B,4EAA4E;IAC5E,OAAO,CAAC,OAAO;IA2Bf,oFAAoF;IACpF,OAAO,CAAC,MAAM;IAqBd,OAAO,CAAC,MAAM;IAKd,OAAO,CAAC,KAAK;CAId"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { createServer, request as httpRequest } from 'http';
|
|
2
|
+
import { connect as netConnect } from 'net';
|
|
3
|
+
import { RedactUrl } from './network-policy.js';
|
|
4
|
+
/**
|
|
5
|
+
* The network-level half of the test page's allowlist: an HTTP proxy on the loopback interface
|
|
6
|
+
* that every context the harness creates sends its traffic through.
|
|
7
|
+
*
|
|
8
|
+
* Request routing only sees what the page's network stack hands it. The browser also makes
|
|
9
|
+
* requests routing never sees: Speculation Rules prefetches, and WebRTC's connections to STUN and
|
|
10
|
+
* TURN servers. The proxy refuses any of them whose origin the allowlist does not allow, so they
|
|
11
|
+
* fail closed even though no route saw them. Plain-HTTP requests arrive in absolute form and are
|
|
12
|
+
* forwarded; HTTPS, WSS and TURN-over-TCP arrive as `CONNECT host:port` and are tunnelled.
|
|
13
|
+
*/
|
|
14
|
+
export class AllowlistProxy {
|
|
15
|
+
/** @param isAllowed decides each request, given its URL (a `CONNECT` is checked as `https://host:port/`). */
|
|
16
|
+
constructor(isAllowed) {
|
|
17
|
+
this.isAllowed = isAllowed;
|
|
18
|
+
this.server = null;
|
|
19
|
+
this.sockets = new Set();
|
|
20
|
+
this.blocked = [];
|
|
21
|
+
}
|
|
22
|
+
/** The proxy's address for Playwright's `proxy.server`. Throws before {@link Start}. */
|
|
23
|
+
get Server() {
|
|
24
|
+
const address = this.server?.address();
|
|
25
|
+
if (!address || typeof address === 'string') {
|
|
26
|
+
throw new Error('AllowlistProxy has not been started');
|
|
27
|
+
}
|
|
28
|
+
return `http://127.0.0.1:${address.port}`;
|
|
29
|
+
}
|
|
30
|
+
/** What the proxy refused, oldest first, as origin + path (query strings dropped). */
|
|
31
|
+
get BlockedRequests() {
|
|
32
|
+
return [...this.blocked];
|
|
33
|
+
}
|
|
34
|
+
/** Listens on a free loopback port. The server is unref'd, so it never keeps the process alive. */
|
|
35
|
+
async Start() {
|
|
36
|
+
if (this.server) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const server = createServer((req, res) => this.forward(req, res));
|
|
40
|
+
server.on('connect', (req, socket, head) => this.tunnel(req, socket, head));
|
|
41
|
+
server.on('connection', socket => this.track(socket));
|
|
42
|
+
await new Promise((resolve, reject) => {
|
|
43
|
+
server.once('error', reject);
|
|
44
|
+
server.listen(0, '127.0.0.1', () => resolve());
|
|
45
|
+
});
|
|
46
|
+
server.unref();
|
|
47
|
+
this.server = server;
|
|
48
|
+
}
|
|
49
|
+
/** Closes the listener and every connection still open through it. */
|
|
50
|
+
async Stop() {
|
|
51
|
+
const server = this.server;
|
|
52
|
+
this.server = null;
|
|
53
|
+
for (const socket of this.sockets) {
|
|
54
|
+
socket.destroy();
|
|
55
|
+
}
|
|
56
|
+
if (server) {
|
|
57
|
+
await new Promise(resolve => server.close(() => resolve()));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Forwards an absolute-form plain-HTTP request, or refuses it with 403. */
|
|
61
|
+
forward(req, res) {
|
|
62
|
+
const url = req.url ?? '';
|
|
63
|
+
if (!/^http:\/\//i.test(url) || !this.isAllowed(url)) {
|
|
64
|
+
this.refuse(url, () => res.writeHead(403, { 'Content-Type': 'text/plain' }).end('Blocked by test harness network policy'));
|
|
65
|
+
req.resume();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const target = new URL(url);
|
|
69
|
+
const headers = { ...req.headers };
|
|
70
|
+
delete headers['proxy-connection'];
|
|
71
|
+
delete headers['proxy-authorization'];
|
|
72
|
+
const upstream = httpRequest({ host: target.hostname, port: target.port || 80, method: req.method, path: `${target.pathname}${target.search}`, headers }, upstreamRes => {
|
|
73
|
+
res.writeHead(upstreamRes.statusCode ?? 502, upstreamRes.headers);
|
|
74
|
+
upstreamRes.pipe(res);
|
|
75
|
+
});
|
|
76
|
+
upstream.on('error', () => {
|
|
77
|
+
if (!res.headersSent) {
|
|
78
|
+
res.writeHead(502);
|
|
79
|
+
}
|
|
80
|
+
res.end();
|
|
81
|
+
});
|
|
82
|
+
req.pipe(upstream);
|
|
83
|
+
}
|
|
84
|
+
/** Opens a `CONNECT` tunnel to an allowed host and port, or refuses it with 403. */
|
|
85
|
+
tunnel(req, client, head) {
|
|
86
|
+
const authority = req.url ?? '';
|
|
87
|
+
const url = `https://${authority}/`;
|
|
88
|
+
const target = parseAuthority(authority);
|
|
89
|
+
client.on('error', () => client.destroy());
|
|
90
|
+
if (!target || !this.isAllowed(url)) {
|
|
91
|
+
this.refuse(url, () => client.end('HTTP/1.1 403 Forbidden\r\n\r\n'));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const upstream = netConnect(target.port, target.host, () => {
|
|
95
|
+
client.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
|
96
|
+
if (head.length > 0) {
|
|
97
|
+
upstream.write(head);
|
|
98
|
+
}
|
|
99
|
+
upstream.pipe(client);
|
|
100
|
+
client.pipe(upstream);
|
|
101
|
+
});
|
|
102
|
+
this.track(upstream);
|
|
103
|
+
upstream.on('error', () => client.end('HTTP/1.1 502 Bad Gateway\r\n\r\n'));
|
|
104
|
+
}
|
|
105
|
+
refuse(url, respond) {
|
|
106
|
+
this.blocked.push(RedactUrl(url));
|
|
107
|
+
respond();
|
|
108
|
+
}
|
|
109
|
+
track(socket) {
|
|
110
|
+
this.sockets.add(socket);
|
|
111
|
+
socket.on('close', () => this.sockets.delete(socket));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** The host and port of a `CONNECT` authority (`host:port`, `[v6]:port`), or null when malformed. */
|
|
115
|
+
function parseAuthority(authority) {
|
|
116
|
+
try {
|
|
117
|
+
const url = new URL(`https://${authority}/`);
|
|
118
|
+
const port = Number(url.port || 443);
|
|
119
|
+
const host = url.hostname.replace(/^\[|\]$/g, '');
|
|
120
|
+
return host && Number.isInteger(port) && port > 0 ? { host, port } : null;
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
//# sourceMappingURL=network-proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network-proxy.js","sourceRoot":"","sources":["../../src/lib/network-proxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,OAAO,IAAI,WAAW,EAA2C,MAAM,MAAM,CAAC;AACrG,OAAO,EAAE,OAAO,IAAI,UAAU,EAAU,MAAM,KAAK,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C;;;;;;;;;GASG;AACH,MAAM,OAAO,cAAc;IAKzB,6GAA6G;IAC7G,YAA6B,SAAmC;QAAnC,cAAS,GAAT,SAAS,CAA0B;QALxD,WAAM,GAAkB,IAAI,CAAC;QACpB,YAAO,GAAG,IAAI,GAAG,EAAU,CAAC;QAC5B,YAAO,GAAa,EAAE,CAAC;IAG2B,CAAC;IAEpE,wFAAwF;IACxF,IAAI,MAAM;QACR,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;QACvC,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YAC5C,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;QACzD,CAAC;QACD,OAAO,oBAAoB,OAAO,CAAC,IAAI,EAAE,CAAC;IAC5C,CAAC;IAED,sFAAsF;IACtF,IAA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