-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
218 lines (189 loc) · 5.65 KB
/
Copy pathapp.js
File metadata and controls
218 lines (189 loc) · 5.65 KB
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
const botLogic = require("./misc/botLogic");
//--
const fs = require("fs");
const crypto = require("crypto");
if (!fs.existsSync("./secrets")){
fs.mkdirSync("./secrets");
}
//gets cipher key
let cipherKey=undefined;
try{
cipherKey = Buffer.from(fs.readFileSync("./secrets/.secretkey",{encoding:"base64"}),"base64");
if(cipherKey.length!=32) throw "wrong length";
}catch{
cipherKey=crypto.randomBytes(32);
fs.writeFileSync("./secrets/.secretkey",cipherKey,{encoding:"base64"});
}
//creates ivs (you need them for the crypto stuff)
let oldIv=undefined;
if(fs.existsSync("./secrets/.iv")){
oldIv=fs.readFileSync("./secrets/.iv");
}
let newIv = crypto.randomBytes(16);
let loginsLoaded=[];
let loginsNoId=[];
function decipher(input){
let thisDecipher=crypto.createDecipheriv("aes256", cipherKey, oldIv);
return Buffer.concat([
thisDecipher.update(input),
thisDecipher.final()
]);
}
function encipher(input){
let thisEncipher=crypto.createCipheriv("aes256", cipherKey, newIv)
return Buffer.concat([
thisEncipher.update(input),
thisEncipher.final()
]);
}
//if there are logins stored, read them
if(fs.existsSync("./secrets/logins.json")){
let fileData=fs.readFileSync("./secrets/logins.json",{encoding:"utf8"})||"[]";
loginsLoaded = JSON.parse(fileData);
loginsNoId = JSON.parse(fileData);
for(let i=0;i<loginsLoaded.length;i++){
loginsLoaded[i].token=decipher(Buffer.from(loginsLoaded[i].token,"base64"));
delete loginsNoId.token;
delete loginsNoId.id;
}
}
let loginsToSave=[];
function addLogin(name,id,rawToken,dontWrite){
for(let i=0;i<loginsToSave.length;i++){
if(loginsToSave[i].id==id){
return;
}
}
loginsToSave.push({
name:name,
id:id,
token:encipher(rawToken).toString("base64")
});
if(!dontWrite)
fs.writeFile("./secrets/logins.json",JSON.stringify(loginsToSave,null,2),{encoding:"utf8"},()=>{});
}
for(let i=0;i<loginsLoaded.length;i++){
addLogin(loginsLoaded[i].name,loginsLoaded[i].id,loginsLoaded[i].token,true);
}
fs.writeFileSync("./secrets/.iv",newIv,{encoding:"base64"});
fs.writeFileSync("./secrets/logins.json",JSON.stringify(loginsToSave,null,2),{encoding:"utf8"});
//--
const { Server } = require('ws');
const internalServer = new Server({ port: 3001 });
internalServer.on('connection', (ws) => {
ws.once("message",(event)=>{
switch(event.toString()){
case "frontEnd":
botLogic.supplyFrontEnd(ws);
break;
case "editor":
botLogic.supplyEditor(ws);
break;
}
});
});
//--
const express = require('express');
const app = express();
app.use(express.static('resources'));
const utils = require('./misc/utils');
const AbstractScene = require('./scenes/AbstractScene').scene;
new AbstractScene(
"/",
"Login",
utils.getTextFile("./scenes/pagedata/Login.html"),
utils.getTextFile("./scenes/pagedata/Login.js"),
[["buttons",JSON.stringify(loginsNoId)]]
).register(app);
new AbstractScene(
"/client",
"Client",
utils.getTextFile("./scenes/pagedata/Client.html"),
utils.getTextFile("./scenes/pagedata/Client.js")
).register(app);
new AbstractScene(
"/editor",
"Editor",
utils.getTextFile("./scenes/pagedata/Editor.html"),
utils.getTextFile("./scenes/pagedata/Editor.js")
).register(app);
new AbstractScene(
"/export",
"Export",
utils.getTextFile("./scenes/pagedata/Export.html"),
utils.getTextFile("./scenes/pagedata/Export.js")
).register(app);
app.get("/login",(req,res)=>{
if(req.query.id!==undefined){
req.query.token=loginsLoaded[req.query.id].token.toString();
}
botLogic.initialize(req.query.token)
.then((data)=>{
addLogin(data.name,data.id,data.token);
res.redirect("/client");
},(e)=>{
let errorMessage="Login error: "+e.code;
switch(e.code){
case "TokenInvalid":
errorMessage="Invalid token";
break;
}
res.redirect("/?error="+errorMessage);
});
});
app.listen(3000);
//--
console.log("\n"+utils.getTextFile("./misc/welcomeLog.txt"));
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on("line", async function(input) {
console.log(await commandLineHandler(...input.split(" ")));
});
const open = require('open');
async function commandLineHandler(...input) {
switch (input[0]) {
case "help":
return "" +
"help: this command\n" +
"restart (r): restarts the bot, running the most updated code and saving all data\n" +
"shutdown: shuts the bot down completely, saving all data\n" +
"sass: recompiles sass\n" +
"open (front OR editor): opens the client or the editor, depending on which one you specify";
case "r":
case "restart":
await require('child_process').exec('cmd /c start cmd /c start.bat');
setTimeout(function() {
process.exit()
}, 100);
return "inactive...";
case "shutdown":
setTimeout(function() {
process.exit()
}, 1000);
return "shutting down...";
case "sass":
await require('child_process').exec('cmd /c start cmd /c compile_sass.bat');
return "recompiled";
case "open":
switch(input[1]){
case "front":
open("http://localhost:3000/");
return "opened in browser";
case "editor":
open("http://localhost:3000/editor");
return "opened in browser";
default:
return "type \"open front\" or \"open editor\"";
}
default:
return "not a command. type help to see all commands";
}
return "something happened. oof";
};
process.on('uncaughtException', function(err) {
console.log('Caught exception: ');
console.log(err);
});