proxy -- canonicalHost, defaultHost

This commit is contained in:
Michael Bradley, Jr 2018-07-15 19:34:33 -05:00
parent ca7a46224d
commit f91baf73bc

View File

@ -3,9 +3,11 @@ const http = require('http');
const constants = require('../constants.json'); const constants = require('../constants.json');
let commList = {}; let commList = {};
let transactions = {}; let transactions = {};
let receipts = {}; let receipts = {};
const {canonicalHost, defaultHost} = require('../utils/host');
const parseRequest = function(reqBody){ const parseRequest = function(reqBody){
let jsonO; let jsonO;
try { try {
@ -42,7 +44,7 @@ const parseResponse = function(ipc, resBody){
commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber; commList[receipts[jsonO.id]].blockNumber = jsonO.result.blockNumber;
commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed; commList[receipts[jsonO.id]].gasUsed = jsonO.result.gasUsed;
commList[receipts[jsonO.id]].status = jsonO.result.status; commList[receipts[jsonO.id]].status = jsonO.result.status;
if(ipc.connected && !ipc.connecting){ if(ipc.connected && !ipc.connecting){
ipc.request('log', commList[receipts[jsonO.id]]); ipc.request('log', commList[receipts[jsonO.id]]);
} else { } else {
@ -61,7 +63,7 @@ const parseResponse = function(ipc, resBody){
exports.serve = function(ipc, host, port, ws){ exports.serve = function(ipc, host, port, ws){
let proxy = httpProxy.createProxyServer({ let proxy = httpProxy.createProxyServer({
target: { target: {
host, host: canonicalHost(host),
port: port + constants.blockchain.servicePortOnProxy port: port + constants.blockchain.servicePortOnProxy
}, },
ws: ws ws: ws
@ -80,7 +82,7 @@ exports.serve = function(ipc, host, port, ws){
if(resBody){ if(resBody){
parseResponse(ipc, resBody); parseResponse(ipc, resBody);
} }
}); });
}); });
let server = http.createServer((req, res) => { let server = http.createServer((req, res) => {
@ -110,16 +112,16 @@ exports.serve = function(ipc, host, port, ws){
parseResponse(ipc, data.toString().substr(data.indexOf("{"))); parseResponse(ipc, data.toString().substr(data.indexOf("{")));
}); });
}); });
proxy.on('proxyReqWs', (proxyReq, req, socket) => { proxy.on('proxyReqWs', (proxyReq, req, socket) => {
var parser = new WsParser(0, false); var parser = new WsParser(0, false);
socket.pipe(parser); socket.pipe(parser);
parser.on('frame', function (frame) { parser.on('frame', function (frame) {
parseRequest(frame.data); parseRequest(frame.data);
}); });
}); });
} }
server.listen(port); server.listen(port, defaultHost);
}; };