gateway: add webchat handshake logging
This commit is contained in:
@@ -24,7 +24,7 @@ import {
|
|||||||
updateSystemPresence,
|
updateSystemPresence,
|
||||||
upsertPresence,
|
upsertPresence,
|
||||||
} from "../infra/system-presence.js";
|
} from "../infra/system-presence.js";
|
||||||
import { logError } from "../logger.js";
|
import { logError, logInfo, logWarn } from "../logger.js";
|
||||||
import { getLogger, getResolvedLoggerSettings } from "../logging.js";
|
import { getLogger, getResolvedLoggerSettings } from "../logging.js";
|
||||||
import { monitorWebProvider, webAuthExists } from "../providers/web/index.js";
|
import { monitorWebProvider, webAuthExists } from "../providers/web/index.js";
|
||||||
import { defaultRuntime } from "../runtime.js";
|
import { defaultRuntime } from "../runtime.js";
|
||||||
@@ -462,6 +462,12 @@ export async function startGatewayServer(port = 18789): Promise<GatewayServer> {
|
|||||||
socket as WebSocket & { _socket?: { remoteAddress?: string } }
|
socket as WebSocket & { _socket?: { remoteAddress?: string } }
|
||||||
)._socket?.remoteAddress;
|
)._socket?.remoteAddress;
|
||||||
logWs("in", "connect", { connId, remoteAddr });
|
logWs("in", "connect", { connId, remoteAddr });
|
||||||
|
const describeHello = (hello: Hello | null | undefined) =>
|
||||||
|
hello
|
||||||
|
? `${hello.client.name ?? "unknown"} ${hello.client.mode ?? "?"} v${hello.client.version ?? "?"}`
|
||||||
|
: "unknown";
|
||||||
|
const isWebchatHello = (hello: Hello | null | undefined) =>
|
||||||
|
hello?.client?.mode === "webchat" || hello?.client?.name === "webchat-ui";
|
||||||
|
|
||||||
const send = (obj: unknown) => {
|
const send = (obj: unknown) => {
|
||||||
try {
|
try {
|
||||||
@@ -483,8 +489,18 @@ export async function startGatewayServer(port = 18789): Promise<GatewayServer> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.once("error", () => close());
|
socket.once("error", (err) => {
|
||||||
socket.once("close", () => {
|
logWarn(
|
||||||
|
`gateway/ws error conn=${connId} remote=${remoteAddr ?? "?"}: ${formatError(err)}`,
|
||||||
|
);
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
socket.once("close", (code, reason) => {
|
||||||
|
if (client && isWebchatHello(client.hello)) {
|
||||||
|
logInfo(
|
||||||
|
`webchat disconnected code=${code} reason=${reason?.toString() || "n/a"} conn=${connId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
if (client) {
|
if (client) {
|
||||||
// mark presence as disconnected
|
// mark presence as disconnected
|
||||||
const key = client.hello.client.instanceId || connId;
|
const key = client.hello.client.instanceId || connId;
|
||||||
@@ -501,7 +517,11 @@ export async function startGatewayServer(port = 18789): Promise<GatewayServer> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
logWs("out", "close", { connId });
|
logWs("out", "close", {
|
||||||
|
connId,
|
||||||
|
code,
|
||||||
|
reason: reason?.toString(),
|
||||||
|
});
|
||||||
close();
|
close();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -517,6 +537,9 @@ export async function startGatewayServer(port = 18789): Promise<GatewayServer> {
|
|||||||
if (!client) {
|
if (!client) {
|
||||||
// Expect hello
|
// Expect hello
|
||||||
if (!validateHello(parsed)) {
|
if (!validateHello(parsed)) {
|
||||||
|
logWarn(
|
||||||
|
`gateway/ws invalid hello conn=${connId} remote=${remoteAddr ?? "?"}`,
|
||||||
|
);
|
||||||
send({
|
send({
|
||||||
type: "hello-error",
|
type: "hello-error",
|
||||||
reason: `invalid hello: ${formatValidationErrors(validateHello.errors)}`,
|
reason: `invalid hello: ${formatValidationErrors(validateHello.errors)}`,
|
||||||
@@ -532,6 +555,9 @@ export async function startGatewayServer(port = 18789): Promise<GatewayServer> {
|
|||||||
maxProtocol < PROTOCOL_VERSION ||
|
maxProtocol < PROTOCOL_VERSION ||
|
||||||
minProtocol > PROTOCOL_VERSION
|
minProtocol > PROTOCOL_VERSION
|
||||||
) {
|
) {
|
||||||
|
logWarn(
|
||||||
|
`gateway/ws protocol mismatch conn=${connId} remote=${remoteAddr ?? "?"} client=${describeHello(hello)}`,
|
||||||
|
);
|
||||||
logWs("out", "hello-error", {
|
logWs("out", "hello-error", {
|
||||||
connId,
|
connId,
|
||||||
reason: "protocol mismatch",
|
reason: "protocol mismatch",
|
||||||
@@ -551,6 +577,9 @@ export async function startGatewayServer(port = 18789): Promise<GatewayServer> {
|
|||||||
// token auth if required
|
// token auth if required
|
||||||
const token = getGatewayToken();
|
const token = getGatewayToken();
|
||||||
if (token && hello.auth?.token !== token) {
|
if (token && hello.auth?.token !== token) {
|
||||||
|
logWarn(
|
||||||
|
`gateway/ws unauthorized conn=${connId} remote=${remoteAddr ?? "?"} client=${describeHello(hello)}`,
|
||||||
|
);
|
||||||
logWs("out", "hello-error", { connId, reason: "unauthorized" });
|
logWs("out", "hello-error", { connId, reason: "unauthorized" });
|
||||||
send({
|
send({
|
||||||
type: "hello-error",
|
type: "hello-error",
|
||||||
@@ -572,6 +601,11 @@ export async function startGatewayServer(port = 18789): Promise<GatewayServer> {
|
|||||||
platform: hello.client.platform,
|
platform: hello.client.platform,
|
||||||
token: hello.auth?.token ? "set" : "none",
|
token: hello.auth?.token ? "set" : "none",
|
||||||
});
|
});
|
||||||
|
if (isWebchatHello(hello)) {
|
||||||
|
logInfo(
|
||||||
|
`webchat connected conn=${connId} remote=${remoteAddr ?? "?"} client=${describeHello(hello)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
upsertPresence(presenceKey, {
|
upsertPresence(presenceKey, {
|
||||||
host: hello.client.name || os.hostname(),
|
host: hello.client.name || os.hostname(),
|
||||||
ip: remoteAddr,
|
ip: remoteAddr,
|
||||||
|
|||||||
Reference in New Issue
Block a user