支持geoip

This commit is contained in:
2023-07-25 19:19:12 +08:00
parent a1ba34955c
commit 40f04fdcc3
6 changed files with 172 additions and 1 deletions

2
.gitignore vendored
View File

@@ -32,3 +32,5 @@ build/
### VS Code ###
.vscode/
*.log
GeoLite2-City/

15
pom.xml
View File

@@ -102,6 +102,21 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.15.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.23.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.ortools/ortools-java -->
<dependency>
<groupId>com.google.ortools</groupId>

View File

@@ -0,0 +1,141 @@
package cn.lihongjie.coal.ip;
import com.maxmind.geoip2.DatabaseReader;
import jakarta.annotation.PostConstruct;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import java.io.*;
import java.net.InetAddress;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
@Service
@Slf4j
public class IpQueryService {
@Autowired
Environment environment;
@Value("${geolite2.url}")
private String geolite2DownloadUrl;
private DatabaseReader reader;
@SneakyThrows
@PostConstruct
public void init() {
log.info("开始初始化GeoLite2-City本地数据库");
Path dbPath = Path.of("./GeoLite2-City/GeoLite2-City.mmdb");
Path dbDir = dbPath.getParent();
if (Files.notExists(dbDir)) {
Files.createDirectories(dbDir);
}
if (Files.exists(dbPath)) {
log.info("发现默认数据库文件: {}", dbPath);
} else if (StringUtils.isNotEmpty(environment.getProperty("geolite2.city.path"))) {
dbPath = Path.of(environment.getProperty("geolite2.city.path"));
log.info("发现环境变量数据库文件: {}", dbPath);
} else {
log.info("没有找到数据库文件, 下载数据库文件");
File downloadPath = dbPath.getParent().resolve("GeoLite2-City.tar.gz").toFile();
FileUtils.copyURLToFile(
new URL(geolite2DownloadUrl),
downloadPath,
10 * 1000,
30 * 1000);
TarArchiveInputStream tarIn = new TarArchiveInputStream(
new GzipCompressorInputStream(
new BufferedInputStream(
new FileInputStream(downloadPath))));
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
Path newDbFile = null;
// tarIn is a TarArchiveInputStream
while (tarEntry != null) {// create a file with the same name as the tarEntry
File destPath = dbDir.resolve(tarEntry.getName()).toFile();
log.info(destPath.toString());
if (tarEntry.isDirectory()) {
destPath.mkdirs();
} else {
destPath.createNewFile();
if (destPath.getName().endsWith("mmdb")) {
newDbFile = destPath.toPath();
}
//byte [] btoRead = new byte[(int)tarEntry.getSize()];
byte[] btoRead = new byte[1024];
//FileInputStream fin
// = new FileInputStream(destPath.getCanonicalPath());
BufferedOutputStream bout =
new BufferedOutputStream(new FileOutputStream(destPath));
int len = 0;
while ((len = tarIn.read(btoRead)) != -1) {
bout.write(btoRead, 0, len);
}
bout.close();
btoRead = null;
}
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
if (newDbFile != null) {
Files.move(newDbFile, dbPath);
}
}
reader = new DatabaseReader.Builder(dbPath.toFile()).build();
}
@SneakyThrows
public Object query(String ipaddr) {
if (reader != null) {
InetAddress ipAddress = InetAddress.getByName(ipaddr);
return reader.city(ipAddress);
}
return null;
}
}

View File

@@ -0,0 +1,12 @@
package cn.lihongjie.coal.log;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {
String msg();
}

View File

@@ -1 +0,0 @@

View File

@@ -0,0 +1,2 @@
geolite2:
url: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=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&suffix=tar.gz'