5 changed files with 68 additions and 34 deletions
@ -0,0 +1,47 @@ |
|||
package app.services.io.readers; |
|||
|
|||
import app.services.io.fileloader.IFileLoader; |
|||
import com.maxmind.geoip2.DatabaseReader; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.io.ByteArrayInputStream; |
|||
import java.io.IOException; |
|||
import java.net.InetAddress; |
|||
import java.util.List; |
|||
|
|||
@Component |
|||
public class GeoIPCity extends BaseReader { |
|||
private final DatabaseReader databaseReader; |
|||
|
|||
@Autowired |
|||
public GeoIPCity(List<IFileLoader> fileLoaders) { |
|||
super(fileLoaders); |
|||
databaseReader = loadConfiguration(); |
|||
if (databaseReader == null) { |
|||
logger.error("GEOIP_CITY file is missing"); |
|||
System.exit(1); |
|||
} |
|||
} |
|||
|
|||
public String GetCity(String ip) { |
|||
try { |
|||
return databaseReader.city(InetAddress.getByName(ip)).getCity().getName(); |
|||
} catch (Exception e) { |
|||
return "Unknown"; |
|||
} |
|||
} |
|||
|
|||
public DatabaseReader loadConfiguration() { |
|||
return (DatabaseReader) super.loadConfiguration( |
|||
"GEOIP_CITY_FILE", |
|||
b -> { |
|||
try { |
|||
return new DatabaseReader.Builder(new ByteArrayInputStream(b)).build(); |
|||
} catch (IOException e) { |
|||
return null; |
|||
} |
|||
} |
|||
); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue