wip create proxy module

This commit is contained in:
Jonathan Rainville 2019-08-05 14:15:12 -04:00 committed by Iuri Matias
parent d169b92f50
commit 4533acaaa8
20 changed files with 261 additions and 121 deletions

View File

@ -52,18 +52,11 @@
"embark-i18n": "^4.1.0-beta.3",
"embark-logger": "^4.1.0-beta.5",
"embark-utils": "^4.1.0-beta.5",
"ethereumjs-tx": "1.3.7",
"ethereumjs-util": "6.0.0",
"fs-extra": "7.0.1",
"http-proxy": "1.17.0",
"netcat": "1.3.5",
"node-http-proxy-json": "0.1.6",
"pkg-up": "2.0.0",
"pump": "3.0.0",
"semver": "5.6.0",
"shelljs": "0.8.3",
"simples": "0.8.8",
"stream-json": "1.1.3",
"ws": "6.1.2"
},
"devDependencies": {

View File

@ -6,10 +6,9 @@ const path = require('path');
const constants = require('embark-core/constants');
const GethClient = require('./gethClient.js');
const ParityClient = require('./parityClient.js');
import { Proxy } from './proxy';
import { IPC } from 'embark-core';
import { compact, dappPath, defaultHost, dockerHostSwap, embarkPath, AccountParser} from 'embark-utils';
import { compact, dappPath, defaultHost, dockerHostSwap, embarkPath} from 'embark-utils';
const Logger = require('embark-logger');
// time between IPC connection attempts (in ms)
@ -24,7 +23,6 @@ var Blockchain = function(userConfig, clientClass) {
this.onExitCallback = userConfig.onExitCallback;
this.logger = userConfig.logger || new Logger({logLevel: 'debug', context: constants.contexts.blockchain}); // do not pass in events as we don't want any log events emitted
this.events = userConfig.events;
this.proxyIpc = null;
this.isStandalone = userConfig.isStandalone;
this.certOptions = userConfig.certOptions;
@ -60,8 +58,7 @@ var Blockchain = function(userConfig, clientClass) {
vmdebug: this.userConfig.vmdebug || false,
targetGasLimit: this.userConfig.targetGasLimit || false,
syncMode: this.userConfig.syncMode || this.userConfig.syncmode,
verbosity: this.userConfig.verbosity,
proxy: this.userConfig.proxy
verbosity: this.userConfig.verbosity
};
this.devFunds = null;
@ -103,7 +100,6 @@ var Blockchain = function(userConfig, clientClass) {
this.logger.error(__(spaceMessage, 'genesisBlock'));
process.exit(1);
}
this.initProxy();
this.client = new clientClass({config: this.config, env: this.env, isDev: this.isDev});
this.initStandaloneProcess();
@ -153,35 +149,6 @@ Blockchain.prototype.initStandaloneProcess = function () {
}
};
Blockchain.prototype.initProxy = function () {
if (this.config.proxy) {
this.config.rpcPort += constants.blockchain.servicePortOnProxy;
this.config.wsPort += constants.blockchain.servicePortOnProxy;
}
};
Blockchain.prototype.setupProxy = async function () {
if (!this.proxyIpc) this.proxyIpc = new IPC({ipcRole: 'client'});
const addresses = AccountParser.parseAccountsConfig(this.userConfig.accounts, false, dappPath(), this.logger);
let wsProxy;
if (this.config.wsRPC) {
wsProxy = new Proxy(this.proxyIpc).serve(this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins, addresses, this.certOptions);
}
[this.rpcProxy, this.wsProxy] = await Promise.all([new Proxy(this.proxyIpc).serve(this.config.rpcHost, this.config.rpcPort, false, null, addresses, this.certOptions), wsProxy]);
};
Blockchain.prototype.shutdownProxy = function () {
if (!this.config.proxy) {
return;
}
if (this.rpcProxy) this.rpcProxy.close();
if (this.wsProxy) this.wsProxy.close();
};
Blockchain.prototype.runCommand = function (cmd, options, callback) {
this.logger.info(__("running: %s", cmd.underline).green);
if (this.config.silent) {
@ -257,9 +224,6 @@ Blockchain.prototype.run = function () {
data = data.toString();
if (!self.readyCalled && self.client.isReady(data)) {
self.readyCalled = true;
if (self.config.proxy) {
await self.setupProxy();
}
self.readyCallback();
}
self.logger.info(`${self.client.name}: ${data}`);
@ -297,7 +261,6 @@ Blockchain.prototype.readyCallback = function () {
};
Blockchain.prototype.kill = function () {
this.shutdownProxy();
if (this.child) {
this.child.kill();
}

View File

@ -3,11 +3,10 @@ const async = require('async');
const { normalizeInput, deconstructUrl } = require('embark-utils');
const constants = require('embark-core/constants');
import { BlockchainProcessLauncher } from './blockchainProcessLauncher';
import { pingEndpoint } from './utils';
import {pingEndpoint} from 'embark-utils';
export { BlockchainClient } from './blockchain';
export { Simulator } from './simulator';
export { Proxy } from './proxy';
export default class BlockchainModule {

View File

@ -1,7 +1,6 @@
const path = require('path');
const pkgUp = require('pkg-up');
let shelljs = require('shelljs');
import { Proxy } from './proxy';
import { IPC } from 'embark-core';
const constants = require('embark-core/constants');
import { AccountParser, dappPath, defaultHost, dockerHostSwap, embarkPath, deconstructUrl } from 'embark-utils';
@ -17,6 +16,7 @@ export class Simulator {
run(options) {
let cmds = [];
// TODO change this probably
let useProxy = this.blockchainConfig.proxy || false;
let {host, port} = deconstructUrl(this.blockchainConfig.endpoint);
host = (dockerHostSwap(options.host || host) || defaultHost);
@ -82,14 +82,5 @@ export class Simulator {
console.log(`running: ${programName} ${cmds.join(' ')}`);
shelljs.exec(`node ${program} ${cmds.join(' ')}`, {async : true});
if(useProxy){
let ipcObject = new IPC({ipcRole: 'client'});
if (this.blockchainConfig.wsRPC) {
return new Proxy(ipcObject).serve(host, port, true, this.blockchainConfig.wsOrigins, []);
}
new Proxy(ipcObject).serve(host, port, false);
}
}
}

View File

@ -1,57 +0,0 @@
export function pingEndpoint(host, port, type, protocol, origin, callback) {
// remove any extra information from a host string, e.g. port, path, query
const _host = require("url").parse(
// url.parse() expects a protocol segment else it won't parse as desired
host.slice(0, 4) === "http" ? host : `${protocol}://${host}`
).hostname;
let closed = false;
const close = (req, closeMethod) => {
if (!closed) {
closed = true;
req[closeMethod]();
}
};
const handleEvent = (req, closeMethod, ...args) => {
close(req, closeMethod);
setImmediate(() => { callback(...args); });
};
const handleError = (req, closeMethod) => {
req.on("error", (err) => {
if (err.code !== "ECONNREFUSED") {
console.error(
`Ping: Network error` +
(err.message ? ` '${err.message}'` : "") +
(err.code ? ` (code: ${err.code})` : "")
);
}
// when closed additional error events will not callback
if (!closed) { handleEvent(req, closeMethod, err); }
});
};
const handleSuccess = (req, closeMethod, event) => {
req.once(event, () => {
handleEvent(req, closeMethod);
});
};
const handleRequest = (req, closeMethod, event) => {
handleError(req, closeMethod);
handleSuccess(req, closeMethod, event);
};
if (type === "ws") {
const url = `${protocol === "https" ? "wss" : "ws"}://${_host}:${port}/`;
const req = new (require("ws"))(url, origin ? {origin} : {});
handleRequest(req, "close", "open");
} else {
const headers = origin ? {Origin: origin} : {};
const req = (protocol === "https" ? require("https") : require("http")).get(
{headers, host: _host, port}
);
handleRequest(req, "abort", "response");
}
}

View File

@ -1,2 +1,5 @@
declare module "embark-core" {
export class IPC {
constructor(options: {ipcRole: string});
}
}

View File

@ -0,0 +1,4 @@
engine-strict = true
package-lock = false
save-exact = true
scripts-prepend-node-path = true

View File

@ -0,0 +1,6 @@
# `embark-proxy`
Proxy to listen to blockchain requests and responses
Visit [embark.status.im](https://embark.status.im/) to get started with
[Embark](https://github.com/embark-framework/embark).

View File

@ -0,0 +1,74 @@
{
"name": "embark-proxy",
"version": "4.1.0-beta.5",
"author": "Iuri Matias <iuri.matias@gmail.com>",
"contributors": [],
"description": "Proxy to listen to blockchain requests and responses",
"homepage": "https://github.com/embark-framework/embark/tree/master/packages/embark-proxy#readme",
"bugs": "https://github.com/embark-framework/embark/issues",
"keywords": [
"blockchain",
"dapps",
"ethereum",
"ipfs",
"serverless",
"solc",
"solidity"
],
"files": [
"dist"
],
"license": "MIT",
"repository": {
"directory": "packages/embark-proxy",
"type": "git",
"url": "https://github.com/embark-framework/embark.git"
},
"main": "./dist/index.js",
"scripts": {
"build": "cross-env BABEL_ENV=node babel src --extensions \".ts\" --out-dir dist --root-mode upward --source-maps",
"ci": "npm run qa",
"clean": "npm run reset",
"lint": "npm-run-all lint:*",
"lint:ts": "tslint -c tslint.json \"src/**/*.ts\"",
"package": "npm pack",
"qa": "npm-run-all lint typecheck build package",
"reset": "npx rimraf dist embark-*.tgz package",
"start": "npm run watch",
"typecheck": "tsc",
"watch": "run-p watch:*",
"watch:build": "npm run build -- --verbose --watch",
"watch:typecheck": "npm run typecheck -- --preserveWatchOutput --watch"
},
"eslintConfig": {
"extends": "../../.eslintrc.json"
},
"dependencies": {
"@babel/runtime-corejs2": "7.3.1",
"embark-core": "^4.1.0-beta.5",
"embark-i18n": "^4.1.0-beta.3",
"embark-utils": "^4.1.0-beta.5",
"ethereumjs-tx": "1.3.7",
"ethereumjs-util": "6.0.0",
"http-proxy": "1.17.0",
"node-http-proxy-json": "0.1.6",
"pump": "3.0.0",
"stream-json": "1.1.3",
"simples": "0.8.8"
},
"devDependencies": {
"@babel/cli": "7.2.3",
"@babel/core": "7.2.2",
"cross-env": "5.2.0",
"eslint": "5.7.0",
"npm-run-all": "4.1.5",
"rimraf": "2.6.3",
"tslint": "5.16.0",
"typescript": "3.4.5"
},
"engines": {
"node": ">=8.12.0 <12.0.0",
"npm": ">=6.4.1",
"yarn": ">=1.12.3"
}
}

View File

@ -0,0 +1,72 @@
import {Embark, Events, Logger} /* supplied by @types/embark in packages/embark-typings */ from "embark";
import {IPC} from "embark-core";
import {AccountParser, dappPath} from "embark-utils";
import {Proxy} from "./proxy";
const constants = require("embark-core/constants");
export default class ProxyManager {
private logger: Logger;
private events: Events;
private proxyIpc: IPC;
private rpcProxy: any;
private wsProxy: any;
// private rpcPort: number;
// private wsPort: number;
constructor(private embark: Embark, _options: any) {
console.log("ALLO");
this.logger = embark.logger;
this.events = embark.events;
this.proxyIpc = new IPC({ipcRole: "client"});
this.embark.config.blockchainConfig.rpcPort += constants.blockchain.servicePortOnProxy;
this.embark.config.blockchainConfig.wsPort += constants.blockchain.servicePortOnProxy;
this.events.once("blockchain:ready", () => {
console.log("SETUP");
this.setupProxy();
});
}
private async setupProxy() {
if (this.embark.config.blockchainConfig.proxy) {
return;
}
const addresses = AccountParser.parseAccountsConfig(this.embark.config.blockchainConfig.accounts, false, dappPath(), this.logger);
let wsProxy;
if (this.embark.config.blockchainConfig.wsRPC) {
wsProxy = new Proxy(this.proxyIpc).serve(
this.embark.config.blockchainConfig.wsHost,
this.embark.config.blockchainConfig.wsPort,
true, this.embark.config.blockchainConfig.wsOrigins,
addresses,
this.embark.config.webServerConfig.certOptions);
}
[this.rpcProxy, this.wsProxy] = await Promise.all([
new Proxy(this.proxyIpc).serve(
this.embark.config.blockchainConfig.rpcHost,
this.embark.config.blockchainConfig.rpcPort,
false,
null,
addresses,
this.embark.config.webServerConfig.certOptions),
wsProxy,
]);
}
public shutdownProxy() {
if (!this.embark.config.blockchainConfig.proxy) {
return;
}
if (this.rpcProxy) {
this.rpcProxy.close();
}
if (this.wsProxy) {
this.wsProxy.close();
}
}
}

View File

@ -3,7 +3,7 @@
require('./httpProxyOverride');
const Asm = require('stream-json/Assembler');
import { __ } from 'embark-i18n';
import { canonicalHost, timer } from 'embark-utils';
import { canonicalHost, timer, pingEndpoint } from 'embark-utils';
const constants = require('embark-core/constants');
const {Duplex} = require('stream');
const http = require('http');
@ -15,7 +15,6 @@ const WsWrapper = require('simples/lib/ws/wrapper');
const modifyResponse = require('node-http-proxy-json');
const Transaction = require('ethereumjs-tx');
const ethUtil = require('ethereumjs-util');
import { pingEndpoint } from './utils';
const METHODS_TO_MODIFY = {accounts: 'eth_accounts'};
const REQUEST_TIMEOUT = 5000;

View File

@ -0,0 +1,5 @@
{
"compilerOptions": { "baseUrl": ".", "paths": { "*": ["types/*"] } },
"extends": "../../tsconfig.json",
"include": ["src/**/*"]
}

View File

@ -0,0 +1,3 @@
{
"extends": "../../tslint.json"
}

View File

@ -24,6 +24,23 @@ export interface Config {
};
generationDir: string;
};
blockchainConfig: {
accounts: any[];
proxy: boolean;
rpcPort: string | number;
wsPort: string | number;
rpcHost: string | number;
wsHost: string | number;
wsOrigins: string;
rpcCorsDomain: string;
wsRPC: boolean;
};
webServerConfig: {
certOptions: {
key: string;
cert: string;
};
};
plugins: Plugins;
reloadConfig(): void;
}

View File

@ -1,8 +1,10 @@
declare module "embark-utils" {
class File {
path: string;
import {Logger} from "embark";
export class File {
public path: string;
constructor(options: any);
prepareForCompilation(isCoverage?: boolean): any;
public prepareForCompilation(isCoverage?: boolean): any;
}
function anchoredValue(anchor: string|null, value: string): string;
@ -23,4 +25,9 @@ declare module "embark-utils" {
function recursiveMerge(target: any, source: any): any;
function pkgPath(...names: string[]): string;
function removePureView(dir: string): void;
function pingEndpoint(host: string, port: number, type: string, protocol: string, origin: string, callback: any): void;
export class AccountParser {
public static parseAccountsConfig(accountsConfig: any[], web3: any, dappPath: string, logger: Logger, nodeAccounts?: any[]): any[];
}
}

View File

@ -4,7 +4,7 @@ const shelljs = require('shelljs');
const clipboardy = require('clipboardy');
const {canonicalHost, defaultCorsHost, defaultHost, dockerHostSwap, isDocker} = require('./host');
const {downloadFile, findNextPort, getJson, httpGet, httpsGet, httpGetJson, httpsGetJson} = require('./network');
const {downloadFile, findNextPort, getJson, httpGet, httpsGet, httpGetJson, httpsGetJson, pingEndpoint} = require('./network');
const logUtils = require('./log-utils');
const toposortGraph = require('./toposort');
import { unitRegex } from './constants';
@ -321,6 +321,7 @@ const Utils = {
httpsGet,
httpGetJson,
httpsGetJson,
pingEndpoint,
setUpEnv,
sha512,
sha3,

View File

@ -91,3 +91,61 @@ export function getJson(url: string, cb: any) {
}
httpGetJson(url, cb);
}
export function pingEndpoint(host: string, port: number, type: string, protocol: string, origin: string, callback: any) {
// remove any extra information from a host string, e.g. port, path, query
const _host = require("url").parse(
// url.parse() expects a protocol segment else it won't parse as desired
host.slice(0, 4) === "http" ? host : `${protocol}://${host}`,
).hostname;
let closed = false;
const close = (req: any, closeMethod: any) => {
if (!closed) {
closed = true;
req[closeMethod]();
}
};
const handleEvent = (req: any, closeMethod: any, ...args: any) => {
close(req, closeMethod);
setImmediate(() => { callback(...args); });
};
const handleError = (req: any, closeMethod: any) => {
req.on("error", (err: any) => {
if (err.code !== "ECONNREFUSED") {
console.error(
`Ping: Network error` +
(err.message ? ` '${err.message}'` : "") +
(err.code ? ` (code: ${err.code})` : ""),
);
}
// when closed additional error events will not callback
if (!closed) { handleEvent(req, closeMethod, err); }
});
};
const handleSuccess = (req: any, closeMethod: any, event: any) => {
req.once(event, () => {
handleEvent(req, closeMethod);
});
};
const handleRequest = (req: any, closeMethod: any, event: any) => {
handleError(req, closeMethod);
handleSuccess(req, closeMethod, event);
};
if (type === "ws") {
const url = `${protocol === "https" ? "wss" : "ws"}://${_host}:${port}/`;
const req = new (require("ws"))(url, origin ? {origin} : {});
handleRequest(req, "close", "open");
} else {
const headers = origin ? {Origin: origin} : {};
const req = (protocol === "https" ? require("https") : require("http")).get(
{headers, host: _host, port},
);
handleRequest(req, "abort", "response");
}
}

View File

@ -116,6 +116,7 @@
"embark-plugin-cmd": "^4.1.0-beta.5",
"embark-process-logs-api": "^4.1.0-beta.5",
"embark-profiler": "^4.1.0-beta.3",
"embark-proxy": "^4.1.0-beta.5",
"embark-reset": "^4.1.0-beta.3",
"embark-scaffolding": "^4.1.0-beta.5",
"embark-solidity": "^4.1.0-beta.5",

View File

@ -204,6 +204,7 @@ class Engine {
// });
this.registerModule('ethereum-blockchain-client');
this.registerModulePackage('embark-proxy');
// this.registerModule('web3', { plugins: this.plugins });
this.registerModulePackage('embark-web3', {plugins: this.plugins});
this.registerModulePackage('embark-specialconfigs', {plugins: this.plugins});