forked from wayangkulit95/iptv-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
192 lines (164 loc) · 5.52 KB
/
install.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
# Update and install required packages
echo "Updating package lists..."
sudo apt-get update -y
echo "Installing Node.js and npm..."
# Install Node.js and npm (for Ubuntu/Debian)
sudo apt-get install -y nodejs npm
# Install SQLite (if not already installed)
echo "Installing SQLite..."
sudo apt-get install -y sqlite3
# Install PM2 globally
echo "Installing PM2..."
sudo npm install -g pm2
# Create project directory and navigate into it
PROJECT_DIR="iptv-panel"
if [ ! -d "$PROJECT_DIR" ]; then
echo "Creating project directory: $PROJECT_DIR"
mkdir "$PROJECT_DIR"
fi
cd "$PROJECT_DIR" || exit
# Initialize npm and install required packages
if [ ! -f "package.json" ]; then
echo "Initializing npm..."
npm init -y
fi
echo "Installing required Node.js packages..."
npm install express sqlite3 body-parser node-fetch dotenv
# Create the database file and tables
echo "Setting up the database..."
cat <<EOF > database.js
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('iptv-panel.db');
db.serialize(() => {
// Create Users Table
db.run(\`CREATE TABLE IF NOT EXISTS users (
userId TEXT PRIMARY KEY,
userCode TEXT NOT NULL,
loginDetails TEXT,
status TEXT NOT NULL DEFAULT 'active',
expiryDate DATETIME,
deviceDetails TEXT
)\`);
// Create Resellers Table
db.run(\`CREATE TABLE IF NOT EXISTS resellers (
resellerId TEXT PRIMARY KEY,
credit INTEGER DEFAULT 0,
resellerDetails TEXT
)\`);
});
module.exports = db;
EOF
# Create the main app file
echo "Creating main application file..."
cat <<EOF > app.js
// app.js
const express = require('express');
const bodyParser = require('body-parser');
const db = require('./database');
const fetch = require('node-fetch');
const dotenv = require('dotenv');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
// Function to send notifications to Telegram
const sendTelegramNotification = async (message) => {
const botToken = process.env.TELEGRAM_BOT_TOKEN;
const chatId = process.env.TELEGRAM_CHAT_ID;
const url = `https://api.telegram.org/bot${botToken}/sendMessage`;
await fetch(url, {
method: 'POST',
body: JSON.stringify({ chat_id: chatId, text: message }),
headers: { 'Content-Type': 'application/json' },
});
};
// Add new user
app.post('/admin/addUser', (req, res) => {
const { userId, userCode, expiryDate } = req.body;
db.run(`INSERT INTO users (userId, userCode, expiryDate) VALUES (?, ?, ?)`,
[userId, userCode, expiryDate], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`New user added: ${userId}`);
res.status(201).send({ userId });
});
});
// Check user login details
app.get('/admin/userDetails/:userId', (req, res) => {
const { userId } = req.params;
db.get(`SELECT * FROM users WHERE userId = ?`, [userId], (err, row) => {
if (err) {
return res.status(500).send(err.message);
}
res.send(row);
});
});
// Ban user
app.post('/admin/banUser', (req, res) => {
const { userId } = req.body;
db.run(`UPDATE users SET status = 'banned' WHERE userId = ?`, [userId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`User banned: ${userId}`);
res.send({ message: 'User banned successfully.' });
});
});
// Unban user
app.post('/admin/unbanUser', (req, res) => {
const { userId } = req.body;
db.run(`UPDATE users SET status = 'active' WHERE userId = ?`, [userId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`User unbanned: ${userId}`);
res.send({ message: 'User unbanned successfully.' });
});
});
// Renew user subscription
app.post('/admin/renewUser', (req, res) => {
const { userId, newExpiryDate } = req.body;
db.run(`UPDATE users SET expiryDate = ? WHERE userId = ?`, [newExpiryDate, userId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`User subscription renewed: ${userId}`);
res.send({ message: 'User subscription renewed successfully.' });
});
});
// Add credit for reseller
app.post('/reseller/addCredit', (req, res) => {
const { resellerId, amount } = req.body;
db.run(`UPDATE resellers SET credit = credit + ? WHERE resellerId = ?`, [amount, resellerId], function (err) {
if (err) {
return res.status(500).send(err.message);
}
sendTelegramNotification(`Reseller ${resellerId} credited: ${amount}`);
res.send({ message: 'Credit added successfully.' });
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
EOF
# Create a .env file with specified entries
echo "Creating .env file for configuration..."
cat <<EOF > .env
TELEGRAM_BOT_TOKEN=your_bot_token
TELEGRAM_CHAT_ID=your_chat_id
PORT=3000
EOF
# Start the application with PM2
echo "Starting the application with PM2..."
pm2 start app.js --name iptv-panel
# Save PM2 process list for automatic restart
pm2 save
# Setup PM2 to start on system boot
pm2 startup
# Make the app executable
chmod +x app.js
echo "Installation completed successfully!"
echo "Remember to edit the .env file with your actual Telegram bot token and chat ID."