-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
234 lines (231 loc) · 6.46 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const https = require('https');
const { shell } = require('electron');
const crypto = require('crypto');
const { app, CLIENT, API, reqOption, BrowserWindow, server, PORT, AppElectron, liveUpdate, io } = require("./socket.js");
const fs = require('fs');
const { checkIDToken, requestActions, guestDeny, checkURL } = require("./checkFunctions.js");
if(require('electron-squirrel-startup')) return AppElectron.quit();
app.post('/login', (req, res) => {
const { username, password, remember } = req.body;
if (!checkIDToken(username, res, password))
return;
if (username == 'guest' && Buffer.from(password, 'base64').toString('utf8') == 'guest' && CLIENT == 'DEMO') {
res.status(200).json({ ok: true, message: 'login success', id: "guest", token: 'guest' });
return;
}
//Convert password from base64 to utf8
const password2 = Buffer.from(password, 'base64').toString('utf8');
//convert password to hash
const hash = crypto.createHash('sha256').update(password2).digest('hex');
const req_option = {
hostname: API,
port: 443,
path: '/login',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const req_data = JSON.stringify({
email: username,
password: hash,
remember: remember,
scoreboard: CLIENT
});
//make a request to remote APIs
const req_post = https.request(req_option, (res_post) => {
requestActions(res_post, res, req_post);
});
req_post.end(req_data);
});
app.post('/checkstat', (req, res) => {
const { id, token } = req.body;
if (!checkIDToken(id, res, token)) return;
if (id == 'guest' && token == 'guest' && CLIENT == 'DEMO') {
res.status(200).json({ ok: true, message: 'guest', user: { email: '', name: 'guest', surname: "", isOwner: true, team: "DEMO" } });
return;
} else if (CLIENT !== 'DEMO'&&id == 'guest' && token == 'guest') {
res.status(400).json({ ok: false, message: 'invalid token' });
return;
}
const req_data = JSON.stringify({
id: id,
token: token
});
//make a request to remote APIs
const req_post = https.request(reqOption.checkstat, (res_post) => {
requestActions(res_post, res, req_post);
});
req_post.end(req_data);
});
app.post('/logout', (req, res) => {
const { id, token } = req.body;
if (!checkIDToken(id, res, token)) return;
if (id == 'guest' && token == 'guest' && CLIENT === 'DEMO') {
res.status(200).json({ ok: true, message: 'guest' });
return;
}
const req_option = {
hostname: API,
port: 443,
path: '/logout',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const req_data = JSON.stringify({
id: id,
token: token
});
//make a request to remote APIs
const req_post = https.request(req_option, (res_post) => {
requestActions(res_post, res, req_post);
});
req_post.end(req_data);
});
app.post("/getAuthUsers", (req, res) => {
const { id, token } = req.body;
if (!checkIDToken(id, res, token)) return;
if (id == 'guest' && token == 'guest') {
res.status(200).json({ ok: true, message: 'guest', users: [{ id: '0', name: "guest", surname: "guest", email: "", team: "DEMO"}] });
return;
}
const req_option = {
hostname: API,
port: 443,
path: '/getAuthUsers',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const req_data = JSON.stringify({
id: id,
token: token,
scoreboard: CLIENT
});
//make a request to remote APIs
const req_post = https.request(req_option, (res_post) => {
requestActions(res_post, res, req_post);
});
req_post.end(req_data);
});
app.post("/addAuthUser", (req, res) => {
const { id, token, email } = req.body;
if (!checkIDToken(id, res, token)) return;
if (!email) {
res.status(400).json({ ok: false, message: 'missing email' });
return;
}
if(!guestDeny(id, token, res)) return;
const req_option = {
hostname: API,
port: 443,
path: '/addAuthUser',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const req_data = JSON.stringify({
id: id,
token: token,
email: email,
scoreboard: CLIENT
});
//make a request to remote APIs
const req_post = https.request(req_option, (res_post) => {
requestActions(res_post, res, req_post);
});
req_post.end(req_data);
});
app.post("/removeAuthUser", (req, res) => {
const { id, token, user_id } = req.body;
if (!checkIDToken(id, res, token)) return;
if (!user_id) {
res.status(400).json({ ok: false, message: 'missing user_id' });
return;
}
if(!guestDeny(id, token, res)) return;
const req_option = {
hostname: API,
port: 443,
path: '/removeAuthUser',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const req_data = JSON.stringify({
id: id,
token: token,
user_id: user_id,
scoreboard: CLIENT
});
//make a request to remote APIs
const req_post = https.request(req_option, (res_post) => {
requestActions(res_post, res, req_post);
});
req_post.end(req_data);
});
app.post('/openExternal', (req, res) => {
const { url } = req.body;
if (!checkURL(url, res))
return;
// Check that the URL is a pattern we expect (fc-software.it/{anything}) or (github.com/{anything})
if (!url.match(/^https?:\/\/(www\.)?fc-software\.it\/.*/) && !url.match(/^https?:\/\/(www\.)?github\.com\/.*/)) {
res.status(400).json({ok:false,message:'invalid url'});
return;
}
// convert the url to a redirect free url (remove the redirect query param)
const redirectFreeUrl = url.replace(/redirect=1&?/,'');
shell.openExternal(redirectFreeUrl);
res.status(200).json({ok:true,message:'ok'});
});
app.post('/newWindow', (req, res) => {
const { url, width, height } = req.body;
if (!checkURL(url, res))
return;
const win = new BrowserWindow({
width: width,
height: height,
useContentSize: true,
webPreferences: {
nodeIntegration: true
}
});
win.removeMenu();
win.loadURL(url);
res.status(200).json({ ok: true, message: 'ok' });
});
app.get("/README.md", (req, res) => {
res.sendFile(__dirname + '/README.md');
});
app.get("/client", (req, res) => {
res.send(CLIENT);
});
server.listen(PORT, '0.0.0.0', () => {
console.log('listening on http://localhost:' + PORT);
fs.readFile(__dirname + '/app/json/settings.json', (err, data) => {
if (err) {
console.log(err);
return;
}
const settings = JSON.parse(data);
if(settings.fibsStreaming&&settings.fibsStreamingCode != '')
liveUpdate(io);
});
});
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600
});
win.loadURL(`http://localhost:${PORT}`);
// Close all windows when the main window is closed
win.on('closed', () => {
AppElectron.quit();
});
};
AppElectron.whenReady().then(createWindow);