Some checks failed
Node.js CI - Dockge / ${{ matrix.os }} (22, ubuntu-latest) (push) Successful in 1m47s
json-yaml-validate / json-yaml-validate (push) Successful in 12s
Nightly Release / release-nightly (push) Failing after 44s
Node.js CI - Dockge / ${{ matrix.os }} (22, macos-latest) (push) Has been cancelled
Node.js CI - Dockge / ${{ matrix.os }} (22, windows-latest) (push) Has been cancelled
52 lines
2.2 KiB
TypeScript
52 lines
2.2 KiB
TypeScript
import { SocketHandler } from "../socket-handler.js";
|
|
import { DockgeServer } from "../dockge-server";
|
|
import { log } from "../log";
|
|
import { callbackError, checkLogin, DockgeSocket } from "../util-server";
|
|
import { AgentSocket } from "../../common/agent-socket";
|
|
import { ALL_ENDPOINTS } from "../../common/util-common";
|
|
|
|
export class AgentProxySocketHandler extends SocketHandler {
|
|
|
|
create2(socket : DockgeSocket, server : DockgeServer, agentSocket : AgentSocket) {
|
|
// Agent - proxying requests if needed
|
|
socket.on("agent", async (endpoint : unknown, eventName : unknown, ...args : unknown[]) => {
|
|
try {
|
|
checkLogin(socket);
|
|
|
|
// Check Type
|
|
if (typeof(endpoint) !== "string") {
|
|
throw new Error("Endpoint must be a string: " + endpoint);
|
|
}
|
|
if (typeof(eventName) !== "string") {
|
|
throw new Error("Event name must be a string");
|
|
}
|
|
|
|
if (endpoint === ALL_ENDPOINTS) { // Send to all endpoints
|
|
log.debug("agent", "Sending to all endpoints: " + eventName);
|
|
socket.instanceManager.emitToAllEndpoints(eventName, ...args);
|
|
|
|
} else if (!endpoint || endpoint === socket.endpoint) { // Direct connection or matching endpoint
|
|
log.debug("agent", "Matched endpoint: " + eventName);
|
|
agentSocket.call(eventName, ...args);
|
|
|
|
} else {
|
|
log.debug("agent", "Proxying request to " + endpoint + " for " + eventName);
|
|
await socket.instanceManager.emitToEndpoint(endpoint, eventName, ...args);
|
|
}
|
|
} catch (e) {
|
|
if (e instanceof Error) {
|
|
log.warn("agent", e.message);
|
|
}
|
|
const callback = args.at(-1);
|
|
if (typeof callback === "function") {
|
|
callbackError(e, callback);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
create(socket : DockgeSocket, server : DockgeServer) {
|
|
throw new Error("Method not implemented. Please use create2 instead.");
|
|
}
|
|
}
|