Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
103 lines (82 loc) · 3.11 KB
/
Copy pathserver.js
File metadata and controls
103 lines (82 loc) · 3.11 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require('express');
const puppeteer = require('puppeteer');
const axios = require('axios');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 3000;
// Serve static files (player.html)
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
// CORS headers for all responses
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
// Scrape .m3u8 links using Puppeteer
async function scrapeM3U8(url) {
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
const page = await browser.newPage();
const found = new Set();
page.on('dialog', async dialog => await dialog.accept());
page.on('response', async response => {
const respUrl = response.url();
if (respUrl.endsWith('.m3u8')) {
console.log('Found .m3u8:', respUrl);
found.add(respUrl);
}
});
await page.goto(url, { waitUntil: 'networkidle2', timeout: 0 });
// await page.waitForTimeout(5000); // wait for JS to load
await browser.close();
return Array.from(found);
}
// Endpoint: scrape and return .m3u8 links
app.get('/scrape', async (req, res) => {
const { url } = req.query;
if (!url) return res.status(400).json({ error: 'Missing ?url=' });
try {
const links = await scrapeM3U8(url);
if (links.length === 0) return res.status(404).json({ message: 'No .m3u8 links found.' });
res.json({ m3u8: links });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Scraping failed.' });
}
});
// Proxy endpoint to bypass CORS
// New route: /proxy?url=https://example.com/stream.m3u8
const stream = require('stream');
app.get('/proxy', async (req, res) => {
const targetUrl = req.query.url;
if (!targetUrl || !targetUrl.startsWith('http')) {
return res.status(400).send('Invalid or missing URL');
}
try {
const response = await axios.get(targetUrl, { responseType: 'text' });
const contentType = response.headers['content-type'];
if (contentType.includes('application/vnd.apple.mpegurl') || targetUrl.endsWith('.m3u8')) {
// Rewrite .ts segment URLs to go through proxy
const baseUrl = targetUrl.substring(0, targetUrl.lastIndexOf('/') + 1);
const rewritten = response.data.replace(/^(?!#)(.*\.ts)/gm, segment => {
const absolute = new URL(segment, baseUrl).href;
return `/proxy?url=${encodeURIComponent(absolute)}`;
});
res.set('Content-Type', contentType);
res.set('Access-Control-Allow-Origin', '*');
res.send(rewritten);
} else {
// For .ts and other media files
const media = await axios.get(targetUrl, { responseType: 'stream' });
res.set('Content-Type', media.headers['content-type']);
res.set('Access-Control-Allow-Origin', '*');
media.data.pipe(res);
}
} catch (err) {
console.error('Proxy error:', err.message);
res.status(502).send('Bad Gateway');
}
});
app.listen(PORT, () => {
console.log(`✅ Server running at http://localhost:${PORT}`);
});