fix: fix node connection test to use the endpoints correctly

This caused issues to connect to Infura for example, because we did
not support HTTPS at all, nor WSS
This commit is contained in:
Jonathan Rainville 2019-12-12 14:57:52 -05:00 committed by Iuri Matias
parent 1a7fc663b7
commit 0503bb24a3
8 changed files with 72 additions and 51 deletions

View File

@ -10,6 +10,7 @@ const constants = require('embark-core/constants');
import { __ } from 'embark-i18n'; import { __ } from 'embark-i18n';
import { import {
buildUrlFromConfig, buildUrlFromConfig,
deconstructUrl,
canonicalHost, canonicalHost,
dappPath, dappPath,
defaultHost, defaultHost,
@ -389,7 +390,18 @@ export class Config {
}); });
} }
if (!this.blockchainConfig.endpoint) { if (this.blockchainConfig.endpoint) {
const {type, host, port} = deconstructUrl(this.blockchainConfig.endpoint);
if (type === 'ws') {
this.blockchainConfig.wsHost = host;
this.blockchainConfig.wsPort = port;
this.blockchainConfig.wsRPC = true;
} else {
this.blockchainConfig.rpcHost = host;
this.blockchainConfig.rpcPort = port;
this.blockchainConfig.wsRPC = false;
}
} else {
const urlConfig = (this.blockchainConfig.wsHost) ? { const urlConfig = (this.blockchainConfig.wsHost) ? {
host: this.blockchainConfig.wsHost, host: this.blockchainConfig.wsHost,
port: this.blockchainConfig.wsPort, port: this.blockchainConfig.wsPort,

View File

@ -1,5 +1,6 @@
const WebSocket = require("ws"); const WebSocket = require("ws");
const http = require("http"); const http = require("http");
const https = require("https");
const LIVENESS_CHECK = `{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":42}`; const LIVENESS_CHECK = `{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":42}`;
@ -20,10 +21,8 @@ const parseAndRespond = (data, cb) => {
cb(null, version); cb(null, version);
}; };
const rpc = (host, port, cb) => { const rpcWithEndpoint = (endpoint, cb) => {
const options = { const options = {
hostname: host, // TODO(andremedeiros): get from config
port: port, // TODO(andremedeiros): get from config
method: "POST", method: "POST",
timeout: 1000, timeout: 1000,
headers: { headers: {
@ -32,7 +31,12 @@ const rpc = (host, port, cb) => {
} }
}; };
const req = http.request(options, (res) => { let obj = http;
if (endpoint.startsWith('https')) {
obj = https;
}
const req = obj.request(endpoint, options, (res) => {
let data = ""; let data = "";
res.on("data", chunk => { data += chunk; }); res.on("data", chunk => { data += chunk; });
res.on("end", () => parseAndRespond(data, cb)); res.on("end", () => parseAndRespond(data, cb));
@ -42,8 +46,8 @@ const rpc = (host, port, cb) => {
req.end(); req.end();
}; };
const ws = (host, port, cb) => { const ws = (endpoint, cb) => {
const conn = new WebSocket("ws://" + host + ":" + port); const conn = new WebSocket(endpoint);
conn.on("message", (data) => { conn.on("message", (data) => {
parseAndRespond(data, cb); parseAndRespond(data, cb);
conn.close(); conn.close();
@ -54,5 +58,5 @@ const ws = (host, port, cb) => {
module.exports = { module.exports = {
ws, ws,
rpc rpcWithEndpoint
}; };

View File

@ -1,8 +1,8 @@
import { __ } from 'embark-i18n'; import { __ } from 'embark-i18n';
const { normalizeInput } = require('embark-utils'); const {normalizeInput} = require('embark-utils');
import { BlockchainProcessLauncher } from './blockchainProcessLauncher'; import { BlockchainProcessLauncher } from './blockchainProcessLauncher';
import { BlockchainClient } from './blockchain'; import { BlockchainClient } from './blockchain';
import { ws, rpc } from './check.js'; import { ws, rpcWithEndpoint } from './check.js';
import DevTxs from "./devtxs"; import DevTxs from "./devtxs";
const constants = require('embark-core/constants'); const constants = require('embark-core/constants');
@ -82,21 +82,16 @@ class Geth {
} }
_doCheck(cb) { _doCheck(cb) {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig; if (this.blockchainConfig.endpoint.startsWith('ws')) {
if (wsRPC) { return ws(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
} }
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb)); rpcWithEndpoint(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
} }
// TODO: need to get correct port taking into account the proxy // TODO: need to get correct port taking into account the proxy
registerServiceCheck() { registerServiceCheck() {
this.events.request("services:register", 'Ethereum', (cb) => { this.events.request("services:register", 'Ethereum', (cb) => {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig; this._doCheck(cb);
if (wsRPC) {
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
}
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb));
}, 5000, 'off'); }, 5000, 'off');
} }

View File

@ -1,5 +1,6 @@
const WebSocket = require("ws"); const WebSocket = require("ws");
const http = require("http"); const http = require("http");
const https = require("https");
const LIVENESS_CHECK=`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":42}`; const LIVENESS_CHECK=`{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":42}`;
@ -20,10 +21,8 @@ const parseAndRespond = (data, cb) => {
cb(null, version); cb(null, version);
}; };
const rpc = (host, port, cb) => { const rpcWithEndpoint = (endpoint, cb) => {
const options = { const options = {
hostname: host, // TODO(andremedeiros): get from config
port: port, // TODO(andremedeiros): get from config
method: "POST", method: "POST",
timeout: 1000, timeout: 1000,
headers: { headers: {
@ -32,7 +31,12 @@ const rpc = (host, port, cb) => {
} }
}; };
const req = http.request(options, (res) => { let obj = http;
if (endpoint.startsWith('https')) {
obj = https;
}
const req = obj.request(endpoint, options, (res) => {
let data = ""; let data = "";
res.on("data", chunk => { data += chunk; }); res.on("data", chunk => { data += chunk; });
res.on("end", () => parseAndRespond(data, cb)); res.on("end", () => parseAndRespond(data, cb));
@ -42,8 +46,8 @@ const rpc = (host, port, cb) => {
req.end(); req.end();
}; };
const ws = (host, port, cb) => { const ws = (endpoint, cb) => {
const conn = new WebSocket("ws://" + host + ":" + port); const conn = new WebSocket(endpoint);
conn.on("message", (data) => { conn.on("message", (data) => {
parseAndRespond(data, cb); parseAndRespond(data, cb);
conn.close(); conn.close();
@ -54,5 +58,5 @@ const ws = (host, port, cb) => {
module.exports = { module.exports = {
ws, ws,
rpc rpcWithEndpoint
}; };

View File

@ -2,7 +2,7 @@ import { __ } from 'embark-i18n';
import {BlockchainClient} from "./blockchain"; import {BlockchainClient} from "./blockchain";
const {normalizeInput} = require('embark-utils'); const {normalizeInput} = require('embark-utils');
import {BlockchainProcessLauncher} from './blockchainProcessLauncher'; import {BlockchainProcessLauncher} from './blockchainProcessLauncher';
import {ws, rpc} from './check.js'; import {ws, rpcWithEndpoint} from './check.js';
const constants = require('embark-core/constants'); const constants = require('embark-core/constants');
class Parity { class Parity {
@ -70,11 +70,10 @@ class Parity {
} }
_doCheck(cb) { _doCheck(cb) {
const { rpcHost, rpcPort, wsRPC, wsHost, wsPort } = this.blockchainConfig; if (this.blockchainConfig.endpoint.startsWith('ws')) {
if (wsRPC) { return ws(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
return ws(wsHost, wsPort, (err, version) => this._getNodeState(err, version, cb));
} }
rpc(rpcHost, rpcPort, (err, version) => this._getNodeState(err, version, cb)); rpcWithEndpoint(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
} }
// TODO: need to get correct port taking into account the proxy // TODO: need to get correct port taking into account the proxy

View File

@ -1,5 +1,6 @@
const WebSocket = require("ws"); const WebSocket = require("ws");
const http = require("http"); const http = require("http");
const https = require("https");
const LIVENESS_CHECK = JSON.stringify({ const LIVENESS_CHECK = JSON.stringify({
jsonrpc:'2.0', jsonrpc:'2.0',
@ -25,10 +26,8 @@ const parseAndRespond = (data, cb) => {
cb(null, resp.result); cb(null, resp.result);
}; };
const rpc = (host, port, cb) => { const rpcWithEndpoint = (endpoint, cb) => {
const options = { const options = {
hostname: host, // TODO(andremedeiros): get from config
port, // TODO(andremedeiros): get from config
method: "POST", method: "POST",
timeout: 1000, timeout: 1000,
headers: { headers: {
@ -37,9 +36,14 @@ const rpc = (host, port, cb) => {
} }
}; };
const req = http.request(options, (res) => { let obj = http;
if (endpoint.startsWith('https')) {
obj = https;
}
const req = obj.request(endpoint, options, (res) => {
let data = ""; let data = "";
res.on("data", (chunk) => { data += chunk; }); res.on("data", chunk => { data += chunk; });
res.on("end", () => parseAndRespond(data, cb)); res.on("end", () => parseAndRespond(data, cb));
}); });
req.on("error", (e) => cb(e)); req.on("error", (e) => cb(e));
@ -47,8 +51,8 @@ const rpc = (host, port, cb) => {
req.end(); req.end();
}; };
const ws = (host, port, cb) => { const ws = (endpoint, cb) => {
const conn = new WebSocket("ws://" + host + ":" + port); const conn = new WebSocket(endpoint);
conn.on("message", (data) => { conn.on("message", (data) => {
parseAndRespond(data, cb); parseAndRespond(data, cb);
conn.close(); conn.close();
@ -59,5 +63,5 @@ const ws = (host, port, cb) => {
module.exports = { module.exports = {
ws, ws,
rpc rpcWithEndpoint
}; };

View File

@ -3,7 +3,7 @@ import { dappPath, canonicalHost, defaultHost } from "embark-utils";
const constants = require("embark-core/constants"); const constants = require("embark-core/constants");
const API = require("./api.js"); const API = require("./api.js");
import { BlockchainProcessLauncher } from "./blockchainProcessLauncher"; import { BlockchainProcessLauncher } from "./blockchainProcessLauncher";
import { ws, rpc } from "./check.js"; import { ws, rpcWithEndpoint } from "./check.js";
const { normalizeInput } = require("embark-utils"); const { normalizeInput } = require("embark-utils");
class Whisper { class Whisper {
@ -64,12 +64,10 @@ class Whisper {
registerServiceCheck() { registerServiceCheck() {
this.events.request("services:register", "Whisper", (cb) => { this.events.request("services:register", "Whisper", (cb) => {
const { host, port, type } = this.communicationConfig.connection; if (this.blockchainConfig.endpoint.startsWith('ws')) {
if (type === "ws") { return ws(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
return ws(host, port, (err, version) => this._getNodeState(err, version, cb));
} }
rpc(host, port, (err, version) => this._getNodeState(err, version, cb)); rpcWithEndpoint(this.blockchainConfig.endpoint, (err, version) => this._getNodeState(err, version, cb));
}, 5000, "off"); }, 5000, "off");
} }

View File

@ -92,8 +92,13 @@ export default class ProxyManager {
} }
this.inited = true; this.inited = true;
let port = this.embark.config.blockchainConfig.rpcPort;
if (!port && port !== 0) {
port = 8545;
}
// setup ports // setup ports
const rpcPort = await findNextPort(this.embark.config.blockchainConfig.rpcPort + constants.blockchain.servicePortOnProxy); const rpcPort = await findNextPort(port + constants.blockchain.servicePortOnProxy);
const wsPort = await findNextPort(rpcPort + 1); const wsPort = await findNextPort(rpcPort + 1);
this.rpcPort = rpcPort; this.rpcPort = rpcPort;
this.wsPort = wsPort; this.wsPort = wsPort;