mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
支持geoip
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -32,3 +32,5 @@ build/
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
*.log
|
||||
|
||||
GeoLite2-City/
|
||||
15
pom.xml
15
pom.xml
@@ -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>
|
||||
|
||||
141
src/main/java/cn/lihongjie/coal/ip/IpQueryService.java
Normal file
141
src/main/java/cn/lihongjie/coal/ip/IpQueryService.java
Normal 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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
12
src/main/java/cn/lihongjie/coal/log/SysLog.java
Normal file
12
src/main/java/cn/lihongjie/coal/log/SysLog.java
Normal 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();
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
|
||||
2
src/main/resources/application.yaml
Normal file
2
src/main/resources/application.yaml
Normal 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'
|
||||
Reference in New Issue
Block a user