Skip to content

Commit

Permalink
interactor: use defined port for push + use custom port for handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Nov 13, 2017
1 parent 34645f1 commit 2b9f9a0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
2 changes: 0 additions & 2 deletions lib/Interactor/Daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ var Daemon = module.exports = {
function doWelcomeQuery(cb) {
HttpRequest.post({
url : self.opts.ROOT_URL,
port : self.opts.ROOT_PORT,
data : {
public_id : self.opts.PUBLIC_KEY,
data : ciphered_data
Expand Down Expand Up @@ -350,7 +349,6 @@ var Daemon = module.exports = {
}
else {
self.opts.ROOT_URL = cst.KEYMETRICS_ROOT_URL;
self.opts.ROOT_PORT = 443;
}

if (Conf.getSync('pm2:passwd'))
Expand Down
39 changes: 27 additions & 12 deletions lib/Interactor/HttpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,46 @@
* can be found in the LICENSE file.
*/

var http = require('http');
var https = require('https');
var debug = require('debug')('interface:http');
var http = require('http');
var https = require('https');
var url = require('url')
var debug = require('debug')('interface:http');

var HttpRequest = module.exports = {};

HttpRequest.post = function(opts, cb) {
if (!(opts.port && opts.data && opts.url))
return cb({msg : 'missing parameters', port : opts.port, data : opts.data, url : opts.url});
if (!(opts.data && opts.url)) {
return cb({
msg: 'missing parameters',
port: opts.port,
data: opts.data,
url: opts.url
})
}

var port = 0;
if (!opts.port) {
var parsed = url.parse(opts.url)
if (parsed.hostname && parsed.port) {
opts.port = parseInt(parsed.port)
opts.url = parsed.hostname
} else {
opts.port = 443
}
}

var options = {
hostname : opts.url,
path : '/api/node/verifyPM2',
method : 'POST',
port : opts.port,
hostname: opts.url,

This comment has been minimized.

Copy link
@Unitech

Unitech Nov 13, 2017

Owner

here

path: '/api/node/verifyPM2',
method: 'POST',
port: opts.port,
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(JSON.stringify(opts.data))
}
};
}

var client = (opts.port == 443) ? https : http;
var client = (opts.port === 443) ? https : http;

var req = client.request(options, function(res){
var dt = '';
Expand Down
42 changes: 21 additions & 21 deletions lib/Interactor/PushInteractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ var LOGS_BUFFER = {};
/**
* Instanciate a new axon connection
*/
function setupConnection(host) {
function setupConnection(host, port) {
var that = this;

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

var client = this.client = axon.socket('pub');
if (port) port = parseInt(port)
if (port === 41624) port = 80

this.host = host;

Expand All @@ -51,8 +53,7 @@ function setupConnection(host) {
client.on('reconnect attempt', function(e) {
console.log('[PUSH] Reconnecting');
});

client.connect(cst.REMOTE_PORT_TCP, host);
client.connect(port || cst.REMOTE_PORT_TCP, host);
};

this.destroy = function() {
Expand All @@ -65,7 +66,7 @@ function setupConnection(host) {
this._setup(this.host);
};

this._setup(host);
this._setup(host, port);

return this;
};
Expand All @@ -75,18 +76,16 @@ var PushInteractor = module.exports = {
* Connect to target host or reconnect if null is passed
* the host param must be formated like (http://HOST:PORT)
*/
connectRemote : function(hostname) {
if (hostname)
hostname = Url.parse(hostname).hostname;
else if (this.socket && this.socket.host)
hostname = this.socket.host;
else
return console.error('NO HOST DEFINED');

if (this.socket)
this.socket.destroy();

this.socket = setupConnection(hostname);
connectRemote: function (hostname, port) {
if (this.socket) this.socket.destroy()
if (hostname) {
var parsed = Url.parse(hostname)
this.socket = setupConnection(parsed.hostname, port || parsed.port)
} else if (this.socket && this.socket.host) {
this.socket = setupConnection(this.socket.host)
} else {
return console.error('NO HOST DEFINED')
}
},
/**
* Start the PushInteractor Singleton
Expand All @@ -105,14 +104,15 @@ var PushInteractor = module.exports = {
this.send_buffer = [];
this._reconnect_counter = 0;

this.port = null
if (process.env.PM2_DEBUG)
cst.REMOTE_PORT_TCP = 3900;
this.port = 3900;
if (process.env.NODE_ENV == 'local_test')
cst.REMOTE_PORT_TCP = 8080;
this.port = 8080;

this.resetPacket();

this.connectRemote(p.url);
this.connectRemote(p.url, this.port);

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

0 comments on commit 2b9f9a0

Please sign in to comment.