diff --git a/.gitignore b/.gitignore
index 0c0a974e..892d190a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -32,3 +32,5 @@ build/
### VS Code ###
.vscode/
*.log
+
+GeoLite2-City/
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 2134e8d8..95cf34b5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -102,6 +102,21 @@
commons-io
2.11.0
+
+
+ com.maxmind.geoip2
+ geoip2
+ 2.15.0
+
+
+
+
+
+ org.apache.commons
+ commons-compress
+ 1.23.0
+
+
com.google.ortools
diff --git a/src/main/java/cn/lihongjie/coal/ip/IpQueryService.java b/src/main/java/cn/lihongjie/coal/ip/IpQueryService.java
new file mode 100644
index 00000000..f9c0ec5d
--- /dev/null
+++ b/src/main/java/cn/lihongjie/coal/ip/IpQueryService.java
@@ -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;
+
+
+ }
+
+}
diff --git a/src/main/java/cn/lihongjie/coal/log/SysLog.java b/src/main/java/cn/lihongjie/coal/log/SysLog.java
new file mode 100644
index 00000000..6bcd08ea
--- /dev/null
+++ b/src/main/java/cn/lihongjie/coal/log/SysLog.java
@@ -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();
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
deleted file mode 100644
index 8b137891..00000000
--- a/src/main/resources/application.properties
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml
new file mode 100644
index 00000000..fecad03a
--- /dev/null
+++ b/src/main/resources/application.yaml
@@ -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'
\ No newline at end of file