-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirewall-control.js
70 lines (63 loc) · 1.56 KB
/
firewall-control.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
/*
* Firewall control for OpenRefine-WS
* ==================================
*
* Copyright: The Open Data Institute 2013
* License: MIT
*
* This script manipulates a ubuntu firewall via UFW to control who has access to the OpenRefine servers
*
* Note: This must be run as root, which is why it is separate to the OpenRefine-WS server itself!
*
*/
var fs = require('fs')
, util = require('util')
, sys = require('sys')
, exec = require('child_process').exec
;
allowToPort("80","any");
console.log("Firewall control ready");
fs.watchFile('servers.json',
function(ev,file) {
processChange();
}
);
function processChange() {
var servers = loadServerData();
for (var i = 0; i<servers.length; i++) {
server = servers[i];
// Find a port to run a server on
if (server.ip != "" && server.user != "") {
allowToPort(server.port, server.ip);
} else {
denyToPort(server.port);
}
}
}
function allowToPort(port, ip) {
var child = exec("ufw delete deny from any to any port " + port,
function (error, stdout, strerr) {
var child = exec("ufw allow from " + ip + " to any port " + port,
function (error, stdout, strerr) {
}
);
}
);
}
function denyToPort(port) {
var child = exec("ufw deny from any to any port " + port,
function (error, stdout, strerr) {
}
);
}
function loadServerData() {
var data = fs.readFileSync('./servers.json'), servers;
try {
servers = JSON.parse(data);
} catch (err) {
console.log('\n\nCould not read config file for servers\n\n');
console.log(err.message);
return false;
}
return servers;
}