Update index.js
Browse files
index.js
CHANGED
|
@@ -192,57 +192,140 @@ app.delete('/api/connections/:id', auth, async (req, res) => {
|
|
| 192 |
}
|
| 193 |
});
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
);
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
// Convert expireDate to Unix timestamp if it exists
|
| 212 |
-
let expTimestamp = null;
|
| 213 |
-
if (connection.expireDate) {
|
| 214 |
-
expTimestamp = Math.floor(new Date(connection.expireDate).getTime() / 1000);
|
| 215 |
}
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
}
|
| 241 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
}
|
|
|
|
| 243 |
|
| 244 |
-
|
|
|
|
| 245 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
const queryParams = new URLSearchParams({
|
| 247 |
username: connection.targetUsername,
|
| 248 |
password: connection.targetPassword,
|
|
@@ -250,64 +333,33 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 250 |
}).toString();
|
| 251 |
|
| 252 |
const targetUrl = `${connection.targetUrl}/player_api.php?${queryParams}`;
|
|
|
|
| 253 |
|
| 254 |
const response = await fetch(targetUrl, {
|
| 255 |
headers: {
|
| 256 |
-
'User-Agent': req.headers['user-agent'],
|
| 257 |
-
'
|
| 258 |
-
|
|
|
|
|
|
|
| 259 |
});
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
const data = await response.json();
|
| 262 |
-
|
| 263 |
res.json(data);
|
| 264 |
-
} catch (error) {
|
| 265 |
-
logger.error('Player API proxy error', { error, username, params: otherParams });
|
| 266 |
-
res.status(502).send('Failed to fetch content');
|
| 267 |
-
}
|
| 268 |
-
});
|
| 269 |
-
|
| 270 |
-
app.get('/xmltv.php', async (req, res) => {
|
| 271 |
-
const { username, password } = req.query;
|
| 272 |
-
const connection = await db.collection('connections').findOne({
|
| 273 |
-
iptvUsername: username,
|
| 274 |
-
iptvPassword: password
|
| 275 |
-
});
|
| 276 |
-
|
| 277 |
-
if (!connection) {
|
| 278 |
-
return res.status(401).send();
|
| 279 |
-
}
|
| 280 |
-
|
| 281 |
-
try {
|
| 282 |
-
const targetUrl = `${connection.targetUrl}/xmltv.php?username=${connection.targetUsername}&password=${connection.targetPassword}`;
|
| 283 |
-
const headers = {
|
| 284 |
-
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
| 285 |
-
'accept-language': req.headers['accept-language'],
|
| 286 |
-
'user-agent': req.headers['user-agent']
|
| 287 |
-
};
|
| 288 |
-
|
| 289 |
-
const response = await fetch(targetUrl, { headers });
|
| 290 |
-
res.status(response.status);
|
| 291 |
-
|
| 292 |
-
const responseHeaders = {
|
| 293 |
-
'Server': 'nginx',
|
| 294 |
-
'Date': response.headers.get('date'),
|
| 295 |
-
'Content-Type': 'application/xml; charset=utf-8',
|
| 296 |
-
'Transfer-Encoding': 'chunked',
|
| 297 |
-
'Connection': 'keep-alive',
|
| 298 |
-
'Keep-Alive': 'timeout=15',
|
| 299 |
-
'Access-Control-Allow-Origin': '*'
|
| 300 |
-
};
|
| 301 |
-
|
| 302 |
-
Object.entries(responseHeaders).forEach(([key, value]) => {
|
| 303 |
-
if (value) res.setHeader(key, value);
|
| 304 |
-
});
|
| 305 |
|
| 306 |
-
response.body.pipe(res);
|
| 307 |
} catch (error) {
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
| 311 |
}
|
| 312 |
});
|
| 313 |
|
|
@@ -316,21 +368,25 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 316 |
const { username, password, id } = req.params;
|
| 317 |
const type = req.path.split('/')[1];
|
| 318 |
|
|
|
|
|
|
|
| 319 |
const connection = await db.collection('connections').findOne({
|
| 320 |
iptvUsername: username,
|
| 321 |
iptvPassword: password
|
| 322 |
});
|
| 323 |
|
| 324 |
if (!connection) {
|
| 325 |
-
|
|
|
|
| 326 |
}
|
| 327 |
|
| 328 |
const targetUrl = `${connection.targetUrl}/${type}/${connection.targetUsername}/${connection.targetPassword}/${id}`;
|
|
|
|
| 329 |
|
| 330 |
try {
|
| 331 |
const headers = {
|
| 332 |
...req.headers,
|
| 333 |
-
'User-Agent': req.headers['user-agent'],
|
| 334 |
'Range': req.headers.range
|
| 335 |
};
|
| 336 |
delete headers.host;
|
|
@@ -350,7 +406,7 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 350 |
try {
|
| 351 |
res.setHeader(key, value);
|
| 352 |
} catch (err) {
|
| 353 |
-
console.warn(`Could not set header ${key}`);
|
| 354 |
}
|
| 355 |
});
|
| 356 |
res.setHeader('Location', redirectUrl);
|
|
@@ -361,7 +417,7 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 361 |
try {
|
| 362 |
res.setHeader(key, value);
|
| 363 |
} catch (err) {
|
| 364 |
-
console.warn(`Could not set header ${key}`);
|
| 365 |
}
|
| 366 |
});
|
| 367 |
|
|
@@ -369,7 +425,11 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 369 |
res.send(buffer);
|
| 370 |
}
|
| 371 |
} catch (error) {
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
}
|
| 374 |
});
|
| 375 |
|
|
@@ -415,7 +475,20 @@ async function initializeAdmin() {
|
|
| 415 |
async function startServer() {
|
| 416 |
await connectDB();
|
| 417 |
await initializeAdmin();
|
| 418 |
-
app.listen(PORT, () =>
|
|
|
|
|
|
|
|
|
|
| 419 |
}
|
| 420 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
startServer().catch(console.error);
|
|
|
|
| 192 |
}
|
| 193 |
});
|
| 194 |
|
| 195 |
+
// XMLTV Route
|
| 196 |
+
app.get('/xmltv.php', async (req, res) => {
|
| 197 |
+
try {
|
| 198 |
+
const { username, password } = req.query;
|
| 199 |
+
console.log('XMLTV request for username:', username);
|
|
|
|
| 200 |
|
| 201 |
+
const connection = await db.collection('connections').findOne({
|
| 202 |
+
iptvUsername: username,
|
| 203 |
+
iptvPassword: password
|
| 204 |
+
});
|
| 205 |
|
| 206 |
+
if (!connection) {
|
| 207 |
+
console.log('Invalid XMLTV access - no connection found for:', username);
|
| 208 |
+
return res.status(401).json({ error: 'Invalid credentials' });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
}
|
| 210 |
|
| 211 |
+
const targetUrl = `${connection.targetUrl}/xmltv.php?username=${connection.targetUsername}&password=${connection.targetPassword}`;
|
| 212 |
+
console.log('Proxying XMLTV request to:', targetUrl);
|
| 213 |
+
|
| 214 |
+
const headers = {
|
| 215 |
+
'accept': 'application/xml,text/xml,*/*',
|
| 216 |
+
'accept-language': req.headers['accept-language'],
|
| 217 |
+
'user-agent': req.headers['user-agent']
|
| 218 |
+
};
|
| 219 |
+
|
| 220 |
+
const response = await fetch(targetUrl, {
|
| 221 |
+
headers,
|
| 222 |
+
timeout: 30000
|
| 223 |
+
});
|
| 224 |
+
|
| 225 |
+
if (!response.ok) {
|
| 226 |
+
throw new Error(`Target server responded with status: ${response.status}`);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
res.status(response.status);
|
| 230 |
+
const responseHeaders = {
|
| 231 |
+
'Content-Type': 'application/xml; charset=utf-8',
|
| 232 |
+
'Access-Control-Allow-Origin': '*',
|
| 233 |
+
'Cache-Control': 'no-cache',
|
| 234 |
+
'Connection': 'keep-alive'
|
| 235 |
+
};
|
| 236 |
+
|
| 237 |
+
Object.entries(responseHeaders).forEach(([key, value]) => {
|
| 238 |
+
if (value) {
|
| 239 |
+
res.setHeader(key, value);
|
| 240 |
+
}
|
| 241 |
+
});
|
| 242 |
+
|
| 243 |
+
response.body.pipe(res);
|
| 244 |
+
|
| 245 |
+
response.body.on('error', (error) => {
|
| 246 |
+
console.error('Error streaming XMLTV response:', error);
|
| 247 |
+
if (!res.headersSent) {
|
| 248 |
+
res.status(502).send('Error streaming XMLTV data');
|
| 249 |
}
|
| 250 |
});
|
| 251 |
+
|
| 252 |
+
} catch (error) {
|
| 253 |
+
console.error('XMLTV proxy error:', error);
|
| 254 |
+
if (!res.headersSent) {
|
| 255 |
+
res.status(502).json({
|
| 256 |
+
error: 'Failed to fetch XMLTV data',
|
| 257 |
+
message: error.message
|
| 258 |
+
});
|
| 259 |
+
}
|
| 260 |
}
|
| 261 |
+
});
|
| 262 |
|
| 263 |
+
// Player API Route
|
| 264 |
+
app.get('/player_api.php', async (req, res) => {
|
| 265 |
try {
|
| 266 |
+
console.log('Player API Request received with query:', req.query);
|
| 267 |
+
const { username, password, ...otherParams } = req.query;
|
| 268 |
+
|
| 269 |
+
if (!username || !password) {
|
| 270 |
+
console.log('Missing credentials');
|
| 271 |
+
return res.status(401).json({
|
| 272 |
+
user_info: { auth: 0, status: "Active", error: "Missing credentials" }
|
| 273 |
+
});
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
const connection = await db.collection('connections').findOne({
|
| 277 |
+
iptvUsername: username,
|
| 278 |
+
iptvPassword: password
|
| 279 |
+
});
|
| 280 |
+
|
| 281 |
+
console.log('Connection found:', connection ? 'Yes' : 'No');
|
| 282 |
+
|
| 283 |
+
if (!connection) {
|
| 284 |
+
console.log('No connection found for credentials');
|
| 285 |
+
return res.status(401).json({
|
| 286 |
+
user_info: { auth: 0, status: "Active", error: "Invalid credentials" }
|
| 287 |
+
});
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
if (Object.keys(otherParams).length === 0) {
|
| 291 |
+
console.log('Sending auth response');
|
| 292 |
+
const serverUrl = new URL(req.protocol + '://' + req.get('host'));
|
| 293 |
+
|
| 294 |
+
let expTimestamp = connection.expireDate ?
|
| 295 |
+
Math.floor(new Date(connection.expireDate).getTime() / 1000) :
|
| 296 |
+
Math.floor(Date.now() / 1000) + (30 * 24 * 60 * 60); // Default 30 days
|
| 297 |
+
|
| 298 |
+
const response = {
|
| 299 |
+
user_info: {
|
| 300 |
+
username: username,
|
| 301 |
+
password: password,
|
| 302 |
+
message: "Successfully logged in",
|
| 303 |
+
auth: 1,
|
| 304 |
+
status: "Active",
|
| 305 |
+
exp_date: expTimestamp,
|
| 306 |
+
is_trial: "0",
|
| 307 |
+
active_cons: "0",
|
| 308 |
+
created_at: Math.floor(connection.createdAt?.getTime() / 1000) || Math.floor(Date.now() / 1000),
|
| 309 |
+
max_connections: "1",
|
| 310 |
+
allowed_output_formats: ["m3u8", "ts"]
|
| 311 |
+
},
|
| 312 |
+
server_info: {
|
| 313 |
+
url: serverUrl.hostname,
|
| 314 |
+
port: serverUrl.port || "80",
|
| 315 |
+
https_port: "443",
|
| 316 |
+
server_protocol: serverUrl.protocol.replace(":", ""),
|
| 317 |
+
rtmp_port: "0",
|
| 318 |
+
timezone: "America/New_York",
|
| 319 |
+
timestamp_now: Math.floor(Date.now() / 1000),
|
| 320 |
+
time_now: new Date().toISOString().replace('T', ' ').slice(0, 19)
|
| 321 |
+
}
|
| 322 |
+
};
|
| 323 |
+
|
| 324 |
+
console.log('Sending response:', response);
|
| 325 |
+
return res.json(response);
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
console.log('Proxying request to target server with params:', otherParams);
|
| 329 |
const queryParams = new URLSearchParams({
|
| 330 |
username: connection.targetUsername,
|
| 331 |
password: connection.targetPassword,
|
|
|
|
| 333 |
}).toString();
|
| 334 |
|
| 335 |
const targetUrl = `${connection.targetUrl}/player_api.php?${queryParams}`;
|
| 336 |
+
console.log('Target URL:', targetUrl);
|
| 337 |
|
| 338 |
const response = await fetch(targetUrl, {
|
| 339 |
headers: {
|
| 340 |
+
'User-Agent': req.headers['user-agent'] || 'Mozilla/5.0',
|
| 341 |
+
'Accept': 'application/json',
|
| 342 |
+
'Connection': 'keep-alive'
|
| 343 |
+
},
|
| 344 |
+
timeout: 10000
|
| 345 |
});
|
| 346 |
|
| 347 |
+
if (!response.ok) {
|
| 348 |
+
console.log('Target server error:', response.status);
|
| 349 |
+
throw new Error(`Target server responded with status: ${response.status}`);
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
const data = await response.json();
|
| 353 |
+
console.log('Successfully received data from target server');
|
| 354 |
res.json(data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
|
|
|
| 356 |
} catch (error) {
|
| 357 |
+
console.error('Player API error:', error);
|
| 358 |
+
res.status(502).json({
|
| 359 |
+
error: 'Failed to fetch content',
|
| 360 |
+
message: error.message,
|
| 361 |
+
status: 'error'
|
| 362 |
+
});
|
| 363 |
}
|
| 364 |
});
|
| 365 |
|
|
|
|
| 368 |
const { username, password, id } = req.params;
|
| 369 |
const type = req.path.split('/')[1];
|
| 370 |
|
| 371 |
+
console.log('Stream request:', { type, username, id });
|
| 372 |
+
|
| 373 |
const connection = await db.collection('connections').findOne({
|
| 374 |
iptvUsername: username,
|
| 375 |
iptvPassword: password
|
| 376 |
});
|
| 377 |
|
| 378 |
if (!connection) {
|
| 379 |
+
console.log('Invalid stream access - no connection found for:', username);
|
| 380 |
+
return res.status(401).json({ error: 'Invalid credentials' });
|
| 381 |
}
|
| 382 |
|
| 383 |
const targetUrl = `${connection.targetUrl}/${type}/${connection.targetUsername}/${connection.targetPassword}/${id}`;
|
| 384 |
+
console.log('Proxying stream request to:', targetUrl);
|
| 385 |
|
| 386 |
try {
|
| 387 |
const headers = {
|
| 388 |
...req.headers,
|
| 389 |
+
'User-Agent': req.headers['user-agent'] || 'Mozilla/5.0',
|
| 390 |
'Range': req.headers.range
|
| 391 |
};
|
| 392 |
delete headers.host;
|
|
|
|
| 406 |
try {
|
| 407 |
res.setHeader(key, value);
|
| 408 |
} catch (err) {
|
| 409 |
+
console.warn(`Could not set header ${key}:`, err.message);
|
| 410 |
}
|
| 411 |
});
|
| 412 |
res.setHeader('Location', redirectUrl);
|
|
|
|
| 417 |
try {
|
| 418 |
res.setHeader(key, value);
|
| 419 |
} catch (err) {
|
| 420 |
+
console.warn(`Could not set header ${key}:`, err.message);
|
| 421 |
}
|
| 422 |
});
|
| 423 |
|
|
|
|
| 425 |
res.send(buffer);
|
| 426 |
}
|
| 427 |
} catch (error) {
|
| 428 |
+
console.error('Stream proxy error:', error);
|
| 429 |
+
res.status(502).json({
|
| 430 |
+
error: 'Failed to fetch stream',
|
| 431 |
+
message: error.message
|
| 432 |
+
});
|
| 433 |
}
|
| 434 |
});
|
| 435 |
|
|
|
|
| 475 |
async function startServer() {
|
| 476 |
await connectDB();
|
| 477 |
await initializeAdmin();
|
| 478 |
+
app.listen(PORT, () => {
|
| 479 |
+
console.log(`Server running on http://localhost:${PORT}`);
|
| 480 |
+
console.log('Environment:', process.env.NODE_ENV || 'development');
|
| 481 |
+
});
|
| 482 |
}
|
| 483 |
|
| 484 |
+
// Handle errors
|
| 485 |
+
process.on('unhandledRejection', (error) => {
|
| 486 |
+
console.error('Unhandled Rejection:', error);
|
| 487 |
+
});
|
| 488 |
+
|
| 489 |
+
process.on('uncaughtException', (error) => {
|
| 490 |
+
console.error('Uncaught Exception:', error);
|
| 491 |
+
process.exit(1);
|
| 492 |
+
});
|
| 493 |
+
|
| 494 |
startServer().catch(console.error);
|