@decionis/agentsafe 0.2.0 → 0.2.1
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/README.md +10 -1
- package/dist/Cli.js +16 -7
- package/dist/Cli.js.map +1 -1
- package/dist/Index.d.ts +5 -1
- package/dist/Index.d.ts.map +1 -1
- package/dist/Index.js +5 -1
- package/dist/Index.js.map +1 -1
- package/dist/cli/CliProcess.d.ts +2 -0
- package/dist/cli/CliProcess.d.ts.map +1 -1
- package/dist/cli/CliProcess.js +9 -1
- package/dist/cli/CliProcess.js.map +1 -1
- package/dist/cli/Commands.d.ts +1 -1
- package/dist/cli/Commands.d.ts.map +1 -1
- package/dist/cli/Commands.js +5 -0
- package/dist/cli/Commands.js.map +1 -1
- package/dist/cli/Help.d.ts.map +1 -1
- package/dist/cli/Help.js +2 -0
- package/dist/cli/Help.js.map +1 -1
- package/dist/cli/Intercept.d.ts +30 -0
- package/dist/cli/Intercept.d.ts.map +1 -0
- package/dist/cli/Intercept.js +146 -0
- package/dist/cli/Intercept.js.map +1 -0
- package/dist/cli/Status.d.ts.map +1 -1
- package/dist/cli/Status.js +13 -1
- package/dist/cli/Status.js.map +1 -1
- package/dist/cli/TestCommand.d.ts +9 -0
- package/dist/cli/TestCommand.d.ts.map +1 -1
- package/dist/cli/TestCommand.js +8 -3
- package/dist/cli/TestCommand.js.map +1 -1
- package/dist/cli/VerifyIntent.d.ts +28 -0
- package/dist/cli/VerifyIntent.d.ts.map +1 -0
- package/dist/cli/VerifyIntent.js +136 -0
- package/dist/cli/VerifyIntent.js.map +1 -0
- package/dist/egress/TransparentDial.d.ts +17 -0
- package/dist/egress/TransparentDial.d.ts.map +1 -0
- package/dist/egress/TransparentDial.js +32 -0
- package/dist/egress/TransparentDial.js.map +1 -0
- package/dist/gateway/Activation.d.ts +10 -3
- package/dist/gateway/Activation.d.ts.map +1 -1
- package/dist/gateway/Activation.js +17 -2
- package/dist/gateway/Activation.js.map +1 -1
- package/dist/gateway/Gateway.d.ts +16 -0
- package/dist/gateway/Gateway.d.ts.map +1 -1
- package/dist/gateway/Gateway.js +33 -4
- package/dist/gateway/Gateway.js.map +1 -1
- package/dist/gateway/GatewayConfig.d.ts +7 -0
- package/dist/gateway/GatewayConfig.d.ts.map +1 -1
- package/dist/gateway/GatewayConfig.js +12 -7
- package/dist/gateway/GatewayConfig.js.map +1 -1
- package/dist/gateway/GatewayReport.d.ts +2 -1
- package/dist/gateway/GatewayReport.d.ts.map +1 -1
- package/dist/gateway/GatewayReport.js +3 -0
- package/dist/gateway/GatewayReport.js.map +1 -1
- package/dist/gateway/InstallSurface.d.ts +26 -0
- package/dist/gateway/InstallSurface.d.ts.map +1 -0
- package/dist/gateway/InstallSurface.js +53 -0
- package/dist/gateway/InstallSurface.js.map +1 -0
- package/dist/gateway/ShadowLedger.d.ts +55 -0
- package/dist/gateway/ShadowLedger.d.ts.map +1 -0
- package/dist/gateway/ShadowLedger.js +82 -0
- package/dist/gateway/ShadowLedger.js.map +1 -0
- package/dist/http/InterceptServer.d.ts +94 -0
- package/dist/http/InterceptServer.d.ts.map +1 -0
- package/dist/http/InterceptServer.js +252 -0
- package/dist/http/InterceptServer.js.map +1 -0
- package/dist/intercept/ClientHello.d.ts +37 -0
- package/dist/intercept/ClientHello.d.ts.map +1 -0
- package/dist/intercept/ClientHello.js +212 -0
- package/dist/intercept/ClientHello.js.map +1 -0
- package/dist/intercept/Destination.d.ts +35 -0
- package/dist/intercept/Destination.d.ts.map +1 -0
- package/dist/intercept/Destination.js +62 -0
- package/dist/intercept/Destination.js.map +1 -0
- package/dist/intercept/InterceptLedger.d.ts +67 -0
- package/dist/intercept/InterceptLedger.d.ts.map +1 -0
- package/dist/intercept/InterceptLedger.js +80 -0
- package/dist/intercept/InterceptLedger.js.map +1 -0
- package/dist/intercept/RequestHead.d.ts +48 -0
- package/dist/intercept/RequestHead.d.ts.map +1 -0
- package/dist/intercept/RequestHead.js +151 -0
- package/dist/intercept/RequestHead.js.map +1 -0
- package/dist/verify/VerifyIntentVector.d.ts +25 -0
- package/dist/verify/VerifyIntentVector.d.ts.map +1 -0
- package/dist/verify/VerifyIntentVector.js +188 -0
- package/dist/verify/VerifyIntentVector.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which distribution this process was installed from, as one closed token,
|
|
3
|
+
* so a hosted call can say `surface=homebrew` beside the version it already
|
|
4
|
+
* carries and the funnel can be read per surface: installs from the release
|
|
5
|
+
* downloads, first governed actions from the authority's own record. The
|
|
6
|
+
* token is the whole of what is derived: the paths looked at to derive it
|
|
7
|
+
* are never reported, and nothing is derived from the network, the user or
|
|
8
|
+
* the machine. A distribution that runs the process itself names the
|
|
9
|
+
* surface in `AGENTSAFE_SURFACE` (the image, the chart, the systemd unit);
|
|
10
|
+
* the others are recognised by where the executable sits. An unknown value
|
|
11
|
+
* in the variable, or a location that is none of these, is no surface at
|
|
12
|
+
* all rather than a guess.
|
|
13
|
+
*/
|
|
14
|
+
export const INSTALL_SURFACES = [
|
|
15
|
+
"homebrew",
|
|
16
|
+
"linux",
|
|
17
|
+
"installer",
|
|
18
|
+
"docker",
|
|
19
|
+
"kubernetes",
|
|
20
|
+
"npm",
|
|
21
|
+
"source",
|
|
22
|
+
];
|
|
23
|
+
export const SURFACE_ENVIRONMENT = "AGENTSAFE_SURFACE";
|
|
24
|
+
function isSurface(value) {
|
|
25
|
+
return INSTALL_SURFACES.includes(value ?? "");
|
|
26
|
+
}
|
|
27
|
+
export function installSurface(facts) {
|
|
28
|
+
const named = facts.env[SURFACE_ENVIRONMENT]?.trim().toLowerCase();
|
|
29
|
+
if (named !== undefined && named !== "")
|
|
30
|
+
return isSurface(named) ? named : null;
|
|
31
|
+
const locations = [facts.execPath, facts.script ?? ""].map((path) => path.replace(/\\/g, "/"));
|
|
32
|
+
const at = (fragment) => locations.some((path) => path.includes(fragment));
|
|
33
|
+
// Homebrew keeps the release archive under the Cellar and links the bin.
|
|
34
|
+
if (at("/Cellar/agentsafe/"))
|
|
35
|
+
return "homebrew";
|
|
36
|
+
// The installer puts the archive under <prefix>/lib/agentsafe/<version>/.
|
|
37
|
+
if (at("/lib/agentsafe/"))
|
|
38
|
+
return "installer";
|
|
39
|
+
// The .deb and .rpm place the one executable at /usr/bin/agentsafe.
|
|
40
|
+
if (locations.some((path) => path.endsWith("/usr/bin/agentsafe")))
|
|
41
|
+
return "linux";
|
|
42
|
+
// A package install runs the bin out of node_modules; a checkout out of packages/.
|
|
43
|
+
if (at("/node_modules/"))
|
|
44
|
+
return "npm";
|
|
45
|
+
if (at("/packages/agentsafe/"))
|
|
46
|
+
return "source";
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
/** The surface of the running process, from its own environment and location. */
|
|
50
|
+
export function processSurface(env = process.env) {
|
|
51
|
+
return installSurface({ env, execPath: process.execPath, script: process.argv[1] });
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=InstallSurface.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InstallSurface.js","sourceRoot":"","sources":["../../src/gateway/InstallSurface.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,UAAU;IACV,OAAO;IACP,WAAW;IACX,QAAQ;IACR,YAAY;IACZ,KAAK;IACL,QAAQ;CACA,CAAC;AAIX,MAAM,CAAC,MAAM,mBAAmB,GAAG,mBAAmB,CAAC;AASvD,SAAS,SAAS,CAAC,KAAyB;IAC1C,OAAQ,gBAAsC,CAAC,QAAQ,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,KAAmB;IAChD,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,mBAAmB,CAAC,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACnE,IAAI,KAAK,KAAK,SAA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/**
|
|
2
|
+
* What shadow mode found, kept by the gateway itself: for every consequential
|
|
3
|
+
* request it forwarded unchanged, what the authority said it would have
|
|
4
|
+
* decided, counted by verdict and by action name, with the first and last
|
|
5
|
+
* time. It is the report an operator reads before turning enforcement on,
|
|
6
|
+
* and it is counts only: the action names are the route table's, the
|
|
7
|
+
* verdicts are the authority's, and no path, body, parameter or identifier
|
|
8
|
+
* is kept. The authority keeps its own record of the same observations, with
|
|
9
|
+
* the dossiers; this is the gateway's, available where the gateway runs.
|
|
10
|
+
*/
|
|
11
|
+
import type { GatewayConfig } from "./GatewayConfig.js";
|
|
12
|
+
export type ShadowVerdict = "ALLOW" | "ESCALATE" | "BLOCK" | "NONE";
|
|
13
|
+
export interface ShadowCounts {
|
|
14
|
+
readonly ALLOW: number;
|
|
15
|
+
readonly ESCALATE: number;
|
|
16
|
+
readonly BLOCK: number;
|
|
17
|
+
/** The authority could not be asked, or did not answer in time: no verdict to count. */
|
|
18
|
+
readonly NONE: number;
|
|
19
|
+
}
|
|
20
|
+
export interface ShadowSummary {
|
|
21
|
+
/** When the first and the last observation settled; null before any did. */
|
|
22
|
+
readonly since: string | null;
|
|
23
|
+
readonly until: string | null;
|
|
24
|
+
readonly observed: number;
|
|
25
|
+
readonly would: ShadowCounts;
|
|
26
|
+
readonly by_action: Readonly<Record<string, ShadowCounts>>;
|
|
27
|
+
}
|
|
28
|
+
/** What `agentsafe` prints on a terminal for a shadow summary, and the switch it ends with. */
|
|
29
|
+
export interface ShadowReport {
|
|
30
|
+
readonly event: "SHADOW_REPORT";
|
|
31
|
+
readonly at: string;
|
|
32
|
+
readonly shadow: ShadowSummary;
|
|
33
|
+
/** How enforcement is turned on for this configuration, in the operator's own terms. */
|
|
34
|
+
readonly enforce: string;
|
|
35
|
+
}
|
|
36
|
+
export declare class ShadowLedger {
|
|
37
|
+
private since;
|
|
38
|
+
private until;
|
|
39
|
+
private observed;
|
|
40
|
+
private would;
|
|
41
|
+
private readonly byAction;
|
|
42
|
+
record(action: string, verdict: ShadowVerdict | null, at: string): void;
|
|
43
|
+
summary(): ShadowSummary;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The one control the report ends with, in the terms this gateway was
|
|
47
|
+
* configured in: a provisional workspace cannot enforce at all, a file
|
|
48
|
+
* changes its own line, and the flag or the variable serves the rest.
|
|
49
|
+
*/
|
|
50
|
+
export declare function enforcementSwitch(config: GatewayConfig): string;
|
|
51
|
+
/** The report for a terminal: the counts, the actions, what enforcement would have changed, and the switch. */
|
|
52
|
+
export declare function renderShadowReport(report: ShadowReport, options: {
|
|
53
|
+
readonly color: boolean;
|
|
54
|
+
}): string;
|
|
55
|
+
//# sourceMappingURL=ShadowLedger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShadowLedger.d.ts","sourceRoot":"","sources":["../../src/gateway/ShadowLedger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAExD,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,UAAU,GAAG,OAAO,GAAG,MAAM,CAAC;AAEpE,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,wFAAwF;IACxF,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,aAAa;IAC5B,4EAA4E;IAC5E,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;CAC5D;AAED,+FAA+F;AAC/F,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC;IAChC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,wFAAwF;IACxF,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAMD,qBAAa,YAAY;IACvB,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmC;IAErD,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,GAAG,IAAI,EAAE,EAAE,EAAE,MAAM,GAAG,IAAI;IAWvE,OAAO,IAAI,aAAa;CAWhC;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa,GAAG,MAAM,CAY/D;AAWD,+GAA+G;AAC/G,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GACnC,MAAM,CAiER"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const ZERO = { ALLOW: 0, ESCALATE: 0, BLOCK: 0, NONE: 0 };
|
|
2
|
+
/** The most action names the ledger keeps apart; the route table bounds them long before this. */
|
|
3
|
+
const MAX_ACTIONS = 1_000;
|
|
4
|
+
export class ShadowLedger {
|
|
5
|
+
since = null;
|
|
6
|
+
until = null;
|
|
7
|
+
observed = 0;
|
|
8
|
+
would = ZERO;
|
|
9
|
+
byAction = new Map();
|
|
10
|
+
record(action, verdict, at) {
|
|
11
|
+
const outcome = verdict ?? "NONE";
|
|
12
|
+
this.since ??= at;
|
|
13
|
+
this.until = at;
|
|
14
|
+
this.observed += 1;
|
|
15
|
+
this.would = { ...this.would, [outcome]: this.would[outcome] + 1 };
|
|
16
|
+
const name = this.byAction.has(action) || this.byAction.size < MAX_ACTIONS ? action : "other";
|
|
17
|
+
const counts = this.byAction.get(name) ?? ZERO;
|
|
18
|
+
this.byAction.set(name, { ...counts, [outcome]: counts[outcome] + 1 });
|
|
19
|
+
}
|
|
20
|
+
summary() {
|
|
21
|
+
return {
|
|
22
|
+
since: this.since,
|
|
23
|
+
until: this.until,
|
|
24
|
+
observed: this.observed,
|
|
25
|
+
would: this.would,
|
|
26
|
+
by_action: Object.fromEntries([...this.byAction.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The one control the report ends with, in the terms this gateway was
|
|
32
|
+
* configured in: a provisional workspace cannot enforce at all, a file
|
|
33
|
+
* changes its own line, and the flag or the variable serves the rest.
|
|
34
|
+
*/
|
|
35
|
+
export function enforcementSwitch(config) {
|
|
36
|
+
if (config.authority.provisional) {
|
|
37
|
+
return "a provisional workspace evaluates in shadow only; for enforcement, run `agentsafe login` with a key from your Decionis organization, then `agentsafe proxy --mode enforcement`";
|
|
38
|
+
}
|
|
39
|
+
switch (config.sources["authority.mode"]) {
|
|
40
|
+
case "file":
|
|
41
|
+
return "set `authority.mode: enforcement` in agentsafe.yaml (`gateway.mode: enforcement` in the chart's values) and restart";
|
|
42
|
+
case "environment":
|
|
43
|
+
return "set `AGENTSAFE_MODE=enforcement` and restart";
|
|
44
|
+
default:
|
|
45
|
+
return "`agentsafe proxy --mode enforcement`, or `AGENTSAFE_MODE=enforcement`, or `authority.mode: enforcement` in agentsafe.yaml";
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const ESC = String.fromCharCode(27);
|
|
49
|
+
const RESET = `${ESC}[0m`;
|
|
50
|
+
const BOLD = `${ESC}[1m`;
|
|
51
|
+
const DIM = `${ESC}[2m`;
|
|
52
|
+
function plural(count, noun) {
|
|
53
|
+
return `${String(count)} ${noun}${count === 1 ? "" : "s"}`;
|
|
54
|
+
}
|
|
55
|
+
/** The report for a terminal: the counts, the actions, what enforcement would have changed, and the switch. */
|
|
56
|
+
export function renderShadowReport(report, options) {
|
|
57
|
+
const bold = (text) => (options.color ? `${BOLD}${text}${RESET}` : text);
|
|
58
|
+
const dim = (text) => (options.color ? `${DIM}${text}${RESET}` : text);
|
|
59
|
+
const row = (label, value) => `${label.padEnd(12)} ${value}`;
|
|
60
|
+
const { shadow } = report;
|
|
61
|
+
const lines = [bold("Shadow report"), ""];
|
|
62
|
+
if (shadow.observed === 0) {
|
|
63
|
+
lines.push(row("Observed", "no consequential action yet; every one will be forwarded unchanged and counted here"), "", row("Turn it on", report.enforce), "");
|
|
64
|
+
return lines.join("\n");
|
|
65
|
+
}
|
|
66
|
+
const { would } = shadow;
|
|
67
|
+
lines.push(row("Since", `${shadow.since ?? ""}${shadow.until === shadow.since ? "" : ` until ${shadow.until ?? ""}`}`), row("Observed", `${plural(shadow.observed, "consequential action")}, every one forwarded unchanged`), row("Would ALLOW", String(would.ALLOW)), row("Would hold", `${String(would.ESCALATE)}${would.ESCALATE === 0 ? "" : dim(" ESCALATE: a person would have been asked first")}`), row("Would BLOCK", `${String(would.BLOCK)}${would.BLOCK === 0 ? "" : dim(" nothing would have reached the upstream")}`), row("No verdict", `${String(would.NONE)}${would.NONE === 0 ? "" : dim(" the authority could not be asked, or not in time")}`));
|
|
68
|
+
const actions = Object.entries(shadow.by_action);
|
|
69
|
+
if (actions.length > 0) {
|
|
70
|
+
const width = Math.max(...actions.map(([name]) => name.length));
|
|
71
|
+
actions.forEach(([name, counts], index) => {
|
|
72
|
+
const total = counts.ALLOW + counts.ESCALATE + counts.BLOCK + counts.NONE;
|
|
73
|
+
lines.push(row(index === 0 ? "By action" : "", `${name.padEnd(width)} ${String(total).padStart(5)} ${dim(`allow ${String(counts.ALLOW)} hold ${String(counts.ESCALATE)} block ${String(counts.BLOCK)}${counts.NONE === 0 ? "" : ` none ${String(counts.NONE)}`}`)}`));
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const changed = would.ESCALATE + would.BLOCK;
|
|
77
|
+
lines.push("", changed === 0
|
|
78
|
+
? `Enforcement would have changed nothing: all ${String(shadow.observed)} would have gone through as they did.`
|
|
79
|
+
: `Enforcement would have held ${String(would.ESCALATE)} and refused ${String(would.BLOCK)} of ${String(shadow.observed)}; ${String(would.ALLOW)} would have gone through as they did.`, row("Turn it on", report.enforce), "");
|
|
80
|
+
return lines.join("\n");
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=ShadowLedger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShadowLedger.js","sourceRoot":"","sources":["../../src/gateway/ShadowLedger.ts"],"names":[],"mappings":"AAwCA,MAAM,IAAI,GAAiB,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AACxE,kGAAkG;AAClG,MAAM,WAAW,GAAG,KAAK,CAAC;AAE1B,MAAM,OAAO,YAAY;IACf,KAAK,GAAkB,IAAI,CAAC;IAC5B,KAAK,GAAkB,IAAI,CAAC;IAC5B,QAAQ,GAAG,CAAC,CAAC;IACb,KAAK,GAAiB,IAAI,CAAC;IAClB,QAAQ,GAAG,IAAI,GAAG,EAAwB,CAAC;IAErD,MAAM,CAAC,MAAc,EAAE,OAA6B,EAAE,EAAU;QACrE,MAAM,OAAO,GAAkB,OAAO,IAAI,MAAM,CAAC;QACjD,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC;QAClB,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,QAAQ,IAAI,CAAC,CAAC;QACnB,IAAI,CAAC,KAAK,GAAG,EAAE,GAAG,IAAI,CAA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,KAAK,CAAC;IAC7C,KAAK,CAAC,IAAI,CACR,EAAE,EACF,OAAO,KAAK,CAAC;QACX,CAAC,CAAC,+CAA+C,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,uCAAuC;QAC/G,CAAC,CAAC,+BAA+B,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,gBAAgB,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,uCAAuC,EACzL,GAAG,CAAC,YAAY,EAAE,MAAM,CAAC,OAAO,CAAC,EACjC,EAAE,CACH,CAAC;IACF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type Dial } from "../egress/TransparentDial.js";
|
|
2
|
+
import { type InterceptProtocol } from "../intercept/Destination.js";
|
|
3
|
+
import { type InterceptRefusal, type InterceptReport } from "../intercept/InterceptLedger.js";
|
|
4
|
+
/** One listener: the port it binds, and the port the redirected connections were addressed to. */
|
|
5
|
+
export interface InterceptListenerSpec {
|
|
6
|
+
readonly port: number;
|
|
7
|
+
readonly destinationPort: number;
|
|
8
|
+
}
|
|
9
|
+
export interface InterceptorOptions {
|
|
10
|
+
/** The address the listeners bind; loopback, since REDIRECT delivers locally generated packets there. */
|
|
11
|
+
readonly bind: string;
|
|
12
|
+
readonly listeners: readonly InterceptListenerSpec[];
|
|
13
|
+
/** How long a client has to send bytes that name a destination. */
|
|
14
|
+
readonly peekTimeoutMs: number;
|
|
15
|
+
readonly connectTimeoutMs: number;
|
|
16
|
+
/** How long a spliced connection may stay silent in both directions. */
|
|
17
|
+
readonly idleTimeoutMs: number;
|
|
18
|
+
readonly maxConnections: number;
|
|
19
|
+
}
|
|
20
|
+
export declare const INTERCEPT_DEFAULTS: {
|
|
21
|
+
readonly bind: "127.0.0.1";
|
|
22
|
+
readonly httpPort: 15001;
|
|
23
|
+
readonly httpsPort: 15002;
|
|
24
|
+
readonly peekTimeoutMs: 5000;
|
|
25
|
+
readonly connectTimeoutMs: 10000;
|
|
26
|
+
readonly idleTimeoutMs: 300000;
|
|
27
|
+
readonly maxConnections: 10000;
|
|
28
|
+
};
|
|
29
|
+
export interface BoundListener {
|
|
30
|
+
readonly address: string;
|
|
31
|
+
readonly port: number;
|
|
32
|
+
readonly destinationPort: number;
|
|
33
|
+
}
|
|
34
|
+
export type InterceptEvent = {
|
|
35
|
+
readonly event: "INTERCEPT_STARTED";
|
|
36
|
+
readonly at: string;
|
|
37
|
+
readonly listeners: readonly {
|
|
38
|
+
readonly listen: string;
|
|
39
|
+
readonly for_port: number;
|
|
40
|
+
}[];
|
|
41
|
+
} | {
|
|
42
|
+
readonly event: "INTERCEPT_OBSERVED";
|
|
43
|
+
readonly at: string;
|
|
44
|
+
readonly protocol: InterceptProtocol;
|
|
45
|
+
readonly host: string;
|
|
46
|
+
readonly port: number;
|
|
47
|
+
readonly method?: string;
|
|
48
|
+
readonly target?: string;
|
|
49
|
+
readonly alpn?: readonly string[];
|
|
50
|
+
} | {
|
|
51
|
+
readonly event: "INTERCEPT_REFUSED";
|
|
52
|
+
readonly at: string;
|
|
53
|
+
readonly reason: InterceptRefusal;
|
|
54
|
+
readonly protocol: InterceptProtocol | null;
|
|
55
|
+
readonly host?: string;
|
|
56
|
+
readonly port?: number;
|
|
57
|
+
readonly detail?: string;
|
|
58
|
+
} | InterceptReport | {
|
|
59
|
+
readonly event: "INTERCEPT_STOPPED";
|
|
60
|
+
readonly at: string;
|
|
61
|
+
readonly signal: string;
|
|
62
|
+
};
|
|
63
|
+
export interface InterceptorDependencies {
|
|
64
|
+
readonly emit: (event: InterceptEvent) => void;
|
|
65
|
+
readonly dial?: Dial;
|
|
66
|
+
readonly now?: () => Date;
|
|
67
|
+
}
|
|
68
|
+
export declare class Interceptor {
|
|
69
|
+
private readonly options;
|
|
70
|
+
private readonly ledger;
|
|
71
|
+
private readonly servers;
|
|
72
|
+
private readonly bound;
|
|
73
|
+
private readonly active;
|
|
74
|
+
private readonly dial;
|
|
75
|
+
private readonly now;
|
|
76
|
+
private readonly emit;
|
|
77
|
+
constructor(options: InterceptorOptions, dependencies: InterceptorDependencies);
|
|
78
|
+
/** Binds every listener; a port that cannot be bound closes the ones already bound and rejects. */
|
|
79
|
+
listen(): Promise<readonly BoundListener[]>;
|
|
80
|
+
listeners(): readonly BoundListener[];
|
|
81
|
+
report(): InterceptReport;
|
|
82
|
+
/** Stops accepting, drops every connection, and waits for the listeners to close. */
|
|
83
|
+
close(): Promise<void>;
|
|
84
|
+
private accept;
|
|
85
|
+
/** Reads the client's first bytes until they name a destination, or refuse one, or run out of time. */
|
|
86
|
+
private peek;
|
|
87
|
+
private place;
|
|
88
|
+
/** Both directions, with back-pressure, half-close, an idle bound, and the bytes counted once at the end. */
|
|
89
|
+
private splice;
|
|
90
|
+
private refuse;
|
|
91
|
+
/** Whether a destination is one of this interceptor's own listeners: a connection that would loop. */
|
|
92
|
+
private isSelf;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=InterceptServer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InterceptServer.d.ts","sourceRoot":"","sources":["../../src/http/InterceptServer.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAmB,KAAK,IAAI,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAGL,KAAK,iBAAiB,EACvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACrB,MAAM,iCAAiC,CAAC;AAEzC,kGAAkG;AAClG,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CAClC;AAED,MAAM,WAAW,kBAAkB;IACjC,yGAAyG;IACzG,QAAQ,CAAC,IAA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xE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAenC,OAAO,CAAC,MAAM;IAWd,uGAAuG;IACvG,OAAO,CAAC,IAAI;YAmCE,KAAK;IAiDnB,6GAA6G;IAC7G,OAAO,CAAC,MAAM;IA2Cd,OAAO,CAAC,MAAM;IAqBd,sGAAsG;IACtG,OAAO,CAAC,MAAM;CAIf"}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The transparent interceptor: the hop a workload's outbound HTTP and TLS
|
|
3
|
+
* connections are redirected into by the network (an iptables REDIRECT in the
|
|
4
|
+
* pod or container, written by `packaging/intercept/Redirect.sh`) without the
|
|
5
|
+
* workload knowing. Each connection arrives with its destination rewritten to
|
|
6
|
+
* this process, so the interceptor reads the destination from the client's
|
|
7
|
+
* own first bytes, the TLS server name or the HTTP host, dials it, and splices
|
|
8
|
+
* the two sockets. A connection whose destination no byte names is refused:
|
|
9
|
+
* the interceptor forwards to what the client asked for, or to nothing.
|
|
10
|
+
*
|
|
11
|
+
* This is the observe phase. Nothing is decrypted, nothing is decided and no
|
|
12
|
+
* request is altered; what is kept is where the workload went, in the ledger,
|
|
13
|
+
* so an operator learns what a workload reaches before governing it. The
|
|
14
|
+
* govern phase terminates TLS for the destinations an operator lists and runs
|
|
15
|
+
* the gateway's lifecycle over them; it is the same hop, and the same ledger.
|
|
16
|
+
*/
|
|
17
|
+
import { createServer } from "node:net";
|
|
18
|
+
import { transparentDial } from "../egress/TransparentDial.js";
|
|
19
|
+
import { readDestination, } from "../intercept/Destination.js";
|
|
20
|
+
import { InterceptLedger, } from "../intercept/InterceptLedger.js";
|
|
21
|
+
export const INTERCEPT_DEFAULTS = {
|
|
22
|
+
bind: "127.0.0.1",
|
|
23
|
+
httpPort: 15_001,
|
|
24
|
+
httpsPort: 15_002,
|
|
25
|
+
peekTimeoutMs: 5_000,
|
|
26
|
+
connectTimeoutMs: 10_000,
|
|
27
|
+
idleTimeoutMs: 300_000,
|
|
28
|
+
maxConnections: 10_000,
|
|
29
|
+
};
|
|
30
|
+
const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "::1", "[::1]", "0.0.0.0"]);
|
|
31
|
+
export class Interceptor {
|
|
32
|
+
options;
|
|
33
|
+
ledger = new InterceptLedger();
|
|
34
|
+
servers = [];
|
|
35
|
+
bound = [];
|
|
36
|
+
active = new Set();
|
|
37
|
+
dial;
|
|
38
|
+
now;
|
|
39
|
+
emit;
|
|
40
|
+
constructor(options, dependencies) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.emit = dependencies.emit;
|
|
43
|
+
this.dial = dependencies.dial ?? transparentDial;
|
|
44
|
+
this.now = dependencies.now ?? (() => new Date());
|
|
45
|
+
}
|
|
46
|
+
/** Binds every listener; a port that cannot be bound closes the ones already bound and rejects. */
|
|
47
|
+
async listen() {
|
|
48
|
+
try {
|
|
49
|
+
for (const spec of this.options.listeners) {
|
|
50
|
+
// Half-open stays ours to handle: a client that finishes sending keeps
|
|
51
|
+
// reading the answer, and the destination's end is the client's end.
|
|
52
|
+
const server = createServer({ noDelay: true, allowHalfOpen: true }, (socket) => this.accept(socket, spec));
|
|
53
|
+
await new Promise((resolve, reject) => {
|
|
54
|
+
server.once("error", reject);
|
|
55
|
+
server.listen(spec.port, this.options.bind, () => {
|
|
56
|
+
server.removeListener("error", reject);
|
|
57
|
+
resolve();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
const address = server.address();
|
|
61
|
+
this.servers.push(server);
|
|
62
|
+
this.bound.push({
|
|
63
|
+
address: address.address,
|
|
64
|
+
port: address.port,
|
|
65
|
+
destinationPort: spec.destinationPort,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
await this.close();
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
this.emit({
|
|
74
|
+
event: "INTERCEPT_STARTED",
|
|
75
|
+
at: this.now().toISOString(),
|
|
76
|
+
listeners: this.bound.map((listener) => ({
|
|
77
|
+
listen: `${listener.address}:${listener.port}`,
|
|
78
|
+
for_port: listener.destinationPort,
|
|
79
|
+
})),
|
|
80
|
+
});
|
|
81
|
+
return this.bound;
|
|
82
|
+
}
|
|
83
|
+
listeners() {
|
|
84
|
+
return this.bound;
|
|
85
|
+
}
|
|
86
|
+
report() {
|
|
87
|
+
return this.ledger.report(this.now().toISOString());
|
|
88
|
+
}
|
|
89
|
+
/** Stops accepting, drops every connection, and waits for the listeners to close. */
|
|
90
|
+
async close() {
|
|
91
|
+
for (const socket of this.active)
|
|
92
|
+
socket.destroy();
|
|
93
|
+
this.active.clear();
|
|
94
|
+
const closing = this.servers.splice(0);
|
|
95
|
+
this.bound.splice(0);
|
|
96
|
+
await Promise.all(closing.map((server) => new Promise((resolve) => {
|
|
97
|
+
server.close(() => resolve());
|
|
98
|
+
})));
|
|
99
|
+
}
|
|
100
|
+
accept(client, spec) {
|
|
101
|
+
client.on("error", () => undefined);
|
|
102
|
+
if (this.active.size >= this.options.maxConnections) {
|
|
103
|
+
this.refuse(client, "TOO_MANY_CONNECTIONS", null, undefined, undefined, true);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
this.active.add(client);
|
|
107
|
+
client.once("close", () => this.active.delete(client));
|
|
108
|
+
this.peek(client, spec);
|
|
109
|
+
}
|
|
110
|
+
/** Reads the client's first bytes until they name a destination, or refuse one, or run out of time. */
|
|
111
|
+
peek(client, spec) {
|
|
112
|
+
const chunks = [];
|
|
113
|
+
let length = 0;
|
|
114
|
+
const finish = () => {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
client.removeListener("data", onData);
|
|
117
|
+
client.removeListener("end", onEnd);
|
|
118
|
+
client.removeListener("close", onEnd);
|
|
119
|
+
};
|
|
120
|
+
const timer = setTimeout(() => {
|
|
121
|
+
finish();
|
|
122
|
+
this.refuse(client, "PEEK_TIMEOUT", null, undefined, undefined, true);
|
|
123
|
+
}, this.options.peekTimeoutMs);
|
|
124
|
+
const onEnd = () => {
|
|
125
|
+
finish();
|
|
126
|
+
this.refuse(client, "DESTINATION_UNKNOWN", null, undefined, undefined, true);
|
|
127
|
+
};
|
|
128
|
+
const onData = (chunk) => {
|
|
129
|
+
chunks.push(chunk);
|
|
130
|
+
length += chunk.length;
|
|
131
|
+
const bytes = Buffer.concat(chunks, length);
|
|
132
|
+
const reading = readDestination(bytes, spec.destinationPort);
|
|
133
|
+
if (reading.kind === "NEED_MORE")
|
|
134
|
+
return;
|
|
135
|
+
finish();
|
|
136
|
+
if (reading.kind === "REFUSED") {
|
|
137
|
+
this.refuse(client, reading.reason, reading.protocol, undefined, reading.detail, true);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
void this.place(client, bytes, reading);
|
|
141
|
+
};
|
|
142
|
+
client.on("data", onData);
|
|
143
|
+
client.once("end", onEnd);
|
|
144
|
+
client.once("close", onEnd);
|
|
145
|
+
}
|
|
146
|
+
async place(client, head, destination) {
|
|
147
|
+
const at = this.now().toISOString();
|
|
148
|
+
const { protocol, host, port } = destination;
|
|
149
|
+
if (this.isSelf(host, port)) {
|
|
150
|
+
this.refuse(client, "HOST_IS_INTERCEPTOR", protocol, { host, port }, undefined, true);
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const key = this.ledger.record(host, port, protocol, destination.method ?? null, at);
|
|
154
|
+
this.emit({
|
|
155
|
+
event: "INTERCEPT_OBSERVED",
|
|
156
|
+
at,
|
|
157
|
+
protocol,
|
|
158
|
+
host,
|
|
159
|
+
port,
|
|
160
|
+
...(destination.method === undefined ? {} : { method: destination.method }),
|
|
161
|
+
...(destination.target === undefined ? {} : { target: destination.target }),
|
|
162
|
+
...(destination.alpn === undefined || destination.alpn.length === 0
|
|
163
|
+
? {}
|
|
164
|
+
: { alpn: destination.alpn }),
|
|
165
|
+
});
|
|
166
|
+
client.pause();
|
|
167
|
+
// A client that has already finished sending while the destination is
|
|
168
|
+
// dialled must still have its end delivered once the splice is up.
|
|
169
|
+
let clientEnded = false;
|
|
170
|
+
const onEnd = () => {
|
|
171
|
+
clientEnded = true;
|
|
172
|
+
};
|
|
173
|
+
client.once("end", onEnd);
|
|
174
|
+
let upstream;
|
|
175
|
+
try {
|
|
176
|
+
upstream = await this.dial(host, port, this.options.connectTimeoutMs);
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
const code = error.code ?? "UNREACHABLE";
|
|
180
|
+
this.refuse(client, "UPSTREAM_UNREACHABLE", protocol, { host, port }, code, false);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
client.removeListener("end", onEnd);
|
|
184
|
+
if (client.destroyed) {
|
|
185
|
+
upstream.destroy();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
this.ledger.placed();
|
|
189
|
+
this.splice(client, upstream, head, key, clientEnded);
|
|
190
|
+
}
|
|
191
|
+
/** Both directions, with back-pressure, half-close, an idle bound, and the bytes counted once at the end. */
|
|
192
|
+
splice(client, upstream, head, key, clientEnded) {
|
|
193
|
+
let toDestination = head.length;
|
|
194
|
+
let fromDestination = 0;
|
|
195
|
+
let closed = false;
|
|
196
|
+
const closeBoth = () => {
|
|
197
|
+
if (closed)
|
|
198
|
+
return;
|
|
199
|
+
closed = true;
|
|
200
|
+
this.ledger.bytes(key, toDestination, fromDestination);
|
|
201
|
+
client.destroy();
|
|
202
|
+
upstream.destroy();
|
|
203
|
+
};
|
|
204
|
+
upstream.on("error", () => undefined);
|
|
205
|
+
upstream.setNoDelay(true);
|
|
206
|
+
upstream.write(head);
|
|
207
|
+
client.setTimeout(this.options.idleTimeoutMs);
|
|
208
|
+
upstream.setTimeout(this.options.idleTimeoutMs);
|
|
209
|
+
client.on("data", (chunk) => {
|
|
210
|
+
toDestination += chunk.length;
|
|
211
|
+
if (!upstream.write(chunk))
|
|
212
|
+
client.pause();
|
|
213
|
+
});
|
|
214
|
+
upstream.on("drain", () => client.resume());
|
|
215
|
+
upstream.on("data", (chunk) => {
|
|
216
|
+
fromDestination += chunk.length;
|
|
217
|
+
if (!client.write(chunk))
|
|
218
|
+
upstream.pause();
|
|
219
|
+
});
|
|
220
|
+
client.on("drain", () => upstream.resume());
|
|
221
|
+
client.on("end", () => upstream.end());
|
|
222
|
+
upstream.on("end", () => client.end());
|
|
223
|
+
for (const socket of [client, upstream]) {
|
|
224
|
+
socket.on("close", closeBoth);
|
|
225
|
+
socket.on("error", closeBoth);
|
|
226
|
+
socket.on("timeout", closeBoth);
|
|
227
|
+
}
|
|
228
|
+
if (clientEnded)
|
|
229
|
+
upstream.end();
|
|
230
|
+
client.resume();
|
|
231
|
+
}
|
|
232
|
+
refuse(client, reason, protocol, destination, detail, counts) {
|
|
233
|
+
const at = this.now().toISOString();
|
|
234
|
+
this.ledger.refuse(reason, at, counts);
|
|
235
|
+
this.emit({
|
|
236
|
+
event: "INTERCEPT_REFUSED",
|
|
237
|
+
at,
|
|
238
|
+
reason,
|
|
239
|
+
protocol,
|
|
240
|
+
...(destination === undefined ? {} : destination),
|
|
241
|
+
...(detail === undefined ? {} : { detail }),
|
|
242
|
+
});
|
|
243
|
+
client.destroy();
|
|
244
|
+
}
|
|
245
|
+
/** Whether a destination is one of this interceptor's own listeners: a connection that would loop. */
|
|
246
|
+
isSelf(host, port) {
|
|
247
|
+
if (!LOOPBACK_HOSTS.has(host) && host !== this.options.bind)
|
|
248
|
+
return false;
|
|
249
|
+
return this.bound.some((listener) => listener.port === port);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
//# sourceMappingURL=InterceptServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InterceptServer.js","sourceRoot":"","sources":["../../src/http/InterceptServer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,YAAY,EAA8C,MAAM,UAAU,CAAC;AACpF,OAAO,EAAE,eAAe,EAAa,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EACL,eAAe,GAGhB,MAAM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AC,GAAG,EAAE,CAAC,WAAW,EAAE,CAAC;QACpC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,CAAC;YACR,KAAK,EAAE,mBAAmB;YAC1B,EAAE;YACF,MAAM;YACN,QAAQ;YACR,GAAG,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,CAAC;YACjD,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC;SAC5C,CAAC,CAAC;QACH,MAAM,CAAC,OAAO,EAAE,CAAC;IACnB,CAAC;IAED,sGAAsG;IAC9F,MAAM,CAAC,IAAY,EAAE,IAAY;QACvC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,CAAC,OAAO,CAAC,IAAI;YAAE,OAAO,KAAK,CAAC;QAC1E,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;IAC/D,CAAC;CACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The TLS ClientHello, read for one thing: the name the client is connecting
|
|
3
|
+
* to, which the server_name extension carries in the clear. A transparent
|
|
4
|
+
* interceptor has no other way to learn where a redirected TLS connection was
|
|
5
|
+
* going, so this parser decides where bytes are sent; it is therefore small,
|
|
6
|
+
* linear, and bounded, and it refuses anything it cannot place rather than
|
|
7
|
+
* guess. It never reads past the handshake, never keeps a byte, and reports
|
|
8
|
+
* the negotiated protocols (ALPN) beside the name because a later phase
|
|
9
|
+
* decides from them what it can terminate.
|
|
10
|
+
*
|
|
11
|
+
* RFC 8446 section 4.1.2 (ClientHello), RFC 6066 section 3 (server_name),
|
|
12
|
+
* RFC 7301 (ALPN).
|
|
13
|
+
*/
|
|
14
|
+
/** The reading of a client's first bytes as TLS. */
|
|
15
|
+
export type ClientHelloReading =
|
|
16
|
+
/** The bytes are a TLS handshake but the ClientHello is not complete yet. */
|
|
17
|
+
{
|
|
18
|
+
readonly kind: "NEED_MORE";
|
|
19
|
+
}
|
|
20
|
+
/** The bytes are not a TLS handshake record at all. */
|
|
21
|
+
| {
|
|
22
|
+
readonly kind: "NOT_TLS";
|
|
23
|
+
}
|
|
24
|
+
/** A ClientHello whose fields could not be read as the RFC lays them out. */
|
|
25
|
+
| {
|
|
26
|
+
readonly kind: "MALFORMED";
|
|
27
|
+
} | {
|
|
28
|
+
readonly kind: "CLIENT_HELLO";
|
|
29
|
+
/** The server_name host, lowercase; null when the client sent none. */
|
|
30
|
+
readonly serverName: string | null;
|
|
31
|
+
readonly alpn: readonly string[];
|
|
32
|
+
};
|
|
33
|
+
/** The most handshake bytes read before a hello is called malformed; well beyond any real one. */
|
|
34
|
+
export declare const MAX_CLIENT_HELLO_BYTES = 65536;
|
|
35
|
+
/** Reads a client's first bytes as a TLS ClientHello, or says why they are not one. */
|
|
36
|
+
export declare function readClientHello(bytes: Uint8Array): ClientHelloReading;
|
|
37
|
+
//# sourceMappingURL=ClientHello.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ClientHello.d.ts","sourceRoot":"","sources":["../../src/intercept/ClientHello.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,oDAAoD;AACpD,MAAM,MAAM,kBAAkB;AAC5B,6EAA6E;AAC3E;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE;AAChC,uDAAuD;GACrD;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAA;CAAE;AAC9B,6EAA6E;GAC3E;IAAE,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAA;CAAE,GAC9B;IACE,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,uEAAuE;IACvE,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;CAClC,CAAC;AAWN,kGAAkG;AAClG,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAsG7C,uFAAuF;AACvF,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,kBAAkB,CAsCrE"}
|