-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
72 lines (61 loc) · 2.08 KB
/
main.js
File metadata and controls
72 lines (61 loc) · 2.08 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
if (process.getuid() != 0) {
console.log('Must run as root to bind on port 80!');
console.log('We will automatically revert to a lesser powerful being');
console.log('You can change this in the configuration files');
process.exit(1);
}
var docroot = '/'
// Check if we have enough arguments and set the docroot
if (process.argv.length < 3) {
throw 'Usage: sudo node main.js <serverroot>';
process.exit(0);
}
else {
docroot = process.argv[2];
}
// We prepare some modules we'll need
var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
log = require('util').log;
// Our server function
http.createServer(function serverHandler(request, response) {
// Parse the url so we can use it
var uri = url.parse(request.url).pathname;
log('Request for ' + uri);
// This will contain our realpath (Resolve any relative path stuf like ..)
var realpath;
// We try because hte fs.realpathSync might error on non-existing stuff
try {
realpath = fs.realpathSync(docroot + uri);
} catch (e) {
response.writeHead(404);
response.end('404 - Not Found');
}
// Serve files if they're in the docroot
if ((new RegExp('^' + docroot)).exec(realpath))
{
// Read the file and write it to the client if it was found
fs.readFile(realpath, 'binary', function(err, file) {
if (err) {
log(err);
response.writeHead(404);
response.end('404 - Not Found');
return;
}
response.writeHead(200);
response.write(file,'binary');
response.end();
});
}
else {
// The file was outside docroot, just act dumb and tell the client we couldn't find it
response.writeHead(404);
response.end('404 - Not Found');
}
}).listen(80); // Wait so what did you write there? .168? :')
log("Server running at http://localhost:80/");
process.setuid('nobody');
log('Now running as nobody');