Skip to content

Instantly share code, notes, and snippets.

@mzambrana
Created April 18, 2018 11:06
Show Gist options
  • Save mzambrana/2d409c7b503c5f3bde7c619431635e21 to your computer and use it in GitHub Desktop.
Save mzambrana/2d409c7b503c5f3bde7c619431635e21 to your computer and use it in GitHub Desktop.
GeoIPV2 Sample
public class GeoIpApiV2
{
public static final Logger logger = LogManager.getLogger(GeoIpApiV2.class.getName());
private DatabaseReader cityReader;
private DatabaseReader ispReader;
public static String lastGeoIPPath;
public static String lastGeoIPSubPath;
private static class InstanceHolder
{
public static final GeoIpApiV2 instance = new GeoIpApiV2();
}
public static GeoIpApiV2 getInstance()
{
return InstanceHolder.instance;
}
private GeoIpApiV2()
{
try
{
// A File object pointing to your GeoIP2 or GeoLite2 database
File databaseCity = new File ( ConfigGeoIP.MAXMIND_GEOIP_VER2_PATH + ConfigGeoIP.MAXMIND_GEOIP_VER2_DATE_SUBPATH + "GeoIP2-City.mmdb" );
File databaseISP = new File ( ConfigGeoIP.MAXMIND_GEOIP_VER2_PATH + ConfigGeoIP.MAXMIND_GEOIP_VER2_DATE_SUBPATH + "GeoIP2-ISP.mmdb" );
// This creates the DatabaseReader object, which should be reused across lookups.
this.cityReader = new DatabaseReader.Builder(databaseCity).fileMode(Reader.FileMode.MEMORY).build();
this.ispReader = new DatabaseReader.Builder(databaseISP).fileMode(Reader.FileMode.MEMORY).build();
// Change last values
GeoIpApiV2.lastGeoIPPath = ConfigGeoIP.MAXMIND_GEOIP_VER2_PATH;
GeoIpApiV2.lastGeoIPSubPath = ConfigGeoIP.MAXMIND_GEOIP_VER2_DATE_SUBPATH;
}
catch (IOException e)
{
logger.error(e);
}
}
public void reloadGeoIPServices ()
{
try
{
// A File object pointing to your GeoIP2 or GeoLite2 database
File databaseCity = new File ( ConfigGeoIP.MAXMIND_GEOIP_VER2_PATH + ConfigGeoIP.MAXMIND_GEOIP_VER2_DATE_SUBPATH + "GeoIP2-City.mmdb" );
File databaseISP = new File ( ConfigGeoIP.MAXMIND_GEOIP_VER2_PATH + ConfigGeoIP.MAXMIND_GEOIP_VER2_DATE_SUBPATH + "GeoIP2-ISP.mmdb" );
// Local readers
DatabaseReader localCityReader;
DatabaseReader localIspReader;
// This creates the DatabaseReader object, which should be reused across lookups.
localCityReader = new DatabaseReader.Builder(databaseCity).fileMode(Reader.FileMode.MEMORY).build();
localIspReader = new DatabaseReader.Builder(databaseISP).fileMode(Reader.FileMode.MEMORY).build();
// Change last values
GeoIpApiV2.lastGeoIPPath = ConfigGeoIP.MAXMIND_GEOIP_VER2_PATH;
GeoIpApiV2.lastGeoIPSubPath = ConfigGeoIP.MAXMIND_GEOIP_VER2_DATE_SUBPATH;
// Replace geoip libs
this.cityReader = localCityReader;
this.ispReader = localIspReader;
}
catch (IOException e)
{
logger.error(e);
}
}
public IspResponse getIspResponse ( String currentIp )
{
try
{
InetAddress ipAddress = InetAddress.getByName(currentIp);
IspResponse response = ispReader.isp(ipAddress);
return response;
}
catch ( Exception e ) {}
return null;
}
public IspResponse getIspResponse ( InetAddress ipAddress )
{
try
{
IspResponse response = ispReader.isp(ipAddress);
return response;
}
catch ( Exception e ) {}
return null;
}
public CityResponse getCityResponse ( String currentIp )
{
try
{
InetAddress ipAddress = InetAddress.getByName(currentIp);
CityResponse response = cityReader.city(ipAddress);
return response;
}
catch ( Exception e ) {}
return null;
}
public CityResponse getCityResponse ( InetAddress ipAddress )
{
try
{
CityResponse response = cityReader.city(ipAddress);
return response;
}
catch ( Exception e ) {}
return null;
}
}
public class Sample
{
public static void main ( String[] args )
{
String ip = "137.21.32.42";
System.out.println("ASN Number: " + GeoIpApiV2.getInstance().getIspResponse(ip).getAutonomousSystemNumber());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment