Skip to content

Commit 70425a0

Browse files
committed
Update websockets should now have access to real client IP
1 parent 8997cd6 commit 70425a0

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

bun.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "uptimemonitor-server",
33
"module": "src/index.ts",
4-
"version": "0.2.9",
4+
"version": "0.2.10",
55
"type": "module",
66
"private": true,
77
"scripts": {
@@ -16,8 +16,8 @@
1616
},
1717
"dependencies": {
1818
"@clickhouse/client": "^1.16.0",
19-
"@rabbit-company/web": "^0.17.0",
20-
"@rabbit-company/web-middleware": "^0.17.0",
19+
"@rabbit-company/web": "^0.18.0",
20+
"@rabbit-company/web-middleware": "^0.18.0",
2121
"nodemailer": "^7.0.12"
2222
}
2323
}

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ app.websocket({
405405
idleTimeout: 120,
406406
maxPayloadLength: 1024 * 1024, // 1 MB
407407
open(ws) {
408-
Logger.audit("WebSocket connection opened", { ip: ws.remoteAddress });
408+
Logger.audit("WebSocket connection opened", { ip: ws.data.clientIp || ws.remoteAddress });
409409
ws.send(
410410
JSON.stringify({
411411
action: "connected",
@@ -679,7 +679,7 @@ app.websocket({
679679
const channel = `slug-${data.slug}`;
680680
ws.subscribe(channel);
681681

682-
Logger.audit(`WebSocket subscribed to channel: ${channel}`, { ip: ws.remoteAddress });
682+
Logger.audit(`WebSocket subscribed to channel: ${channel}`, { ip: ws.data.clientIp || ws.remoteAddress });
683683

684684
ws.send(
685685
JSON.stringify({
@@ -721,7 +721,7 @@ app.websocket({
721721

722722
ws.unsubscribe(channel);
723723

724-
Logger.audit(`WebSocket unsubscribed from ${channel}`, { ip: ws.remoteAddress });
724+
Logger.audit(`WebSocket unsubscribed from ${channel}`, { ip: ws.data.clientIp || ws.remoteAddress });
725725

726726
ws.send(
727727
JSON.stringify({
@@ -763,15 +763,15 @@ app.websocket({
763763
);
764764
},
765765
close(ws, code, reason) {
766-
Logger.audit(`WebSocket connection closed`, { ip: ws.remoteAddress, code, reason: reason || "none" });
766+
Logger.audit(`WebSocket connection closed`, { ip: ws.data.clientIp || ws.remoteAddress, code, reason: reason || "none" });
767767

768768
ws.subscriptions.forEach((subscription) => {
769769
ws.unsubscribe(subscription);
770770
});
771771
},
772772
error(ws, error) {
773773
Logger.error("WebSocket runtime error", {
774-
ip: ws.remoteAddress,
774+
ip: ws.data.clientIp || ws.remoteAddress,
775775
error,
776776
});
777777
},

src/pulsemonitor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Server } from "@rabbit-company/web";
1+
import type { Server, WebSocketData } from "@rabbit-company/web";
22
import { cache } from "./cache";
33
import { Logger } from "./logger";
44
import type { Monitor, PulseMonitor } from "./types";
@@ -194,7 +194,7 @@ export function notifyAllPulseMonitorClients(server: Server): void {
194194
* Returns the subscription response or null if invalid
195195
*/
196196
export function handlePulseMonitorSubscription(
197-
ws: Bun.ServerWebSocket<undefined>,
197+
ws: Bun.ServerWebSocket<WebSocketData<Record<string, unknown>>>,
198198
token: string,
199199
): { success: true; pulseMonitor: PulseMonitor; channel: string; configs: any[] } | { success: false; error: string } {
200200
const pulseMonitor = cache.getPulseMonitorByToken(token);
@@ -210,7 +210,7 @@ export function handlePulseMonitorSubscription(
210210
ws.subscribe(channel);
211211

212212
Logger.audit("PulseMonitor client subscribed", {
213-
ip: ws.remoteAddress,
213+
ip: ws.data.clientIp || ws.remoteAddress,
214214
pulseMonitorId: pulseMonitor.id,
215215
pulseMonitorName: pulseMonitor.name,
216216
channel,

0 commit comments

Comments
 (0)