Extracted proxy to its own file

This commit is contained in:
Richard Ramos 2018-06-07 13:03:04 -04:00
parent c571e0d492
commit f84970f824
2 changed files with 60 additions and 59 deletions

52
lib/cmds/proxy.js Normal file
View File

@ -0,0 +1,52 @@
const httpProxy = require('http-proxy');
const http = require('http');
exports.serve = function(host, port){
let commList = {};
let proxy = httpProxy.createProxyServer({});
let server = http.createServer((req, res) => {
let reqBody = [];
req.on('data', (b) => { reqBody.push(b); })
.on('end', () => {
reqBody = Buffer.concat(reqBody).toString();
if(reqBody){
let jsonO = JSON.parse(reqBody);
if(jsonO.method == "eth_sendTransaction"){
commList[jsonO.id] = {
requestData: jsonO.params.data
};
}
}
});
proxy.proxyRequest(req, res, {
target: `http://${host}:${port + 1}`
});
proxy.on('proxyRes', (proxyRes, req, res) => {
let resBody = [];
proxyRes.on('data', (b) => resBody.push(b))
proxyRes.on('end', function () {
resBody = Buffer.concat(resBody).toString();
try {
let jsonO = JSON.parse(resBody);
if(commList[jsonO.id]){
commList[jsonO.id].transactionHash = resBody;
// TODO: decode commlist
// ” SimpleStorage> set(5) | tx: 0xef234f16etc ”
delete commList[jsonO.id];
}
} catch(e){
//
}
});
});
});
server.listen(port);
}

View File

@ -1,5 +1,5 @@
let shelljs = require('shelljs');
let proxy = require('./proxy');
class Simulator {
constructor(options) {
this.blockchainConfig = options.blockchainConfig;
@ -19,10 +19,11 @@ class Simulator {
process.exit();
}
cmds.push("-p " + ((options.port || this.blockchainConfig.rpcPort || 8545) + 1));
let host = (options.host || this.blockchainConfig.rpcHost || 'localhost');
let port = (options.port || this.blockchainConfig.rpcPort || 8545);
cmds.push("-h " + (options.host || this.blockchainConfig.rpcHost || 'localhost'));
cmds.push("-p " + (port + 1));
cmds.push("-h " + host);
cmds.push("-a " + (options.numAccounts || 10));
cmds.push("-e " + (options.defaultBalance || 100));
cmds.push("-l " + (options.gasLimit || 8000000));
@ -41,61 +42,9 @@ class Simulator {
const program = ganache ? 'ganache-cli' : 'testrpc';
shelljs.exec(`${program} ${cmds.join(' ')} &`, {async : true});
let httpProxy = require('http-proxy');
let http = require('http');
let _port = options.port || this.blockchainConfig.rpcPort || 8545;
let _host = (options.host || this.blockchainConfig.rpcHost || 'localhost');
let commList = {};
let proxy = httpProxy.createProxyServer({});
let server = http.createServer((req, res) => {
let reqBody = [];
req.on('data', (b) => { reqBody.push(b); })
.on('end', () => {
reqBody = Buffer.concat(reqBody).toString();
if(reqBody){
let jsonO = JSON.parse(reqBody);
if(jsonO.method == "eth_sendTransaction"){
commList[jsonO.id] = {
requestData: jsonO.params.data
};
}
}
});
proxy.proxyRequest(req, res, {
target: `http://${_host}:${_port + 1}`
});
proxy.on('proxyRes', function (proxyRes, req, res) {
let resBody = [];
proxyRes.on('data', (b) => resBody.push(b))
proxyRes.on('end', function () {
resBody = Buffer.concat(resBody).toString();
try {
let jsonO = JSON.parse(resBody);
if(commList[jsonO.id]){
commList[json0.id].transactionHash = resBody;
// TODO: decode commlist
// ” SimpleStorage> set(5) | tx: 0xef234f16etc ”
delete commList[json0.id];
}
} catch(e){
//
}
});
});
});
server.listen(_port);
shelljs.exec(`${program} ${cmds.join(' ')} &`, {async : true});
proxy.serve(host, port);
}
}