http-party / node-portfinder

A simple tool to find an open port or domain socket on the current machine
https://github.com/http-party/node-portfinder
Other
887 stars 95 forks source link

Encountered an error when host [::] #52

Closed wenner closed 7 years ago

wenner commented 7 years ago

When i run the code , i get the result is 8080;

portfinder.basePort = 8080; //set basePort = 8080
portfinder.getPort(function(err , port){
    console.log(port);
   //return 8080
})

here is log

in eachSeries() iteration callback: host is 0.0.0.0
in eachSeries() iteration callback: host is 0.0.0.0
entered testPort(): trying 0.0.0.0 port 8080
done w/ testPort(): OK 0.0.0.0 port 8080
in eachSeries() iteration callback testPort() callback with a success for port 8080
in eachSeries() iteration callback: host is fe80::15d0:1e6b:e232:f2b3
in eachSeries() iteration callback: host is fe80::15d0:1e6b:e232:f2b3
entered testPort(): trying fe80::15d0:1e6b:e232:f2b3 port 8080
done w/ testPort(): OK fe80::15d0:1e6b:e232:f2b3 port 8080
in eachSeries() iteration callback testPort() callback with a success for port 8080
in eachSeries() iteration callback: host is 10.1.113.89
in eachSeries() iteration callback: host is 10.1.113.89
entered testPort(): trying 10.1.113.89 port 8080
done w/ testPort(): OK 10.1.113.89 port 8080
in eachSeries() iteration callback testPort() callback with a success for port 8080
in eachSeries() iteration callback: host is ::1
in eachSeries() iteration callback: host is ::1
entered testPort(): trying ::1 port 8080
done w/ testPort(): OK ::1 port 8080
in eachSeries() iteration callback testPort() callback with a success for port 8080
in eachSeries() iteration callback: host is 127.0.0.1
in eachSeries() iteration callback: host is 127.0.0.1
entered testPort(): trying 127.0.0.1 port 8080
done w/ testPort(): OK 127.0.0.1 port 8080
in eachSeries() iteration callback testPort() callback with a success for port 8080
in eachSeries() result callback: openPorts is [ 8080, 8080, 8080, 8080, 8080 ]

Here is my system net information

>node
> os.networkInterfaces()
{ '以太网':
   [ { address: 'fe80::15d0:1e6b:e232:f2b3',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: '08:9e:01:d0:95:14',
       scopeid: 17,
       internal: false },
     { address: '10.1.113.89',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: '08:9e:01:d0:95:14',
       internal: false } ],
  'Loopback Pseudo-Interface 1':
   [ { address: '::1',
       netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
       family: 'IPv6',
       mac: '00:00:00:00:00:00',
       scopeid: 0,
       internal: true },
     { address: '127.0.0.1',
       netmask: '255.0.0.0',
       family: 'IPv4',
       mac: '00:00:00:00:00:00',
       internal: true } ] }

but the 8080 Port is occupied

PS D:\> netstat -ano | findstr "8080"
  TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       54108
  TCP    [::]:8080              [::]:0                 LISTENING       54108
  TCP    [::1]:8080             [::1]:51001            ESTABLISHED     54108
  TCP    [::1]:8080             [::1]:51544            ESTABLISHED     54108
  TCP    [::1]:51001            [::1]:8080             ESTABLISHED     11256
  TCP    [::1]:51544            [::1]:8080             ESTABLISHED     11256

and there have a host [::] , then i change the code portfinder.js

//line 444 , insert "::" to the array
      results = [hiddenButImportantHost , '::'];

it's OK and Get the right results I want 8081

i don't know why , but i think it's the ipv6 address of '0.0.0.0'

      hiddenButImportantHost = '0.0.0.0', // !important - dont remove, hence the naming :)

and how fix it?

wenner commented 7 years ago

OK , in my code , puth "::" to the _defaultHosts , it's work fine

portfinder._defaultHosts.push("::") //push the host in myself code 
portfinder.basePort = 8080; //set basePort = 8080
portfinder.getPort(function(err , port){
    console.log(port);
})
eriktrom commented 7 years ago

Interesting. What version of windows are u using? Thanks for any os info u can provide.

I'll check this out. I need to make a release soon and will try to handle this case. Thanks for reporting the issue.

wenner commented 7 years ago

my OSinfo : Microsoft Windows [版本 10.0.14393]

eriktrom commented 7 years ago

@wenner - i have to thank u - seriously - :: might just be the the host needed to fix windows 10 support, which has been lingering as #39 for a few months now - I had a rather creative hack in mind but honestly didn't understand why windows was allowing rebinding and held off (procrastinated u could say too) b/c it just didn't make sense that they would allow rebinding to an interface with the same host + port

:: though - like u mention - is the new 0.0.0.0 - but for ipv6 - which means that while it doesn't show up when running os.networkInterfaces() it is nonetheless a catchall non-loopback host that can be bound too and just perhaps answers why rebinding in windows 10 was seeming to occur (when, as I suspected all along, it really wasn't)

If ur interested in contributing - I'll accept adding :: to _defaultHosts array - I can test on my mac and linux that this won't break POSIX based systems.

Let me know ur thoughts - my comment here is just preliminary and needs vetting but if it has any potential to lead to a clean solution, I'm willing to explore it.

Appreciate ur help :)

wenner commented 7 years ago

Thank you for your answer