New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get IP address of the final destination host #356
Comments
Got returns the final URL in the response object, so you can use that to resolve it to an IP-address. Untested, but something like this should do it: const util = require('util');
const dns = require('dns');
const got = require('got');
(async () => {
const response = await got('sindresorhus.com');
const ip = await util.promisify(dns.lookup)(new URL(response.url).hostname, {family: 4}).address;
console.log(ip);
})(); |
@sindresorhus With native NodeJS ( |
@Cerberooo Can you show a code example of how it would look like and how you would use it? |
@sindresorhus I use it in a link checker tool to check only links from a specific IP range. Unfortunately I have to make two requests (when I want to use Got, request, ...) to get the IP address and the html of a link. Edit: Revised code because I misunderstood your question... Example usage: const got = require('got');
(async () => {
try {
const response = await got('https://sindresorhus.com');
console.log(response.body); //=> '<!doctype html> ...'
console.log(response.connection.remoteAdress); //=> 104.28.26.119
} catch (error) {
console.log(error.response.body); //=> 'Internal server error ...'
console.log(error.connection.remoteAdress); //=> 104.28.26.119, if there is an connection there is an ip
}
})(); |
And you want this, correct? const got = require('got');
(async () => {
try {
const response = await got('https://sindresorhus.com');
console.log(response.body); //=> '<!doctype html> ...'
console.log(response.ip); //=> 104.28.26.119
} catch (error) {
const {response} = error;
console.log(response.body); //=> 'Internal server error ...'
console.log(response.ip); //=> 104.28.26.119
}
})(); Should it be @szmarczak Thoughts? |
Yep, revised code because I misunderstood your question. In Got there is the connection object too. So I think its good when the ip is there, because when there is no connection there cant be an ip. |
|
But it’s on the “response”. Doesn’t that make it clear it’s the remote IP? |
Yeah, it does. So maybe let's go with just |
👍 |
@issuehunt has funded $40.00 to this issue.
|
@sindresorhus has rewarded $36.00 to @cerberooo. See it on IssueHunt
|
Hi,
How can I get the ip address of the final host after redirection?
Thanks,
IssueHunt Summary
cerberooo has been rewarded.
Backers (Total: $40.00)
Submitted pull Requests
Tips
IssueHunt has been backed by the following sponsors. Become a sponsor
The text was updated successfully, but these errors were encountered: