-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (172 loc) · 5.87 KB
/
index.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const DiscordStrategy = require('passport-discord').Strategy;
const { WebhookClient, MessageEmbed } = require('discord.js'); // Add this line
const mongoose = require('mongoose');
const User = require('./models/User');
const PanelData = require('./models/Uptime');
const path = require('path')
// ...
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views')); // Add this line
app.use(express.static('public'));
const clientId = process.env.REACT_APP_CLIENT_ID;
const clientSecret = process.env.REACT_APP_CLIENT_SECRET;
const redirectUri = process.env.REACT_APP_REDIRECT_URI;
const webhookURL = process.env.webhookURL;
app.use(session({
secret: 'your-secret-key',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new DiscordStrategy({
clientID: clientId,
clientSecret: clientSecret,
callbackURL: redirectUri,
scope: ['identify'],
}, (accessToken, refreshToken, profile, done) => {
return done(null, profile);
}));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
app.get('/login', passport.authenticate('discord'));
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
}).then(() => {
console.log('Connected to MongoDB');
}).catch((error) => {
console.error('Error connecting to MongoDB:', error);
});
app.get('/callback', passport.authenticate('discord', {
failureRedirect: '/'
}), async (req, res) => {
try {
console.log(`User logged in: ${req.user.username}#${req.user.discriminator} (${req.user.id})`);
const newUser = await User.findOneAndUpdate(
{ discordId: req.user.id },
{
discordId: req.user.id,
username: req.user.username,
discriminator: req.user.discriminator,
avatar: req.user.avatar,
},
{ upsert: true, new: true }
);
const webhook = new WebhookClient({ url: process.env.webhookURL });
const embed = new MessageEmbed()
.setColor('#0099ff')
.setDescription(`** :inbox_tray: ${req.user.username} Adlı Kullanıcı Siteye Giriş Yaptı! **`);
webhook.send({ embeds: [embed] });
res.redirect('/panel');
} catch (error) {
console.error('Error saving user to MongoDB:', error);
res.redirect('/');
}
});
app.get('/', (req, res) => {
res.render('index', { user: req.user });
});
app.get('/panel', (req, res) => {
if (req.isAuthenticated()) {
res.render('pages/panel', { user: req.user });
} else {
res.redirect('/login');
}
});
app.get('/panel/ekle', async (req, res) => {
if (req.isAuthenticated()) {
try {
const userLinks = await PanelData.find({ userId: req.user.id });
res.render('pages/ekle', { user: req.user, userLinks });
} catch (error) {
console.error('Error fetching user links from MongoDB:', error);
res.redirect('/panel/ekle?error=db');
}
} else {
res.redirect('/login');
}
});
app.use(express.urlencoded({ extended: true }));
const cron = require('node-cron');
const axios = require('axios');
app.post('/panel/ekle', async (req, res) => {
if (req.isAuthenticated()) {
const { name, url } = req.body;
const isPremium = req.user.premium || false;
const maxLinkLimit = isPremium ? 10 : 3;
const userLinkCount = await PanelData.countDocuments({ userId: req.user.id });
if (userLinkCount >= maxLinkLimit) {
return res.redirect('/panel/ekle?error=limit');
}
const newData = new PanelData({
name,
url,
userId: req.user.id,
});
newData.save()
.then(async () => {
// Log to webhook
const webhook = new WebhookClient({ url: process.env.webhookURL });
const embed = new MessageEmbed()
.setColor('#8BDFA9')
.setDescription(`**<:uptime:1202930398183559218> ${req.user.username} Adlı Kullanıcı Sisteme Link Ekledi: ${name}**`);
webhook.send({ embeds: [embed] });
const userLinks = await PanelData.find({ userId: req.user.id });
res.render('pages/panel', { user: req.user, userLinks });
})
.catch((error) => {
console.error('Error saving data to MongoDB:', error);
res.redirect(`/panel/ekle?error=${error.message}`);
});
} else {
res.redirect('/login');
}
});
// Schedule pinging every 30 seconds
cron.schedule('*/30 * * * * *', async () => {
try {
const allLinks = await PanelData.find();
for (const link of allLinks) {
// Send a ping to the URL using axios or any other HTTP request library
await axios.get(link.url);
}
console.log('Pinged all URLs successfully');
} catch (error) {
console.error('Error pinging URLs:', error);
}
});
app.post('/panel/delete', async (req, res) => {
if (req.isAuthenticated()) {
try {
const linkId = req.body.linkId;
// Fetch the name of the link before deleting
const deletedLink = await PanelData.findOne({ _id: linkId, userId: req.user.id });
const deletedLinkName = deletedLink.name;
const webhook = new WebhookClient({ url: process.env.webhookURL });
const embed = new MessageEmbed()
.setColor('#E54343')
.setDescription(`**<:f_delete:1202931247538503752> ${req.user.username} Adlı Kullanıcı Sistemden Link Sildi: ${deletedLinkName}**`);
webhook.send({ embeds: [embed] });
await PanelData.deleteOne({ _id: linkId, userId: req.user.id });
res.redirect('/panel/ekle');
} catch (error) {
console.error('Error deleting link from MongoDB:', error);
res.redirect('/panel/ekle?error=db');
}
} else {
res.redirect('/login');
}
});
app.listen(port, () => {
console.log(`Uygulama http://localhost:${port} adresinde çalışıyor.`);
});