com.myb.common.util.Iputil Maven / Gradle / Ivy
package com.myb.common.util;
import cn.hutool.core.io.resource.ClassPathResource;
import com.myb.common.constant.CommonConstant;
import com.myb.common.enmus.CommonEnum;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import sun.applet.Main;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.InetAddress;
import java.net.URL;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
/**
* @Author meiyubin
* @Date 2023/11/29
* @DESC ip工具类
*/
@Component
public class Iputil {
public static String getAddress(String ip) {
if(StringUtils.isEmpty(ip)||!isValidIP(ip)){
return CommonEnum.IP_FORMAT_ERROR.getDesc();
}
// 1、创建 searcher 对象 (修改为离线库路径)
// ClassPathResource resource = new ClassPathResource(CommonConstant.IP_FILE_PATH);
InputStream inputStream = Iputil.class.getResourceAsStream(CommonConstant.IP_FILE_PATH);
String filePath = "./file.xdb";
try {
if(!new File(filePath).exists()){
convertStreamToFile(inputStream, filePath);}
} catch (IOException e) {
throw new RuntimeException(e);
}
String dbPath =filePath;
String region="";
Searcher searcher = null;
try {
// dbPath = Paths.get(resourceUrl.toURI()).toString();
searcher = Searcher.newWithFileOnly(dbPath);
} catch (Exception e) {
return CommonEnum.FILE_PATH_NOT_FIND.getDesc();
}
try {
long sTime = System.nanoTime();
region = searcher.search(ip);
long cost = TimeUnit.NANOSECONDS.toMicros((long) (System.nanoTime() - sTime));
} catch (Exception e) {
}
// 3、关闭资源
try {
searcher.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return region;
}
public static boolean isValidIP(String ip) {
try {
InetAddress inetAddr = InetAddress.getByName(ip);
return true;
} catch (Exception e) {
return false;
}
}
public static void convertStreamToFile(InputStream inputStream, String filePath) throws IOException {
try (OutputStream outputStream = new FileOutputStream(filePath)) {
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
}
}
/***
* 获取客户端ip地址
* @param request
*/
public static String getIP(final HttpServletRequest request) throws Exception {
if (request == null) {
throw (new Exception("getIpAddr method HttpServletRequest Object is null"));
}
String ipStr = request.getHeader("x-forwarded-for");
if (StringUtils.isEmpty(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
ipStr = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
ipStr = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(ipStr) || "unknown".equalsIgnoreCase(ipStr)) {
ipStr = request.getRemoteAddr();
}
// 多个路由时,取第一个非unknown的ip
final String[] arr = ipStr.split(",");
for (final String str : arr) {
if (!"unknown".equalsIgnoreCase(str)) {
ipStr = str;
break;
}
}
//目的是将localhost访问对应的ip 0:0:0:0:0:0:0:1 转成 127.0.0.1。
return ipStr.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ipStr;
}
}