Skip to content

Commit

Permalink
add ip to geolocation util
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushaga14 committed Nov 22, 2024
1 parent 0117c86 commit 06e1f95
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/api-threat-detection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
<version>2.9.3</version>
</dependency>

<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.15.0</version>
</dependency>

</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.akto.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.nio.file.Paths;

import com.akto.log.LoggerMaker;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CountryResponse;

public class IpGeolocation {

// todo: modify file path
private static File database = new File("/Users/admin/Downloads/GeoLite2-Country_20241119/GeoLite2-Country.mmdb");
private static DatabaseReader reader;
private static final LoggerMaker loggerMaker = new LoggerMaker(IpGeolocation.class);

public IpGeolocation() {
try {
reader = new DatabaseReader.Builder(database).build();
} catch (Exception e) {
loggerMaker.errorAndAddToDb("IpGeolocation init failed " + e.getMessage());
}
}

public String fetchLocationByIp(String ip) throws Exception {
if (reader == null) {
throw new Exception("IpGeolocation not init properly, unable to fetch location");
}
InetAddress ipAddress = InetAddress.getByName(ip);

CountryResponse response = reader.country(ipAddress);
String country = response.getCountry().getName();
return country;
}

}

0 comments on commit 06e1f95

Please sign in to comment.