-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
123 lines (111 loc) · 3.12 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
const LIBRARIES = {
Express: require("express"),
BodyParser: require("body-parser"),
SocketIO: require("socket.io"),
Path: require("path"),
HTTP: require("http"),
FS: require("fs"),
LM_TO_NC: require("./LMToNC"),
};
const SETTINGS = JSON.parse(
LIBRARIES.FS.readFileSync(
LIBRARIES.Path.join(__dirname, "settings.json"),
"utf8"
)
);
// We check that the user has filled in the license key.
if (SETTINGS.licenseKey !== "non-commercial-and-evaluation") {
console.log("Your license key is invalid.");
process.exit();
}
// If the application does not have an API key, we generate it.
if (SETTINGS.APIKey == null) {
SETTINGS.APIKey = random(40);
LIBRARIES.FS.writeFileSync(
LIBRARIES.Path.join(__dirname, "settings.json"),
JSON.stringify(SETTINGS, null, 4),
"utf8"
);
}
const EXPRESS = LIBRARIES.Express();
EXPRESS.use(LIBRARIES.BodyParser.urlencoded({ extended: true }));
EXPRESS.use(LIBRARIES.BodyParser.json());
EXPRESS.post("/notification", function requestHandler(req, res) {
const API_KEY = req.headers["api-key"];
if (API_KEY != undefined) {
if (API_KEY == SETTINGS.APIKey) {
const NOTIFICATION = {
icon: {
x: 0,
y: 0,
value: null,
},
message: {
x: 0,
y: 0,
value: null,
},
remaining_time: 5,
sound: null,
};
IO_SERVER.emit("notification", {
...NOTIFICATION,
...req.body,
});
res.end("Notification sended !");
} else {
res.end("Wrong API key.");
}
} else {
res.end(
'An API key is required. You will find it in the "settings.json" file.'
);
}
});
EXPRESS.use(LIBRARIES.Express.static(LIBRARIES.Path.join(__dirname, "public")));
const HTTP = LIBRARIES.HTTP.createServer(EXPRESS);
const IO_SERVER = LIBRARIES.SocketIO(HTTP);
IO_SERVER.on("connection", function (socket) {
IO_SERVER.emit("settings", SETTINGS.client);
sendIconScripts();
// The client wants us to download a missing icon.
socket.on("download_icon", function (_iconName) {
LIBRARIES.LM_TO_NC.download(_iconName, undefined, () => {
sendIconScripts();
});
});
// When the user makes a direct request to the server, it is redirected to it.
socket.on("server", function (_message) {
//IO_SERVER.emit("server", _message);
});
});
HTTP.listen(SETTINGS.serverPort, function () {
console.log(
"You can access the GUI on http://localhost:" + SETTINGS.serverPort + "."
);
});
/**
* This function sends the client the list of available icons.
*/
function sendIconScripts() {
IO_SERVER.emit(
"icons",
LIBRARIES.FS.readdirSync(
LIBRARIES.Path.join(__dirname, "public", "js", "icons")
).filter((file) => file.endsWith(".js"))
);
}
/**
* This function returns a random string of characters.
* @param {*} _length Chain length.
* @returns Random chain created.
*/
function random(_length) {
const CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let back = "";
for (let i = 0; i < _length; i++) {
back += CHARS.charAt(Math.floor(Math.random() * CHARS.length));
}
return back;
}