unjs / get-port-please

🔌 Get an available open port
MIT License
255 stars 13 forks source link

Exclude `169.254.*` range from host checks #97

Open vaecebyZ opened 5 months ago

vaecebyZ commented 5 months ago

Environment

get-port-please:3.1.2 os: Windwos11 23H2 22631.3593 node: 20.12.2 pnpm: 9.1.1 vite: 5.2.7

Reproduction

just fixed. now it works.

Describe the bug

today i start my progarm. up faild by "Unable to find a random port on any host" , then I find function 'checkPort()' return false. in function the _getLocalHosts() get array host but host[0] = undefined, so _tryPort() will get one false , then the false value will be return and get checkPort() fail .

I commented out this line.now its work (temporarily)🤣🤣🤣 image

thanks.

Additional context

this is console value: ` if (!host) { host = _getLocalHosts([void 0, "0.0.0.0"]); console.log(host) } //[ undefined, '0.0.0.0', '192.168.0.5', '192.168.56.1', '192.168.150.2', '169.254.158.97', '192.168.79.1', '10.144.103.31', '192.168.60.1' ]

=====================

for (const _host of host) { const _port = await _tryPort(port, _host); console.log("_port",_port) .... }

//_port 3070 _port 3070 _port 3070 _port 3070 _port 3070 _port false `

Logs

error when starting dev server:
GetPortError: Unable to find a random port on any host
    at getRandomPort (file:///D:/code/fcadmin/node_modules/.pnpm/get-port-please@3.1.2/node_modules/get-port-please/dist/index.mjs:337:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async detectPort (file:///D:/code/fcadmin/node_modules/.pnpm/unplugin-turbo-console@1.8.6_esbuild@0.20.2_rollup@4.17.2_vite@5.2.11_@types+node@20.12.12_sa_ippzb4y57jtfn5cquesa6lt3mq/node_modules/unplugin-turbo-console/dist/chunk-CSBEKKJ2.js:554:22)
    at async configureServer (file:///D:/code/fcadmin/node_modules/.pnpm/unplugin-turbo-console@1.8.6_esbuild@0.20.2_rollup@4.17.2_vite@5.2.11_@types+node@20.12.12_sa_ippzb4y57jtfn5cquesa6lt3mq/node_modules/unplugin-turbo-console/dist/chunk-CSBEKKJ2.js:582:11)
    at async _createServer (file:///D:/code/fcadmin/node_modules/.pnpm/vite@5.2.11_@types+node@20.12.12_sass@1.77.2_terser@5.31.0/node_modules/vite/dist/node/chunks/dep-cNe07EU9.js:65014:24)
    at async CAC.<anonymous> (file:///D:/code/fcadmin/node_modules/.pnpm/vite@5.2.11_@types+node@20.12.12_sass@1.77.2_terser@5.31.0/node_modules/vite/dist/node/cli.js:762:24)
 ELIFECYCLE  Command failed with exit code 1.
pi0 commented 5 months ago

Hi @vaecebyZ thanks for the investigation. The undefined host trial is to check the operating system's default host (which is not predictable easily).

If you try _tryPort(0, undefined) on your machine would it return false?

vaecebyZ commented 5 months ago

Hi @vaecebyZ thanks for the investigation. The undefined host trial is to check the operating system's default host (which is not predictable easily).

If you try _tryPort(0, undefined) on your machine would it return false?

I try _tryPort(0, undefined) it not return false,but for of host one of them returns false image

now i find get false ip and port: find reason,my nic dhcp fail ,I disabled this nic now it works🤣🤣🤣🤣 image

pi0 commented 5 months ago

Hmm interesting. I think we have to exclude 169.254.* IP (it is invalid IP) from hosts reported by operating system (funny enough, windows returns them unchecked)