Start HTTP and WS proxies individually

As geth is starting up, the output is monitored for endpoint info. Once the HTTP or WS endpoints are opened, the corresponding proxies are started.

This is more maintainable in the long run in case the geth process being started does not allow for rpc or websockets, or geth modifies the order in which endpoint are opened.
This commit is contained in:
emizzle 2018-09-20 15:08:02 +10:00
parent eba9ce361e
commit c477445896
1 changed files with 16 additions and 11 deletions

View File

@ -18,6 +18,7 @@ var Blockchain = function(options) {
this.isDev = options.isDev;
this.onReadyCallback = options.onReadyCallback || (() => {});
this.onExitCallback = options.onExitCallback;
this.proxyIpc = null;
if ((this.blockchainConfig === {} || JSON.stringify(this.blockchainConfig) === '{"enabled":true}') && this.env !== 'development') {
console.log("===> " + __("warning: running default config on a non-development environment"));
@ -93,17 +94,18 @@ Blockchain.prototype.initProxy = function() {
this.config.wsPort += constants.blockchain.servicePortOnProxy;
};
Blockchain.prototype.setupProxy = async function() {
Blockchain.prototype.setupProxy = async function(type) {
const proxy = require('./proxy');
const Ipc = require('../../core/ipc');
let ipcObject = new Ipc({ipcRole: 'client'});
const [rpcProxy, wsProxy] = await Promise.all([
proxy.serve(ipcObject, this.config.rpcHost, this.config.rpcPort, false),
proxy.serve(ipcObject, this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins)
]);
this.rpcProxy = rpcProxy;
this.wsProxy = wsProxy;
if(!this.proxyIpc) this.proxyIpc = new Ipc({ipcRole: 'client'});
if (type === 'rpc') {
this.rpcProxy = await proxy.serve(this.proxyIpc, this.config.rpcHost, this.config.rpcPort, false);
}
else if (type === 'ws'){
this.wsProxy = await proxy.serve(this.proxyIpc, this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins);
}
};
Blockchain.prototype.shutdownProxy = function() {
@ -111,8 +113,8 @@ Blockchain.prototype.shutdownProxy = function() {
return;
}
this.rpcProxy.close();
this.wsProxy.close();
if(this.rpcProxy) this.rpcProxy.close();
if(this.wsProxy) this.wsProxy.close();
};
Blockchain.prototype.runCommand = function(cmd, options, callback) {
@ -181,9 +183,12 @@ Blockchain.prototype.run = function() {
// Geth logs appear in stderr somehow
self.child.stderr.on('data', async (data) => {
data = data.toString();
if (!self.readyCalled && data.indexOf('HTTP endpoint opened') > -1 && self.config.proxy) {
await self.setupProxy('rpc');
}
if (!self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) {
if (self.config.proxy) {
await self.setupProxy();
await self.setupProxy('ws');
}
if (self.isDev) {
self.createFundAndUnlockAccounts((err) => {