I need to listening https traffic, which my code I Can listening all http traffic, but which https I can't capture. How can I do it?
This is my code
var http = require('http');
var url = require('url');
var fs = require('fs');
http.createServer(function(request, response) {
var url_part = url.parse(request.url,true);
var originUrl = url_part.href;
var options = url.parse(originUrl);
var proxy = http.request(options);
proxy.addListener('response', function (proxy_response) {
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {
response.end();
});
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
request.addListener('data', function(chunk) {
proxy.write(chunk, 'application/json');
});
request.addListener('end', function() {
proxy.end();
});
}).listen(3128);
Related
I am trying to setup a proxy for http and https. Here is my code,
const http = require('http');
var https = require('https');
const fs = require('fs');
var url = require('url');
var net = require('net');
const config = require('./config');
let proxify = function (req, res) {
var urlObj = url.parse(req.url);
var target = urlObj.protocol + '//' + urlObj.host;
if (!req.headers['x-target']) req.headers['x-target'] = target;
req.headers['x-proxy-username'] = config.username;
req.headers['x-proxy-password'] = config.password;
console.log(target);
console.log('Proxy HTTP request for:', target);
var proxy = httpProxy.createProxyServer({});
proxy.on('error', function (err, req, res) {
console.log('proxy error', err);
res.end();
});
proxy.web(req, res, { target: config.server, changeOrigin: true });
};
var httpserver = http.createServer(proxify).listen(2890); //this is the port your clients will connect to
const httpsserver = https
.createServer(
{
cert: fs.readFileSync('./ssl_cert/cert.pem'),
key: fs.readFileSync('./ssl_cert/key.pem'),
},
proxify
)
.listen(2891);
var regex_hostport = /^([^:]+)(:([0-9]+))?$/;
var getHostPortFromString = function (hostString, defaultPort) {
var host = hostString;
var port = defaultPort;
var result = regex_hostport.exec(hostString);
if (result != null) {
host = result[1];
if (result[2] != null) {
port = result[3];
}
}
return [host, port];
};
httpserver.addListener('connect', function (req, socket, bodyhead) {
var hostPort = getHostPortFromString(req.url, 443);
var hostDomain = hostPort[0];
var port = parseInt(hostPort[1]);
console.log('Proxying HTTPS request for:', hostDomain, port);
req.headers['x-target'] = 'http://' + hostDomain + ':' + port;
req.headers['x-proxy-username'] = config.username;
req.headers['x-proxy-password'] = config.password;
var proxyHost = new URL(config.server);
var proxySocket = new net.Socket();
proxySocket.connect(
{ port: proxyHost.port, host: proxyHost.hostname },
function () {
console.log('bodyhead', bodyhead.toString()); //debug
proxySocket.write(bodyhead);
socket.write(
'HTTP/' + req.httpVersion + ' 200 Connection established\r\n\r\n'
);
}
);
proxySocket.on('data', function (chunk) {
console.log('proxy data chunk', chunk.toString()); // debug
socket.write(chunk);
});
proxySocket.on('end', function () {
socket.end();
});
proxySocket.on('error', function () {
socket.write('HTTP/' + req.httpVersion + ' 500 Connection error\r\n\r\n');
socket.end();
});
socket.on('data', function (chunk) {
console.log('data chunk', chunk.toString('utf8')); // debug
proxySocket.write(chunk);
});
socket.on('end', function () {
proxySocket.end();
});
socket.on('error', function () {
proxySocket.end();
});
});
Don't judge me too hard, just trying to get it working first.
When proxying http with windows 10 proxy settings, it works fine. But when I am trying to proxy https, it logs encoded data like `↕►♦♦♦☺♣♣♣♠♠☺↕3+)/1.1♣♣☺
☺↔ \s☻�t�DQ��g}T�c\‼sO��♦��U��ޝ∟-☻☺☺+♂
→→♥♦♥♥♥☻♥☺♥☻☻j☺§�` and gives a 400 bad request.I don't know if its the encoding of https response or something else, I have no idea what i am doing at this point and need help.
it is because https uses tls/ssl to encrypt the data.
I'm new to node.js, So how to send duration in response in http module i tried sending it through req.write and req.writeHead(), but its not working.Help me with this issue
var https = require('https');
const config_KEYS = require('./config.js');
exports.handler = (event, context, callback) => {
var userLat = event.userLat;
var userLong = event.userLong;
var destinationLat = event.destinationLat;
var destinationLong = event.destinationLong;
var params = {
host:'maps.googleapis.com',
path: '/maps/api/distancematrix/json?units=imperial&origins='+userLat+","+userLong+'&destinations='+destinationLat+","+destinationLong+'&key='+config_KEYS.GOOGLE_API_KEY+'&departure_time=now'
};
var req = https.request(params, function(res) {
let data = '';
console.log('STATUS: ' + res.statusCode);
// res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
console.log("DONE");
const parsedData = JSON.parse(data);
console.log("data ===>>>>",parsedData);
var duration = parsedData.rows[0].elements[0].duration_in_traffic.text;
var obj = {}
obj.duration = duration
res.end(duration) ;
});
});
req.write(callback)
req.end();
};
In node js https there is one method like res.end() to send data after https request ends
Example:
const https = require('https');
const fs = require('fs');
const options = {
pfx: fs.readFileSync('test/fixtures/test_cert.pfx'),
passphrase: 'sample'
};
https.createServer(options, (req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
Here what you want to achieve is use function in res.on('end', ) and then return to that function. So, In your case it will not send in res.on('end', ) because untimately you're returning a value to function not a method.
Here is the solution:
const parsedData = JSON.parse(data);
console.log("data ===>>>>",parsedData);
var duration = parsedData.rows[0].elements[0].duration_in_traffic.text;
req.end('duration');
one more way is you can use callback. For the reference I am providing one link
Callback https
I am using hapi v17.1 .I am not an expert programmer. I need to get the resolution of an image in hapi js for server side image validation .
I have tried image-size plugin
var sizeOf = require('image-size');
var { promisify } = require('util');
var url = require('url');
var https = require('http');
................
// my code
................
const host = 'http://' + request.info.host + '/';
imageName = host + path;
try {
var options = url.parse(imageName);
https.get(options, function (response) {
var chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
var buffer = Buffer.concat(chunks);
console.log("image height and width = ",sizeOf(buffer));
});
});
} catch (err) {
console.log('error occured = ', err);
}
image-size plugin
for http it is working fine but I cant do it for https
when I tried for https url and showing the error
error occured = TypeError: https.get is not a function
at handler (/home/jeslin/projects/hapi/gg-admin/app/controllers/web/advertisement.js:178:31)
at <anonymous>
how can I implement this for https image url
For https request you should require https module require('https'), Sample snippet to handle http & https request for your reference.
var sizeOf = require('image-size');
var https = require('https');
var http = require('http');
var url = require('url');
const host = 'http://picsum.photos/200/300';
const request = (host.indexOf('https') > -1) ? https : http;
try {
request.get(host, function (response) {
var chunks = [];
response.on('data', function (chunk) {
chunks.push(chunk);
}).on('end', function () {
var buffer = Buffer.concat(chunks);
console.log("image height and width = ",sizeOf(buffer));
});
});
} catch (err) {
console.log('error occured = ', err);
};
I need to able to balance websocket on application level. Lets say forward websocket request on the basis of message I received, decode it on proxy and then using that data send to another socket server using some logic.
But I am unable to do this. This is the initial code I wrote and trying to do that. This is the server
var http = require('http');
var httpProxy = require('http-proxy');
var WebSocket = require('ws');
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
var proxy = httpProxy.createServer({target: 'ws://localhost:8080', ws:true});
var server = httpProxy.createServer(function(req, res) {
//SOME LOGIC HERE TO PARSE WS DATA AND SEND TO WS SERVER
proxy.ws(req, res, { target: 'ws://localhost:8080'});
}).listen(8014);
CLIENT
var http = require('http');
var httpProxy = require('http-proxy');
var WebSocket = require('ws');
var ws = new WebSocket('ws://localhost:8014/');
ws.on('open', function () {
ws.send("CLIENT");
});
ws.on('message', function (msg) {
console.log(msg);
});
Here's an example. In this case the client connects directly to outer.js which then forwards connections to upstream servers (inner.js).
outer.js
var http = require('http');
var httpProxy = require('http-proxy');
var proxies = {
foo: new httpProxy.createProxyServer({
target: {
host: "foo.com",
port: 8080
}
}),
bar: new httpProxy.createProxyServer({
target: {
host: "bar.com",
port: 8080
}
})
// extend this...
};
var findUpstream = function(req){
// TODO return key for lookup in #proxies
};
var proxyServer = http.createServer(function (req, res){
var upstream = findUpstream(req);
proxies[upstream].web(req, res);
});
proxyServer.on('upgrade', function (req, socket, head) {
var upstream = findUpstream(req);
proxies[upstream].ws(req, socket, head);
});
proxyServer.listen(8014);
inner.js
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
In this example you'll need to fill in the findUpstream to return the key such as foo or bar based on data in the request. It'd also be worth putting some error handling in for when the proper upstream server isn't found, but this should illustrate the general idea.
I'm new to nodejs and I'm trying to solve communication issue with external system.
There is a gateway to external system which can handle websocket requests on port 5000. In the example below, when you request homepage, the nodejs opens websocket connection, then on websocket open event it sends request and waits for response which is used for the HTTP response.
Do you know how to open websocket to external system only once and handle requests based on request id?
var ws = require('ws');
var express = require('express');
var async = require('async');
var uuid = require('node-uuid');
app = express();
app.get('/', function (req, res) {
var webSocket = new ws('ws://localhost:5000/');
async.series([
function (callback) {
webSocket.on('open', function () {
webSocket.send(JSON.stringify({query:'data query', requestid: uuid.v4()}));
callback(null, 'data query');
});
},
function (callback) {
webSocket.on('message', function (data, flags) {
callback(null, data);
})
}
], function (err, results) {
res.setHeader('content-type', 'text/javascript');
res.send(results[1]);
webSocket.terminate();
});
});
var server = app.listen(3000, function () {
var port = server.address().port
console.log('Listening at %s', port)
});
Thanks for the hints. I ended with the following solution which does what I expect:
var ws = require('ws');
var express = require('express');
var uuid = require('node-uuid');
var requests = {};
app = express();
var webSocket = new ws('ws://localhost:5000/');
webSocket.on('open', function () {
console.log('Connected!');
});
webSocket.on('message', function (data, flags) {
var json = JSON.parse(data);
console.log(json.requestId);
var res = requests[json.requestId];
res.setHeader('content-type', 'text/javascript');
res.send(json.data);
delete requests[json.requestId];
});
app.get('/', function (req, res) {
var rid = uuid.v4();
requests[rid] = res;
webSocket.send(JSON.stringify({query:'data query', requestId: rid}));
});
var server = app.listen(3000, function () {
var port = server.address().port
console.log('Listening at %s', port)
});