Update index.js
Browse files
index.js
CHANGED
|
@@ -10,12 +10,9 @@ const path = require('path');
|
|
| 10 |
const app = express();
|
| 11 |
const PORT = process.env.PORT || 3000;
|
| 12 |
|
| 13 |
-
// MongoDB connection
|
| 14 |
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://new-iptv-manage:<db_password>@cluster0.o0eba.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0';
|
| 15 |
|
| 16 |
-
// Validate MongoDB URI
|
| 17 |
if (!MONGODB_URI.startsWith('mongodb://') && !MONGODB_URI.startsWith('mongodb+srv://')) {
|
| 18 |
-
console.error('Invalid MongoDB URI. Must start with mongodb:// or mongodb+srv://');
|
| 19 |
process.exit(1);
|
| 20 |
}
|
| 21 |
|
|
@@ -25,14 +22,11 @@ async function connectDB() {
|
|
| 25 |
try {
|
| 26 |
const client = await MongoClient.connect(MONGODB_URI);
|
| 27 |
db = client.db();
|
| 28 |
-
console.log('Connected to MongoDB');
|
| 29 |
} catch (error) {
|
| 30 |
-
console.error('MongoDB connection error:', error);
|
| 31 |
process.exit(1);
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
-
// Middleware
|
| 36 |
app.use(express.json());
|
| 37 |
app.use(express.urlencoded({ extended: true }));
|
| 38 |
app.use(session({
|
|
@@ -42,11 +36,10 @@ app.use(session({
|
|
| 42 |
store: MongoStore.create({
|
| 43 |
mongoUrl: MONGODB_URI,
|
| 44 |
collectionName: 'sessions',
|
| 45 |
-
ttl: 24 * 60 * 60
|
| 46 |
})
|
| 47 |
}));
|
| 48 |
|
| 49 |
-
// Auth check middleware
|
| 50 |
app.use((req, res, next) => {
|
| 51 |
const protectedPages = ['/hideme/dashboard.html', '/hideme/admin.html'];
|
| 52 |
const loginPage = '/hideme/index.html';
|
|
@@ -64,7 +57,6 @@ app.use((req, res, next) => {
|
|
| 64 |
|
| 65 |
app.use(express.static('public'));
|
| 66 |
|
| 67 |
-
// Auth middleware
|
| 68 |
const auth = (req, res, next) => {
|
| 69 |
if (!req.session.userId) {
|
| 70 |
return res.status(401).json({ error: 'Unauthorized' });
|
|
@@ -72,7 +64,6 @@ const auth = (req, res, next) => {
|
|
| 72 |
next();
|
| 73 |
};
|
| 74 |
|
| 75 |
-
// Admin Settings Route
|
| 76 |
app.post('/api/admin/update', auth, async (req, res) => {
|
| 77 |
try {
|
| 78 |
const { newUsername, newPassword, currentPassword } = req.body;
|
|
@@ -98,31 +89,23 @@ app.post('/api/admin/update', auth, async (req, res) => {
|
|
| 98 |
}
|
| 99 |
});
|
| 100 |
|
| 101 |
-
// API Routes
|
| 102 |
app.post('/api/login', async (req, res) => {
|
| 103 |
try {
|
| 104 |
const { username, password } = req.body;
|
| 105 |
-
console.log('Login attempt for username:', username);
|
| 106 |
-
|
| 107 |
const user = await db.collection('users').findOne({ username });
|
|
|
|
| 108 |
if (!user) {
|
| 109 |
-
console.log('User not found:', username);
|
| 110 |
return res.status(401).json({ error: 'Invalid credentials' });
|
| 111 |
}
|
| 112 |
|
| 113 |
const isValidPassword = await bcrypt.compare(password, user.password);
|
| 114 |
-
console.log('Password valid:', isValidPassword);
|
| 115 |
-
|
| 116 |
if (isValidPassword) {
|
| 117 |
req.session.userId = user._id;
|
| 118 |
-
console.log('Login successful for user:', username);
|
| 119 |
res.json({ success: true });
|
| 120 |
} else {
|
| 121 |
-
console.log('Invalid password for user:', username);
|
| 122 |
res.status(401).json({ error: 'Invalid credentials' });
|
| 123 |
}
|
| 124 |
} catch (error) {
|
| 125 |
-
console.error('Login error:', error);
|
| 126 |
res.status(500).json({ error: 'Login failed' });
|
| 127 |
}
|
| 128 |
});
|
|
@@ -192,25 +175,19 @@ app.delete('/api/connections/:id', auth, async (req, res) => {
|
|
| 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'],
|
|
@@ -223,7 +200,7 @@ app.get('/xmltv.php', async (req, res) => {
|
|
| 223 |
});
|
| 224 |
|
| 225 |
if (!response.ok) {
|
| 226 |
-
throw new Error(`Target server
|
| 227 |
}
|
| 228 |
|
| 229 |
res.status(response.status);
|
|
@@ -235,39 +212,22 @@ app.get('/xmltv.php', async (req, res) => {
|
|
| 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 |
});
|
|
@@ -278,24 +238,20 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 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);
|
| 297 |
|
| 298 |
-
|
| 299 |
user_info: {
|
| 300 |
username: username,
|
| 301 |
password: password,
|
|
@@ -319,13 +275,9 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 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,8 +285,6 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 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',
|
|
@@ -345,43 +295,33 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 345 |
});
|
| 346 |
|
| 347 |
if (!response.ok) {
|
| 348 |
-
|
| 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 |
|
| 366 |
-
// Stream routes
|
| 367 |
app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/series/:username/:password/:id'], async (req, res) => {
|
| 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 = {
|
|
@@ -405,9 +345,7 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 405 |
Object.entries(originalHeaders).forEach(([key, value]) => {
|
| 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);
|
| 413 |
res.end();
|
|
@@ -416,79 +354,45 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 416 |
response.headers.forEach((value, key) => {
|
| 417 |
try {
|
| 418 |
res.setHeader(key, value);
|
| 419 |
-
} catch (err) {
|
| 420 |
-
console.warn(`Could not set header ${key}:`, err.message);
|
| 421 |
-
}
|
| 422 |
});
|
| 423 |
|
| 424 |
const buffer = await response.buffer();
|
| 425 |
res.send(buffer);
|
| 426 |
}
|
| 427 |
} catch (error) {
|
| 428 |
-
|
| 429 |
-
res.status(502).json({
|
| 430 |
-
error: 'Failed to fetch stream',
|
| 431 |
-
message: error.message
|
| 432 |
-
});
|
| 433 |
}
|
| 434 |
});
|
| 435 |
|
| 436 |
-
// Initialize admin user
|
| 437 |
async function initializeAdmin() {
|
| 438 |
try {
|
| 439 |
-
|
| 440 |
-
await db.collection('users').drop().catch(() => console.log('No users collection to drop'));
|
| 441 |
|
| 442 |
-
// Create admin user
|
| 443 |
const defaultUsername = process.env.ADMIN_USERNAME || 'admin';
|
| 444 |
const defaultPassword = process.env.ADMIN_PASSWORD || 'admin';
|
| 445 |
-
console.log('Creating admin user with username:', defaultUsername);
|
| 446 |
-
|
| 447 |
const hashedPassword = await bcrypt.hash(defaultPassword, 10);
|
| 448 |
-
|
|
|
|
| 449 |
username: defaultUsername,
|
| 450 |
password: hashedPassword,
|
| 451 |
createdAt: new Date(),
|
| 452 |
isAdmin: true
|
| 453 |
});
|
| 454 |
-
|
| 455 |
-
console.log('Admin user created successfully:', {
|
| 456 |
-
username: defaultUsername,
|
| 457 |
-
id: result.insertedId,
|
| 458 |
-
timestamp: new Date()
|
| 459 |
-
});
|
| 460 |
-
|
| 461 |
-
// Verify the user was created
|
| 462 |
-
const verifyUser = await db.collection('users').findOne({ username: defaultUsername });
|
| 463 |
-
if (verifyUser) {
|
| 464 |
-
console.log('Verified admin user exists in database');
|
| 465 |
-
} else {
|
| 466 |
-
console.error('Failed to verify admin user creation');
|
| 467 |
-
}
|
| 468 |
} catch (error) {
|
| 469 |
-
console.error('Error initializing admin user:', error);
|
| 470 |
throw error;
|
| 471 |
}
|
| 472 |
}
|
| 473 |
|
| 474 |
-
// Start server
|
| 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 |
-
|
| 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()
|
|
|
|
| 10 |
const app = express();
|
| 11 |
const PORT = process.env.PORT || 3000;
|
| 12 |
|
|
|
|
| 13 |
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://new-iptv-manage:<db_password>@cluster0.o0eba.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0';
|
| 14 |
|
|
|
|
| 15 |
if (!MONGODB_URI.startsWith('mongodb://') && !MONGODB_URI.startsWith('mongodb+srv://')) {
|
|
|
|
| 16 |
process.exit(1);
|
| 17 |
}
|
| 18 |
|
|
|
|
| 22 |
try {
|
| 23 |
const client = await MongoClient.connect(MONGODB_URI);
|
| 24 |
db = client.db();
|
|
|
|
| 25 |
} catch (error) {
|
|
|
|
| 26 |
process.exit(1);
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
|
|
|
| 30 |
app.use(express.json());
|
| 31 |
app.use(express.urlencoded({ extended: true }));
|
| 32 |
app.use(session({
|
|
|
|
| 36 |
store: MongoStore.create({
|
| 37 |
mongoUrl: MONGODB_URI,
|
| 38 |
collectionName: 'sessions',
|
| 39 |
+
ttl: 24 * 60 * 60
|
| 40 |
})
|
| 41 |
}));
|
| 42 |
|
|
|
|
| 43 |
app.use((req, res, next) => {
|
| 44 |
const protectedPages = ['/hideme/dashboard.html', '/hideme/admin.html'];
|
| 45 |
const loginPage = '/hideme/index.html';
|
|
|
|
| 57 |
|
| 58 |
app.use(express.static('public'));
|
| 59 |
|
|
|
|
| 60 |
const auth = (req, res, next) => {
|
| 61 |
if (!req.session.userId) {
|
| 62 |
return res.status(401).json({ error: 'Unauthorized' });
|
|
|
|
| 64 |
next();
|
| 65 |
};
|
| 66 |
|
|
|
|
| 67 |
app.post('/api/admin/update', auth, async (req, res) => {
|
| 68 |
try {
|
| 69 |
const { newUsername, newPassword, currentPassword } = req.body;
|
|
|
|
| 89 |
}
|
| 90 |
});
|
| 91 |
|
|
|
|
| 92 |
app.post('/api/login', async (req, res) => {
|
| 93 |
try {
|
| 94 |
const { username, password } = req.body;
|
|
|
|
|
|
|
| 95 |
const user = await db.collection('users').findOne({ username });
|
| 96 |
+
|
| 97 |
if (!user) {
|
|
|
|
| 98 |
return res.status(401).json({ error: 'Invalid credentials' });
|
| 99 |
}
|
| 100 |
|
| 101 |
const isValidPassword = await bcrypt.compare(password, user.password);
|
|
|
|
|
|
|
| 102 |
if (isValidPassword) {
|
| 103 |
req.session.userId = user._id;
|
|
|
|
| 104 |
res.json({ success: true });
|
| 105 |
} else {
|
|
|
|
| 106 |
res.status(401).json({ error: 'Invalid credentials' });
|
| 107 |
}
|
| 108 |
} catch (error) {
|
|
|
|
| 109 |
res.status(500).json({ error: 'Login failed' });
|
| 110 |
}
|
| 111 |
});
|
|
|
|
| 175 |
}
|
| 176 |
});
|
| 177 |
|
|
|
|
| 178 |
app.get('/xmltv.php', async (req, res) => {
|
| 179 |
try {
|
| 180 |
const { username, password } = req.query;
|
|
|
|
|
|
|
| 181 |
const connection = await db.collection('connections').findOne({
|
| 182 |
iptvUsername: username,
|
| 183 |
iptvPassword: password
|
| 184 |
});
|
| 185 |
|
| 186 |
if (!connection) {
|
|
|
|
| 187 |
return res.status(401).json({ error: 'Invalid credentials' });
|
| 188 |
}
|
| 189 |
|
| 190 |
const targetUrl = `${connection.targetUrl}/xmltv.php?username=${connection.targetUsername}&password=${connection.targetPassword}`;
|
|
|
|
|
|
|
| 191 |
const headers = {
|
| 192 |
'accept': 'application/xml,text/xml,*/*',
|
| 193 |
'accept-language': req.headers['accept-language'],
|
|
|
|
| 200 |
});
|
| 201 |
|
| 202 |
if (!response.ok) {
|
| 203 |
+
throw new Error(`Target server error: ${response.status}`);
|
| 204 |
}
|
| 205 |
|
| 206 |
res.status(response.status);
|
|
|
|
| 212 |
};
|
| 213 |
|
| 214 |
Object.entries(responseHeaders).forEach(([key, value]) => {
|
| 215 |
+
if (value) res.setHeader(key, value);
|
|
|
|
|
|
|
| 216 |
});
|
| 217 |
|
| 218 |
response.body.pipe(res);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
} catch (error) {
|
|
|
|
| 220 |
if (!res.headersSent) {
|
| 221 |
+
res.status(502).json({ error: 'Failed to fetch XMLTV data' });
|
|
|
|
|
|
|
|
|
|
| 222 |
}
|
| 223 |
}
|
| 224 |
});
|
| 225 |
|
|
|
|
| 226 |
app.get('/player_api.php', async (req, res) => {
|
| 227 |
try {
|
|
|
|
| 228 |
const { username, password, ...otherParams } = req.query;
|
| 229 |
|
| 230 |
if (!username || !password) {
|
|
|
|
| 231 |
return res.status(401).json({
|
| 232 |
user_info: { auth: 0, status: "Active", error: "Missing credentials" }
|
| 233 |
});
|
|
|
|
| 238 |
iptvPassword: password
|
| 239 |
});
|
| 240 |
|
|
|
|
|
|
|
| 241 |
if (!connection) {
|
|
|
|
| 242 |
return res.status(401).json({
|
| 243 |
user_info: { auth: 0, status: "Active", error: "Invalid credentials" }
|
| 244 |
});
|
| 245 |
}
|
| 246 |
|
| 247 |
if (Object.keys(otherParams).length === 0) {
|
|
|
|
| 248 |
const serverUrl = new URL(req.protocol + '://' + req.get('host'));
|
| 249 |
|
| 250 |
let expTimestamp = connection.expireDate ?
|
| 251 |
Math.floor(new Date(connection.expireDate).getTime() / 1000) :
|
| 252 |
+
Math.floor(Date.now() / 1000) + (30 * 24 * 60 * 60);
|
| 253 |
|
| 254 |
+
return res.json({
|
| 255 |
user_info: {
|
| 256 |
username: username,
|
| 257 |
password: password,
|
|
|
|
| 275 |
timestamp_now: Math.floor(Date.now() / 1000),
|
| 276 |
time_now: new Date().toISOString().replace('T', ' ').slice(0, 19)
|
| 277 |
}
|
| 278 |
+
});
|
|
|
|
|
|
|
|
|
|
| 279 |
}
|
| 280 |
|
|
|
|
| 281 |
const queryParams = new URLSearchParams({
|
| 282 |
username: connection.targetUsername,
|
| 283 |
password: connection.targetPassword,
|
|
|
|
| 285 |
}).toString();
|
| 286 |
|
| 287 |
const targetUrl = `${connection.targetUrl}/player_api.php?${queryParams}`;
|
|
|
|
|
|
|
| 288 |
const response = await fetch(targetUrl, {
|
| 289 |
headers: {
|
| 290 |
'User-Agent': req.headers['user-agent'] || 'Mozilla/5.0',
|
|
|
|
| 295 |
});
|
| 296 |
|
| 297 |
if (!response.ok) {
|
| 298 |
+
throw new Error(`Target server error: ${response.status}`);
|
|
|
|
| 299 |
}
|
| 300 |
|
| 301 |
const data = await response.json();
|
|
|
|
| 302 |
res.json(data);
|
|
|
|
| 303 |
} catch (error) {
|
|
|
|
| 304 |
res.status(502).json({
|
| 305 |
error: 'Failed to fetch content',
|
|
|
|
| 306 |
status: 'error'
|
| 307 |
});
|
| 308 |
}
|
| 309 |
});
|
| 310 |
|
|
|
|
| 311 |
app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/series/:username/:password/:id'], async (req, res) => {
|
| 312 |
const { username, password, id } = req.params;
|
| 313 |
const type = req.path.split('/')[1];
|
| 314 |
|
|
|
|
|
|
|
| 315 |
const connection = await db.collection('connections').findOne({
|
| 316 |
iptvUsername: username,
|
| 317 |
iptvPassword: password
|
| 318 |
});
|
| 319 |
|
| 320 |
if (!connection) {
|
|
|
|
| 321 |
return res.status(401).json({ error: 'Invalid credentials' });
|
| 322 |
}
|
| 323 |
|
| 324 |
const targetUrl = `${connection.targetUrl}/${type}/${connection.targetUsername}/${connection.targetPassword}/${id}`;
|
|
|
|
| 325 |
|
| 326 |
try {
|
| 327 |
const headers = {
|
|
|
|
| 345 |
Object.entries(originalHeaders).forEach(([key, value]) => {
|
| 346 |
try {
|
| 347 |
res.setHeader(key, value);
|
| 348 |
+
} catch (err) {}
|
|
|
|
|
|
|
| 349 |
});
|
| 350 |
res.setHeader('Location', redirectUrl);
|
| 351 |
res.end();
|
|
|
|
| 354 |
response.headers.forEach((value, key) => {
|
| 355 |
try {
|
| 356 |
res.setHeader(key, value);
|
| 357 |
+
} catch (err) {}
|
|
|
|
|
|
|
| 358 |
});
|
| 359 |
|
| 360 |
const buffer = await response.buffer();
|
| 361 |
res.send(buffer);
|
| 362 |
}
|
| 363 |
} catch (error) {
|
| 364 |
+
res.status(502).json({ error: 'Failed to fetch stream' });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
}
|
| 366 |
});
|
| 367 |
|
|
|
|
| 368 |
async function initializeAdmin() {
|
| 369 |
try {
|
| 370 |
+
await db.collection('users').drop().catch(() => {});
|
|
|
|
| 371 |
|
|
|
|
| 372 |
const defaultUsername = process.env.ADMIN_USERNAME || 'admin';
|
| 373 |
const defaultPassword = process.env.ADMIN_PASSWORD || 'admin';
|
|
|
|
|
|
|
| 374 |
const hashedPassword = await bcrypt.hash(defaultPassword, 10);
|
| 375 |
+
|
| 376 |
+
await db.collection('users').insertOne({
|
| 377 |
username: defaultUsername,
|
| 378 |
password: hashedPassword,
|
| 379 |
createdAt: new Date(),
|
| 380 |
isAdmin: true
|
| 381 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
} catch (error) {
|
|
|
|
| 383 |
throw error;
|
| 384 |
}
|
| 385 |
}
|
| 386 |
|
|
|
|
| 387 |
async function startServer() {
|
| 388 |
await connectDB();
|
| 389 |
await initializeAdmin();
|
| 390 |
+
app.listen(PORT);
|
|
|
|
|
|
|
|
|
|
| 391 |
}
|
| 392 |
|
| 393 |
+
process.on('unhandledRejection', (error) => {});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
process.on('uncaughtException', (error) => {
|
|
|
|
| 395 |
process.exit(1);
|
| 396 |
});
|
| 397 |
|
| 398 |
+
startServer();
|