forked from mtusi2026/mtusi2026lab7
Prepare verified laboratory 7 implementation and evidence
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
import { connect } from "node:http2";
|
||||
import { connect as tlsConnect } from "node:tls";
|
||||
import { local, requireThat } from "../scripts/runtime";
|
||||
// Native HTTP/2 performs framing/HPACK/TLS. The protobuf Empty Export request is five zero bytes:
|
||||
// the gRPC identity-compression flag and zero message length, no handwritten protobuf encoder.
|
||||
export async function grpcProbe(
|
||||
ip: string,
|
||||
port: number,
|
||||
hostname: string,
|
||||
role: string,
|
||||
) {
|
||||
const [ca, cert, key] = await Promise.all(
|
||||
["ca.crt", "cert.pem", "key.pem"].map((name) =>
|
||||
readFile(resolve(local, "pki", role, name)),
|
||||
),
|
||||
);
|
||||
return new Promise<void>((done, fail) => {
|
||||
let finished = false;
|
||||
const session = connect(`https://${hostname}:${port}`, {
|
||||
createConnection: () =>
|
||||
tlsConnect({
|
||||
host: ip,
|
||||
port,
|
||||
servername: hostname,
|
||||
ca,
|
||||
cert,
|
||||
key,
|
||||
rejectUnauthorized: true,
|
||||
minVersion: "TLSv1.2",
|
||||
ALPNProtocols: ["h2"],
|
||||
}),
|
||||
});
|
||||
const timer = setTimeout(
|
||||
() => finish(new Error("GRPC_PROBE_TIMEOUT")),
|
||||
4000,
|
||||
);
|
||||
function finish(error?: Error) {
|
||||
if (finished) return;
|
||||
finished = true;
|
||||
clearTimeout(timer);
|
||||
session.destroy();
|
||||
if (error) fail(error);
|
||||
else done();
|
||||
}
|
||||
session.on("error", finish);
|
||||
const request = session.request({
|
||||
":method": "POST",
|
||||
":path": "/opentelemetry.proto.collector.trace.v1.TraceService/Export",
|
||||
"content-type": "application/grpc",
|
||||
te: "trailers",
|
||||
});
|
||||
let status: string | undefined,
|
||||
bytes = 0,
|
||||
httpStatus = 0;
|
||||
request.on("response", (headers) => {
|
||||
httpStatus = Number(headers[":status"]);
|
||||
if (headers["grpc-status"] !== undefined)
|
||||
status = String(headers["grpc-status"]);
|
||||
});
|
||||
request.on("trailers", (headers) => {
|
||||
if (headers["grpc-status"] !== undefined)
|
||||
status = String(headers["grpc-status"]);
|
||||
});
|
||||
request.on("data", (chunk) => {
|
||||
bytes += chunk.length;
|
||||
if (bytes > 1048576) finish(new Error("GRPC_RESPONSE_TOO_LARGE"));
|
||||
});
|
||||
request.on("error", finish);
|
||||
request.on("end", () => {
|
||||
try {
|
||||
requireThat(
|
||||
httpStatus === 200 && status === "0",
|
||||
"TEMPO_GRPC_EXPORT_NOT_ACCEPTED",
|
||||
);
|
||||
finish();
|
||||
} catch (error) {
|
||||
finish(error as Error);
|
||||
}
|
||||
});
|
||||
request.end(Buffer.alloc(5));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user