Skip to content

Commit 72970bd

Browse files
committed
add tls logger example
1 parent e5e6671 commit 72970bd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

examples/tls-logger/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var proxy = require("node-tcp-proxy");
2+
var util = require("util");
3+
var replace = require('buffer-replace');
4+
5+
var serviceHosts = ["www.google.com", "www.google.com"];
6+
var servicePorts = [443, 443];
7+
8+
var newProxy = proxy.createProxy(8080, serviceHosts, servicePorts, {
9+
tls: "both",
10+
upstream: function(context, data) {
11+
log(context.proxySocket, data);
12+
data = replace(data, "localhost:8080", "www.google.com");
13+
return data;
14+
},
15+
downstream: function(context, data) {
16+
log(context.serviceSocket, data);
17+
data = replace(data, "www.google.com", "localhost:8080");
18+
return data;
19+
},
20+
serviceHostSelected: function(proxySocket, i) {
21+
console.log(util.format("Service host %s:%s selected for client %s:%s.",
22+
serviceHosts[i],
23+
servicePorts[i],
24+
proxySocket.remoteAddress,
25+
proxySocket.remotePort));
26+
// use your own strategy to calculate i
27+
return i;
28+
}
29+
});
30+
31+
function log(socket, data) {
32+
console.log(util.format("%s:%s sent:",
33+
socket.remoteAddress,
34+
socket.remotePort));
35+
console.log(data.toString('hex'));
36+
}
37+
38+
console.log("Open https://localhost:8080 in the browser.");
39+
40+
console.log("press Enter key to quit...");
41+
setTimeout(handleTimeout, 1000);
42+
function handleTimeout() {
43+
var data = process.stdin.read(1);
44+
if (data) {
45+
console.log("bye.");
46+
newProxy.end();
47+
process.exit(0);
48+
}
49+
setTimeout(handleTimeout, 1000);
50+
}

0 commit comments

Comments
 (0)