forked from puux/iptables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
69 lines (59 loc) · 1.83 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
var http = require('http');
var handle = require('./handlers');
var url = require("url");
var fs = require("fs");
var handles = {};
handles["/"] = handle.index;
handles["/channel"] = handle.showChannel;
handles["/delete"] = handle.deleteRule;
handles["/insert"] = handle.insertRule;
handles["/mon"] = handle.monitor;
handles["/save"] = handle.save;
handles["/load"] = handle.load;
handles["/settings"] = handle.settings;
handles["/chainlist"] = handle.chainList;
handles["/login"] = handle.authMe;
handles["/logout"] = handle.logout;
handles["/users"] = handle.userList;
http.createServer(function handler(req, res) {
var pathname = url.parse(req.url).pathname;
req.setEncoding("utf8");
if (handles[pathname]) {
if(handle.isAuth(req)) {
handles[pathname](req, res);
}
else {
handle.authMe(req, res);
}
}
else {
var file = "./tpl" + pathname;
fs.exists(file, function(ex) {
if(ex) {
fs.readFile(file, [], function(err, data) {
//res.writeHead(320, {"Content-Type": "text/plain"});
res.end(data);
});
}
else {
console.log("No request handler found for " + pathname);
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not found");
res.end();
}
});
}
}).listen(1337);
console.log('Server running at http://*:1337/');
// ------------------ WebSocket ------------------------------------------------
var proc = require('child_process');
var ws = require("nodejs-websocket");
var server = ws.createServer(function (conn) {
var log = proc.spawn("tail", ["-f", "/var/log/syslog"]);
log.stdout.on('data', function (data) {
conn.sendText(JSON.stringify(data.toString().split("\n")));
});
conn.on("close", function (code, reason) {
log.kill('SIGHUP');
});
}).listen(8001);