|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+package com.ruoyi.common.utils.ip;
|
|
|
2
|
+
|
|
|
3
|
+import java.io.InputStream;
|
|
|
4
|
+import org.lionsoul.ip2region.service.Config;
|
|
|
5
|
+import org.lionsoul.ip2region.service.Ip2Region;
|
|
|
6
|
+import org.slf4j.Logger;
|
|
|
7
|
+import org.slf4j.LoggerFactory;
|
|
|
8
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
9
|
+
|
|
|
10
|
+/**
|
|
|
11
|
+ * ip2region 离线 IP 归属地查询工具。
|
|
|
12
|
+ */
|
|
|
13
|
+public final class Ip2RegionUtils
|
|
|
14
|
+{
|
|
|
15
|
+ private static final Logger log = LoggerFactory.getLogger(Ip2RegionUtils.class);
|
|
|
16
|
+
|
|
|
17
|
+ private static final String XDB_CLASSPATH = "/ip2region/ip2region_v4.xdb";
|
|
|
18
|
+
|
|
|
19
|
+ private static volatile Ip2Region ip2Region;
|
|
|
20
|
+
|
|
|
21
|
+ private static volatile boolean initAttempted;
|
|
|
22
|
+
|
|
|
23
|
+ private Ip2RegionUtils()
|
|
|
24
|
+ {
|
|
|
25
|
+ }
|
|
|
26
|
+
|
|
|
27
|
+ /**
|
|
|
28
|
+ * 查询 IP 归属地原始字符串,格式:Country|Province|City|ISP|Code。
|
|
|
29
|
+ */
|
|
|
30
|
+ public static String searchRegion(String ip)
|
|
|
31
|
+ {
|
|
|
32
|
+ if (StringUtils.isEmpty(ip))
|
|
|
33
|
+ {
|
|
|
34
|
+ return null;
|
|
|
35
|
+ }
|
|
|
36
|
+ Ip2Region service = getIp2Region();
|
|
|
37
|
+ if (service == null)
|
|
|
38
|
+ {
|
|
|
39
|
+ return null;
|
|
|
40
|
+ }
|
|
|
41
|
+ try
|
|
|
42
|
+ {
|
|
|
43
|
+ return service.search(ip.trim());
|
|
|
44
|
+ }
|
|
|
45
|
+ catch (Exception ex)
|
|
|
46
|
+ {
|
|
|
47
|
+ log.warn("ip2region 查询失败 ip={}: {}", ip, ex.getMessage());
|
|
|
48
|
+ return null;
|
|
|
49
|
+ }
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ /**
|
|
|
53
|
+ * 从 ip2region 结果中提取可用于区县展示的名称。
|
|
|
54
|
+ */
|
|
|
55
|
+ public static String extractDistrictName(String region)
|
|
|
56
|
+ {
|
|
|
57
|
+ if (StringUtils.isEmpty(region))
|
|
|
58
|
+ {
|
|
|
59
|
+ return null;
|
|
|
60
|
+ }
|
|
|
61
|
+ String[] parts = region.split("\\|");
|
|
|
62
|
+ String city = parts.length > 2 ? nullIfZero(parts[2]) : null;
|
|
|
63
|
+ String province = parts.length > 1 ? nullIfZero(parts[1]) : null;
|
|
|
64
|
+ String country = parts.length > 0 ? nullIfZero(parts[0]) : null;
|
|
|
65
|
+ return firstNonEmpty(city, province, country);
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ private static Ip2Region getIp2Region()
|
|
|
69
|
+ {
|
|
|
70
|
+ if (!initAttempted)
|
|
|
71
|
+ {
|
|
|
72
|
+ synchronized (Ip2RegionUtils.class)
|
|
|
73
|
+ {
|
|
|
74
|
+ if (!initAttempted)
|
|
|
75
|
+ {
|
|
|
76
|
+ initAttempted = true;
|
|
|
77
|
+ ip2Region = createIp2Region();
|
|
|
78
|
+ }
|
|
|
79
|
+ }
|
|
|
80
|
+ }
|
|
|
81
|
+ return ip2Region;
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ private static Ip2Region createIp2Region()
|
|
|
85
|
+ {
|
|
|
86
|
+ try (InputStream inputStream = Ip2RegionUtils.class.getResourceAsStream(XDB_CLASSPATH))
|
|
|
87
|
+ {
|
|
|
88
|
+ if (inputStream == null)
|
|
|
89
|
+ {
|
|
|
90
|
+ log.warn("ip2region 数据库未找到: {}", XDB_CLASSPATH);
|
|
|
91
|
+ return null;
|
|
|
92
|
+ }
|
|
|
93
|
+ Config v4Config = Config.custom()
|
|
|
94
|
+ .setCachePolicy(Config.BufferCache)
|
|
|
95
|
+ .setXdbInputStream(inputStream)
|
|
|
96
|
+ .asV4();
|
|
|
97
|
+ return Ip2Region.create(v4Config, null);
|
|
|
98
|
+ }
|
|
|
99
|
+ catch (Exception ex)
|
|
|
100
|
+ {
|
|
|
101
|
+ log.error("ip2region 初始化失败", ex);
|
|
|
102
|
+ return null;
|
|
|
103
|
+ }
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ private static String nullIfZero(String value)
|
|
|
107
|
+ {
|
|
|
108
|
+ if (StringUtils.isEmpty(value) || "0".equals(value.trim()))
|
|
|
109
|
+ {
|
|
|
110
|
+ return null;
|
|
|
111
|
+ }
|
|
|
112
|
+ return value.trim();
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+ private static String firstNonEmpty(String... values)
|
|
|
116
|
+ {
|
|
|
117
|
+ if (values == null)
|
|
|
118
|
+ {
|
|
|
119
|
+ return null;
|
|
|
120
|
+ }
|
|
|
121
|
+ for (String value : values)
|
|
|
122
|
+ {
|
|
|
123
|
+ if (StringUtils.isNotEmpty(value))
|
|
|
124
|
+ {
|
|
|
125
|
+ return value;
|
|
|
126
|
+ }
|
|
|
127
|
+ }
|
|
|
128
|
+ return null;
|
|
|
129
|
+ }
|
|
|
130
|
+}
|