embark/lib/modules/blockchain_process/proxy.js

158 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-06-11 20:40:14 +00:00
const httpProxy = require('http-proxy');
const http = require('http');
const constants = require('../../constants.json');
2018-09-20 00:59:59 +00:00
const utils = require('../../utils/utils');
2018-06-11 20:40:14 +00:00
2018-06-12 12:50:49 +00:00
let commList = {};
2018-07-16 00:34:33 +00:00
let transactions = {};
2018-06-12 12:50:49 +00:00
let receipts = {};
const {canonicalHost, defaultHost} = require('../../utils/host');
2018-07-16 00:34:33 +00:00
2018-08-14 13:20:31 +00:00
const parseRequest = function (reqBody) {
let jsonO;
try {
jsonO = JSON.parse(reqBody);
} catch (e) {
return; // Request is not a json. Do nothing
}
if (jsonO.method === "eth_sendTransaction") {
commList[jsonO.id] = {
type: 'contract-log',
address: jsonO.params[0].to,
data: jsonO.params[0].data
};
} else if (jsonO.method === "eth_getTransactionReceipt") {
if (transactions[jsonO.params[0]]) {
transactions[jsonO.params[0]].receiptId = jsonO.id;
receipts[jsonO.id] = transactions[jsonO.params[0]].commListId;
2018-06-12 12:50:49 +00:00
}
2018-08-14 13:20:31 +00:00
}
2018-06-12 12:50:49 +00:00
};
2018-06-11 20:40:14 +00:00
2018-08-14 13:20:31 +00:00
const parseResponse = function (ipc, resBody) {
let jsonO;
try {
jsonO = JSON.parse(resBody);
} catch (e) {
return; // Response is not a json. Do nothing
}
if (commList[jsonO.id]) {
commList[jsonO.id].transactionHash = jsonO.result;
transactions[jsonO.result] = {commListId: jsonO.id};
} else if (receipts[jsonO.id] && jsonO.result && jsonO.result.blockNumber) {
// TODO find out why commList[receipts[jsonO.id]] is sometimes not defined
if (!commList[receipts[jsonO.id]]) {
commList[receipts[jsonO.id]] = {};
}
2018-08-14 13:20:31 +00:00
commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber;
commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed;
commList[receipts[jsonO.id]].status = jsonO.result.status;
if (ipc.connected && !ipc.connecting) {
ipc.request('log', commList[receipts[jsonO.id]]);
} else {
ipc.connecting = true;
ipc.connect(() => {
ipc.connecting = false;
});
2018-06-12 12:50:49 +00:00
}
2018-08-14 13:20:31 +00:00
delete transactions[commList[receipts[jsonO.id]].transactionHash];
delete commList[receipts[jsonO.id]];
delete receipts[jsonO.id];
}
2018-06-12 14:15:54 +00:00
};
2018-06-12 12:50:49 +00:00
2018-09-20 00:59:59 +00:00
exports.serve = async function (ipc, host, port, ws, origin) {
2018-09-20 02:12:37 +00:00
const _origin = origin ? origin.split(',')[0] : undefined;
2018-09-20 00:59:59 +00:00
const start = Date.now();
function awaitTarget() {
2018-09-20 02:13:06 +00:00
return new Promise(resolve => {
2018-09-20 00:59:59 +00:00
utils.pingEndpoint(
canonicalHost(host), port, ws ? 'ws': false, 'http', _origin, async (err) => {
if (!err || (Date.now() - start > 10000)) {
// if (Date.now() - start > 10000) {
// console.warn('!!! TIMEOUT !!!');
// } else {
// console.warn('!!! CONNECT !!!');
// }
return resolve();
}
// console.warn('!!! WAITING !!!');
await utils.timer(250).then(awaitTarget).then(resolve);
}
);
});
}
await awaitTarget();
2018-08-14 13:20:31 +00:00
let proxy = httpProxy.createProxyServer({
target: {
host: canonicalHost(host),
2018-09-18 11:12:17 +00:00
port: port
2018-08-14 13:20:31 +00:00
},
ws: ws
});
proxy.on('error', function (e) {
console.error(__("Error forwarding requests to blockchain/simulator"), e.message);
});
proxy.on('proxyRes', (proxyRes) => {
let resBody = [];
proxyRes.on('data', (b) => resBody.push(b));
proxyRes.on('end', function () {
resBody = Buffer.concat(resBody).toString();
if (resBody) {
parseResponse(ipc, resBody);
}
2018-06-11 20:40:14 +00:00
});
2018-08-14 13:20:31 +00:00
});
let server = http.createServer((req, res) => {
let reqBody = [];
req.on('data', (b) => {
reqBody.push(b);
})
.on('end', () => {
reqBody = Buffer.concat(reqBody).toString();
if (reqBody) {
parseRequest(reqBody);
}
});
2018-06-11 20:40:14 +00:00
2018-08-14 13:20:31 +00:00
if (!ws) {
proxy.web(req, res);
}
});
2018-08-14 13:20:31 +00:00
if (ws) {
const WsParser = require('simples/lib/parsers/ws'); // npm install simples
2018-06-11 20:40:14 +00:00
2018-08-14 13:20:31 +00:00
server.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head);
2018-06-11 20:40:14 +00:00
});
2018-08-14 13:20:31 +00:00
proxy.on('open', (proxySocket) => {
proxySocket.on('data', (data) => {
parseResponse(ipc, data.toString().substr(data.indexOf("{")));
});
});
2018-07-16 00:34:33 +00:00
2018-08-14 13:20:31 +00:00
proxy.on('proxyReqWs', (proxyReq, req, socket) => {
var parser = new WsParser(0, false);
socket.pipe(parser);
parser.on('frame', function (frame) {
parseRequest(frame.data);
});
2018-07-16 00:34:33 +00:00
2018-08-14 13:20:31 +00:00
});
}
2018-09-18 11:12:17 +00:00
const listenPort = port - constants.blockchain.servicePortOnProxy;
server.listen(listenPort, defaultHost);
2018-08-14 13:20:31 +00:00
return server;
2018-06-11 20:40:14 +00:00
};