Giter Site home page Giter Site logo

ipinfo / java Goto Github PK

View Code? Open in Web Editor NEW
99.0 22.0 99.0 405 KB

Official Java library for IPinfo API (IP geolocation and other types of IP data)

Home Page: https://ipinfo.io

License: Apache License 2.0

Java 99.09% Shell 0.91%
api java ipinfo ip-geolocation ip-data ip-address

java's Introduction

IPinfo IPinfo Java Client Library

License

This is the official Java client library for the IPinfo.io IP address API, allowing you to look up your own IP address, or get any of the following details for an IP:

  • IP geolocation data (city, region, country, postal code, latitude, and longitude)
  • ASN information (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
  • Company data (the name and domain of the business that uses the IP address)
  • Carrier details (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)

Check all the data we have for your IP address here.

Getting Started

You'll need an IPinfo API access token, which you can get by signing up for a free account at https://ipinfo.io/signup.

The free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as IP type and company data. To enable all the data fields and additional request volumes see https://ipinfo.io/pricing

Click here to view the Java SDK's API documentation.

Installation

Maven

Add these values to your pom.xml file:

Dependency:

<dependencies>
    <dependency>
        <groupId>io.ipinfo</groupId>
        <artifactId>ipinfo-api</artifactId>
        <version>3.0.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Quick Start

IP Information
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;

public class Main {
    public static void main(String... args) {
        IPinfo ipInfo = new IPinfo.Builder()
                .setToken("YOUR TOKEN")
                .build();

        try {
            IPResponse response = ipInfo.lookupIP("8.8.8.8");

            // Print out the hostname
            System.out.println(response.getHostname());
        } catch (RateLimitedException ex) {
            // Handle rate limits here.
        }
    }
}
ASN Information
import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;

public class Main {
    public static void main(String... args) {
        IPinfo ipInfo = new IPinfo.Builder()
                .setToken("YOUR TOKEN")
                .build();

        try {
            ASNResponse response = ipInfo.lookupASN("AS7922");

            // Print out country name
            System.out.println(response.getCountry());
        } catch (RateLimitedException ex) {
            // Handle rate limits here.
        }
    }
}

Errors

  • ErrorResponseException: A runtime exception accessible through the ExecutionException of a future. This exception signals that something went wrong when mapping the API response to the wrapper. You probably can't recover from this exception.
  • RateLimitedException An exception signaling that you've been rate limited.

Caching

This library provides a very simple caching system accessible in SimpleCache. Simple cache is an in-memory caching system that resets every time you restart your code.

If you prefer a different caching methodology, you may use the Cache interface and implement your own caching system around your own infrastructure.

The default cache length is 1 day, this can be changed by calling the SimpleCache constructor yourself.

import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;

public class Main {
    public static void main(String... args) {
        // 5 Day Cache
        IPinfo ipInfo = new IPinfo.Builder()
                .setToken("YOUR TOKEN")
                .setCache(new SimpleCache(Duration.ofDays(5)))
                .build();

        try {
            IPResponse response = ipInfo.lookupIP("8.8.8.8");

            // Print out the hostname
            System.out.println(response.getHostname());
        } catch (RateLimitedException ex) {
            // Handle rate limits here.
        }
    }
}

Country Name Lookup

This library provides a system to lookup country names through ISO2 country codes.

import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;

public class Main {
    public static void main(String... args) {
        IPinfo ipInfo = new IPinfo.Builder()
                .setToken("YOUR TOKEN")
                .build();

        try {
            IPResponse response = ipInfo.lookupIP("8.8.8.8");

            // Print out the country code
            System.out.println(response.getCountryCode());

            // Print out the country name
            System.out.println(response.getCountryName());
        } catch (RateLimitedException ex) {
            // Handle rate limits here.
        }
    }
}

EU Country Lookup

This library provides a system to lookup if a country is a member of the European Union (EU) through ISO2 country codes.

import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;

public class Main {
    public static void main(String... args) {
        IPinfo ipInfo = new IPinfo.Builder()
                .setToken("YOUR TOKEN")
                .build();

        try {
            IPResponse response = ipInfo.lookupIP("8.8.8.8");

            // Print out whether the country is a member of the EU
            System.out.println(response.isEU());
        } catch (RateLimitedException ex) {
            // Handle rate limits here.
        }
    }
}

Internationalization

This library provides a system to lookup if a country is a member of the European Union (EU), emoji and unicode of the country's flag, code and symbol of the country's currency, and public link to the country's flag image as an SVG and continent code and name through ISO2 country codes.

import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;

public class Main {
    public static void main(String... args) {
        IPinfo ipInfo = new IPinfo.Builder()
                .setToken("YOUR TOKEN")
                .build();

        try {
            IPResponse response = ipInfo.lookupIP("8.8.8.8");

            // Print out whether the country is a member of the EU
            System.out.println(response.isEU());

            // CountryFlag{emoji='πŸ‡ΊπŸ‡Έ',unicode='U+1F1FA U+1F1F8'}
            System.out.println(response.getCountryFlag());

            // https://cdn.ipinfo.io/static/images/countries-flags/US.svg
            System.out.println(response.getCountryFlagURL());

            // CountryCurrency{code='USD',symbol='$'}
            System.out.println(response.getCountryCurrency());

            // Continent{code='NA',name='North America'}
            System.out.println(response.getContinent());
        } catch (RateLimitedException ex) {
            // Handle rate limits here.
        }
    }
}

Location Information

This library provides an easy way to get the latitude and longitude of an IP Address:

import io.ipinfo.api.IPinfo;
import io.ipinfo.api.errors.RateLimitedException;
import io.ipinfo.api.model.IPResponse;

public class Main {
    public static void main(String... args) {
        IPinfo ipInfo = new IPinfo.Builder()
                .setToken("YOUR TOKEN")
                .build();

        try {
            IPResponse response = ipInfo.lookupIP("8.8.8.8");

            // Print out the Latitude and Longitude
            System.out.println(response.getLatitude());
            System.out.println(response.getLongitude());

        } catch (RateLimitedException ex) {
            // Handle rate limits here.
        }
    }
}

Extra Information

  • This library is thread-safe. Feel free to call the different endpoints from different threads.
  • This library uses Square's http client. Please refer to their documentation to get information on more functionality you can use.

Other Libraries

There are official IPinfo client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.

About IPinfo

Founded in 2013, IPinfo prides itself on being the most reliable, accurate, and in-depth source of IP address data available anywhere. We process terabytes of data to produce our custom IP geolocation, company, carrier, VPN detection, hosted domains, and IP type data sets. Our API handles over 40 billion requests a month for 100,000 businesses and developers.

image

java's People

Contributors

aaomidi avatar abdullahdevrel avatar abu-usama avatar anas-dev-92 avatar awaismslm avatar deltwalrus avatar dependabot[bot] avatar hackthebridge avatar markvink avatar mkotb avatar rm-umar avatar st-polina avatar talhahwahla avatar umanshahzad avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

java's Issues

Guava version bump vulnerability

Hi

You have a vulnerabilities from guava here.
You bot already created PR for this and has no conflicts, please merge and release new version.

Thank you!

Ben

io.ipinfo.api.errors.ErrorResponseException: javax.net.ssl.SSLHandshakeException

io.ipinfo.api.errors.ErrorResponseException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at io.ipinfo.api.request.BaseRequest.handleRequest(BaseRequest.java:35) ~[ipinfo-api-1.1.jar!/:?]
at io.ipinfo.api.request.IPRequest.handle(IPRequest.java:24) ~[ipinfo-api-1.1.jar!/:?]
at io.ipinfo.api.IPInfo.lookupIP(IPInfo.java:51) ~[ipinfo-api-1.1.jar!/:?]

Version : 1.1
Java version: 1.8
OS: Amazon linux

Map integration

Create a simple function that accepts an IP list (max 500k) and returns the JSON response from https://ipinfo.io/maps.

Maven download wrong class verison

When downloading the Files through Maven and attempting to build the project, the Files downloaded through maven have the wrong version.
java: cannot access io.ipinfo.api.model.IPResponse bad class file: /C:/Users/H3xabyt3/.m2/repository/io/ipinfo/ipinfo-api/3.0.0/ipinfo-api-3.0.0.jar!/io/ipinfo/api/model/IPResponse.class class file has wrong version 61.0, should be 52.0 Please remove or make sure it appears in the correct subdirectory of the classpath.

Maven
<dependency> <groupId>io.ipinfo</groupId> <artifactId>ipinfo-api</artifactId> <version>3.0.0</version> <scope>compile</scope> </dependency>

General code review request

Just take a quick look at the codebase, I'll be writing some unit tests for it too so we can ensure all of its components work properly.

Let me know if you have any design/maintainability questions :)

(Feel free to unassign yourself)

Convert to Kotlin

I think it'd be nice to get the codebase to be Kotlin rather than Java. With the auto-converter tool we can have a nice starting point in 5 minutes.

Can also use this opportunity to document how to use the library in Kotlin as well as Java.

Error Handle Rate Limit

Hello i have a problem with the implementation of the API.
So this is my code
public void OnPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
IPInfo ipInfo = IPInfo.builder().setToken("***********").build();

	try {
		IPResponse response = ipInfo.lookupIP(player.getAddress().getAddress().getHostAddress());
		
		System.out.println(response.getOrg());
		int index = response.getOrg().toString().indexOf("Mobile");
		if (index == 1) {
			System.out.println("Vous venez de vous connecter avec une adresse IP mobile");
			player.kickPlayer("Veuillez vous rendre sur le teamspeak.");
		}

	} catch (RateLimitedException ex) {
		ex.getMessage();
		// Handle rate limits here.
	}

	System.out.println("Le joueur qui vient de se connecter avec l'ip suivante : " + player.getAddress());
}

I got the error

Caused by: java.lang.ClassNotFoundException: io.ipinfo.api.errors.RateLimitedException
I know i have to handler error after the try catch but i don't know how !
Please your help can be helpfull thank you

Get null for ip address

I got null for my ip address.

IPInfo ipInfo = IPInfo.builder().setToken("<redacted>").build();
    String ipAddress = request.getRemoteAddr();
    try {
      IPResponse ipResponse = ipInfo.lookupIP(ipAddress);
      // Print out the hostname
      System.out.println("Testing starts here");
      System.out.println(ipAddress);
      System.out.println(ipResponse);
      System.out.println(ipResponse.getCountryName());
      System.out.println(ipResponse.getHostname());

      UserLocation userLocation = new UserLocation(user, ipResponse.getCountryName());
      datastore.storeLocation(userLocation);

    } catch (RateLimitedException ex) {
      System.out.println("Exceed rate limit");
    }

I have deployed it.

java.lang.NoClassDefFoundError: io/ipinfo/api/errors/RateLimitedException

I'm having issues using the libraries:

org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: io/ipinfo/api/errors/RateLimitedException
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:149) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:394) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:301) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_19_R1.CraftServer.loadPlugins(CraftServer.java:412) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at org.bukkit.craftbukkit.v1_19_R1.CraftServer.reload(CraftServer.java:920) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at org.bukkit.Bukkit.reload(Bukkit.java:801) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.craftbukkit.v1_19_R1.CraftServer.dispatchCommand(CraftServer.java:831) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at org.bukkit.craftbukkit.v1_19_R1.CraftServer.dispatchServerCommand(CraftServer.java:816) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at net.minecraft.server.dedicated.DedicatedServer.bg(DedicatedServer.java:419) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at net.minecraft.server.dedicated.DedicatedServer.b(DedicatedServer.java:395) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:1197) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at net.minecraft.server.MinecraftServer.v(MinecraftServer.java:1010) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at net.minecraft.server.MinecraftServer.lambda$0(MinecraftServer.java:291) ~[spigot-1.19-R0.1-SNAPSHOT.jar:3553-Spigot-14a2382-ef09464]
at java.lang.Thread.run(Thread.java:833) [?:?]
Caused by: java.lang.NoClassDefFoundError: io/ipinfo/api/errors/RateLimitedException
at java.lang.Class.forName0(Native Method) ~[?:?]
at java.lang.Class.forName(Class.java:467) ~[?:?]
at org.bukkit.plugin.java.PluginClassLoader.(PluginClassLoader.java:67) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:145) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
... 15 more
Caused by: java.lang.ClassNotFoundException: io.ipinfo.api.errors.RateLimitedException
at org.bukkit.plugin.java.PluginClassLoader.loadClass0(PluginClassLoader.java:147) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.PluginClassLoader.loadClass(PluginClassLoader.java:99) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?]
at java.lang.Class.forName0(Native Method) ~[?:?]
at java.lang.Class.forName(Class.java:467) ~[?:?]
at org.bukkit.plugin.java.PluginClassLoader.(PluginClassLoader.java:67) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:145) ~[spigot-api-1.19-R0.1-SNAPSHOT.jar:?]
... 15 more

Upgrade okhttp to 4.10.

We use 3.9.0 which looks pretty old now and this may make some users not want to install the library if it takes on too old of a subdependency from their side.

"kotlin" repo?

Is it possible in GitHub to make a dummy "kotlin" repo that basically points to this? Kotlin devs probably know to look here anyway, but it'd be nice to have the name there.

Use versioned cache key

Make sure the cache key contains a number to indicate the version of the cached data. Data changes that change what's expected in cached data require a version change.

Add optional IP selection handler

Add an optional IP selection handler to the SDK client initialization step which accepts the request context and expects returning an IP.

Add a default handler for this which looks at the X-Forwarded-For header and falls back to the source IP.

The resulting IP is the IP for which details are fetched.

Check for bogon IPs without API call & give more info

Right now for bogon IPs we need to make requests to the API even though technically it's possible to determine a bogon IP statically fully locally.

Additionally bogon IPs come in different flavors so it'd be nice that if our SDK is doing this bogon check, that it additionally adds some extra metadata about the bogon IP too.

See https://ipinfo.io/bogon

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.