sindresorhus / public-ip

Get your public IP address - very fast!
MIT License
1.02k stars 65 forks source link

Add method to get either IPv6 or IPv4 #45

Closed subins2000 closed 2 years ago

subins2000 commented 4 years ago

Currently, in the browser version if v6 function is called and the API request returns a v4 version, then an error is made and we can't get the IP.

It'd be good if there's a single function to return IPV6/IPV4, if v6 is available :

console.log(await publicIp.get());
//=> '46.5.21.123' or v6 if available (preferred)
sindresorhus commented 4 years ago

If it returns v4 even though you requested v6, it's a bug and should be properly fixed.

sindresorhus commented 4 years ago

It'd be good if there's a single function to return IPV6/IPV4, if v6 is available :

You can do this yourself with https://github.com/sindresorhus/p-any and eventually Promise.any

subins2000 commented 4 years ago

My ISP is only assigning me a IPv4 address. So, it is kinda valid that https://api6.ipify.org/ is giving me v4 result.

I'm interested in submitting a patch to add a generic function: IPV6 first, if no then IPV4 which can be done with Cloudflare's API and many others. Would you be interested in your library having such a function ?

sindresorhus commented 4 years ago

Sure. It should be another method with a descriptive name. Any suggestions for the name?

subins2000 commented 4 years ago

How about simply get ? That'd go good with the module name :

const publicIp = require('public-ip');
(async () => {
    console.log(await publicIp.get());
    //=> IPv6, if not IPv4
})();
sindresorhus commented 4 years ago

I don't think that's clear enough. If you were reading that code without having read the public-ip readme, would you know that it would return either IPv6 or IPv4?

subins2000 commented 4 years ago

For someone who has an application where it doesn't matter if it's v6 or v4 can just use it right ? If they really wanted the specifics, they have 2 perfectly named functions for it.

The ambiguity in just the name get will also help the dev who cares what the output should be read the README :sweat_smile:

sindresorhus commented 4 years ago

How about .v6or4()? Not super pretty, but I think it's nice that it's very descriptive what it does.

subins2000 commented 4 years ago

Ah, that's a better name. I shall work on this.

sindresorhus commented 3 years ago

If anyone wants to work on this, see https://github.com/sindresorhus/public-ip/pull/46 and the feedback given there.

sindresorhus commented 2 years ago

I have changed my mind. It can just be await publicIp() to get either IPv6 or IPv4, with IPv6 being preferred.

Octalbyte commented 2 years ago

@sindresorhus I'm making a draft for this proposal and I realized it would be fairly easy to implement a synchronous function that sets a timeout for IPv6 and then falls to IPv4. Of course, we could return a Promise to turn this back into asynchronous, but the timeout would still be there. Would this be good enough?

Octalbyte commented 2 years ago

@sindresorhus P.S: how long do you think the default timeout should be?

fisker commented 2 years ago

https://api64.ipify.org