Skip to content
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
25 changes: 23 additions & 2 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,28 @@ function checkPortStatus (port) {
// Return after the socket has closed
socket.on('close', function (exception) {
if (exception && !connectionRefused) { error = error || exception } else { error = null }
callback(error, status)

// For windows localhost, we should check if the port is in excluded range by actually binding to it
if ((host === '127.0.0.1' || host === 'localhost') && process.platform === 'win32' && status === 'closed') {
var server = net.createServer()
server.on('error', function (e) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not handle it up above @ line 120 socket.on('error',, instead of creating a new server again?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket above @ line 120 is a client socket, and this is a server socket.
We must bind a server socket to detect 'excluded ports' in Windows. (Just trying to connect to the excluded port range doesn't throw EACCES error)

if (e.code === 'EACCES') {
status = 'reserved'
error = null
}
server.close()
})
// on close, we should call the callback
server.on('close', function () {
callback(error, status)
})
server.listen(port, host, function () {
server.close()
})

} else {
callback(error, status)
}
})

socket.connect(port, host)
Expand All @@ -138,7 +159,7 @@ function checkPortStatus (port) {
* Callback for {@link checkPortStatus}
* @callback checkPortCallback
* @param {Error|null} error - Any error that occurred while port scanning, or null.
* @param {String} status - Status: 'open' if the port is in use, 'closed' if the port is available.
* @param {String} status - Status: 'open' if the port is in use, 'closed' if the port is available, or 'reserved' if the port is reserved (Windows only).
*/

/**
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import net from 'net'
import { execSync } from 'child_process'
import test from 'ava'
import portScanner from '.'

Expand All @@ -9,6 +10,7 @@ findPortNotInUse()
promise()
reverseOrder()
portsAsStrings()
findExcludedPorts()

function initialize () {
test.before.cb('Set #1 test server', t => {
Expand Down Expand Up @@ -487,3 +489,37 @@ function portsAsStrings () {
})
})
}

function findExcludedPorts () {
if (process.platform !== 'win32') return
const output = execSync(
'netsh interface ipv4 show excludedportrange protocol=tcp'
).toString()
const ports = output
.split('\n')
.filter((line) => line.match(/^ +[0-9]+ +[0-9]+/))
.map((line) => line.match(/ +([0-9]+) +([0-9]+)/).slice(1, 3))
console.log(`ports:`, ports)
const portsToCheck = []
for (const [startPort, endPort] of ports) {
for (
let i = Number(startPort);
i <= Math.min(Number(startPort) + 2, endPort);
i++
) {
portsToCheck.push(i)
}
}
if (!portsToCheck.length) return
console.log(`portsToCheck:`, portsToCheck)
test.cb('Test reserved ports on Windows', (t) => {
t.plan(portsToCheck.length)
for (const port of portsToCheck) {
console.log(`testing:`, port)
portScanner.checkPortStatus(port, function (error, status) {
console.log(port, { error, status })
t.is(status, 'reserved')
})
}
})
}