Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions requestHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,31 @@ module.exports = (config, hlsSessions) => (req, res) => {
res.setHeader(corsOption, cors[corsOption]);
});

if (method === 'GET') {
try {
if (method !== 'GET') throw new Error('Only GET method is supported')

const filePath = path.resolve(mediaPath + url);
if (filePath.endsWith('.m3u8') || filePath.endsWith('.ts')) {
send(req, filePath)
.on('error', (err) => logger.error('Error serving ' + filePath, err))
.pipe(res);

const sessionID = url.split('/')[1];
const hlsSession = hlsSessions.get(sessionID);

const requestIp = (typeof req.headers['x-forwarded-for'] === 'string' && req.headers['x-forwarded-for'].split(',').shift()) ||
req.connection?.remoteAddress ||
req.socket?.remoteAddress ||
req.connection?.socket?.remoteAddress;

hlsSession.addViewer(requestIp);
hlsSession.updateLastRequestAt();
} else {
res.statusCode = 400;
res.end();
}
} else {
if (!(filePath.endsWith('.m3u8') || filePath.endsWith('.ts'))) throw new Error('Bad extension file requested')

send(req, filePath)
.on('error', (err) => logger.error('Error serving ' + filePath, err))
.pipe(res);

const sessionID = url.split('/')[1];
const hlsSession = hlsSessions.get(sessionID);

if (!hlsSession) throw new Error('Stream not found')

const requestIp = (typeof req.headers['x-forwarded-for'] === 'string' && req.headers['x-forwarded-for'].split(',').shift()) ||
req.connection?.remoteAddress ||
req.socket?.remoteAddress ||
req.connection?.socket?.remoteAddress;

hlsSession.addViewer(requestIp);
hlsSession.updateLastRequestAt();
} catch (error) {
logger.error('Error on request ', error)
res.statusCode = 400;
res.end();
res.end(String(error));
}
};