forked from lawrenceamer/0xsp-Mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
187 lines (167 loc) · 5.03 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
const path = require('path');
const serveStatic = require('serve-static');
const express = require('express');
const server = express();
const bodyParser = require('body-parser');
const sqlite3 = require('sqlite3').verbose();
const jwt = require('jsonwebtoken');
const https = require('https');
const http = require('http');
const cors = require('cors');
const dotenv = require("dotenv");
const fs = require('fs');
dotenv.config();
let db = new sqlite3.Database('./db/0xsp.db');
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: false }));
server.use(cors());
server.use(express.static(__dirname + '/public'));
server.use("/uploads", express.static(__dirname + "/uploads"));
server.use("/plugins", express.static(__dirname + "/plugins"));
server.use("/release", express.static(__dirname + "/release"));
db.once('open', function () {
console.log('DB is ready for work!');
});
server.get('/api/postReq', function (req, res) {
res.send("GET Method is not Supported.");
});
server.post('/api/postReq', function (req, res, next) {
let userSql = `SELECT * FROM users WHERE username = ? AND password = ?`;
var i;
db.all(userSql, [req.body.username, req.body.password], (err, rows) => {
if (err) {
next(err);
return;
}
if (!rows) {
res.status(400);
return
}
rows.forEach((row) => {
if (row.username === req.body.username && row.password === req.body.password) {
i = 1;
}
else {
i = 2;
db.close();
}
})
if (i === 1) {
var request = {
output: req.body.output,
owner: 'admin',
category: req.body.category,
host: req.body.host,
created_at: new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''),
sys: req.body.sys,
random_val: req.body.random_string,
};
var sql = 'INSERT INTO outputs (output_body, output_owner, output_category,host,created_at,sys,random_val) VALUES (?,?,?,?,?,?,?)'
db.serialize(function () {
var stmt = db.prepare(sql);
stmt.run(request.output, request.owner, request.category, request.host, request.created_at, request.sys, request.random_val);
stmt.finalize();
});
res.json({
message: "success",
});
}
else {
res.status(404).json({ "error": "please check username & password and try again!" });
}
})
});
server.post('/api/cmd_commands', function (req, res, next) {
let userSql = `SELECT * FROM users WHERE username = ? AND password = ?`;
var i;
db.all(userSql, [req.body.username, req.body.password], (err, rows) => {
if (err) {
next(err);
return;
}
if (!rows) {
res.status(400);
return
}
rows.forEach((row) => {
if (row.username === req.body.username && row.password === req.body.password) {
i = 1;
}
else {
i = 2;
db.close();
}
})
if (i === 1) {
var request = {
command_output: req.body.command_output,
};
var sql = 'UPDATE commands SET command_output = ?'
db.serialize(function () {
var stmt = db.prepare(sql);
stmt.run(request.command_output);
stmt.finalize();
});
res.json({
message: "success",
});
}
else {
res.status(404).json({ "error": "please check username & password and try again!" });
}
})
});
server.post('/auth', function (req, res) {
let userSql = `SELECT * FROM users WHERE username = ? AND password = ?`;
var i;
db.all(userSql, [req.body.username, req.body.password], function (err, row) {
if (err) return res.status(500).send('Server error!');
row.forEach((user) => {
if (user.username === req.body.username && user.password === req.body.password) {
i = 1;
}
else {
i = 2;
db.close();
}
})
if (i === 1) {
// Generate an access token
const accessToken = jwt.sign({ username: "admin" }, process.env.TOKEN_SECRET, { expiresIn: '24h' });
res.json({
user: "admin",
accessToken: accessToken
});
} else {
res.json({ msg: "username or password incorrect!" })
}
});
});
server.get('/api/getcommands', function (req, res, next) {
let sql = "SELECT * FROM commands WHERE id = 1";
db.get(sql, (err, data) => {
if (err) {
throw err;
}
res.send(data.command);
});
});
let dashboard = require('./routes/dashboard');
let upload = require('./routes/upload');
server.use('/mydashboard', dashboard);
server.use('/upload', upload);
server.get('*', (req, res) => res.sendFile(__dirname + '/public/index.html'));
var args = process.argv;
if(args.slice(2) == 'useSSL'){
https.createServer({
cert: fs.readFileSync(process.env.CERT_PATH),
ca: fs.readFileSync(process.env.CHAIN_PATH),
key: fs.readFileSync(process.env.KEY_PATH)
},server).listen(4000,'0.0.0.0', function () {
console.log('0xsp Started on port 4000');
});
}else{
http.createServer(server).listen(4000,'0.0.0.0', function () {
console.log('0xsp Started on port 4000');
});
}