-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetFunctions.js
More file actions
327 lines (312 loc) · 11.3 KB
/
Copy pathnetFunctions.js
File metadata and controls
327 lines (312 loc) · 11.3 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
var xmldom = require('xmldom');
var uaparser = require('ua-parser');
var util = require('util');
var dns = require('dns');
exports.getExtras = function(url) {
get = {};
var over = url;
for (var first = 0; first < url.length; first++) {
if (over[first] === '=') {
var key = over.substring(0, first);
over = over.substring(first + 1);
for (and = 0; and < over.length; and++) {
if (over[and] === '&') {
break;
};
};
get[key] = over.substring(0, and);
first -= over.substring(0, and).length;
over = over.substring(and + 1);
};
};
return get;
};
exports.urlType = function(url) {
var special = {
'php' : 'text/html',
'js' : 'text/javascript',
'map' : 'text/javascript',
'py' : 'text/plain ',
'sh' : 'text/plain',
'exe' : 'application/octet-stream',
'iso' : 'application/x-unknown',
'java' : 'text/x-java-source',
'class' : 'application/java-vm',
'jar' : 'application/java-archive',
'zip' : 'application/zip',
'ico' : 'image/x-icon',
'cpp' : 'text/plain',
'c' : 'text/plain'
};
return special[url.substring(url.length - (url.split("").reverse().join("").indexOf(".")))] || ("text/" + url.substring(url.length - (url.split("").reverse().join("").indexOf("."))));
};
exports.sendErrorPage = function(client, errorCode) {
fs.readFile(DIR + "/error/" + errorCode + ".html", function(error, html) {
var options = { 'statusCode': errorCode,
'Content-Type': 'text/html; charset=utf-8',
'Content-Language': 'nl',
'Encoding': 'UTF-8',
'Connection': client.requestHeaders['connection'] === 'keep-alive' ? 'Keep-Alive' : 'Close' };
if (error) {
if (error.code === "ENOENT") {
functions.log("ErrorPage File not found: '" + errorCode + "'.");
} else {
functions.log("Unknown Error while searching for ErrorPage: '" + errorCode + "': " + error.stack);
};
options['Content-Length'] = 0;
html = '';
} else {
options['Content-Length'] = html.length;
}
client.write(functions.buildMetaData(options));
client.write(html);
if (client.requestHeaders['connection'] != "keep-alive") {
client.end();
};
});
}
exports.write = function(str, file) {
if (str === undefined || file === undefined) {
return;
};
fs.readFile(LOGFILEDIR + file, function(error, data) {
if (error) {
console.log("Could not read from '" + LOGFILEDIR + file + + "'. Reason: " + error.stack);
return;
};
fs.writeFile(LOGFILEDIR + file, new Date().toLocaleString() + ": " + str + '\n' + data, function(error) {
error && console.log("Could not write to '" + LOGFILEDIR + file + "'. Reason: " + error.stack);
});
});
};
exports.log = function(str) {
exports.write(str, LOGFILE);
};
exports.error = function(str) {
exports.write(str, ERRORLOGFILE);
};
exports.checkError = function(error, rows, fields) {
process.nextTick(function() {
error && exports.error("Vanaf functions.checkError: " + error);
});
};
exports.getXML = function(error, rows, fields) {
if (error) { functions.log(error.stack); return; };
var doc = new xmldom.DOMParser().parseFromString('<lijst></lijst>');
var xml = doc.documentElement;
var table = doc.createElement('table');
table.setAttribute('nodeValue', fields[0].table);
xml.appendChild(table);
var keys = doc.createElement('keys');
for (var field = 0; field < fields.length; field++) {
var key = doc.createElement('key');
key.setAttribute('nodeValue', fields[field].name);;
keys.appendChild(key);
}
xml.appendChild(keys);
for (var row = 0; row < rows.length; row++) {
var overzicht = doc.createElement('overzicht');
for (field = 0; field < fields.length; field++) {
var value = doc.createElement(fields[field].name);
value.setAttribute('nodeValue' ,rows[row][fields[field].name]);
overzicht.appendChild(value);
}
xml.appendChild(overzicht);
};
return new xmldom.XMLSerializer().serializeToString(xml);
};
exports.loadTable = function(error, rows, fields) {
if (error) {
functions.log(error.stack);
return;
};
var result = "";
rows.reverse();
if (fields[0].table === "beheer_info") {
fields[0].table = "Bezoekers Beheer";
}else if (fields[0].table === "beheer_passed") {
fields[0].table = "Beheer Inlog Gelukt";
}else if (fields[0].table === "beheer_trys") {
fields[0].table = "Beheer Inlog Pogingen";
}else if (fields[0].table === "namen") {
fields[0].table = "Berichten";
}else{
fields[0].table = fields[0].table.replace(fields[0].table[0], fields[0].table.toUpperCase());
};
result += "<table padding='2' border='2'><thead><tr><th colspan=" + fields.length + ">" + fields[0].table + "</th></tr><tr>";
for (var field = 0; field < fields.length; field++) {
if (fields[field].name === "madeit") {
result += "<th>Gelukt</th>";
}else{
result +="<th>" + fields[field].name + "</th>";
};
};
result += "</tr></thead><tbody>";
for (var row = 0; row < rows.length; row++) {
result += "<tr>";
for (field = 0; field < fields.length; field++) {
if (rows[row][fields[field].name] === '1') {
rows[row][fields[field].name] = "Ja";
}else if (rows[row][fields[field].name] === '0') {
rows[row][fields[field].name] = "Nee";
}
result += "<td>" + rows[row][fields[field].name] + "</td>";
};
};
return result +"</tr></tbody></table>";
};
exports.setClient = function(client) {
if (client.remoteAddress.indexOf("0.0.0.0") > -1 || client.remoteAddress.indexOf("127.0.0.1") > -1 || client.remoteAddress.indexOf("192.168.2.") > -1) {
return;
};
client.ipData = client.remoteAddress.substring(7) + ":" + client.remotePort;
dns.reverse(client.remoteAddress.substring(7), function(error, ipData) {
try {
var hostname;
if (error) {
hostname = error.code;
} else {
hostname = ipData[0]
};
var dbUrl = url.replace(/\./g, '$').replace(/\//g, '_').replace(/\-/g, 'Q');
var query = "INSERT INTO " + dbUrl + " (IPv4, Hostname, Browser, Platform) VALUES ('" + client.ipData + "', '" + hostname + "', ";
if (client.requestHeaders['user-agent']) {
var userAgentData = uaparser.parse(client.requestHeaders['user-agent']);
exports.write(util.inspect(userAgentData), USERAGENTFILE);
var osString = 'undefined';
if (userAgentData.os.family != 'Other') {
osString = userAgentData.os.family;
if (userAgentData.os.major == null && userAgentData.os.minor == null) {
} else if (userAgentData.os.minor == null) {
osString += ' ' + userAgentData.os.major;
} else if (userAgentData.os.major != null && userAgentData.os.minor != null) {
osString += ' ' +userAgentData.os.major + '.' + userAgentData.os.minor;
};
if (userAgentData.device.family !== 'Other') {
osString += " on device: " + userAgentData.device.family;
};
};
var uaString = client.requestHeaders['user-agent'];
if (userAgentData.ua.family != 'Other') {
uaString = userAgentData.ua.family;
if (userAgentData.ua.major == null && userAgentData.ua.minor == null) {
} else if (userAgentData.ua.minor == null) {
uaString += ' ' + userAgentData.ua.major;
} else if (userAgentData.ua.major != null && userAgentData.ua.minor != null) {
uaString += ' ' + userAgentData.ua.major + "." + userAgentData.ua.minor;
};
}
query += "'" + uaString + "', '" + osString + "')";
} else {
query += "'undefined', 'undefined')";
};
database.query(query, function(error, deRest) {
if (error) {
if (error.code === 'ER_NO_SUCH_TABLE') {
var dbQuery = "CREATE TABLE " + dbUrl + " (" +
"id INT AUTO_INCREMENT PRIMARY KEY NOT NULL, " +
"IPV4 VARCHAR(50) NOT NULL, " +
"Browser VARCHAR(200) NOT NULL, " +
"Platform VARCHAR(100) NOT NULL, " +
"Hostname VARCHAR(100) NOT NULL, " +
"Datum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)";
database.query(dbQuery, function(error, rows, fields) {
if (error) {
functions.log("Error while creating table with query:\n" + dbQuery + "\n" + error.stack);
return;
};
database.query(query, exports.checkError);
});
console.log("Created table: " + dbUrl);
return;
}else{
functions.log("Unknown error while quering visitors to set client:\n" + query + "\n" + error.stack);
return;
};
};
});
} catch (error) {
functions.error("Caught unknown error in setClient: " + error.stack);
};
});
};
exports.extractHeaders = function(data) {
var requestHeaders = {};
data = data.toLowerCase();
while (true) {
data = data.substring(data.indexOf("\r\n") + 2);
if (data.substring(0, 2) === '\r\n' || data.indexOf('\r\n') < 0) {
break;
};
requestHeaders[data.substring(0, data.indexOf(":"))] = data.indexOf(": ") > -1 ? data.substring(data.indexOf(": ") + 2, data.indexOf("\r\n")) : data.substring(data.indexOf(":") + 1, data.indexOf("\r\n"));
};
return requestHeaders;
};
exports.trimUrl = function(url) {
if (url[url.length - 1] === '?') {
url = url.substring(0, url.length - 2);
};
if (url[url.length - 1] === '/') {
url += 'index.html';
};
if (url.indexOf(".") < 0) {
url += '/index.html';
};
return url;
};
exports.buildMetaData = function(options) {
if (!options) return;
if (!(options instanceof Object)) return;
var keys = Object.keys(options);
var meta = "HTTP/1.1 " + options.statusCode + " " + exports.statusCodeStrings[options.statusCode.toString()] + "\r\n";
keys.forEach(function(key) {
if (key !== 'statusCode') {
meta += key + ': ' + options[key] + "\r\n";
};
});
meta += "Server: " + ServerName + "\r\n";
meta += "Date: " + new Date().toUTCString() + "\r\n\r\n";
return meta;
};
exports.statusCodeStrings = {
'100': 'Continue',
'101': 'Switching Protocols',
'200': 'OK',
'201': 'Created',
'202': 'Accepted',
'203': 'Non-Authorative Information',
'204': 'No Content',
'205': 'Reset Content',
'206': 'Partial Content',
'300': 'Multiple Choices',
'301': 'Moved Permanently',
'302': 'Found',
'303': 'See Other',
'304': 'Not Modified',
'305': 'Use Proxy',
'307': 'Temporary Redirect',
'400': 'Bad Request',
'401': 'Unauthorized',
'402': 'Payment Required',
'403': 'Forbidden',
'404': 'Not Found',
'405': 'Method Not Allowed',
'406': 'Not Acceptable',
'407': 'Proxy Authentication Required',
'408': 'Request Timeout',
'409': 'Conflict',
'410': 'Gone',
'411': 'Length Required',
'412': 'Precondition Failed',
'413': 'Request Entity Too Large',
'414': 'Request-URI Too Long',
'415': 'Unsupported Media Type',
'416': 'Requested Range Not Satisfiable',
'417': 'Expectation Failed',
'500': 'Internal Server Error',
'501': 'Not Implemented',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway Timeout',
'505': 'HTTP Version Not Supported'
};