Skip to content

Commit

Permalink
fetch lat,lng values
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushaga14 committed Nov 22, 2024
1 parent 896c930 commit c860299
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
import java.io.File;
import java.net.InetAddress;

import com.akto.log.LoggerMaker;
import com.akto.dto.api_threat_detection.IpGeoLocationResp;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CountryResponse;
import com.maxmind.geoip2.model.CityResponse;

public class IpGeolocation {

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

public IpGeolocation() throws Exception {
reader = new DatabaseReader.Builder(database).build();
}

public String fetchLocationByIp(String ip) throws Exception {
public IpGeoLocationResp 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);
CityResponse response = reader.city(ipAddress);
String country = response.getCountry().getName();
return country;
IpGeoLocationResp resp = new IpGeoLocationResp(country, response.getLocation().getLatitude(), response.getLocation().getLongitude());
return resp;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.akto.dto.api_threat_detection;

public class IpGeoLocationResp {
private String country;
private double lat;
private double lon;

public IpGeoLocationResp() {
}

public IpGeoLocationResp(String country, double lat, double lon) {
this.country = country;
this.lat = lat;
this.lon = lon;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public double getLat() {
return lat;
}

public void setLat(double lat) {
this.lat = lat;
}

public double getLon() {
return lon;
}

public void setLon(double lon) {
this.lon = lon;
}

}

0 comments on commit c860299

Please sign in to comment.