-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send socket.io error only through middleware
- Loading branch information
1 parent
944801d
commit a897964
Showing
6 changed files
with
12 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,20 @@ | ||
import * as process from 'node:process'; | ||
import type { Socket } from 'socket.io'; | ||
import type { Probe } from '../../../probe/types.js'; | ||
import { WsError } from '../ws-error.js'; | ||
import { fetchSockets } from '../server.js'; | ||
import { scopedLogger } from '../../logger.js'; | ||
import { InternalError } from '../../internal-error.js'; | ||
|
||
const logger = scopedLogger('ws:limit'); | ||
|
||
export const verifyIpLimit = async (socket: Socket): Promise<void> => { | ||
export const verifyIpLimit = async (ip: string, socketId: string): Promise<void> => { | ||
if (process.env['FAKE_PROBE_IP']) { | ||
return; | ||
} | ||
|
||
const probe = socket.data['probe'] as Probe; | ||
|
||
const socketList = await fetchSockets(); | ||
const previousSocket = socketList.find(s => s.data.probe.ipAddress === probe.ipAddress && s.id !== socket.id); | ||
const previousSocket = socketList.find(s => s.data.probe.ipAddress === ip && s.id !== socketId); | ||
|
||
if (previousSocket) { | ||
logger.info(`ws client ${socket.id} has reached the concurrent IP limit.`, { message: previousSocket.data.probe.ipAddress }); | ||
throw new WsError( | ||
'IP Limit', | ||
{ | ||
code: 'ip_limit', | ||
socketId: socket.id, | ||
probe, | ||
ipAddress: probe.ipAddress, | ||
}, | ||
); | ||
logger.info(`ws client ${socketId} has reached the concurrent IP limit.`, { message: previousSocket.data.probe.ipAddress }); | ||
throw new InternalError('ip limit', true); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters