Skip to content
Snippets Groups Projects
Commit fd8e0491 authored by xaralis's avatar xaralis
Browse files

feat: provide signature for keepalive

parent 279ddfef
No related branches found
No related tags found
No related merge requests found
......@@ -4316,6 +4316,11 @@
"randomfill": "^1.0.3"
}
},
"crypto-js": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.0.0.tgz",
"integrity": "sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg=="
},
"css": {
"version": "2.2.4",
"resolved": "https://registry.npmjs.org/css/-/css-2.2.4.tgz",
......
......@@ -7,6 +7,7 @@
"@rooks/use-window-size": "^4.5.0",
"@sentry/react": "^5.23.0",
"classnames": "^2.2.6",
"crypto-js": "^4.0.0",
"date-fns": "^2.16.1",
"immer": "^7.0.15",
"keycloak-js": "^10.0.2",
......
......@@ -27,6 +27,7 @@ export const loadMe = createAsyncAction(
state.user.id = result.payload.id;
state.user.group = result.payload.group;
state.user.isBanned = result.payload.is_banned;
state.user.secret = result.payload.secret || "";
}
});
}
......
import hex from "crypto-js/enc-hex";
import hmacSHA1 from "crypto-js/hmac-sha1";
import WaitQueue from "wait-queue";
import { GlobalInfoStore } from "stores";
import { AuthStore, GlobalInfoStore } from "stores";
import { handlers } from "./handlers";
......@@ -44,6 +46,18 @@ function Worker() {
};
}
const buildKeepalivePayload = async () => {
const { user } = AuthStore.getRawState();
const payload = user && user.id ? user.id.toString() : "";
const signature = user.secret ? hmacSHA1(payload, user.secret) : null;
return {
event: "KEEPALIVE",
payload,
sig: hex.stringify(signature),
};
};
export const connect = ({ onConnect }) => {
return new Promise((resolve, reject) => {
const worker = Worker();
......@@ -61,14 +75,17 @@ export const connect = ({ onConnect }) => {
state.connectionState = "connected";
});
console.log("[ws] Connected.");
ws.send("CONNECT");
keepAliveInterval = setInterval(() => {
ws.send("KEEPALIVE");
const sendKeepalive = async () => {
ws.send(JSON.stringify(await buildKeepalivePayload()));
console.debug("[ws] Sending keepalive.");
}, 30 * 1000);
};
sendKeepalive();
keepAliveInterval = setInterval(sendKeepalive, 15 * 1000);
const self = { ws, worker };
const self = { ws, worker, sendKeepalive };
if (onConnect) {
return onConnect(self).then(() => resolve(self));
......
......@@ -46,6 +46,7 @@ declare namespace CF2021 {
id?: number;
isBanned?: boolean;
group?: string;
secret?: string;
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment