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

Check for bogon IPs without API call & give more info #34

Merged
merged 4 commits into from
Oct 12, 2022

Conversation

talhahwahla
Copy link
Contributor

@talhahwahla talhahwahla commented Oct 3, 2022

Resolves #27

@linear
Copy link

linear bot commented Oct 3, 2022

Copy link
Contributor

@UmanShahzad UmanShahzad left a comment

Choose a reason for hiding this comment

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

Great start. Some feedback that may require quite a few changes:

  • We don't need bogonType, it's not even in our API actually and usually not valuable information.
  • Doing that means you don't need to be parsing with gson as you added - you can simply return a boolean from some isBogon helper, and if true, just create a blank IPResponse with only ip and bogon set, and everything else shall default to empty strings or similar.
  • The list of strings containing bogon ranges - can we make that not within the function itself? Likely the JVM will optimize this away but we can help it a bit by making it always available globally as a static variable (or however it's done in Java).
  • If bogon type doesn't matter, just create a single "bogon" string list, with comments in the code to separate them out. See for example our CLI that does a similar thing - https://github.com/ipinfo/cli/blob/master/lib/bogon.go
  • As a further optimization, you should probably not even make it a 'string list', but rather a 'IpAddressMatcher list', because then you don't have to re-initialize a separate 'IpAddressMatcher' for every single item in the list each time you run the bogon check function, that's lots of extra computations for no reason.
  • Just to confirm, matches just checks to see if the input IP address is 'within' the CIDR right? Like cidr_start <= ip <= cidr_end is its effective operation, correct? I'm a bit confused when skimming its logic because usually to check this, in the CLI I simply converted those 3 variables in the equation into numbers (64-bit for IPv4, 128-bit for IPv6 using a home-grown 128-bit library in https://github.com/ipinfo/cli/blob/master/lib/u128.go), and the comparison is then purely numerical. But this is doing something else, or is it?
  • It works fine with IPv6 bogons correct?

@talhahwahla
Copy link
Contributor Author

talhahwahla commented Oct 11, 2022

Yes, essentially it is checking if the IP is within the CIDR. It does so by seeing if the network bits of the bogon range and the corresponding bits of the IP Address match exactly.

Works fine for IPv6:

IPResponse response = ipInfo.lookupIP("2001:0:c000:200::0:255:1");
System.out.println(response); // IPResponse{ip='2001:0:c000:200::0:255:1', bogon='true'}

Copy link
Contributor

@UmanShahzad UmanShahzad left a comment

Choose a reason for hiding this comment

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

Looks good but can we just write a test case or two? If all works let's merge.

@UmanShahzad UmanShahzad merged commit 964a526 into master Oct 12, 2022
@UmanShahzad UmanShahzad deleted the talha/be-578 branch October 12, 2022 01:34
@Qer75
Copy link

Qer75 commented Oct 12, 2022 via email

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.

Check for bogon IPs without API call & give more info
3 participants