forked from songotony/FileWSServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
215 lines (199 loc) · 6.51 KB
/
server.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
var server = require('http').createServer();
var ws = require('ws');
var path = require('path');
var rimraf = require('rimraf');
var file = require('./lib/file');
var chat = require('./lib/chat');
var auth = require('./lib/auth');
var pr = require('./lib/pullrequest');
var responses = require('./lib/responses');
var exceptions = require('./lib/exceptions');
var config = require('./lib/config');
var console = require('./lib/logger');
var wss = new ws.Server({server : server});
wss.on('connection', function(socket) {
console.log("New client connected");
socket.token = "";
socket.file = {
username : null,
project : {
name : null,
pullrequest : []
},
contentList : {}
};
socket.chat = {
username : null,
room : null
};
socket.pr = {
username : null,
actual : {
title : null,
description : null,
date : null,
dir : null,
files : []
}
};
socket.on('message', function(data, flags) {
try {
try {
var event = JSON.parse(data);
} catch (e) {
throw new exceptions.ParametersException(e.message);
}
if (event.type == undefined || event.type == "")
throw new exceptions.ParametersException("Missing type");
if (event.type == "message" || event.type == "close")
throw new exceptions.ParametersException("Wrong type");
if (event.type != "authenticate" && event.type != "file" && event.type != "chat" && event.type != "pullrequest")
throw new exceptions.ParametersException("Undefined type");
this.emit(event.type, event);
}
catch (e) {
if (e.response != undefined && socket.readyState === ws.OPEN) {
socket.send(JSON.stringify(new e.response(e.message)));
console.warn(e);
}
else
console.error(e);
}
});
socket.on('authenticate', function(result) {
try {
if (result.data == undefined || result.data.token == undefined)
throw new exceptions.ParametersException("Missing token to authenticate");
socket.token = result.data.token;
var decoded = auth.verify(socket, config.key);
if (result.data.file != undefined) {
file.auth(socket, result.data.file);
if (socket.file.username != decoded.streamername || socket.file.project.name != decoded.streamname)
throw new exceptions.AuthException("Your token doesn't match your provided values");
}
if (result.data.chat != undefined) {
chat.auth(socket, result.data.chat);
if (socket.chat.username != decoded.username || socket.chat.room != decoded.room)
throw new exceptions.AuthException("Your token doesn't match your provided values");
}
if (result.data.pullrequest != undefined) {
pr.auth(socket, result.data.pullrequest, wss);
if (socket.pr.username != decoded.username || socket.file.username != decoded.streamername || socket.file.project.name != decoded.streamname)
throw new exceptions.AuthException("Your token doesn't match your provided values");
}
if (socket.readyState == ws.OPEN)
socket.send(JSON.stringify(new responses.OK("authentication", {message : "OK", opts : { sep : path.sep }})));
console.log("Client " + (socket.file.username == null ? socket.chat.username == null ? socket.pr.username : socket.chat.username : socket.file.username) + " authentified");
}
catch (e) {
socket.token = "";
if (e.response != undefined && socket.readyState === ws.OPEN) {
socket.send(JSON.stringify(new e.response(e.message)));
console.warn(e);
}
else
console.error(e);
}
});
socket.on('file', function(result) {
try {
if (result.subtype == undefined || result.subtype == "")
throw new exceptions.ParametersException('Missing file subtype');
var i = 0;
for (; i < file.funcs.length; i++)
if (file.funcs[i].subtype == result.subtype) {
file.funcs[i].func(socket, result.data, wss);
break;
}
if (i == file.funcs.length)
throw new exceptions.ParametersException('Undefined file subtype');
}
catch (e) {
if (e.response != undefined && socket.readyState === ws.OPEN) {
var response = new e.response(e.message);
if (result.data && result.data.name)
response.file = result.data.name;
socket.send(JSON.stringify(response));
console.warn(e);
}
else
console.error(e);
}
});
socket.on('chat', function(result) {
try {
if (result.subtype == undefined || result.subtype == "")
throw new exceptions.ParametersException('Missing chat subtype');
var i = 0;
for (; i < chat.funcs.length; i++)
if (chat.funcs[i].subtype == result.subtype) {
chat.funcs[i].func(socket, result.data, wss);
break;
}
if (i == chat.funcs.length)
throw new exceptions.ParametersException('Undefined chat subtype');
}
catch (e) {
if (e.response != undefined && socket.readyState === ws.OPEN) {
socket.send(JSON.stringify(new e.response(e.message)));
console.warn(e);
}
else
console.error(e);
}
});
socket.on('pullrequest', function(result) {
try {
if (result.subtype == undefined || result.subtype == "")
throw new exceptions.ParametersException('Missing pull request subtype');
var i = 0;
for (; i < pr.funcs.length; i++)
if (pr.funcs[i].subtype == result.subtype) {
pr.funcs[i].func(socket, result.data, wss);
break;
}
if (i == pr.funcs.length)
throw new exceptions.ParametersException('Undefined pull request subtype');
}
catch (e) {
if (e.response != undefined && socket.readyState === ws.OPEN) {
socket.send(JSON.stringify(new e.response(e.message)));
console.warn(e);
}
else
console.error(e);
}
});
socket.on('close', function(code, reason) {
if (socket.file.username != null && socket.pr.username == null)
rimraf(path.join(config.baseDir, socket.file.username), {disableGlob : true}, function(err) {
if (err)
return console.error(err);
for (let client of server.clients)
if (client !== socket && client.file.username == socket.file.username && client.file.project.name == socket.file.project.name)
client.send(JSON.stringify({type : "streamerDisconnected"}))
console.log("Directory for client " + socket.file.username + " successfully cleaned");
});
console.log("Client disconnection with code " + code + (reason && reason != "" ? " with reason " + reason : ""));
});
});
function main() {
var port = 3005;
if (config.port != undefined && typeof config.port == "number")
port = config.port;
if (process.argv.length > 2) {
try {
if (!isNaN(process.argv[2]))
port = parseInt(process.argv[2]);
} catch (e) {
}
}
try {
server.listen(port);
console.log('Server listening on ' + port + ' !');
}
catch (e) {
console.error(e);
}
}
main();