0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 13:13:00 -05:00

fully proxy hls

This commit is contained in:
dragongoose 2023-03-25 15:28:14 -04:00
parent b42f7b189f
commit fcfb1ce73c

View file

@ -38,21 +38,38 @@ proxyRouter.get('/stream/:username/hls.m3u8', async (req: Request, res: Response
for (let url of matches) {
const base64data = Buffer.from(url).toString('base64url')
m3u8Data = m3u8Data.replace(url, `${process.env.URL}/proxy/hls/${base64data}`)
m3u8Data = m3u8Data.replace(url, `${process.env.URL}/proxy/stream/sub/${base64data}`)
}
res.setHeader('Content-type','application/vnd.apple.mpegurl')
res.send(m3u8Data)
})
proxyRouter.get('/hls/:encodedUrl' , async (req: Request, res: Response, next: NextFunction) => {
proxyRouter.get('/stream/sub/:encodedUrl' , async (req: Request, res: Response, next: NextFunction) => {
const unencodedUrl = Buffer.from(req.params.encodedUrl, 'base64url').toString()
const m3u8Fetch = await fetch(unencodedUrl)
var m3u8Data = await m3u8Fetch.text()
let m3u8Data = await m3u8Fetch.text()
const urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
const matches = m3u8Data.match(urlRegex)
if(!matches) return;
for (let url of matches) {
const base64data = Buffer.from(url).toString('base64url')
m3u8Data = m3u8Data.replace(url, `${process.env.URL}/proxy/stream/segment/${base64data}`)
}
res.setHeader('Content-type','application/vnd.apple.mpegurl')
res.send(m3u8Data)
})
proxyRouter.get('/stream/segment/:encodedUrl', async (req: Request, res: Response) => {
const unencodedUrl = Buffer.from(req.params.encodedUrl, 'base64url').toString()
const data = await fetch(unencodedUrl)
const arrayBuffer = await data.arrayBuffer()
const buf = Buffer.from(arrayBuffer)
res.send(buf)
})
// IRC PROXY