Skip to content
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

Closed
blue-cp opened this issue Aug 11, 2017 · 12 comments · Fixed by #827
Closed

How to get IP address of the final destination host #356

blue-cp opened this issue Aug 11, 2017 · 12 comments · Fixed by #827
Labels
enhancement This change will extend Got features 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt

Comments

@blue-cp
Copy link

blue-cp commented Aug 11, 2017

Issuehunt badges

Hi,

How can I get the ip address of the final host after redirection?

Thanks,


IssueHunt Summary

cerberooo cerberooo has been rewarded.

Backers (Total: $40.00)

Submitted pull Requests


Tips


IssueHunt has been backed by the following sponsors. Become a sponsor

@sindresorhus
Copy link
Owner

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);
})();

@Zauberbutter
Copy link
Contributor

@sindresorhus With native NodeJS ({ get } = require('http')) the IP is stored in the response object response.connection.remoteAddress. Is it possible to implement that also in Got, so no extra step is needed?

@sindresorhus
Copy link
Owner

@Cerberooo Can you show a code example of how it would look like and how you would use it?

@sindresorhus sindresorhus reopened this Jun 23, 2019
@Zauberbutter
Copy link
Contributor

Zauberbutter commented Jun 23, 2019

@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
	}
})();

@sindresorhus
Copy link
Owner

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 .ip, .ipAddress, or remoteAddress?

@szmarczak Thoughts?

@Zauberbutter
Copy link
Contributor

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.

@szmarczak
Copy link
Collaborator

.ip or .ipAddress seems too general. I don't know if it's a local or remote IP. So either .remoteIP or .remoteAddress... Anyway I'm good with anything, it just needs to be documented. Let's choose one.

@sindresorhus
Copy link
Owner

But it’s on the “response”. Doesn’t that make it clear it’s the remote IP?

@szmarczak
Copy link
Collaborator

Yeah, it does. So maybe let's go with just .ip?

@sindresorhus
Copy link
Owner

👍

@sindresorhus sindresorhus added the enhancement This change will extend Got features label Jun 23, 2019
@issuehunt-oss issuehunt-oss bot added the 💵 Funded on Issuehunt The issue has been funded on Issuehunt label Jul 1, 2019
@issuehunt-oss
Copy link

issuehunt-oss bot commented Jul 1, 2019

@issuehunt has funded $40.00 to this issue.


@issuehunt-oss
Copy link

issuehunt-oss bot commented Oct 3, 2019

@sindresorhus has rewarded $36.00 to @cerberooo. See it on IssueHunt

  • 💰 Total deposit: $40.00
  • 🎉 Repository reward(0%): $0.00
  • 🔧 Service fee(10%): $4.00

@issuehunt-oss issuehunt-oss bot added 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt and removed 💵 Funded on Issuehunt The issue has been funded on Issuehunt labels Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This change will extend Got features 🎁 Rewarded on Issuehunt This issue has been rewarded on Issuehunt
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants