-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.js
77 lines (64 loc) · 2.07 KB
/
simulator.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
process.env.DEBUG = "acdp-receiver:*";
var pfsenseUrl;
console.log('working with ' + process.env.NODE_ENV + ' config');
if (process.env.NODE_ENV == 'local') {
pfsenseUrl = 'http://192.168.2.122/';
}
if (process.env.NODE_ENV == 'test1') {
pfsenseUrl = 'http://10.10.80.254/'
}
var pfsense = require('pfsense-fwcontrol');
var acdpReceiver = require('receiver-nodejs');
var async = require('async');
var firewall = require('./firewall');
var simulate = function (options, acdprequest, callback) {
if (options.valid) {
console.log('We received something to respond to in the main program');
// just tell the client that we have received his stuff
var resp = {
"type": "ACDPRESPONSE",
"receiver": {
"description": "acdp-test1-receiver"
}
};
resp.demands = [];
acdprequest.demands.forEach(function (demand) {
var demandId;
if (typeof demand.producer === 'undefined') {
demandId = demand.consumer.id;
} else {
demandId = demand.producer.id;
}
resp.demands.push({'id': demandId, 'state': 'RECEIVED'});
});
callback(null, resp); // send the callback, don't wait for firewall configuration
var rules = [];
async.each(acdprequest.demands, function (demand, callback) {
var tmpRules = firewall.createRuleTemplates(demand, options);
tmpRules.forEach(function (rule) {
rules.push(rule);
});
callback();
}, function (err) {
if (err) {
console.error(err.message);
} else {
firewall.activateRules(rules);
}
});
} else {
var resp = {
"type": "ACDPRESPONSE",
"receiver": {
"description": "acdp-test1-receiver"
},
"request": {
"state": "INVALID"
}
};
callback(null, resp);
}
};
module.exports = {
simulate: simulate
};