Skip to content

Commit

Permalink
some websocket guards
Browse files Browse the repository at this point in the history
  • Loading branch information
jmlee337 committed Jan 29, 2025
1 parent 6ed51e7 commit e4b2c91
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/main/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Output,
WebSocketServerStatus,
} from '../common/types';
import { IncomingMessage } from 'http';

Check failure on line 16 in src/main/host.ts

View workflow job for this annotation

GitHub Actions / windows

`http` import should occur before import of `../common/types`

Check failure on line 16 in src/main/host.ts

View workflow job for this annotation

GitHub Actions / macos

`http` import should occur before import of `../common/types`

Check failure on line 16 in src/main/host.ts

View workflow job for this annotation

GitHub Actions / ubuntu

`http` import should occur before import of `../common/types`

Check failure on line 16 in src/main/host.ts

View workflow job for this annotation

GitHub Actions / macos

`http` import should occur before import of `../common/types`

Check failure on line 16 in src/main/host.ts

View workflow job for this annotation

GitHub Actions / windows

`http` import should occur before import of `../common/types`

Check failure on line 16 in src/main/host.ts

View workflow job for this annotation

GitHub Actions / ubuntu

`http` import should occur before import of `../common/types`

const PORT = 52455;

Expand Down Expand Up @@ -481,6 +482,7 @@ export function kickCopyClient(clientAddress: string) {
nameAndWebSocket.webSocket.close();
}

let broadcastSocket: Socket | null = null;
let webSocketServer: WebSocketServer | null = null;
const subdirToWriteDir = new Map<string, string>();
const fdToFileHandle = new Map<number, FileHandle>();
Expand All @@ -494,7 +496,13 @@ export function startHostServer(): Promise<string> {

clientAddressToNameAndWebSocket.clear();
sendCopyClients();
webSocketServer = new WebSocketServer({ port: PORT });
webSocketServer = new WebSocketServer({
port: PORT,
verifyClient: ({ req }: { req: IncomingMessage }) =>
!!broadcastSocket &&
req.socket.remoteAddress &&
!clientAddressToNameAndWebSocket.has(req.socket.remoteAddress),
});
webSocketServer.on('error', (error) => {
webSocketServerStatusError = error.message;
});
Expand Down Expand Up @@ -781,7 +789,6 @@ export function stopHostServer() {
});
}

let broadcastSocket: Socket | null = null;
let timeout: NodeJS.Timeout | null = null;
export function startBroadcasting() {
if (broadcastSocket) {
Expand Down

0 comments on commit e4b2c91

Please sign in to comment.