Skip to content

Commit

Permalink
Clean up, upgraded bitbridge4
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrchen committed Oct 11, 2016
1 parent 556d822 commit 1e3fe46
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 31 deletions.
Binary file modified bin/real/bitbridge4
Binary file not shown.
8 changes: 4 additions & 4 deletions controllers/netbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class netBot extends ControllerBot {
} else if (o.macVendor != null) {
name = "(?)" + o.macVendor + " (" + o.ipv4Addr + ")";
}
this.tx(this.primarygid, "New host found in network: " + name, "Found new host " + name);
this.tx2(this.primarygid, "New host found in network: " + name, "Found new host " + name, {uid:o.ipv4Addr});
}
});
this.subscriber.subscribe("MonitorEvent", "Monitor:Flow:Out", null, (channel, type, ip, msg) => {
Expand All @@ -254,7 +254,7 @@ class netBot extends ControllerBot {
}
}
if (m)
this.tx(this.primarygid, m, n);
this.tx2(this.primarygid, m, n, {id:msg.id});
});
this.subscriber.subscribe("MonitorEvent", "Monitor:Flow:In", null, (channel, type, ip, msg) => {
let m = null;
Expand All @@ -271,7 +271,7 @@ class netBot extends ControllerBot {
}
}
if (m)
this.tx(this.primarygid, m, n);
this.tx2(this.primarygid, m, n, {id:msg.id});
});

setTimeout(() => {
Expand Down Expand Up @@ -338,7 +338,7 @@ class netBot extends ControllerBot {

console.log("Sending Msg:", msg);

this.txQ(this.primarygid, msg, msg);
this.txQ2(this.primarygid, msg, msg, {uid: obj.id});
});
}
if (callback)
Expand Down
22 changes: 22 additions & 0 deletions encipher/lib/encipherio.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,28 @@ var legoEptCloud = class {
});
}

sendTextToGroup2(gid, _msg, beepmsg, beepdata,from, callback) {
let msg = {
msg: _msg,
type: 'msg',
from: from
};
let beep = null;
if (beepmsg != null) {
beep = {
cmd: 'apn',
msg: beepmsg,
data: beepdata
};
}
this.sendMsgToGroup(gid, msg, beep, "msg", null, null, (e, r) => {
console.log("sending logs ", e, r);
if (callback) {
callback(e);
}
});
}

sendDataToGroup(gid, _msg, obj, type, from, beepmsg, whisper, callback) {
let msg = {
msg: _msg,
Expand Down
22 changes: 21 additions & 1 deletion lib/ControllerBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ devinfo eph:devinfo:D9uJgjY_qnYpi7_Had6lAA { gid: '6a755fde-f2a9-4070-90b1-376f4
this._txQueue.push(msg);
this._txQ();
}
txQ2(gid, m, beepmsg,beepdata) {
let msg = JSON.stringify({
gid: gid,
m: m,
beepmsg: beepmsg,
beepdata: beepdata,
});
if (this._txQueue.indexOf(msg) > -1 && this.compress == true) {
return;
}
this._txQueue.push(msg);
this._txQ();
}

_txQ() {
if (this._txQueueTxing == true) {
Expand All @@ -296,7 +309,7 @@ devinfo eph:devinfo:D9uJgjY_qnYpi7_Had6lAA { gid: '6a755fde-f2a9-4070-90b1-376f4
if (p != null) {
var name = this.groupsdb[p.gid].me.displayName;
setTimeout(() => {
this.eptcloud.sendTextToGroup(p.gid, p.m, p.beepmsg, name, (e, r) => {
this.eptcloud.sendTextToGroup2(p.gid, p.m, p.beepmsg,p.beepdata, name, (e, r) => {
this._txQueueTxing = false;
this.lastmsg = p;
this._txQ();
Expand All @@ -314,4 +327,11 @@ devinfo eph:devinfo:D9uJgjY_qnYpi7_Had6lAA { gid: '6a755fde-f2a9-4070-90b1-376f4
console.log("sending text", e, r);
});
}

tx2(gid, m, beepmsg,beepdata) {
var name = this.groupsdb[gid].me.displayName;
this.eptcloud.sendTextToGroup2(gid, m, beepmsg, beepdata, name, (e, r) => {
console.log("sending text", e, r);
});
}
};
37 changes: 24 additions & 13 deletions monitor/FlowMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var sysManager = new SysManager('info');
var FlowManager = require('../net2/FlowManager.js');
var flowManager = new FlowManager('info');

var uuid = require('uuid');

rclient.on("error", function (err) {
console.log("Redis(alarm) Error " + err);
});
Expand Down Expand Up @@ -134,7 +136,8 @@ module.exports = class FlowMonitor {
alarmManager.alarm(flow.sh, c, 'info', '0', {"msg":msg}, actionobj, (err,obj,action)=> {
if (obj != null) {
this.publisher.publish("DiscoveryEvent", "Notice:Detected", flow.sh, {
msg:msg
msg:msg,
obj:obj
});
}
});
Expand All @@ -159,7 +162,8 @@ module.exports = class FlowMonitor {
alarmManager.alarm(flow.sh,c, 'info', '0', {"msg":msg}, actionobj, (err,obj,action)=> {
if (obj!=null) {
this.publisher.publish("DiscoveryEvent", "Notice:Detected", flow.sh, {
msg:msg
msg:msg,
obj:obj
});
}
});
Expand All @@ -172,6 +176,7 @@ module.exports = class FlowMonitor {
let intelobj = null;
if (flow.fd == "in") {
intelobj = {
uid: uuid.v4(),
ts: flow.ts,
"id.orig_h": flow.sh,
"id.resp_h": flow.dh,
Expand All @@ -184,6 +189,7 @@ module.exports = class FlowMonitor {
}
} else {
intelobj = {
uid: uuid.v4(),
ts: flow.ts,
shname: flow["shname"],
dhname: flow["dhname"],
Expand Down Expand Up @@ -240,7 +246,8 @@ module.exports = class FlowMonitor {
alarmManager.alarm(flow.sh, c, 'minor', '0', {"msg":msg}, actionobj, (err, obj, action)=>{
if (obj!=null) {
this.publisher.publish("DiscoveryEvent", "Notice:Detected", flow.sh, {
msg:msg
msg:msg,
obj:obj
});
}
});
Expand Down Expand Up @@ -502,10 +509,6 @@ module.exports = class FlowMonitor {
(outSpec.txRatioRanked && outSpec.txRatioRanked.length > 0)) {
this.processSpec("out", outSpec.txRatioRanked, (err, direction, flow) => {
if (flow) {
this.publisher.publish("MonitorEvent", "Monitor:Flow:Out", host.o.ipv4Addr, {
direction: "out",
"txRatioRanked": [flow]
});
let copy = JSON.parse(JSON.stringify(flow));
let msg = "Warning: " + flowManager.toStringShortShort2(flow, 'out', 'txdata');
copy.msg = msg;
Expand All @@ -519,7 +522,13 @@ module.exports = class FlowMonitor {
//infourl:
msg: msg
}
alarmManager.alarm(host.o.ipv4Addr, "outflow", 'major', '50', copy, actionobj);
alarmManager.alarm(host.o.ipv4Addr, "outflow", 'major', '50', copy, actionobj,(err,data)=>{
this.publisher.publish("MonitorEvent", "Monitor:Flow:Out", host.o.ipv4Addr, {
direction: "out",
"txRatioRanked": [flow],
id:data.id,
});
});
}
});
}
Expand All @@ -530,10 +539,6 @@ module.exports = class FlowMonitor {
(inSpec.txRatioRanked && inSpec.txRatioRanked.length > 0)) {
this.processSpec("in", inSpec.txRatioRanked, (err, direction, flow) => {
if (flow) {
this.publisher.publish("MonitorEvent", "Monitor:Flow:Out", host.o.ipv4Addr, {
direction: "in",
"txRatioRanked": [flow]
});
let copy = JSON.parse(JSON.stringify(flow));
let msg = "Warning: " + flowManager.toStringShortShort2(flow, 'in', 'txdata');
copy.msg = msg;
Expand All @@ -545,7 +550,13 @@ module.exports = class FlowMonitor {
target: flow.lh,
msg: msg
}
alarmManager.alarm(host.o.ipv4Addr, "inflow", 'major', '50', copy, actionobj);
alarmManager.alarm(host.o.ipv4Addr, "inflow", 'major', '50', copy, actionobj,(err,data)=>{
this.publisher.publish("MonitorEvent", "Monitor:Flow:Out", host.o.ipv4Addr, {
direction: "in",
"txRatioRanked": [flow],
id:data.id,
});
});
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion net2/AlarmManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ module.exports = class {

alarm(hip, alarmtype, alarmseverity, severityscore, obj, actionobj, callback) {
let key = "alarm:ip4:" + hip;
obj['id'] = uuid.v4();
if (obj.uid!=null) {
obj['id'] = obj.uid;
} else {
obj['id'] = uuid.v4();
}
obj['alarmtype'] = alarmtype;
obj['alarmseverity'] = alarmseverity;
obj['severityscore'] = severityscore;
Expand Down
11 changes: 7 additions & 4 deletions net2/Discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,9 @@ module.exports = class {
actions: ["hblock","ignore"],
target: host.ipv4Addr,
}
alarmManager.alarm(host.ipv4Addr, "newhost", "info", "0", d, actionobj, null);
this.publisher.publish("DiscoveryEvent", "Host:Found", "0", host);
alarmManager.alarm(host.ipv4Addr, "newhost", "info", "0", d, actionobj, (err,alarm)=>{
this.publisher.publish("DiscoveryEvent", "Host:Found", "0", alarm);
});
}
});
}
Expand Down Expand Up @@ -452,6 +453,7 @@ module.exports = class {
let changeset = this.mergeHosts(data, host);
changeset['lastActiveTimestamp'] = Date.now() / 1000;
changeset['firstFoundTimestamp'] = data.firstFoundTimestamp;
changeset['mac'] = host.mac;
log.info("Discovery:Nmap:Redis:Merge", key, changeset, {});
rclient.hmset(key, changeset, (err, result) => {
if (err) {
Expand Down Expand Up @@ -527,8 +529,9 @@ module.exports = class {
target: data.ipv4Addr,
mac: data.mac,
}
alarmManager.alarm(data.ipv4Addr, "newhost", 'info', '0', d, actionobj, null);
this.publisher.publish("DiscoveryEvent", "Host:Found", "0", data);
alarmManager.alarm(data.ipv4Addr, "newhost", 'info', '0', d, actionobj, (err,alarm) => {
this.publisher.publish("DiscoveryEvent", "Host:Found", "0", alarm);
});
}
});
} else {
Expand Down
56 changes: 48 additions & 8 deletions net2/HostManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ var MobileDetect = require('mobile-detect');


class Host {
constructor(obj, callback) {
constructor(obj,mgr, callback) {
this.callbacks = {};
this.o = obj;
this.mgr = mgr;
if (this.o.ipv4) {
this.o.ipv4Addr = this.o.ipv4;
}
Expand Down Expand Up @@ -510,6 +511,10 @@ class Host {
});
});
} else if (type == "HostPolicy:Changed" && this.type == "server") {
this.applyPolicy((err)=>{
});
log.info("HostPolicy:Changed", channel, ip, type, obj);
/*
this.loadPolicy((err, data) => {
log.debug("HostPolicy:Changed", JSON.stringify(this.policy));
policyManager.execute(this, this.o.ipv4Addr, this.policy, (err) => {
Expand All @@ -522,11 +527,31 @@ class Host {
});
});
});
log.info("HostPolicy:Changed", channel, ip, type, obj);
*/
}
});
}

applyPolicy(callback) {
this.loadPolicy((err, data) => {
log.debug("HostPolicy:Changed", JSON.stringify(this.policy));
let policy = JSON.parse(JSON.stringify(this.policy));
// check for global
if (this.mgr.policy.monitor != null && this.mgr.policy.monitor == false) {
policy.monitor = false;
}
policyManager.execute(this, this.o.ipv4Addr, policy, (err) => {
dnsManager.queryAcl(this.policy.acl,(err,acls)=> {
policyManager.executeAcl(this, this.o.ipv4Addr, acls, (err, changed) => {
if (err == null && changed == true) {
this.savePolicy(callback);
}
});
});
});
});
}

// type:
// { 'human': 0-100
// 'type': 'Phone','desktop','server','thing'
Expand Down Expand Up @@ -1146,7 +1171,7 @@ module.exports = class {
return;
}
if (err == null && o != null) {
host = new Host(o);
host = new Host(o,this);
host.type = this.type;
//this.hosts.all.push(host);
this.hostsdb['host:ip4:' + o.ipv4Addr] = host;
Expand Down Expand Up @@ -1240,22 +1265,33 @@ module.exports = class {
let hostbyip = this.hostsdb["host:ip4:" + o.ipv4Addr];

if (hostbymac == null) {
hostbymac = new Host(o);
hostbymac = new Host(o,this);
hostbymac.type = this.type;
this.hosts.all.push(hostbymac);
this.hostsdb['host:ip4:' + o.ipv4Addr] = hostbymac;
this.hostsdb['host:mac:' + o.mac] = hostbymac;
} else {
hostbymac.update(o);
}
// two mac have the same IP, pick the latest, until the otherone update itself
/*
if (hostbyip != null) {
if (hostbyip.mac != o.mac) {
hostbyip.mac = o.mac;
if (hostbyip != null && hostbyip.o.mac != hostbymac.o.mac) {
log.info("HOSTMANAGER:DOUBLEMAPPING", hostbyip.o, hostbymac.o);
if (hostbymac.o.lastActiveTimestamp > hostbyip.o.lastActiveTimestamp) {
this.hostsdb['host:ip4:' + o.ipv4Addr] = hostbymac;
}
// need to take care of ip address change by checking mac
}
*/
this.syncHost(hostbymac, true, (err) => {
if (this.type == "server") {
hostbymac.applyPolicy((err)=>{
cb();
});
} else {
cb();
}
});
/*
hostbymac.loadPolicy((err, policy) => {
this.syncHost(hostbymac, true, (err) => {
if (this.type == "server") {
Expand All @@ -1272,6 +1308,7 @@ module.exports = class {
cb();
});
});
*/
} else {
cb();
}
Expand Down Expand Up @@ -1413,6 +1450,9 @@ module.exports = class {
if (changed == true && err == null) {
this.savePolicy(null);
}
for (let i in this.hosts.all) {
this.hosts.all[i].applyPolicy();
}
});
});
});
Expand Down
1 change: 1 addition & 0 deletions vpn/VpnManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports = class {
this.upnpClient.portUnmapping(opts,(err)=>{
this.upnpClient.close();
this.upnpClient = null;
this.portmapped = false;
if (callback) {
callback(err);
}
Expand Down

0 comments on commit 1e3fe46

Please sign in to comment.