-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproxy_server.mjs
More file actions
57 lines (50 loc) · 1.75 KB
/
proxy_server.mjs
File metadata and controls
57 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// https://www28.gogocdn.club/www04/DguGWt0YYiBxIm6AqsWnfA/1645298559/e7b499c11b82dfdaa72dc10c08c31757/ep.1.1634310704.720.m3u8
import express from 'express';
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import got from 'got';
import stream from 'stream';
import cors from 'cors';
const app = express()
app.use(cors())
const port = process.env.PORT || 3000
const Transform = stream.Transform;
app.get('/fetch_ts', async (req, res) => {
const url = req.query.url
res.setHeader('content-type', 'video/mp2t');
console.log(url)
got.stream(url).pipe(res)
})
app.get('/fetch_m3u8', async (req, res) => {
const url = req.query.url
const base_url = url.split('/').slice(0, -1).join('/')
const parser = new Transform();
res.setHeader('content-type', 'application/x-mpegURL');
parser._transform = function(data, encoding, done) {
const regex = /([a-z0-9./-]*.ts)/gm
const m3u8_filtered = Buffer.from(
data.toString()
.replace(regex, `/fetch_ts?url=${base_url}/$1`)
)
this.push(m3u8_filtered)
// console.log(m3u8_filtered.toString())
done();
};
got.stream(url).pipe(parser).pipe(res);
})
// app.get('/fetch_m3u8', async (req, res) => {
// const url = req.query.url
// const m3u8_raw = await fetch(url).then(res => res.text())
// const regex = /([a-z0-9./-]*.ts)/gm
// const m3u8_filtered = m3u8_raw.replace(regex, `http://localhost:3000/fetch_ts?url=${url}/$1`)
// res.setHeader('content-type', 'application/x-mpegURL');
// res.send(m3u8_filtered)
// })
app.get('/player', function(req, res) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
res.sendFile(path.join(__dirname, '/player.html'));
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})