New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

net.renfei:ip2location

Package Overview
Dependencies
Maintainers
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

net.renfei:ip2location

IP2Location Java Component enables applications to get info from IP address such as the visitor’s country, region, city, latitude, longitude, ZIP code, ISP name, domain name, time zone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, elevation and usage type.

  • 1.2.1
  • Source
  • Maven
  • Socket score

Version published
Maintainers
2
Source

简体中文 | English

./doc/logo.png

This site or product includes IP2Location LITE data available from http://www.ip2location.com

IP2Location for Java

The repository code is based on ip2location repository.

Example: https://www.renfei.net/kitbox/ip

Download

The update cycle of database bin file is once a month.

Database bin file:

  • Releases

Or download the zip file in the warehouse and decompress it by yourself.

Installing in maven


<dependency>
    <groupId>net.renfei</groupId>
    <artifactId>ip2location</artifactId>
    <version>1.2.3</version>
</dependency>

Compared with the official client, I removed the ip2locationweb service and the dependency on other libraries.

Usage

import net.renfei.ip2location.*;

public class Main {
    public Main() {
    }

    public static void main(String[] args) {
        IP2Location loc = new IP2Location();
        try {
            String ip = "8.8.8.8";
            String binfile = "/usr/data/IP2LOCATION-LITE-DB11.BIN";

            // this is to initialize with a BIN file
            loc.Open(binfile, true);

            // this is to initialize with a byte array
            // Path binpath = Paths.get(binfile);
            // byte[] binFileBytes = Files.readAllBytes(binpath);
            // loc.Open(binFileBytes);

            IPResult rec = loc.IPQuery(ip);
            if ("OK".equals(rec.getStatus())) {
                System.out.println(rec);
            } else if ("EMPTY_IP_ADDRESS".equals(rec.getStatus())) {
                System.out.println("IP address cannot be blank.");
            } else if ("INVALID_IP_ADDRESS".equals(rec.getStatus())) {
                System.out.println("Invalid IP address.");
            } else if ("MISSING_FILE".equals(rec.getStatus())) {
                System.out.println("Invalid database path.");
            } else if ("IPV6_NOT_SUPPORTED".equals(rec.getStatus())) {
                System.out.println("This BIN does not contain IPv6 data.");
            } else {
                System.out.println("Unknown error." + rec.getStatus());
            }
            System.out.println("Java Component: " + rec.getVersion());
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace(System.out);
        } finally {
            loc.Close();
        }
    }
}

Database Fields

NameTypeDescription
ip_fromINT (10) / DECIMAL (39,0)First IP address show netblock.
ip_toINT (10) / DECIMAL (39,0)Last IP address show netblock.
country_codeCHAR(2)Two-character country code based on ISO 3166.
country_nameVARCHAR(64)Country name based on ISO 3166.
region_nameVARCHAR(128)Region or state name.
city_nameVARCHAR(128)City name.
latitudeDOUBLECity latitude. Default to capital city latitude if city is unknown.
longitudeDOUBLECity longitude. Default to capital city longitude if city is unknown.
zip_codeVARCHAR(30)ZIP/Postal code.
time_zoneVARCHAR(8)UTC time zone (with DST supported).

IPTOOLS CLASS

Methods

Below are the methods supported in this class.

Method NameDescription
public boolean IsIPv4(String IPAddress)Returns true if string contains an IPv4 address. Otherwise false.
public boolean IsIPv6(String IPAddress)Returns true if string contains an IPv6 address. Otherwise false.
public BigInteger IPv4ToDecimal(String IPAddress)Returns the IP number for an IPv4 address.
public BigInteger IPv6ToDecimal(String IPAddress)Returns the IP number for an IPv6 address.
public String DecimalToIPv4(BigInteger IPNum)Returns the IPv4 address for the supplied IP number.
public String DecimalToIPv6(BigInteger IPNum)Returns the IPv6 address for the supplied IP number.
public String CompressIPv6(String IPAddress)Returns the IPv6 address in compressed form.
public String ExpandIPv6(String IPAddress)Returns the IPv6 address in expanded form.
public List IPv4ToCIDR(String IPFrom, String IPTo)Returns a list of CIDR from the supplied IPv4 range.
public List IPv6ToCIDR(String IPFrom, String IPTo)Returns a list of CIDR from the supplied IPv6 range.
public String[] CIDRToIPv4(String CIDR)Returns the IPv4 range from the supplied CIDR.
public String[] CIDRToIPv6(String CIDR)Returns the IPv6 range from the supplied CIDR.

Usage

import com.ip2location.*;

import java.math.BigInteger;
import java.util.*;

public class Main {
    public Main() {
    }

    public static void main(String[] args) {
        try {
            IPTools tools = new IPTools();

            System.out.println(tools.IsIPv4("60.54.166.38"));
            System.out.println(tools.IsIPv6("2600:1f18:45b0:5b00:f5d8:4183:7710:ceec"));
            System.out.println(tools.IPv4ToDecimal("60.54.166.38"));
            System.out.println(tools.IPv6ToDecimal("2600:118:450:5b00:f5d8:4183:7710:ceec"));
            System.out.println(tools.DecimalToIPv4(new BigInteger("1010214438")));
            System.out.println(tools.DecimalToIPv6(new BigInteger("50510686025047391022278667396705210092")));
            System.out.println(tools.CompressIPv6("0000:0000:0000:0035:0000:FFFF:0000:0000"));
            System.out.println(tools.ExpandIPv6("500:6001:FE:35:0:FFFF::"));
            List<String> stuff = tools.IPv4ToCIDR("10.0.0.0", "10.10.2.255");
            stuff.forEach(System.out::println);
            List<String> stuff2 = tools.IPv6ToCIDR("2001:4860:4860:0000:0000:0000:0000:8888", "2001:4860:4860:0000:eeee:ffff:ffff:ffff");
            stuff2.forEach(System.out::println);
            String[] stuff3 = tools.CIDRToIPv4("10.123.80.0/12");
            System.out.println(stuff3[0]);
            System.out.println(stuff3[1]);
            String[] stuff4 = tools.CIDRToIPv6("2002:1234::abcd:ffff:c0a8:101/62");
            System.out.println(stuff4[0]);
            System.out.println(stuff4[1]);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace(System.out);
        }
    }
}

Mirror

FAQs

Package last updated on 16 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc