-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0117c86
commit 06e1f95
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
apps/api-threat-detection/src/main/java/com/akto/utils/IpGeolocation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |