Skip to content

Commit 9f9f8b9

Browse files
use named socket
1 parent 77c8aeb commit 9f9f8b9

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ Starts the proxy listening on the given port.
111111
__Arguments__
112112

113113
* options - An object with the following options:
114-
* port - The port to listen on (default: 8080).
114+
* port - The port or named socket to listen on (default: 8080).
115115
* sslCaDir - Path to the certificates cache directory (default: process.cwd() + '/.http-mitm-proxy')
116116
* silent - if set to true, nothing will be written to console (default: false)
117117
* timeout - The number of milliseconds of inactivity before a socket is presumed to have timed out. Defaults to no timeout.
118118
* httpAgent - The [http.Agent](https://nodejs.org/api/http.html#http_class_http_agent) to use when making http requests. Useful for chaining proxys. (default: internal Agent)
119119
* httpsAgent - The [https.Agent](https://nodejs.org/api/https.html#https_class_https_agent) to use when making https requests. Useful for chaining proxys. (default: internal Agent)
120120
* forceSNI - force use of [SNI](https://en.wikipedia.org/wiki/Server_Name_Indication) by the client. Allow node-http-mitm-proxy to handle all HTTPS requests with a single internal server.
121+
* httpsPort - The port or named socket for https server to listen on. _(forceSNI must be enabled)_
122+
* useNamedSocket - use named socket (i.e. unix socket or named pipe) instead of TCP ports for internal server(s)
121123

122124
__Example__
123125

lib/proxy.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var events = require('events');
1111
var mkdirps = require('mkdirps');
1212
var WebSocket = require('ws');
1313
var url = require('url');
14+
var os = require('os');
1415
var semaphore = require('semaphore');
1516
var ca = require('./ca.js');
1617

@@ -53,6 +54,8 @@ Proxy.prototype.listen = function(options, callback) {
5354
if (this.forceSNI && !this.silent) {
5455
console.log('SNI enabled. Clients not supporting SNI may fail');
5556
}
57+
this.useNamedSocket = !!options.useNamedSocket;
58+
this.httpsPort = this.forceSNI ? options.httpsPort : undefined;
5659
this.sslCaDir = options.sslCaDir || path.resolve(process.cwd(), '.http-mitm-proxy');
5760
this.ca = new ca(this.sslCaDir);
5861
this.sslServers = {};
@@ -70,10 +73,13 @@ Proxy.prototype.listen = function(options, callback) {
7073
self.wsServer.on('connection', self._onWebSocketServerConnect.bind(self, false));
7174
if (self.forceSNI) {
7275
// start the single HTTPS server now
73-
self._createHttpsServer({}, function(httpsServer, wssServer) {
76+
self._createHttpsServer({}, function(portOrSocket, httpsServer, wssServer) {
77+
if (!self.silent) {
78+
console.log('https server started on '+portOrSocket);
79+
}
7480
self.httpsServer = httpsServer;
7581
self.wssServer = wssServer;
76-
self.httpsPort = httpsServer.address().port;
82+
self.httpsPort = portOrSocket;
7783
self.httpServer.listen(self.httpPort, callback);
7884
});
7985
} else {
@@ -93,8 +99,9 @@ Proxy.prototype._createHttpsServer = function (options, callback) {
9399
httpsServer.on('request', this._onHttpServerRequest.bind(this, true));
94100
var wssServer = new WebSocket.Server({ server: httpsServer });
95101
wssServer.on('connection', this._onWebSocketServerConnect.bind(this, true));
96-
httpsServer.listen(function() {
97-
if (callback) callback(httpsServer, wssServer);
102+
var portOrSocket = this.httpsPort || (this.useNamedSocket ? Proxy.newNamedSocket() : undefined);
103+
httpsServer.listen(portOrSocket, function() {
104+
if (callback) callback(portOrSocket || httpsServer.address().port, httpsServer, wssServer);
98105
});
99106
};
100107

@@ -312,9 +319,9 @@ Proxy.prototype._onHttpServerConnect = function(req, socket, head) {
312319
return makeConnection(this.httpPort);
313320
}
314321

315-
function makeConnection(port) {
322+
function makeConnection(portOrSocket) {
316323
// open a TCP connection to the remote host
317-
var conn = net.connect(port, 'localhost', function() {
324+
var conn = net.connect(portOrSocket, function() {
318325
// create a tunnel between the two hosts
319326
socket.pipe(conn);
320327
conn.pipe(socket);
@@ -403,20 +410,19 @@ Proxy.prototype._onHttpServerConnect = function(req, socket, head) {
403410
if (!self.silent) {
404411
console.log('starting server for ' + hostname);
405412
}
406-
self._createHttpsServer(results.httpsOptions, function(httpsServer, wssServer) {
407-
var openPort = httpsServer.address().port;
413+
self._createHttpsServer(results.httpsOptions, function(portOrSocket, httpsServer, wssServer) {
408414
if (!self.silent) {
409-
console.log('server started for %s on port %d', hostname, openPort);
415+
console.log('https server started for %s on %s', hostname, portOrSocket);
410416
}
411417
var sslServer = {
412418
server: httpsServer,
413419
wsServer: wssServer,
414-
port: openPort
420+
port: portOrSocket
415421
};
416422
hosts.forEach(function(host) {
417423
self.sslServers[hostname] = sslServer;
418424
});
419-
return callback(null, openPort);
425+
return callback(null, portOrSocket);
420426
});
421427
}
422428
});
@@ -1010,3 +1016,14 @@ Proxy.filterAndCanonizeHeaders = function(originalHeaders) {
10101016
}
10111017
return headers;
10121018
};
1019+
1020+
Proxy.NAMED_SOCKET_PREFIX = 'mitm-proxy-' + Math.random().toString(36).substring(2, 10);
1021+
Proxy.NAMED_SOCKET_INDEX = 0;
1022+
Proxy.newNamedSocket = function() {
1023+
var socketName = Proxy.NAMED_SOCKET_PREFIX + "-" + Proxy.NAMED_SOCKET_INDEX++;
1024+
if (/^win/.test(process.platform)) {
1025+
return '\\\\.\\pipe\\' + socketName + '-sock';
1026+
} else {
1027+
return path.join(os.tmpdir(), socketName+".sock")
1028+
}
1029+
};

0 commit comments

Comments
 (0)