Skip to content

Commit dc737ef

Browse files
committed
interactor: use defined port for push + use custom port for handshake
1 parent 34645f1 commit dc737ef

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

lib/Interactor/Daemon.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ var Daemon = module.exports = {
246246
function doWelcomeQuery(cb) {
247247
HttpRequest.post({
248248
url : self.opts.ROOT_URL,
249-
port : self.opts.ROOT_PORT,
250249
data : {
251250
public_id : self.opts.PUBLIC_KEY,
252251
data : ciphered_data
@@ -350,7 +349,6 @@ var Daemon = module.exports = {
350349
}
351350
else {
352351
self.opts.ROOT_URL = cst.KEYMETRICS_ROOT_URL;
353-
self.opts.ROOT_PORT = 443;
354352
}
355353

356354
if (Conf.getSync('pm2:passwd'))

lib/Interactor/HttpRequest.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,46 @@
44
* can be found in the LICENSE file.
55
*/
66

7-
var http = require('http');
8-
var https = require('https');
9-
var debug = require('debug')('interface:http');
7+
var http = require('http');
8+
var https = require('https');
9+
var url = require('url')
10+
var debug = require('debug')('interface:http');
1011

1112
var HttpRequest = module.exports = {};
1213

1314
HttpRequest.post = function(opts, cb) {
14-
if (!(opts.port && opts.data && opts.url))
15-
return cb({msg : 'missing parameters', port : opts.port, data : opts.data, url : opts.url});
15+
if (!(opts.data && opts.url)) {
16+
return cb({
17+
msg: 'missing parameters',
18+
port: opts.port,
19+
data: opts.data,
20+
url: opts.url
21+
})
22+
}
1623

17-
var port = 0;
24+
if (!opts.port) {
25+
var parsed = url.parse(opts.url)
26+
if (parsed.hostname && parsed.port) {
27+
opts.port = parseInt(parsed.port)
28+
opts.url = parsed.hostname
29+
} else {
30+
opts.port = 443
31+
}
32+
}
1833

1934
var options = {
20-
hostname : opts.url,
21-
path : '/api/node/verifyPM2',
22-
method : 'POST',
23-
port : opts.port,
35+
hostname: opts.url,
36+
path: '/api/node/verifyPM2',
37+
method: 'POST',
38+
port: opts.port,
2439
rejectUnauthorized: false,
2540
headers: {
2641
'Content-Type': 'application/json',
2742
'Content-Length': Buffer.byteLength(JSON.stringify(opts.data))
2843
}
29-
};
44+
}
3045

31-
var client = (opts.port == 443) ? https : http;
46+
var client = (opts.port === 443) ? https : http;
3247

3348
var req = client.request(options, function(res){
3449
var dt = '';
@@ -38,6 +53,7 @@ HttpRequest.post = function(opts, cb) {
3853
});
3954

4055
res.on('end',function(){
56+
console.log(dt)
4157
try {
4258
cb(null, JSON.parse(dt));
4359
} catch(e) {

lib/Interactor/PushInteractor.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ var LOGS_BUFFER = {};
2626
/**
2727
* Instanciate a new axon connection
2828
*/
29-
function setupConnection(host) {
29+
function setupConnection(host, port) {
3030
var that = this;
3131

32-
this._setup = function(host) {
33-
console.log('[PUSH] Connecting %s:%s', host, cst.REMOTE_PORT_TCP);
32+
this._setup = function(host, port) {
33+
console.log('[PUSH] Connecting %s:%s', host, port || cst.REMOTE_PORT_TCP);
3434

3535
var client = this.client = axon.socket('pub');
36+
if (port) port = parseInt(port)
37+
if (port === 41624) port = 80
3638

3739
this.host = host;
3840

@@ -51,8 +53,7 @@ function setupConnection(host) {
5153
client.on('reconnect attempt', function(e) {
5254
console.log('[PUSH] Reconnecting');
5355
});
54-
55-
client.connect(cst.REMOTE_PORT_TCP, host);
56+
client.connect(port || cst.REMOTE_PORT_TCP, host);
5657
};
5758

5859
this.destroy = function() {
@@ -65,7 +66,7 @@ function setupConnection(host) {
6566
this._setup(this.host);
6667
};
6768

68-
this._setup(host);
69+
this._setup(host, port);
6970

7071
return this;
7172
};
@@ -75,18 +76,16 @@ var PushInteractor = module.exports = {
7576
* Connect to target host or reconnect if null is passed
7677
* the host param must be formated like (http://HOST:PORT)
7778
*/
78-
connectRemote : function(hostname) {
79-
if (hostname)
80-
hostname = Url.parse(hostname).hostname;
81-
else if (this.socket && this.socket.host)
82-
hostname = this.socket.host;
83-
else
84-
return console.error('NO HOST DEFINED');
85-
86-
if (this.socket)
87-
this.socket.destroy();
88-
89-
this.socket = setupConnection(hostname);
79+
connectRemote: function (hostname, port) {
80+
if (this.socket) this.socket.destroy()
81+
if (hostname) {
82+
var parsed = Url.parse(hostname)
83+
this.socket = setupConnection(parsed.hostname, port || parsed.port)
84+
} else if (this.socket && this.socket.host) {
85+
this.socket = setupConnection(this.socket.host)
86+
} else {
87+
return console.error('NO HOST DEFINED')
88+
}
9089
},
9190
/**
9291
* Start the PushInteractor Singleton
@@ -105,14 +104,15 @@ var PushInteractor = module.exports = {
105104
this.send_buffer = [];
106105
this._reconnect_counter = 0;
107106

107+
this.port = null
108108
if (process.env.PM2_DEBUG)
109-
cst.REMOTE_PORT_TCP = 3900;
109+
this.port = 3900;
110110
if (process.env.NODE_ENV == 'local_test')
111-
cst.REMOTE_PORT_TCP = 8080;
111+
this.port = 8080;
112112

113113
this.resetPacket();
114114

115-
this.connectRemote(p.url);
115+
this.connectRemote(p.url, this.port);
116116

117117
this.ipm2.on('ready', function() {
118118
console.log('[PUSH] PM2 interface ready, listening to PM2');

0 commit comments

Comments
 (0)