2018-06-11 20:40:14 +00:00
|
|
|
const httpProxy = require('http-proxy');
|
|
|
|
const http = require('http');
|
|
|
|
|
|
|
|
exports.serve = function(ipc, host, port, ws){
|
|
|
|
let commList = {};
|
2018-06-08 19:30:20 +00:00
|
|
|
let transactions = {};
|
|
|
|
let receipts = {};
|
2018-06-11 20:40:14 +00:00
|
|
|
|
|
|
|
let proxy = httpProxy.createProxyServer({
|
|
|
|
target: {
|
|
|
|
host: host,
|
2018-06-08 17:44:16 +00:00
|
|
|
port: port + 10
|
|
|
|
},
|
|
|
|
ws: ws
|
2018-06-11 20:40:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
proxy.on('proxyRes', (proxyRes) => {
|
|
|
|
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 = jsonO.result;
|
2018-06-08 19:30:20 +00:00
|
|
|
transactions[jsonO.result] = {commListId: jsonO.id};
|
|
|
|
} else if(receipts[jsonO.id]){
|
|
|
|
commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber;
|
|
|
|
commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed;
|
|
|
|
commList[receipts[jsonO.id]].status = jsonO.result.status;
|
|
|
|
|
2018-06-11 20:40:14 +00:00
|
|
|
if(ipc.connected && !ipc.connecting){
|
2018-06-08 19:30:20 +00:00
|
|
|
ipc.request('log', commList[receipts[jsonO.id]]);
|
2018-06-11 20:40:14 +00:00
|
|
|
} else {
|
|
|
|
ipc.connecting = true;
|
|
|
|
ipc.connect((err) => {
|
|
|
|
ipc.connecting = false;
|
|
|
|
});
|
|
|
|
}
|
2018-06-08 19:30:20 +00:00
|
|
|
|
|
|
|
delete transactions[commList[receipts[jsonO.id]].transactionHash];
|
|
|
|
delete receipts[jsonO.id];
|
2018-06-11 20:40:14 +00:00
|
|
|
delete commList[jsonO.id];
|
|
|
|
}
|
|
|
|
} catch(e){
|
2018-06-08 17:44:16 +00:00
|
|
|
//
|
2018-06-11 20:40:14 +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){
|
|
|
|
let jsonO = JSON.parse(reqBody);
|
|
|
|
if(jsonO.method == "eth_sendTransaction"){
|
|
|
|
commList[jsonO.id] = {
|
|
|
|
type: 'contract-log',
|
|
|
|
address: jsonO.params[0].to,
|
|
|
|
data: jsonO.params[0].data
|
|
|
|
};
|
2018-06-08 19:30:20 +00:00
|
|
|
} 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-11 20:40:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-08 17:44:16 +00:00
|
|
|
if(!ws){
|
2018-06-11 20:40:14 +00:00
|
|
|
proxy.web(req, res);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-08 17:44:16 +00:00
|
|
|
if(ws){
|
|
|
|
server.on('upgrade', function (req, socket, head) {
|
|
|
|
proxy.ws(req, socket, head);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-11 20:40:14 +00:00
|
|
|
server.listen(port);
|
|
|
|
};
|