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); | ||
}; |
{ | ||
"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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4824
73
69
3
3
+ Addedgot@^6.7.1
+ Addedcapture-stack-trace@1.0.2(transitive)
+ Addedcreate-error-class@3.0.2(transitive)
+ Addedduplexer3@0.1.5(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedgot@6.7.1(transitive)
+ Addedis-redirect@1.0.0(transitive)
+ Addedis-retry-allowed@1.2.0(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedlowercase-keys@1.0.1(transitive)
+ Addedprepend-http@1.0.4(transitive)
+ Addedtimed-out@4.0.1(transitive)
+ Addedunzip-response@2.0.1(transitive)
+ Addedurl-parse-lax@1.0.0(transitive)
Updateddns-socket@^1.6.1