Update index.js
Browse files
index.js
CHANGED
|
@@ -10,23 +10,47 @@ const path = require('path');
|
|
| 10 |
const app = express();
|
| 11 |
const PORT = process.env.PORT || 3000;
|
| 12 |
|
| 13 |
-
|
|
|
|
| 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 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,6 +64,7 @@ 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,13 +82,14 @@ app.use((req, res, next) => {
|
|
| 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,11 +115,12 @@ app.post('/api/admin/update', auth, async (req, res) => {
|
|
| 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,9 +128,9 @@ app.post('/api/login', async (req, res) => {
|
|
| 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 {
|
|
@@ -122,6 +149,7 @@ app.get('/api/connections', auth, async (req, res) => {
|
|
| 122 |
try {
|
| 123 |
const connections = await db.collection('connections')
|
| 124 |
.find({ userId: req.session.userId.toString() })
|
|
|
|
| 125 |
.toArray();
|
| 126 |
res.json(connections);
|
| 127 |
} catch (error) {
|
|
@@ -135,10 +163,20 @@ app.post('/api/connections', auth, async (req, res) => {
|
|
| 135 |
userId: req.session.userId.toString(),
|
| 136 |
expireDate: req.body.expireDate || null,
|
| 137 |
createdAt: new Date(),
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
};
|
| 140 |
|
| 141 |
const result = await db.collection('connections').insertOne(newConnection);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
res.json({ ...newConnection, _id: result.insertedId });
|
| 143 |
} catch (error) {
|
| 144 |
res.status(500).json({ error: 'Failed to add connection' });
|
|
@@ -147,17 +185,17 @@ app.post('/api/connections', auth, async (req, res) => {
|
|
| 147 |
|
| 148 |
app.put('/api/connections/:id', auth, async (req, res) => {
|
| 149 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
const result = await db.collection('connections').findOneAndUpdate(
|
| 151 |
{
|
| 152 |
_id: new MongoClient.ObjectId(req.params.id),
|
| 153 |
-
userId: req.session.userId
|
| 154 |
-
},
|
| 155 |
-
{
|
| 156 |
-
$set: {
|
| 157 |
-
...req.body,
|
| 158 |
-
updatedAt: new Date()
|
| 159 |
-
}
|
| 160 |
},
|
|
|
|
| 161 |
{ returnDocument: 'after' }
|
| 162 |
);
|
| 163 |
|
|
@@ -173,16 +211,21 @@ app.put('/api/connections/:id', auth, async (req, res) => {
|
|
| 173 |
|
| 174 |
app.delete('/api/connections/:id', auth, async (req, res) => {
|
| 175 |
try {
|
| 176 |
-
await db.collection('connections').deleteOne({
|
| 177 |
_id: new MongoClient.ObjectId(req.params.id),
|
| 178 |
-
userId: req.session.userId
|
| 179 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
res.json({ success: true });
|
| 181 |
} catch (error) {
|
| 182 |
res.status(500).json({ error: 'Failed to delete connection' });
|
| 183 |
}
|
| 184 |
});
|
| 185 |
-
|
| 186 |
app.get('/xmltv.php', async (req, res) => {
|
| 187 |
try {
|
| 188 |
const { username, password } = req.query;
|
|
@@ -231,6 +274,7 @@ app.get('/xmltv.php', async (req, res) => {
|
|
| 231 |
}
|
| 232 |
});
|
| 233 |
|
|
|
|
| 234 |
app.get('/player_api.php', async (req, res) => {
|
| 235 |
try {
|
| 236 |
const { username, password, ...otherParams } = req.query;
|
|
@@ -252,9 +296,9 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 252 |
});
|
| 253 |
}
|
| 254 |
|
|
|
|
| 255 |
if (Object.keys(otherParams).length === 0) {
|
| 256 |
const serverUrl = new URL(req.protocol + '://' + req.get('host'));
|
| 257 |
-
|
| 258 |
let expTimestamp = connection.expireDate ?
|
| 259 |
Math.floor(new Date(connection.expireDate).getTime() / 1000) :
|
| 260 |
Math.floor(Date.now() / 1000) + (30 * 24 * 60 * 60);
|
|
@@ -286,6 +330,7 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 286 |
});
|
| 287 |
}
|
| 288 |
|
|
|
|
| 289 |
const queryParams = new URLSearchParams({
|
| 290 |
username: connection.targetUsername,
|
| 291 |
password: connection.targetPassword,
|
|
@@ -316,6 +361,7 @@ app.get('/player_api.php', async (req, res) => {
|
|
| 316 |
}
|
| 317 |
});
|
| 318 |
|
|
|
|
| 319 |
app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/series/:username/:password/:id'], async (req, res) => {
|
| 320 |
const { username, password, id } = req.params;
|
| 321 |
const type = req.path.split('/')[1];
|
|
@@ -373,34 +419,47 @@ app.get(['/live/:username/:password/:id', '/movie/:username/:password/:id', '/se
|
|
| 373 |
}
|
| 374 |
});
|
| 375 |
|
|
|
|
| 376 |
async function initializeAdmin() {
|
| 377 |
try {
|
| 378 |
-
await db.collection('users').
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
| 390 |
} catch (error) {
|
| 391 |
throw error;
|
| 392 |
}
|
| 393 |
}
|
| 394 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
async function startServer() {
|
| 396 |
await connectDB();
|
| 397 |
await initializeAdmin();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
app.listen(PORT);
|
| 399 |
}
|
| 400 |
|
| 401 |
-
process.on('unhandledRejection', (error) => {});
|
| 402 |
-
process.on('uncaughtException', (error) => {
|
| 403 |
-
process.exit(1);
|
| 404 |
-
});
|
| 405 |
-
|
| 406 |
startServer();
|
|
|
|
| 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/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 |
+
// Add MongoDB connection options for better persistence
|
| 27 |
+
const options = {
|
| 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 |
})
|
| 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 |
|
| 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 |
+
// Admin Settings Route
|
| 93 |
app.post('/api/admin/update', auth, async (req, res) => {
|
| 94 |
try {
|
| 95 |
const { newUsername, newPassword, currentPassword } = req.body;
|
|
|
|
| 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 |
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.toString() })
|
| 133 |
+
.sort({ createdAt: -1 })
|
| 134 |
.toArray();
|
| 135 |
res.json({ success: true, connections });
|
| 136 |
} else {
|
|
|
|
| 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) {
|
|
|
|
| 163 |
userId: req.session.userId.toString(),
|
| 164 |
expireDate: req.body.expireDate || null,
|
| 165 |
createdAt: new Date(),
|
| 166 |
+
updatedAt: new Date(),
|
| 167 |
+
name: req.body.name,
|
| 168 |
+
targetUrl: req.body.targetUrl,
|
| 169 |
+
targetUsername: req.body.targetUsername,
|
| 170 |
+
targetPassword: req.body.targetPassword,
|
| 171 |
+
iptvUsername: req.body.iptvUsername,
|
| 172 |
+
iptvPassword: req.body.iptvPassword
|
| 173 |
};
|
| 174 |
|
| 175 |
const result = await db.collection('connections').insertOne(newConnection);
|
| 176 |
+
|
| 177 |
+
// Create an index on userId for better query performance
|
| 178 |
+
await db.collection('connections').createIndex({ userId: 1 });
|
| 179 |
+
|
| 180 |
res.json({ ...newConnection, _id: result.insertedId });
|
| 181 |
} catch (error) {
|
| 182 |
res.status(500).json({ error: 'Failed to add connection' });
|
|
|
|
| 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.toString()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
},
|
| 198 |
+
{ $set: updateData },
|
| 199 |
{ returnDocument: 'after' }
|
| 200 |
);
|
| 201 |
|
|
|
|
| 211 |
|
| 212 |
app.delete('/api/connections/:id', auth, async (req, res) => {
|
| 213 |
try {
|
| 214 |
+
const result = await db.collection('connections').deleteOne({
|
| 215 |
_id: new MongoClient.ObjectId(req.params.id),
|
| 216 |
+
userId: req.session.userId.toString()
|
| 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 |
+
// XMLTV Route
|
| 229 |
app.get('/xmltv.php', async (req, res) => {
|
| 230 |
try {
|
| 231 |
const { username, password } = req.query;
|
|
|
|
| 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 |
});
|
| 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 |
});
|
| 331 |
}
|
| 332 |
|
| 333 |
+
// Proxy request to target server
|
| 334 |
const queryParams = new URLSearchParams({
|
| 335 |
username: connection.targetUsername,
|
| 336 |
password: connection.targetPassword,
|
|
|
|
| 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 |
}
|
| 420 |
});
|
| 421 |
|
| 422 |
+
// Initialize admin user
|
| 423 |
async function initializeAdmin() {
|
| 424 |
try {
|
| 425 |
+
const adminExists = await db.collection('users').findOne({ username: process.env.ADMIN_USERNAME || 'admin' });
|
| 426 |
+
if (!adminExists) {
|
| 427 |
+
const defaultUsername = process.env.ADMIN_USERNAME || 'admin';
|
| 428 |
+
const defaultPassword = process.env.ADMIN_PASSWORD || 'admin';
|
| 429 |
+
const hashedPassword = await bcrypt.hash(defaultPassword, 10);
|
| 430 |
+
|
| 431 |
+
await db.collection('users').insertOne({
|
| 432 |
+
username: defaultUsername,
|
| 433 |
+
password: hashedPassword,
|
| 434 |
+
createdAt: new Date(),
|
| 435 |
+
isAdmin: true
|
| 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();
|