-
Notifications
You must be signed in to change notification settings - Fork 20
/
pzp_connectPeer.js
67 lines (63 loc) · 2.44 KB
/
pzp_connectPeer.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
var PzpClient = function () {
"use strict";
var PzpCommon = require("./pzp.js");
var PzpObject = this;
var logger = PzpCommon.wUtil.webinosLogging (__filename) || console;
/**
*
*/
function pzpClient_PeerCleanup() {
var existsSync = PzpCommon.fs.existsSync || PzpCommon.path.existsSync;
logger.log("Clean up SiB leftovers");
var own = path.join(PzpObject.getWebinosPath(), "keys", "conn.pem");
var other = path.join(PzpObject.getWebinosPath(), "keys", "otherconn.pem");
var exlist = path.join(PzpObject.getWebinosPath(), "exCertList.json");
if(existsSync(own)) {
PzpCommon.fs.unlink(own, function(err){
if(err) throw err;
logger.log("removed" + own);
});
}
if(existsSync(other)) {
PzpCommon.fs.unlink(other, function(err){
if(err) throw err;
logger.log("removed" + other);
});
}
if(existsSync(exlist)) {
PzpCommon.fs.unlink(exlist, function(err){
if(err) throw err;
logger.log("removed - " + exlist);
});
}
PzpObject.setConnectingPeerAddr("");
}
/**
* 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) {
PzpObject.setConnectionParameters(function (status, options) {
var name = msg.name, n, client;
if(name && (n = name.indexOf("/"))) options.servername = name.substring(0, n);
client = PzpCommon.tls.connect(PzpObject.getPorts().pzp_tlsServer, msg.address, options, function () {
if (client.authorized) {
PzpObject.handlePeerAuthorization(msg.name, client);
pzpClient_PeerCleanup();
} else {
PzpObject.unAuthentication(client);
}
});
client.on("data", function (buffer) {
PzpObject.handleMsg(client, buffer);
});
client.on("end", function () {
PzpObject.cleanUp(client.id);
});
client.on("error", function (err) {
PzpObject.handleError(err);
});
});
};
};
module.exports = PzpClient;