-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
150 lines (128 loc) · 4.32 KB
/
app.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
global.__base = __dirname + '/';
const
use_https = true,
argv = require('minimist')(process.argv.slice(2)),
https = require('https'),
fs = require('fs'),
app = require('express')(),
_ = require('lodash'),
parser = require('xmldom').DOMParser,
XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest,
sendPostRequest = require('request').post;
const researchers = ['A4SSYO0HDVD4E', 'A1BOIDKD33QSDK', 'A1MMCS8S8CTWKU','A1MMCS8S8CTWKV','A1MMCS8S8CTWKS', 'A1KXXBD1M6NBK5'];
const blockResearcher = false;
let gameport;
if(argv.gameport) {
gameport = argv.gameport;
console.log('using port ' + gameport);
} else {
gameport = 8887;
console.log('no gameport specified: using 8887\nUse the --gameport flag to change');
}
//let server;
//let io;
try {
var pathToCerts = '/etc/letsencrypt/live/cogtoolslab.org/';
var privateKey = fs.readFileSync(pathToCerts + 'privkey.pem'),
certificate = fs.readFileSync(pathToCerts + 'cert.pem'),
options = { key: privateKey, cert: certificate },
server = require('https').createServer(options,app).listen(gameport),
io = require('socket.io')(server);
} catch (err) {
console.log("cannot find SSL certificates; falling back to http");
server = app.listen(gameport),
io = require('socket.io')(server);
}
// serve stuff that the client requests
app.get('/*', (req, res) => {
const id = req.query.workerId;
const isResearcher = _.includes(researchers, id);
if(!id || id === 'undefined' || (isResearcher && !blockResearcher)) {
// Let through if researcher, or in 'testing' mode
serveFile(req, res);
} else if(!valid_id(id)) {
// If invalid id, block them
console.log('invalid id, blocked');
return handleInvalidID(req, res);
} else {
// If the database shows they've already participated, block them.
checkPreviousParticipant(id, (exists) => {
return exists ? handleDuplicate(req, res) : serveFile(req, res);
});
}
});
io.on('connection', function (socket) {
// Recover query string information and set condition
const query = socket.handshake.query;
// Set up callback for writing client data to mongo
socket.on('currentData', function(data) {
console.log('currentData received: ' + JSON.stringify(data));
sendPostRequest(
'http://localhost:6005/db/insert',
{ json: data },
(error, res, body) => {
if (!error && res.statusCode === 200) {
console.log(`sent data to store`);
} else {
console.log(`error sending data to store: ${error} ${body}`);
}
}
);
});
});
const serveFile = function(req, res) {
const fileName = req.params[0];
console.log('\t :: Express :: file requested: ' + fileName);
return res.sendFile(fileName, {root: __dirname});
};
const handleDuplicate = function(req, res) {
console.log("duplicate id: blocking request");
res.sendFile('duplicate.html', {root: __dirname});
return res.redirect('/duplicate.html');
};
const valid_id = function(id) {
return (id.length <= 15 && id.length >= 12) || id.length == 41;
};
const handleInvalidID = function(req, res) {
console.log("invalid id: blocking request");
return res.redirect('/invalid.html');
};
function checkPreviousParticipant (workerId, callback) {
const p = {'wID': workerId};
const postData = {
dbname: 'color-ref',
query: p,
projection: {'_id': 1}
};
sendPostRequest(
'http://localhost:6005/db/exists',
{json: postData},
(error, res, body) => {
try {
if (!error && res.statusCode === 200) {
console.log("success! Received data " + JSON.stringify(body));
callback(body);
} else {
throw `${error}`;
}
}
catch (err) {
console.log(err);
console.log('no database; allowing participant to continue');
return callback(false);
}
}
);
};
function UUID () {
var baseName = (Math.floor(Math.random() * 10) + '' +
Math.floor(Math.random() * 10) + '' +
Math.floor(Math.random() * 10) + '' +
Math.floor(Math.random() * 10));
var template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
var id = baseName + '-' + template.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
return id;
};