2022-07-08 19:17:56 +01:00
|
|
|
import NodeCache from "node-cache";
|
|
|
|
|
|
|
|
import { UUID, encrypt } from "../sub/crypto.js";
|
|
|
|
import { streamLifespan } from "../config.js";
|
|
|
|
|
|
|
|
const streamCache = new NodeCache({ stdTTL: streamLifespan, checkperiod: 120 });
|
2022-11-12 16:40:11 +00:00
|
|
|
const salt = process.env.streamSalt;
|
2022-07-08 19:17:56 +01:00
|
|
|
|
|
|
|
export function createStream(obj) {
|
|
|
|
let streamUUID = UUID(),
|
|
|
|
exp = Math.floor(new Date().getTime()) + streamLifespan,
|
2022-11-12 16:40:11 +00:00
|
|
|
ghmac = encrypt(`${streamUUID},${obj.service},${obj.ip},${exp}`, salt)
|
|
|
|
|
2022-07-08 19:17:56 +01:00
|
|
|
streamCache.set(streamUUID, {
|
|
|
|
id: streamUUID,
|
|
|
|
service: obj.service,
|
|
|
|
type: obj.type,
|
2022-07-22 09:05:36 +01:00
|
|
|
urls: obj.u,
|
2022-07-08 19:17:56 +01:00
|
|
|
filename: obj.filename,
|
|
|
|
hmac: ghmac,
|
2022-11-12 16:40:11 +00:00
|
|
|
ip: obj.ip,
|
2022-07-08 19:17:56 +01:00
|
|
|
exp: exp,
|
2022-10-24 14:03:11 +01:00
|
|
|
isAudioOnly: !!obj.isAudioOnly,
|
2022-08-12 14:36:19 +01:00
|
|
|
audioFormat: obj.audioFormat,
|
2022-07-24 11:54:05 +01:00
|
|
|
time: obj.time,
|
2022-10-24 14:03:11 +01:00
|
|
|
copy: obj.copy,
|
2022-10-27 17:27:20 +01:00
|
|
|
metadata: obj.fileMetadata ? obj.fileMetadata : false
|
2022-07-08 19:17:56 +01:00
|
|
|
});
|
|
|
|
return `${process.env.selfURL}api/stream?t=${streamUUID}&e=${exp}&h=${ghmac}`;
|
|
|
|
}
|
|
|
|
|
2022-11-12 16:40:11 +00:00
|
|
|
export function verifyStream(ip, id, hmac, exp) {
|
2022-07-08 19:17:56 +01:00
|
|
|
try {
|
|
|
|
let streamInfo = streamCache.get(id);
|
|
|
|
if (streamInfo) {
|
2022-11-12 16:40:11 +00:00
|
|
|
let ghmac = encrypt(`${id},${streamInfo.service},${ip},${exp}`, salt);
|
|
|
|
if (hmac == ghmac && ip == streamInfo.ip && ghmac == streamInfo.hmac && exp > Math.floor(new Date().getTime()) && exp == streamInfo.exp) {
|
2022-07-08 19:17:56 +01:00
|
|
|
return streamInfo;
|
|
|
|
} else {
|
|
|
|
return { error: 'Unauthorized', status: 401 };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return { error: 'this stream token does not exist', status: 400 };
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return { status: 500, body: { status: "error", text: "Internal Server Error" } };
|
|
|
|
}
|
2022-08-01 16:48:37 +01:00
|
|
|
}
|