Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

public-ip

Package Overview
Dependencies
Maintainers
2
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

public-ip - npm Package Compare versions

Comparing version 2.0.1 to 2.1.0

69

index.js
'use strict';
const dgram = require('dgram');
const dns = require('dns-socket');
const got = require('got');
const pify = require('pify');
const defaults = {
timeout: 5000,
https: false
};
const type = {
v4: {
server: '208.67.222.222',
question: {
dnsServer: '208.67.222.222',
dnsQuestion: {
name: 'myip.opendns.com',
type: 'A'
}
},
httpsUrl: 'https://ipv4.icanhazip.com/'
},
v6: {
server: '2620:0:ccc::2',
question: {
dnsServer: '2620:0:ccc::2',
dnsQuestion: {
name: 'myip.opendns.com',
type: 'AAAA'
}
},
httpsUrl: 'https://ipv6.icanhazip.com/'
}
};
const query = version => {
const queryDns = (version, opts) => {
const data = type[version];
const socket = dns({
socket: dgram.createSocket(version === 'v6' ? 'udp6' : 'udp4')
retries: 0,
socket: dgram.createSocket(version === 'v6' ? 'udp6' : 'udp4'),
timeout: opts.timeout
});
return pify(socket.query.bind(socket))({
questions: [data.question]
}, 53, data.server).then(res => {
questions: [data.dnsQuestion]
}, 53, data.dnsServer).then(res => {
const ip = res.answers[0] && res.answers[0].data;

@@ -44,3 +54,38 @@ socket.destroy();

module.exports.v4 = () => query('v4');
module.exports.v6 = () => query('v6');
const queryHttps = (version, opts) => {
const gotOpts = {
family: (version === 'v6') ? 6 : 4,
retries: 0,
timeout: opts.timeout
};
return got(type[version].httpsUrl, gotOpts).then(res => {
const ip = (res.body || '').trim();
if (!ip) {
throw new Error('Couldn\'t find your IP');
}
return ip;
});
};
module.exports.v4 = opts => {
opts = Object.assign({}, defaults, opts);
if (opts.https) {
return queryHttps('v4', opts);
}
return queryDns('v4', opts);
};
module.exports.v6 = opts => {
opts = Object.assign({}, defaults, opts);
if (opts.https) {
return queryHttps('v6', opts);
}
return queryDns('v6', opts);
};

10

package.json
{
"name": "public-ip",
"version": "2.0.1",
"version": "2.1.0",
"description": "Get your public IP address - very fast!",

@@ -35,3 +35,4 @@ "license": "MIT",

"dependencies": {
"dns-socket": "^1.0.0",
"dns-socket": "^1.6.1",
"got": "^6.7.1",
"pify": "^2.3.0"

@@ -41,8 +42,5 @@ },

"ava": "*",
"is-ip": "^1.0.0",
"is-ip": "^2.0.0",
"xo": "*"
},
"xo": {
"esnext": true
}
}

@@ -34,11 +34,28 @@ # public-ip [![Build Status](https://travis-ci.org/sindresorhus/public-ip.svg?branch=master)](https://travis-ci.org/sindresorhus/public-ip)

### publicIp.v4()
### publicIp.v4([options])
Returns a Promise for your public IPv4 address.
Returns a `Promise` for your public IPv4 address.
### publicIp.v6()
### publicIp.v6([options])
Returns a Promise for your public IPv6 address.
Returns a `Promise` for your public IPv6 address.
#### options
Type: `Object`
##### https
Type: `boolean`<br>
Default: `false`
Use a HTTPS check using the [icanhazip.com](https://github.com/major/icanhaz) service instead of the DNS query. This check is much more secure and tamper-proof, but also a lot slower.
##### timeout
Type: `number`<br>
Default: `5000`
The time in milliseconds until a request is considered timed out.
## Related

@@ -45,0 +62,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc