|
@@ -1,82 +1,74 @@
|
|
|
package cn.iocoder.yudao.framework.ip.core.utils;
|
|
|
|
|
|
-import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
|
|
+import cn.hutool.core.io.resource.ResourceUtil;
|
|
|
import cn.iocoder.yudao.framework.ip.core.Area;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.lionsoul.ip2region.xdb.Searcher;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
-import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
|
|
-
|
|
|
/**
|
|
|
* IP 工具类
|
|
|
- * <p>
|
|
|
- * 依赖于ip2region.xdb精简版,来源于<a href="https://gitee.com/zhijiantianya/ip2region"/>
|
|
|
- * region精简为城市编码
|
|
|
*
|
|
|
- * @author 芋道源码
|
|
|
+ * IP 数据源来自 ip2region.xdb 精简版,基于 <a href="https://gitee.com/zhijiantianya/ip2region"/> 项目
|
|
|
+ *
|
|
|
+ * @author wanglhup
|
|
|
*/
|
|
|
@Slf4j
|
|
|
public class IPUtils {
|
|
|
|
|
|
/**
|
|
|
- * 根据ip搜索
|
|
|
- * 启动加载到内存中
|
|
|
+ * 初始化 SEARCHER
|
|
|
*/
|
|
|
- private static Searcher SEARCHER;
|
|
|
+ @SuppressWarnings("InstantiationOfUtilityClass")
|
|
|
+ private final static IPUtils INSTANCE = new IPUtils();
|
|
|
|
|
|
/**
|
|
|
- * 初始化SEARCHER
|
|
|
+ * IP 查询器,启动加载到内存中
|
|
|
*/
|
|
|
- private final static IPUtils INSTANCE = new IPUtils();
|
|
|
-
|
|
|
+ private static Searcher SEARCHER;
|
|
|
|
|
|
/**
|
|
|
* 私有化构造
|
|
|
*/
|
|
|
private IPUtils() {
|
|
|
- String dbPath = "ip2region.xdb";
|
|
|
- dbPath = IPUtils.class.getClassLoader().getResource(dbPath).getPath();
|
|
|
try {
|
|
|
- SEARCHER = Searcher.newWithBuffer(Searcher.loadContentFromFile(dbPath));
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
+ byte[] bytes = ResourceUtil.readBytes("ip2region.xdb");
|
|
|
+ SEARCHER = Searcher.newWithBuffer(bytes);
|
|
|
+ log.info("启动加载 IPUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
|
|
|
} catch (IOException e) {
|
|
|
- // 加载xdb文件异常,不影响启动
|
|
|
- log.error("启动加载IP SEARCH异常", e);
|
|
|
- SEARCHER = null;
|
|
|
+ log.error("启动加载 IPUtils 失败", e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 查询IP对应的地区ID,格式应为127.0.0.1
|
|
|
- * @param ip ip地址
|
|
|
+ * 查询 IP 对应的地区编号
|
|
|
+ *
|
|
|
+ * @param ip IP 地址,格式为 127.0.0.1
|
|
|
* @return 地区id
|
|
|
*/
|
|
|
+ @SneakyThrows
|
|
|
public static Integer getAreaId(String ip) {
|
|
|
- try {
|
|
|
- return Integer.parseInt(SEARCHER.search(ip));
|
|
|
- } catch (Exception e) {
|
|
|
- throw new ServiceException(INTERNAL_SERVER_ERROR);
|
|
|
- }
|
|
|
+ return Integer.parseInt(SEARCHER.search(ip));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查询IP对应的地区ID,格式参考{@link Searcher#checkIP(String)} 的返回
|
|
|
- * @param ip ip地址
|
|
|
- * @return 地区id
|
|
|
+ * 查询 IP 对应的地区编号
|
|
|
+ *
|
|
|
+ * @param ip IP 地址的时间戳,格式参考{@link Searcher#checkIP(String)} 的返回
|
|
|
+ * @return 地区编号
|
|
|
*/
|
|
|
+ @SneakyThrows
|
|
|
public static Integer getAreaId(long ip) {
|
|
|
- try {
|
|
|
- return Integer.parseInt(SEARCHER.search(ip));
|
|
|
- } catch (Exception e) {
|
|
|
- throw new ServiceException(INTERNAL_SERVER_ERROR);
|
|
|
- }
|
|
|
+ return Integer.parseInt(SEARCHER.search(ip));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查询IP对应的地区,格式应为127.0.0.1
|
|
|
- * @param ip ip地址
|
|
|
+ * 查询 IP 对应的地区
|
|
|
+ *
|
|
|
+ * @param ip IP 地址,格式为 127.0.0.1
|
|
|
* @return 地区
|
|
|
*/
|
|
|
public static Area getArea(String ip) {
|
|
@@ -84,8 +76,9 @@ public class IPUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查询IP对应的地区,格式参考{@link Searcher#checkIP(String)} 的返回
|
|
|
- * @param ip ip地址
|
|
|
+ * 查询 IP 对应的地区
|
|
|
+ *
|
|
|
+ * @param ip IP 地址的时间戳,格式参考{@link Searcher#checkIP(String)} 的返回
|
|
|
* @return 地区
|
|
|
*/
|
|
|
public static Area getArea(long ip) {
|