2021-08-18 00:01:11 +02:00
|
|
|
const express = require('express')
|
|
|
|
const Path = require('path')
|
2022-11-24 22:53:58 +01:00
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
|
|
|
|
|
|
|
const fs = require('../libs/fsExtra')
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
class HlsRouter {
|
2023-07-05 01:14:44 +02:00
|
|
|
constructor(auth, playbackSessionManager) {
|
2021-08-18 00:01:11 +02:00
|
|
|
this.auth = auth
|
2022-03-18 01:10:47 +01:00
|
|
|
this.playbackSessionManager = playbackSessionManager
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
this.router = express()
|
2023-02-01 21:34:01 +01:00
|
|
|
this.router.disable('x-powered-by')
|
2021-08-18 00:01:11 +02:00
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.router.get('/:stream/:file', this.streamFileRequest.bind(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
parseSegmentFilename(filename) {
|
2021-10-01 01:52:32 +02:00
|
|
|
var basename = Path.basename(filename, Path.extname(filename))
|
2021-08-18 00:01:11 +02:00
|
|
|
var num_part = basename.split('-')[1]
|
|
|
|
return Number(num_part)
|
|
|
|
}
|
|
|
|
|
2023-10-14 19:50:48 +02:00
|
|
|
/**
|
|
|
|
* Ensure filepath is inside streamDir
|
|
|
|
* Used to prevent arbitrary file reads
|
|
|
|
* @see https://nodejs.org/api/path.html#pathrelativefrom-to
|
|
|
|
*
|
|
|
|
* @param {string} streamDir
|
|
|
|
* @param {string} filepath
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
validateStreamFilePath(streamDir, filepath) {
|
|
|
|
const relative = Path.relative(streamDir, filepath)
|
|
|
|
return relative && !relative.startsWith('..') && !Path.isAbsolute(relative)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /hls/:stream/:file
|
|
|
|
* File must have extname .ts or .m3u8
|
|
|
|
*
|
|
|
|
* @param {express.Request} req
|
|
|
|
* @param {express.Response} res
|
|
|
|
*/
|
2021-08-18 00:01:11 +02:00
|
|
|
async streamFileRequest(req, res) {
|
2023-10-14 19:50:48 +02:00
|
|
|
const streamId = req.params.stream
|
|
|
|
// Ensure stream is open
|
|
|
|
const stream = this.playbackSessionManager.getStream(streamId)
|
|
|
|
if (!stream) {
|
|
|
|
Logger.error(`[HlsRouter] Stream "${streamId}" does not exist`)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure stream filepath is valid
|
|
|
|
const streamDir = Path.join(this.playbackSessionManager.StreamsPath, streamId)
|
|
|
|
const fullFilePath = Path.join(streamDir, req.params.file)
|
|
|
|
if (!this.validateStreamFilePath(streamDir, fullFilePath)) {
|
|
|
|
Logger.error(`[HlsRouter] Invalid file parameter "${req.params.file}"`)
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileExt = Path.extname(req.params.file)
|
|
|
|
if (fileExt !== '.ts' && fileExt !== '.m3u8') {
|
|
|
|
Logger.error(`[HlsRouter] Invalid file parameter "${req.params.file}" extname. Must be .ts or .m3u8`)
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
2021-09-01 20:47:18 +02:00
|
|
|
|
2023-10-14 19:50:48 +02:00
|
|
|
if (!(await fs.pathExists(fullFilePath))) {
|
2021-08-21 01:29:10 +02:00
|
|
|
Logger.warn('File path does not exist', fullFilePath)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2023-10-14 19:50:48 +02:00
|
|
|
if (fileExt === '.ts') {
|
|
|
|
const segNum = this.parseSegmentFilename(req.params.file)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
if (stream.isResetting) {
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.info(`[HlsRouter] Stream ${streamId} is currently resetting`)
|
2021-08-18 00:01:11 +02:00
|
|
|
} else {
|
2023-10-14 19:50:48 +02:00
|
|
|
const startTimeForReset = await stream.checkSegmentNumberRequest(segNum)
|
2021-08-18 00:01:11 +02:00
|
|
|
if (startTimeForReset) {
|
2021-08-21 01:29:10 +02:00
|
|
|
// HLS.js will restart the stream at the new time
|
2022-03-18 01:10:47 +01:00
|
|
|
Logger.info(`[HlsRouter] Resetting Stream - notify client @${startTimeForReset}s`)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('stream_reset', {
|
2021-08-18 00:01:11 +02:00
|
|
|
startTime: startTimeForReset,
|
|
|
|
streamId: stream.id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-14 19:50:48 +02:00
|
|
|
return res.sendStatus(404)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-10-01 01:52:32 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
res.sendFile(fullFilePath)
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 01:10:47 +01:00
|
|
|
module.exports = HlsRouter
|