-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
|
||
var PzpClient = function () { | ||
"use strict"; | ||
var PzpCommon = require("./pzp.js"); | ||
var logger = PzpCommon.wUtil.webinosLogging (__filename) || console; | ||
/** | ||
* | ||
*/ | ||
function pzpClient_PeerCleanup() { | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
var existsSync = fs.existsSync || path.existsSync; | ||
logger.log("Clean up SiB leftovers"); | ||
var own = path.join(parent.config.metaData.webinosRoot, "keys", "conn.pem"); | ||
var other = path.join(parent.config.metaData.webinosRoot, "keys", "otherconn.pem"); | ||
var exlist = path.join(parent.config.metaData.webinosRoot, "exCertList.json"); | ||
if(existsSync(own)) { | ||
fs.unlink(own, function(err){ | ||
if(err) throw err; | ||
logger.log("removed" + own); | ||
}); | ||
} | ||
if(existsSync(other)) { | ||
fs.unlink(other, function(err){ | ||
if(err) throw err; | ||
logger.log("removed" + other); | ||
}); | ||
} | ||
if(existsSync(exlist)) { | ||
fs.unlink(exlist, function(err){ | ||
if(err) throw err; | ||
logger.log("removed - " + exlist); | ||
}); | ||
} | ||
parent.pzp_state.connectingPeerAddr = ""; | ||
} | ||
/** | ||
* Connect Peer PZPs. This is either triggered by PZH sending PZPUpdate message or else from PZP local discovery | ||
* @param msg - msg is an object containing port, address and name of PZP to be connected | ||
*/ | ||
this.connectPeer = function (msg) { | ||
parent.setConnectionParameters(function (options) { | ||
var name = msg.name, n, client; | ||
if(name && (n = name.indexOf("/"))) options.servername = name.substring(0, n); | ||
client = require("tls").connect(parent.getPorts().pzp_tlsServer, msg.address, options, function () { | ||
if (client.authorized) { | ||
parent.handlePeerAuthorization(msg.name, client); | ||
pzpClient_PeerCleanup(); | ||
} else { | ||
logger.error("pzp client - connection failed, " + client.authorizationError); | ||
} | ||
}); | ||
|
||
client.on("data", function (buffer) { | ||
parent.handleMsg(client, buffer); | ||
}); | ||
|
||
client.on("end", function () { | ||
parent.cleanUp(client.id); | ||
}); | ||
|
||
client.on("error", function (err) { | ||
logger.error(err.message); | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = PzpClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
|
||
/** | ||
* Connects with PZH and handle respective events | ||
*/ | ||
var PzpOtherManager = require("./pzp_otherManager.js"); | ||
var PzpCommon = require("./pzp.js"); | ||
|
||
var PzpConnectHub = function () { | ||
"use strict"; | ||
PzpOtherManager.call(this); | ||
var self = this; | ||
var logger = PzpCommon.wutil.webinosLogging (__filename) || console; | ||
|
||
/** | ||
* If PZP fails to connect to PZH, this tries to connect back to PZH | ||
*/ | ||
function retryConnecting () { | ||
if (self.getEnrolledStatus()) { | ||
setTimeout (function () { | ||
self.connect (function (status) { | ||
logger.log ("retrying to connect back to the PZH " + (status ? "successful" : "failed")); | ||
}); | ||
}, 60000);//increase time limit to suggest when it should retry connecting back to the PZH | ||
} | ||
} | ||
/** | ||
* | ||
* @param callback | ||
*/ | ||
this.connectHub = function (callback) { | ||
var pzpClient; | ||
try { | ||
self.setConnectionParameters(function (certificateConfiguration) { | ||
pzpClient = require("tls").connect(parent.getPorts().provider, | ||
self.getServerAddress(), | ||
certificateConfiguration, function() { | ||
logger.log ("connection to pzh status: " + pzpClient.authorized); | ||
if (pzpClient.authorized) { | ||
authenticated (pzpClient, callback); | ||
} else { | ||
unauthenticated (pzpClient, callback); | ||
} | ||
}); | ||
pzpClient.setTimeout(100); | ||
|
||
pzpClient.on ("data", function (buffer) { | ||
handleMsg(pzpClient, buffer); | ||
}); | ||
|
||
pzpClient.on ("close", function(had_error) { | ||
if(had_error) { | ||
logger.log("transmission error lead to disconnect"); | ||
} | ||
}); | ||
pzpClient.on ("end", function() { | ||
if (pzpClient.id) cleanUp(pzpClient.id); | ||
retryConnecting(); | ||
}); | ||
|
||
pzpClient.on ("error", function(err) { | ||
handlePzpError(err); | ||
}); | ||
}); | ||
} catch (err) { | ||
logger.error ("Connecting Personal Zone Hub Failed : " + err); | ||
} | ||
} | ||
}; | ||
|
||
require("util").inherits(PzpConnectHub, PzpOtherManager); | ||
module.exports = PzpConnectHub; |