-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (158 loc) · 4.57 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
const app = require("express")();
var http = require("http").createServer(app);
const favicon = require("express-favicon");
const path = require("path");
const bodyParser = require("body-parser");
var io = require("socket.io")(http);
const { Sequelize, Model, DataTypes } = require("sequelize");
const sequelize = new Sequelize({
dialect: "sqlite",
storage: "database.sqlite",
});
const port = 3000; // default http port is 3000
//Preprocessors
// body parser is enabled
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(bodyParser.json());
// Setup Database
class logs extends Model {}
logs.init(
{
time: DataTypes.TIME,
log: DataTypes.STRING,
},
{ sequelize, modelName: "logs" }
);
class ssids extends Model {}
ssids.init(
{
time: DataTypes.TIME,
ssid: DataTypes.STRING,
},
{ sequelize, modelName: "ssids" }
);
ssids.sync(); // Sync database
logs.sync();
global.database = {
logs: [], // when log is used it stores each log here
ssids: [], // All ssids are stored here
};
// Needed functions
async function log(info) {
/**
* receives a log, stores it to the database, and prints it to the display
*/
let time = Date.now(); // grab the current time
let logText = `[${time}]: ${info}`; // generate log text
const logdata = await logs.create({ // store log data
log: info,
time: time,
});
io.emit("log", logText); // send the log to all clients
// display log to the terminal
console.log(logText);
}
//socket.io
io.on("connection", function(socket) {
/**
* Run when a new web client connects
*/
socket.on("requestLogs", async function(msg) {
/**
* Activated when a client requests a lot
*/
log("Sending previous logs");
let logsArray = [];
const logdata = await logs.findAll({});
if (logdata) {
for (i in logdata) {
logsArray.push(`[${logdata[i].get("time")}] ${logdata[i].get("log")}`);
}
}
socket.emit("init", {
// Sends an init with previous ssids to the new client
logs: logsArray,
});
});
socket.on("requestSSIDs", async function(msg) {
// Sends array of SSIDs to client
let ssidsArray = [];
const ssiddata = await ssids.findAll({});
if (ssiddata) {
for (i in ssiddata) {
ssidsArray.push(
`[${ssiddata[i].get("time")}] ${ssiddata[i].get("ssid")}`
);
}
}
socket.emit("init", {
// Sends an init with previous ssids to the new client
ssids: ssidsArray,
});
});
});
//Setting up each page
app.get("/", (req, res) => {
/**
* When a user connects, they are send the index.html file
*/
res.sendFile(__dirname + "/public/html/index.html");
});
app.get("/logs", async (req, res) => {
res.sendFile(__dirname + "/public/html/logs.html");
});
app.get("*.css", (req, res) => {
/**
* When a css file is requested, /public/css is searched
*/
res.sendFile(__dirname + `/public/css/${req.path.split("/")[1]}`);
console.log(`/public/css/${req.path.split("/")[1]}`);
});
app.get("/scripts/*.js", (req, res) => {
/**
* when a js file is requested, /public/scripts is searched
*/
res.sendFile(__dirname + `/public/scripts/${req.path.split("/")[2]}`);
console.log(`/public/scripts/${req.path.split("/")[2]}`);
});
app.post("/post", async (req, res) => {
/**
* Used when a node finds a new ssid. The post request will contain an ssid in the format like the following:
* {ssid: "SSID"}
* The ssid is stored in the local database, then it is sent to all web clients.
*/
let ssid = req.body.ssid; // Extract ssid
if (await ssids.count({ where: { ssid: ssid } }) === 0) {
// Check if the ssid exists already
//database.ssids.push(output); // Push the ssid to the database
let time = Date.now();
const ssiddata = await ssids.create({
ssid: ssid,
time: time,
});
log(`${ssid} has been found!`); // Log that the ssid is found
io.emit("ssid", `[${time}] ${ssid}`); // send new ssid+time to all clients
} else {
log("SSID %s already exists." % ssid); // If the ssid exists
}
res.send({
ssid: ssid,
});
});
app.use(favicon(__dirname + "/public/images/favicon.jpg")); // Send favicon when requested
app.use((req, res) => {
/**
* Used when requested webpage is not found.
*/
res.status(404); // set page to 404
log(
// Log that a user requested invalid page
`${req.body.host} connected to the server. They requested ${req.path}, but this page was unavailable.`
);
res.send("Error 404"); // Send Error 404 to user
});
http.listen(port, () => console.log(`Running on port ${port}.`));