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

Fix returning invalid ip address #3

Merged
merged 7 commits into from
Mar 5, 2018

Conversation

pedrocarrico
Copy link
Owner

This explicitly returns a PublipIp::Service::InvalidIpAddress exception when the extracted ip address is not a valid one.

@pedrocarrico
Copy link
Owner Author

@nhinds can you review?

@pedrocarrico pedrocarrico force-pushed the fix-returning-invalid-ip-address branch from 5437722 to 24f2b17 Compare April 21, 2017 11:46

ip_address = extract_ip(response)

unless IPV4_REGEX.match(ip_address)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About half of the services currently return my IPv6 address. This will now cause those services to fail instead of returning the correct address...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Services from PublicIp.list_services that consistently return my IPv6 address:

  • :smart_ip
  • :what_is_my_ip_address
  • :i_can_haz_ip
  • :ident_me
  • :wtf_is_my_ip

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nhinds That is interesting, I'll try to broaden the regex to allow both IPv4 and IPv6 addresses.

Thanks for the review 👍

@pedrocarrico pedrocarrico force-pushed the fix-returning-invalid-ip-address branch 3 times, most recently from 2c91231 to 90d5658 Compare March 3, 2018 17:32
@pedrocarrico
Copy link
Owner Author

@nhinds sorry to get back to you after so long hope you're still using the gem, can I get a re-review again before merging?

I'll bump gem version when this hits master.

@pedrocarrico pedrocarrico force-pushed the fix-returning-invalid-ip-address branch from 90d5658 to 683cd00 Compare March 3, 2018 19:40
@@ -12,7 +12,8 @@ def self.headers
end

def self.parse_ip_address(response_body)
Nokogiri::HTML(response_body).css('.ip div').text
doc = Nokogiri::HTML(response_body)
doc.at('h3:contains("Your Public IPv4 is: ")').text.strip.sub('Your Public IPv4 is: ', '')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the page load fails, this line now fails with "undefined method text' for nil:NilClass" because .atreturns either the matchingElementornil. This error is not caught by PublicIp.get_ip` so it gets thrown all the way to callers.

Previously it would return "", because .css(...) returns a NodeSet which always implements .text.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, that service returns "You have viewed this page 5 times. Your lookup limit has been reached." after calling it through PublicIp.get_ip a few times, which is probably why I'm getting errors here.

@nhinds
Copy link

nhinds commented Mar 4, 2018

Other than the new error coming from the what_is_my_ip service, PublicIp.get_ip now returns either my ipv4 or my ipv6 address. Which one it uses is pretty random, but it's consistently returning an IP address or throwing an error, which is better than before.

@pedrocarrico pedrocarrico force-pushed the fix-returning-invalid-ip-address branch from 683cd00 to ae8e6bc Compare March 4, 2018 21:43
@pedrocarrico
Copy link
Owner Author

@nhinds Ok, I managed to replicate the issue, I believe everything should be OK now.

In the future I might add some service specific exceptions but for now this should be fine.

@nhinds
Copy link

nhinds commented Mar 5, 2018

Thanks, everything is working from my machine now -- PublicIp.get_ip always returns one of my IPs, and if I ask for a specific service then I get one of the expected results: my IP, a timeout error, or a "not a valid IP" error.

Thanks for tackling this :)

@pedrocarrico pedrocarrico merged commit 3cfebcb into master Mar 5, 2018
@pedrocarrico pedrocarrico deleted the fix-returning-invalid-ip-address branch March 5, 2018 13:53
@pedrocarrico
Copy link
Owner Author

Thanks for the reviews @nhinds , pushed the gem with these changes.
If you need anything else feel free to ping or if you need any other feature.

https://rubygems.org/gems/public_ip/versions/0.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants