add secure flag to zod connection

This commit is contained in:
nicktrn
2024-03-25 18:59:05 +00:00
parent 3ee096d50c
commit 671dff6d6f
6 changed files with 22 additions and 4 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
HTTP_SERVER_PORT=8020
PLATFORM_ENABLED=true
PLATFORM_WS_PORT=3030
PLATFORM_WS_PORT=3030
SECURE_CONNECTION=false
+2
View File
@@ -26,6 +26,7 @@ const PLATFORM_ENABLED = ["1", "true"].includes(process.env.PLATFORM_ENABLED ??
const PLATFORM_HOST = process.env.PLATFORM_HOST || "127.0.0.1";
const PLATFORM_WS_PORT = process.env.PLATFORM_WS_PORT || 3030;
const PLATFORM_SECRET = process.env.PLATFORM_SECRET || "coordinator-secret";
const SECURE_CONNECTION = ["1", "true"].includes(process.env.SECURE_CONNECTION ?? "true");
const logger = new SimpleLogger(`[${NODE_NAME}]`);
@@ -365,6 +366,7 @@ class TaskCoordinator {
namespace: "coordinator",
host: PLATFORM_HOST,
port: Number(PLATFORM_WS_PORT),
secure: SECURE_CONNECTION,
clientMessages: CoordinatorToPlatformMessages,
serverMessages: PlatformToCoordinatorMessages,
authToken: PLATFORM_SECRET,
+1
View File
@@ -2,6 +2,7 @@ HTTP_SERVER_PORT=8050
PLATFORM_WS_PORT=3030
PLATFORM_SECRET=provider-secret
SECURE_CONNECTION=false
# Use this if you are on macOS
# COORDINATOR_HOST="host.docker.internal"
+1
View File
@@ -2,6 +2,7 @@ HTTP_SERVER_PORT=8060
PLATFORM_WS_PORT=3030
PLATFORM_SECRET=provider-secret
SECURE_CONNECTION=false
# Use this if you are on macOS
# COORDINATOR_HOST="host.docker.internal"
+3
View File
@@ -18,6 +18,7 @@ const MACHINE_NAME = process.env.MACHINE_NAME || "local";
const PLATFORM_HOST = process.env.PLATFORM_HOST || "127.0.0.1";
const PLATFORM_WS_PORT = process.env.PLATFORM_WS_PORT || 3030;
const PLATFORM_SECRET = process.env.PLATFORM_SECRET || "provider-secret";
const SECURE_CONNECTION = ["1", "true"].includes(process.env.SECURE_CONNECTION ?? "true");
const logger = new SimpleLogger(`[${MACHINE_NAME}]`);
@@ -87,6 +88,7 @@ export class ProviderShell implements Provider {
namespace: "shared-queue",
host: PLATFORM_HOST,
port: Number(PLATFORM_WS_PORT),
secure: SECURE_CONNECTION,
clientMessages: ClientToSharedQueueMessages,
serverMessages: SharedQueueToClientMessages,
authToken: PLATFORM_SECRET,
@@ -138,6 +140,7 @@ export class ProviderShell implements Provider {
namespace: "provider",
host: PLATFORM_HOST,
port: Number(PLATFORM_WS_PORT),
secure: SECURE_CONNECTION,
clientMessages: ProviderToPlatformMessages,
serverMessages: PlatformToProviderMessages,
authToken: PLATFORM_SECRET,
+13 -3
View File
@@ -263,7 +263,8 @@ interface ZodSocketConnectionOptions<
TServerMessages extends ZodSocketMessageCatalogSchema,
> {
host: string;
port: number;
port?: number;
secure?: boolean;
namespace: string;
clientMessages: TClientMessages;
serverMessages: TServerMessages;
@@ -302,15 +303,24 @@ export class ZodSocketConnection<
#logger: StructuredLogger;
constructor(opts: ZodSocketConnectionOptions<TClientMessages, TServerMessages>) {
this.socket = io(`ws://${opts.host}:${opts.port}/${opts.namespace}`, {
const uri = `${opts.secure ? "wss" : "ws"}://${opts.host}:${
opts.port ?? (opts.secure ? "443" : "80")
}/${opts.namespace}`;
const logger = new SimpleStructuredLogger(opts.namespace, LogLevel.info);
logger.log("new zod socket", { uri });
this.socket = io(uri, {
transports: ["websocket"],
auth: {
token: opts.authToken,
},
extraHeaders: opts.extraHeaders,
reconnectionDelay: 500,
reconnectionDelayMax: 1000,
});
this.#logger = new SimpleStructuredLogger(opts.namespace, LogLevel.info, {
this.#logger = logger.child({
socketId: this.socket.id,
});