Update index.js
Browse files
index.js
CHANGED
|
@@ -10,47 +10,23 @@ const path = require('path');
|
|
| 10 |
const app = express();
|
| 11 |
const PORT = process.env.PORT || 3000;
|
| 12 |
|
| 13 |
-
|
| 14 |
-
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://new-iptv-manage:<db_password>@cluster0.o0eba.mongodb.net/iptv_manager?retryWrites=true&w=majority&appName=Cluster0';
|
| 15 |
|
| 16 |
if (!MONGODB_URI.startsWith('mongodb://') && !MONGODB_URI.startsWith('mongodb+srv://')) {
|
| 17 |
-
console.error('Invalid MongoDB URI. Must start with mongodb:// or mongodb+srv://');
|
| 18 |
process.exit(1);
|
| 19 |
}
|
| 20 |
|
| 21 |
let db;
|
| 22 |
-
let client;
|
| 23 |
|
| 24 |
async function connectDB() {
|
| 25 |
try {
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
useNewUrlParser: true,
|
| 29 |
-
useUnifiedTopology: true,
|
| 30 |
-
serverSelectionTimeoutMS: 5000,
|
| 31 |
-
socketTimeoutMS: 45000,
|
| 32 |
-
connectTimeoutMS: 10000,
|
| 33 |
-
maxPoolSize: 50
|
| 34 |
-
};
|
| 35 |
-
|
| 36 |
-
client = await MongoClient.connect(MONGODB_URI, options);
|
| 37 |
-
db = client.db('iptv_manager'); // Specify the database name
|
| 38 |
-
|
| 39 |
-
// Handle connection events
|
| 40 |
-
client.on('connected', () => console.log('MongoDB connected'));
|
| 41 |
-
client.on('error', (err) => console.error('MongoDB error:', err));
|
| 42 |
-
client.on('disconnected', async () => {
|
| 43 |
-
console.log('MongoDB disconnected, attempting to reconnect...');
|
| 44 |
-
await connectDB();
|
| 45 |
-
});
|
| 46 |
-
|
| 47 |
} catch (error) {
|
| 48 |
-
console.error('MongoDB connection error:', error);
|
| 49 |
process.exit(1);
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
-
// Middleware
|
| 54 |
app.use(express.json());
|
| 55 |
app.use(express.urlencoded({ extended: true }));
|
| 56 |
app.use(session({
|
|
@@ -64,7 +40,6 @@ app.use(session({
|
|
| 64 |
})
|
| 65 |
}));
|
| 66 |
|
| 67 |
-
// Auth check middleware
|
| 68 |
app.use((req, res, next) => {
|
| 69 |
const protectedPages = ['/hideme/dashboard.html', '/hideme/admin.html'];
|
| 70 |
const loginPage = '/hideme/index.html';
|
|
@@ -82,14 +57,13 @@ app.use((req, res, next) => {
|
|
| 82 |
|
| 83 |
app.use(express.static('public'));
|
| 84 |
|
| 85 |
-
// Auth middleware
|
| 86 |
const auth = (req, res, next) => {
|
| 87 |
if (!req.session.userId) {
|
| 88 |
return res.status(401).json({ error: 'Unauthorized' });
|
| 89 |
}
|
| 90 |
next();
|
| 91 |
};
|
| 92 |
-
|
| 93 |
app.post('/api/admin/update', auth, async (req, res) => {
|
| 94 |
try {
|
| 95 |
const { newUsername, newPassword, currentPassword } = req.body;
|
|
@@ -115,12 +89,11 @@ app.post('/api/admin/update', auth, async (req, res) => {
|
|
| 115 |
}
|
| 116 |
});
|
| 117 |
|
| 118 |
-
// API Routes
|
| 119 |
app.post('/api/login', async (req, res) => {
|
| 120 |
try {
|
| 121 |
const { username, password } = req.body;
|
| 122 |
-
|
| 123 |
const user = await db.collection('users').findOne({ username });
|
|
|
|
| 124 |
if (!user) {
|
| 125 |
return res.status(401).json({ error: 'Invalid credentials' });
|
| 126 |
}
|
|
@@ -128,9 +101,9 @@ app.post('/api/login', async (req, res) => {
|
|
| 128 |
const isValidPassword = await bcrypt.compare(password, user.password);
|
| 129 |
if (isValidPassword) {
|
| 130 |
req.session.userId = user._id;
|
|
|
|
| 131 |
const connections = await db.collection('connections')
|
| 132 |
-
.find({ userId: user._id
|
| 133 |
-
.sort({ createdAt: -1 })
|
| 134 |
.toArray();
|
| 135 |
res.json({ success: true, connections });
|
| 136 |
} else {
|
|
@@ -147,9 +120,10 @@ app.post('/api/logout', (req, res) => {
|
|
| 147 |
|
| 148 |
app.get('/api/connections', auth, async (req, res) => {
|
| 149 |
try {
|
|
|
|
| 150 |
const connections = await db.collection('connections')
|
| 151 |
.find({ userId: req.session.userId.toString() })
|
| 152 |
-
.sort({ createdAt: -1 })
|
| 153 |
.toArray();
|
| 154 |
res.json(connections);
|
| 155 |
} catch (error) {
|
|
@@ -185,17 +159,17 @@ app.post('/api/connections', auth, async (req, res) => {
|
|
| 185 |
|
| 186 |
app.put('/api/connections/:id', auth, async (req, res) => {
|
| 187 |
try {
|
| 188 |
-
const updateData = {
|
| 189 |
-
...req.body,
|
| 190 |
-
updatedAt: new Date()
|
| 191 |
-
};
|
| 192 |
-
|
| 193 |
const result = await db.collection('connections').findOneAndUpdate(
|
| 194 |
{
|
| 195 |
_id: new MongoClient.ObjectId(req.params.id),
|
| 196 |
-
userId: req.session.userId
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
},
|
| 198 |
-
{ $set: updateData },
|
| 199 |
{ returnDocument: 'after' }
|
| 200 |
);
|
| 201 |
|
|
@@ -211,21 +185,16 @@ app.put('/api/connections/:id', auth, async (req, res) => {
|
|
| 211 |
|
| 212 |
app.delete('/api/connections/:id', auth, async (req, res) => {
|
| 213 |
try {
|
| 214 |
-
|
| 215 |
_id: new MongoClient.ObjectId(req.params.id),
|
| 216 |
-
userId: req.session.userId
|
| 217 |
});
|
| 218 |
-
|
| 219 |
-
if (result.deletedCount === 0) {
|
| 220 |
-
return res.status(404).json({ error: 'Connection not found' });
|
| 221 |
-
}
|
| 222 |
-
|
| 223 |
res.json({ success: true });
|
| 224 |
} catch (error) {
|
| 225 |
res.status(500).json({ error: 'Failed to delete connection' });
|
| 226 |
}
|
| 227 |
});
|
| 228 |
-
|
| 229 |
app.get('/xmltv.php', async (req, res) => {
|
| 230 |
try {
|
| 231 |
const { username, password } = req.query;
|
|
@@ -274,7 +243,6 @@ app.get('/xmltv.php', async (req, res) => {
|
|
| 274 |
}
|
| 275 |
});
|
| 276 |
|
| 277 |
-
// Player API Route
|
| 278 |
app.get('/player_api.php', async (req, res) => {
|
| 279 |
try {
|
| 280 |
const { username, password, ...otherParams } = req.query;
|
|
@@ -296,9 +264,9 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 296 |
});
|
| 297 |
}
|
| 298 |
|
| 299 |
-
// If no additional parameters, return auth response
|
| 300 |
if (Object.keys(otherParams).length === 0) {
|
| 301 |
const serverUrl = new URL(req.protocol + '://' + req.get('host'));
|
|
|
|
| 302 |
let expTimestamp = connection.expireDate ?
|
| 303 |
Math.floor(new Date(connection.expireDate).getTime() / 1000) :
|
| 304 |
Math.floor(Date.now() / 1000) + (30 * 24 * 60 * 60);
|
|
@@ -330,7 +298,6 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 330 |
});
|
| 331 |
}
|
| 332 |
|
| 333 |
-
// Proxy request to target server
|
| 334 |
const queryParams = new URLSearchParams({
|
| 335 |
username: connection.targetUsername,
|
| 336 |
password: connection.targetPassword,
|
|
@@ -361,7 +328,6 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 361 |
}
|
| 362 |
});
|
| 363 |
|
| 364 |
-
// Stream routes
|
| 365 |
app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/series/:username/:password/:id'], async (req, res) => {
|
| 366 |
const { username, password, id } = req.params;
|
| 367 |
const type = req.path.split('/')[1];
|
|
@@ -419,47 +385,34 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 419 |
}
|
| 420 |
});
|
| 421 |
|
| 422 |
-
// Initialize admin user
|
| 423 |
async function initializeAdmin() {
|
| 424 |
try {
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
}
|
| 438 |
} catch (error) {
|
| 439 |
throw error;
|
| 440 |
}
|
| 441 |
}
|
| 442 |
|
| 443 |
-
// Handle graceful shutdown
|
| 444 |
-
process.on('SIGINT', async () => {
|
| 445 |
-
try {
|
| 446 |
-
await client.close();
|
| 447 |
-
process.exit(0);
|
| 448 |
-
} catch (error) {
|
| 449 |
-
process.exit(1);
|
| 450 |
-
}
|
| 451 |
-
});
|
| 452 |
-
|
| 453 |
-
// Start server
|
| 454 |
async function startServer() {
|
| 455 |
await connectDB();
|
| 456 |
await initializeAdmin();
|
| 457 |
-
|
| 458 |
-
// Create necessary indexes
|
| 459 |
-
await db.collection('connections').createIndex({ userId: 1 });
|
| 460 |
-
await db.collection('users').createIndex({ username: 1 }, { unique: true });
|
| 461 |
-
|
| 462 |
app.listen(PORT);
|
| 463 |
}
|
| 464 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
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 |
|
| 19 |
let db;
|
|
|
|
| 20 |
|
| 21 |
async function connectDB() {
|
| 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({
|
|
|
|
| 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' });
|
| 63 |
}
|
| 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 |
}
|
|
|
|
| 101 |
const isValidPassword = await bcrypt.compare(password, user.password);
|
| 102 |
if (isValidPassword) {
|
| 103 |
req.session.userId = user._id;
|
| 104 |
+
// Fetch all connections for this user
|
| 105 |
const connections = await db.collection('connections')
|
| 106 |
+
.find({ userId: user._id })
|
|
|
|
| 107 |
.toArray();
|
| 108 |
res.json({ success: true, connections });
|
| 109 |
} else {
|
|
|
|
| 120 |
|
| 121 |
app.get('/api/connections', auth, async (req, res) => {
|
| 122 |
try {
|
| 123 |
+
// Convert userId to string for comparison
|
| 124 |
const connections = await db.collection('connections')
|
| 125 |
.find({ userId: req.session.userId.toString() })
|
| 126 |
+
.sort({ createdAt: -1 }) // Sort by creation date, newest first
|
| 127 |
.toArray();
|
| 128 |
res.json(connections);
|
| 129 |
} catch (error) {
|
|
|
|
| 159 |
|
| 160 |
app.put('/api/connections/:id', auth, async (req, res) => {
|
| 161 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
const result = await db.collection('connections').findOneAndUpdate(
|
| 163 |
{
|
| 164 |
_id: new MongoClient.ObjectId(req.params.id),
|
| 165 |
+
userId: req.session.userId
|
| 166 |
+
},
|
| 167 |
+
{
|
| 168 |
+
$set: {
|
| 169 |
+
...req.body,
|
| 170 |
+
updatedAt: new Date()
|
| 171 |
+
}
|
| 172 |
},
|
|
|
|
| 173 |
{ returnDocument: 'after' }
|
| 174 |
);
|
| 175 |
|
|
|
|
| 185 |
|
| 186 |
app.delete('/api/connections/:id', auth, async (req, res) => {
|
| 187 |
try {
|
| 188 |
+
await db.collection('connections').deleteOne({
|
| 189 |
_id: new MongoClient.ObjectId(req.params.id),
|
| 190 |
+
userId: req.session.userId
|
| 191 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
res.json({ success: true });
|
| 193 |
} catch (error) {
|
| 194 |
res.status(500).json({ error: 'Failed to delete connection' });
|
| 195 |
}
|
| 196 |
});
|
| 197 |
+
|
| 198 |
app.get('/xmltv.php', async (req, res) => {
|
| 199 |
try {
|
| 200 |
const { username, password } = req.query;
|
|
|
|
| 243 |
}
|
| 244 |
});
|
| 245 |
|
|
|
|
| 246 |
app.get('/player_api.php', async (req, res) => {
|
| 247 |
try {
|
| 248 |
const { username, password, ...otherParams } = req.query;
|
|
|
|
| 264 |
});
|
| 265 |
}
|
| 266 |
|
|
|
|
| 267 |
if (Object.keys(otherParams).length === 0) {
|
| 268 |
const serverUrl = new URL(req.protocol + '://' + req.get('host'));
|
| 269 |
+
|
| 270 |
let expTimestamp = connection.expireDate ?
|
| 271 |
Math.floor(new Date(connection.expireDate).getTime() / 1000) :
|
| 272 |
Math.floor(Date.now() / 1000) + (30 * 24 * 60 * 60);
|
|
|
|
| 298 |
});
|
| 299 |
}
|
| 300 |
|
|
|
|
| 301 |
const queryParams = new URLSearchParams({
|
| 302 |
username: connection.targetUsername,
|
| 303 |
password: connection.targetPassword,
|
|
|
|
| 328 |
}
|
| 329 |
});
|
| 330 |
|
|
|
|
| 331 |
app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/series/:username/:password/:id'], async (req, res) => {
|
| 332 |
const { username, password, id } = req.params;
|
| 333 |
const type = req.path.split('/')[1];
|
|
|
|
| 385 |
}
|
| 386 |
});
|
| 387 |
|
|
|
|
| 388 |
async function initializeAdmin() {
|
| 389 |
try {
|
| 390 |
+
await db.collection('users').drop().catch(() => {});
|
| 391 |
+
|
| 392 |
+
const defaultUsername = process.env.ADMIN_USERNAME || 'admin';
|
| 393 |
+
const defaultPassword = process.env.ADMIN_PASSWORD || 'admin';
|
| 394 |
+
const hashedPassword = await bcrypt.hash(defaultPassword, 10);
|
| 395 |
+
|
| 396 |
+
await db.collection('users').insertOne({
|
| 397 |
+
username: defaultUsername,
|
| 398 |
+
password: hashedPassword,
|
| 399 |
+
createdAt: new Date(),
|
| 400 |
+
isAdmin: true
|
| 401 |
+
});
|
|
|
|
| 402 |
} catch (error) {
|
| 403 |
throw error;
|
| 404 |
}
|
| 405 |
}
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
async function startServer() {
|
| 408 |
await connectDB();
|
| 409 |
await initializeAdmin();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 410 |
app.listen(PORT);
|
| 411 |
}
|
| 412 |
|
| 413 |
+
process.on('unhandledRejection', (error) => {});
|
| 414 |
+
process.on('uncaughtException', (error) => {
|
| 415 |
+
process.exit(1);
|
| 416 |
+
});
|
| 417 |
+
|
| 418 |
startServer();
|