Extracted proxy to its own file

This commit is contained in:
Richard Ramos 2018-06-07 13:03:04 -04:00
parent 339ffa5869
commit 84c01c3197
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));
@ -43,59 +44,7 @@ class Simulator {
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);
proxy.serve(host, port);
}
}