-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (98 loc) · 3.05 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
const fs = require("fs");
// Do a quick check if the config exists, if not, copy the default and exit telling the end user to configure it
if (!fs.existsSync("./config.json")) {
fs.copyFileSync("./config.default.json", "config.json");
console.error("Please set up config.json then restart!");
process.exit(1);
}
const config = require("./config.json");
const mariadb = require('mariadb');
const express = require('express');
const app = express();
mariadb.createConnection(config.sql).then(db => {
console.log("connected to database")
if (fs.existsSync("./bans.sql")) {
db.query(fs.readFileSync("./bans.sql").toString());
} else {
console.log("bans.sql does not exist! skipping table creation\nWARNING: THIS MAY CAUSE ISSUES IF YOU HAVENT RAN IT AT LEAST ONCE")
}
app.listen(config.stormworks.port, config.stormworks.hostname, () => {
console.log("webserver started");
});
app.get('/check', (req, res) => {
if (!req.query.steam_id || !req.query.p) {
return res.send({
code: 400,
status: false
}).end();
}
if (req.query.p != config.stormworks.password) return res.send({
code: 401,
status: false
}).end();
db.query(`SELECT * FROM bans WHERE steam_id = ?`, [req.query.steam_id]).then((response) => {
if (response[0] === undefined) return res.send({
status: false
}).end();
response[0].status = true;
res.send(response[0]).end();
})
});
app.get('/checkall', async (req, res) => {
console.log(req.query)
if (!req.query.ids || !req.query.p) {
return res.send({
code: 400,
status: false
}).end();
}
if (req.query.p != config.stormworks.password) return res.send({
code: 401,
status: false
}).end();
// Rip steam IDs out of the array
let ids = JSON.parse(req.query.ids, (key, value) => {
if (key === 'steam_id') {
return value.toString();
}
return value;
});
let steam_ids = [];
for (let i = 0; i < ids.length; i++) {
await steam_ids.push(ids[i].steam_id);
}
db.query(`SELECT * FROM bans WHERE steam_id IN (?)`, [steam_ids]).then((response) => {
if (response[0] === undefined) return res.send({
status: false
}).end();
res1 = {}
res1.status = true;
res1.bans = response
res.send(res1).end();
})
})
app.get("/ban", (req, res) => {
if (!req.query.steam_id || !req.query.moderator || !req.query.banned_from || !req.query.username || !req.query.p) return res.send({
code: 400,
status: false
}).end();
if (req.query.p != config.stormworks.password) return res.send({
code: 401,
status: false
}).end();
if (req.query.reason == "") req.query.reason = "No Reason Provided"
db.query('INSERT INTO bans (steam_id, username, reason, moderator, banned_from) VALUES (?,?,?,?,?)', [req.query.steam_id, req.query.username, req.query.reason, req.query.moderator, req.query.banned_from]).then(response => {
response.status = true;
response.query = req.query
res.send(response).end();
console.log(response);
}).catch((err) => {
err.status = false;
err.query = req.query
if (err) {
console.log(err);
return res.send(err).end()
}
})
})
});