-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
268 lines (257 loc) · 8.73 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Discord Nginx Auth
// By Elijah R
// Copyright 2022
// Licensed under the GNU General Public License, latest version made available by the FSF
// Imports
const mysql = require("mysql2");
const fs = require("fs");
const http = require("http");
const url = require("url");
const axios = require('axios');
const querystring = require('querystring');
// Import the config file
if (!fs.existsSync("config.json")) {
console.error("config.json not found. Please make sure you've copied config.example.json and filled out all fields.");
process.exit(1);
}
var config;
try {
config = JSON.parse(fs.readFileSync("config.json"))
} catch {
console.error("Failed to parse config.json. Please make sure it contains valid JSON data.");
process.exit(1);
}
// Create the mysql connection
const db = mysql.createConnection({
host: config.mysqlHost,
user: config.mysqlUsername,
password: config.mysqlPass,
database: config.mysqlDb
});
// Init the database
async function dbConnect() {
return new Promise((resolve, reject) => {
db.connect((err) => {
if (err) {reject(err);} else {
console.log("Connected to the database");
resolve();
}
})
});
}
async function initDb() {
return new Promise(async (res, rej) => {
await dbConnect();
db.query(`CREATE TABLE IF NOT EXISTS ${config.mysqlTablePrefix}ips (username TEXT, ip TEXT)`, (err, result) => {
if (err) {
rej(err);
} else {
res(result);
}
});
});
}
// Check for a row
async function isIpInDb(ip) {
return new Promise((res, rej) => {
db.query(`SELECT ip FROM ${config.mysqlTablePrefix}ips WHERE ip=${db.escape(ip)}`, (err, result, fields) => {
if (err) {
rej(err);
} else {
if (result.length == 0) {
res(false);
} else {
res(true);
}
}
});
});
}
// Insert row into database
async function whitelistIp(username, ip) {
return new Promise((res, rej) => {
db.query(`INSERT INTO ${config.mysqlTablePrefix}ips (username, ip) VALUES (${db.escape(username)}, ${db.escape(ip)})`, (err, result) => {
if (err) {
rej(err);
} else {
res(result);
}
});
});
}
// Function to query discord api
var discord = {
authorize: async (usertoken, ip) => {
return new Promise (async (res, rej) => {
var token;
try {
token = await discord.getToken(usertoken);
} catch (err) {
res(["INVALID_TOKEN"]);
return;
}
var servers;
try { servers = await discord.guildList(token.access_token);} catch {
res("INTERNAL_ERROR");
return;
}
var ids = [];
for (var i = 0; i < Object.keys(servers).length; i++) {
ids.push(servers[i].id);
}
if (!ids.includes(config.allowedGuild)) {
res(["NOT_IN_GUILD"]);
return;
}
var roles;
try {roles = await discord.roleList(token.access_token);} catch {
res("INTERNAL_ERROR");
return;
}
var hasRequiredRole = false;
roles.roles.forEach((curr) => {
if (config.allowedRoles.includes(curr)) {
hasRequiredRole = true;
}
});
if (hasRequiredRole) {
var userdata;
try {
userdata = await discord.getUser(token.access_token);
} catch {
res("INTERNAL_ERROR");
return;
}
if (await isIpInDb(ip)) {
res(["ALREADY_AUTHORIZED"]);
} else {
whitelistIp(userdata.username, ip);
res(["AUTHORIZED", userdata.username]);
}
} else {
res(["NO_ROLE"]);
}
});
},
getToken: async (usertoken) => {
return new Promise((res, rej) => {
const data = `client_id=${config.discordClientId}&client_secret=${config.discordClientSecret}&grant_type=authorization_code&code=${usertoken}&redirect_uri=${config.canonicalUrl}${config.baseurl}/`;
axios.post("https://discord.com/api/v10/oauth2/token", data, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}}).then((result) => {
res(result.data);
}).catch((err) => {
rej(false);
});
});
},
guildList: async (usertoken) => {
return new Promise((res, rej) => {
axios.get(`https://discord.com/api/v10/users/@me/guilds`, {
headers: {
'authorization': `Bearer ${usertoken}`
}
}).then((result) => {
res(result.data);
});
});
},
roleList: async (usertoken) => {
return new Promise((res, rej) => {
axios.get(`https://discord.com/api/v10/users/@me/guilds/${config.allowedGuild}/member`, {
headers: {
'authorization': `Bearer ${usertoken}`
}
}).then((result => {
res(result.data);
})).catch((err) => {
rej(err);
});
});
},
getUser: async (usertoken) => {
return new Promise((res, rej) => {
axios.get(`https://discord.com/api/v10/users/@me`, {
headers: {
'authorization': `Bearer ${usertoken}`
}
}).then((result => {
res(result.data);
})).catch((err) => {
rej(err);
});
});
}
}
// Create HTTP Server
const server = http.createServer(async (req, res) => {
var requrl = url.parse(req.url, true);
var ip;
if (Array.isArray(req.headers["x-forwarded-for"])) {
ip = req.headers["x-forwarded-for"][0];
} else if (req.headers["x-forwarded-for"].includes(", ")) {
ip = req.headers["x-forwarded-for"].split(", ")[0];
} else {
ip = req.headers["x-forwarded-for"]
}
switch(requrl.pathname) {
case `${config.baseurl}/`:
if (requrl.query.code == undefined) {
res.writeHead(302, {
Location: `https://discord.com/api/oauth2/authorize?client_id=${encodeURIComponent(config.discordClientId)}&redirect_uri=${encodeURIComponent(`${config.canonicalUrl}${config.baseurl}/`)}&response_type=code&scope=identify%20guilds%20guilds.members.read`
});
res.end();
} else {
var token = await discord.authorize(requrl.query.code, ip);
switch (token[0]) {
case "INVALID_TOKEN":
res.writeHead(400);
res.end("400: Invalid Token");
break;
case "NOT_IN_GUILD":
res.writeHead(403);
res.end("403: You are not in the required guild");
break;
case "NO_ROLE":
res.writeHead(403);
res.end("403: You do not have the required role.");
break;
case "ALREADY_AUTHORIZED":
res.writeHead(200);
res.end("This ip is already authorized.");
break;
case "AUTHORIZED":
res.writeHead(200);
res.end(`Successfully authorized ${token[1]} at ${ip}`);
console.log(`Authorized ${token[1]} to ${ip}`);
break;
default:
res.writeHead(500);
res.end("500: Internal Server Error.");
break;
}
}
break;
case `${config.baseurl}/authrequest`:
if (await isIpInDb(ip)) {
res.writeHead(200);
} else {
res.writeHead(403);
}
res.end();
break;
default:
res.writeHead(404);
res.end("404");
break;
}
});
// All the async shit goes in here
async function main() {
// Connect to the database
await initDb();
console.log(`Starting webserver on port ${config.listenPort}`);
server.listen(config.listenPort);
}
main();