5.3.2: link sharing and nanoid

- you can now share video links directly from cobalt!
- cobalt is now using nanoid for stream ids instead of giant sha256 hashes
- one more fix to address the copy animation, this time on pc
This commit is contained in:
wukko
2023-04-08 22:55:44 +06:00
parent 307da3dce4
commit 68703ae300
8 changed files with 38 additions and 18 deletions

View File

@@ -1,4 +1,5 @@
import NodeCache from "node-cache";
import { nanoid } from 'nanoid';
import { sha256 } from "../sub/crypto.js";
import { streamLifespan } from "../config.js";
@@ -11,9 +12,9 @@ streamCache.on("expired", (key) => {
});
export function createStream(obj) {
let streamID = sha256(`${obj.ip},${obj.service},${obj.filename},${obj.audioFormat},${obj.mute}`, salt),
let streamID = nanoid(),
exp = Math.floor(new Date().getTime()) + streamLifespan,
ghmac = sha256(`${streamID},${obj.service},${obj.ip},${exp}`, salt);
ghmac = sha256(`${streamID},${obj.ip},${obj.service},${exp}`, salt);
if (!streamCache.has(streamID)) {
streamCache.set(streamID, {
@@ -42,13 +43,15 @@ export function createStream(obj) {
export function verifyStream(ip, id, hmac, exp) {
try {
let streamInfo = streamCache.get(id);
if (!streamInfo) return { error: 'this stream token does not exist', status: 400 };
let ghmac = sha256(`${id},${streamInfo.service},${ip},${exp}`, salt);
if (String(hmac) === ghmac && String(exp) === String(streamInfo.exp) && ghmac === String(streamInfo.hmac)
&& String(ip) === streamInfo.ip && Number(exp) > Math.floor(new Date().getTime())) {
return streamInfo;
if (id.length === 21) {
let streamInfo = streamCache.get(id);
if (!streamInfo) return { error: 'this stream token does not exist', status: 400 };
let ghmac = sha256(`${id},${ip},${streamInfo.service},${exp}`, salt);
if (String(hmac) === ghmac && String(exp) === String(streamInfo.exp) && ghmac === String(streamInfo.hmac)
&& String(ip) === streamInfo.ip && Number(exp) > Math.floor(new Date().getTime())) {
return streamInfo;
}
}
return { error: 'Unauthorized', status: 401 };
} catch (e) {