Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to reject connection with ECONNREFUSED #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ Mitm.prototype.connect = function connect(orig, Socket, opts, done) {
writable: true
}, opts))

this.emit("connect", client, opts)
let clientConnectError = false;
try {
this.emit("connect", client, opts)
} catch (error) {
clientConnectError = error;
}

if (client.bypassed) return orig.call(this, opts, done)

// Don't use just "server" because socket.server is used in Node v8.12 and
Expand All @@ -108,11 +114,19 @@ Mitm.prototype.connect = function connect(orig, Socket, opts, done) {

this.emit("connection", server, opts)

// Ensure connect is emitted in next ticks, otherwise it would be impossible
// to listen to it after calling Net.connect or listening to it after the
// Ensure events are emitted in next ticks, otherwise it would be impossible
// to listen to them after calling Net.connect or listening to it after the
// ClientRequest emits "socket".
setTimeout(client.emit.bind(client, "connect"))
setTimeout(server.emit.bind(server, "connect"))
if (clientConnectError !== false) {
setTimeout(client.emit.bind(client, "error", clientConnectError))
setTimeout(server.emit.bind(server, "close", true))
} else {
// If client "fails to connect" dont bother emitting connect event on
// client or server
setTimeout(client.emit.bind(client, "connect"))
setTimeout(server.emit.bind(server, "connect"))
}


return client
}
Expand Down
Loading