-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
227 lines (209 loc) · 5.96 KB
/
index.ts
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
import express from 'express';
import cors from 'cors';
import bodyParser from 'body-parser';
import {Category, Challenge, CTF} from './models/index';
import { resolve } from 'node:path';
import { rejects } from 'assert/strict';
/*
* WEBSOCKET
*/
const webSocketsServerPort = 8345;
const webSocketServer = require('websocket').server;
const http = require('http');
// Spinning the http server and the websocket server.
const server = http.createServer();
server.listen(webSocketsServerPort);
const wsServer = new webSocketServer({
httpServer: server
});
// Generates unique ID for every new connection
const getUniqueID = () => {
const s4 = () => Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
return s4() + s4() + '-' + s4();
};
// I'm maintaining all active connections in this object
const clients = {};
// I'm maintaining all active users in this object
interface User {
id: string;
username: string;
}
const users:User[] = [];
const sendMessage = (json) => {
// We are sending the current data to all connected clients
Object.keys(clients).map((client) => {
clients[client].sendUTF(json);
});
}
enum wsTypes {
USER_EVENT = "userevent",
}
interface wsResult{
type: wsTypes;
data: any;
}
wsServer.on('request', function(request) {
var userID = getUniqueID();
console.log((new Date()) + ' Recieved a new connection from origin ' + request.origin + '.');
// You can rewrite this part of the code to accept only the requests from allowed origin
const connection = request.accept(null, request.origin);
clients[userID] = connection;
console.log('connected: ' + userID + ' in ' + Object.getOwnPropertyNames(clients));
connection.on('message', function(message) {
if (message.type === 'utf8') {
const dataFromClient = JSON.parse(message.utf8Data);
const json: wsResult = { type: dataFromClient.type, data: null };
if (dataFromClient.type === wsTypes.USER_EVENT) {
users.push({id: userID, username: dataFromClient.username});
json.data = users;
}
sendMessage(JSON.stringify(json));
}
});
// user disconnected
connection.on('close', function(connection) {
console.log((new Date()) + " Peer " + userID + " disconnected.");
const json = { type: wsTypes.USER_EVENT, data: users };
delete clients[userID];
users.splice(users.findIndex(u => u.id === userID),1);
sendMessage(JSON.stringify(json));
});
});
/*
* EXPRESS
*/
const app = express();
const expressPort = 9999
app.use(cors());
app.options('*', cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.listen(expressPort, () => {
console.log(`Server listening at http://localhost:${expressPort}`);
});
app.route('/ctf')
.get((req, res) => {
const id = parseInt(req.query.id.toString(), 10);
CTF.get(id)
.then(ctf => {
res.send(ctf);
})
.catch(err => {
res.statusCode = 500;
res.send(err);
});
})
.post((req, res) => {
CTF.create(req.body.name, req.body.start, req.body.end, req.body.url).then(_ => res.send(true)).catch(_ => res.send(false));
})
.put((req, res) => {
const ctf: CTF = Object.setPrototypeOf(req.body, CTF.prototype);
ctf.update().then(_ => res.send(true)).catch(err => {
res.statusCode = 500;
res.send(err);
});
})
.delete((req, res) => {
const id = parseInt(req.query.id.toString(), 10);
CTF.delete(id).then(_ => res.send(true)).catch(err => {
res.statusCode = 500;
res.send(err);
});
});
app.get('/ctfs', (req, res) => {
CTF.getAll()
.then(ctfs => {
res.send(ctfs);
})
.catch(err => {
res.statusCode = 500;
res.send(err);
})
});
app.route('/chal')
.get((req, res) => {
const id = parseInt(req.query.id.toString(), 10);
Challenge.get(id)
.then(chal => {
res.send(chal);
})
.catch(err => {
res.statusCode = 500;
res.send(err);
});
})
.post((req, res) => {
Challenge.create(req.body.ctf, req.body.name, req.body.category, req.body.points, req.body.done).then(_ => res.send(true)).catch(_ => res.send(false));
})
.put((req, res) => {
const chal: Challenge = Object.setPrototypeOf(req.body, Challenge.prototype);
chal.update().then(_ => res.send(true)).catch(err => {
res.statusCode = 500;
res.send(err);
});
})
.delete((req, res) => {
const id = parseInt(req.query.id.toString(), 10);
Challenge.delete(id).then(_ => res.send(true)).catch(err => {
res.statusCode = 500;
res.send(err);
});
});
app.get('/chals', (req, res) => {
if(!req.query.ctf)
return res.status(500).send({message: 'no ctf Id provided'})
const ctfId = parseInt(req.query.ctf.toString(), 10);
Challenge.getAll(ctfId)
.then(chals => {
res.send(chals);
})
.catch(err => {
res.statusCode = 500;
res.send(err);
})
})
app.route('/cat')
.get((req, res) => {
const id = parseInt(req.query.id.toString(), 10);
Category.get(id)
.then(cat => {
res.send(cat);
})
.catch(err => {
res.statusCode = 500;
res.send(err);
});
})
.post((req, res) => {
Category.create(req.body.name)
.then((cat: Category) => {
res.send(cat);
});
})
.put((req, res) => {
const cat: Category = Object.setPrototypeOf(req.body, Category.prototype);
cat.update().then(_ => res.send(true)).catch(err => {
res.statusCode = 500;
res.send(err);
});
})
.delete((req, res) => {
const id = parseInt(req.query.id.toString(), 10);
Category.delete(id).then(_ => res.send(true)).catch(err => {
res.statusCode = 500;
res.send(err);
});
});
app.get('/cats', (req, res) => {
if(!req.query.ctf)
return res.status(500).send({message: 'no ctf Id provided'})
const ctfId = parseInt(req.query.ctf.toString(), 10);
Category.getAll(ctfId)
.then(cats => {
res.send(cats);
})
.catch(err => {
res.statusCode = 500;
res.send(err);
})
})