-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxtream-proxy.js
More file actions
64 lines (57 loc) · 2.49 KB
/
xtream-proxy.js
File metadata and controls
64 lines (57 loc) · 2.49 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
// Backend proxy básico para Xtream Codes
// Uso: node xtream-proxy.js
// Requiere: npm install express node-fetch@2 cors
const express = require('express');
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 4000;
app.use(cors());
app.use(express.json());
// Endpoint principal: recibe credenciales Xtream y devuelve M3U o JSON
app.post('/api/xtream', async (req, res) => {
const { server, username, password, format } = req.body;
if (!server || !username || !password) {
return res.status(400).json({ error: 'Faltan datos: server, username, password' });
}
// Normalizar URL
let base = server.trim();
if (!/^https?:\/\//i.test(base)) base = 'http://' + base;
// Remove trailing slashes safely (avoid ReDoS)
while (base.endsWith('/')) {
base = base.slice(0, -1);
}
// 1) Intentar get.php (M3U)
try {
const m3uUrl = `${base}/get.php?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}&type=m3u_plus`;
const m3uRes = await fetch(m3uUrl);
if (!m3uRes.ok) throw new Error('No se pudo obtener M3U');
const m3uText = await m3uRes.text();
if (format === 'json') {
// Opcional: parsear M3U y devolver JSON (simple)
const channels = m3uText.split('\n').filter(l => l.startsWith('#EXTINF')).map((l, i, arr) => {
const name = l.split(',')[1] || `Canal ${i+1}`;
return { name };
});
return res.json({ channels });
}
return res.type('text/plain').send(m3uText);
} catch (err) {
// 2) Intentar player_api.php (JSON)
try {
const apiUrl = `${base}/player_api.php?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}&action=get_live_streams`;
const apiRes = await fetch(apiUrl);
if (!apiRes.ok) throw new Error('No se pudo obtener player_api.php');
const apiJson = await apiRes.json();
return res.json({ channels: apiJson });
} catch (err2) {
return res.status(500).json({ error: 'No se pudo conectar al servidor Xtream', details: err2.message });
}
}
});
app.get('/', (req, res) => {
res.send('Xtream Proxy activo. POST /api/xtream con server, username, password.');
});
app.listen(PORT, () => {
console.log(`Xtream Proxy escuchando en puerto ${PORT}`);
});