Commit aa428362 by HlQ

[add] IP 归属地添加省份

1 parent 3e318684
...@@ -2,6 +2,7 @@ package vion.utils.ip; ...@@ -2,6 +2,7 @@ package vion.utils.ip;
import com.maxmind.db.CHMCache; import com.maxmind.db.CHMCache;
import com.maxmind.geoip2.DatabaseReader; import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.record.AbstractNamedRecord;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.io.file.FileUtil; import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
...@@ -12,6 +13,7 @@ import java.net.InetAddress; ...@@ -12,6 +13,7 @@ import java.net.InetAddress;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author vion * @author vion
...@@ -200,16 +202,18 @@ public class IPUtil { ...@@ -200,16 +202,18 @@ public class IPUtil {
var asn = asnReader.asn(address); var asn = asnReader.asn(address);
var asnNum = asn.getAutonomousSystemNumber(); var asnNum = asn.getAutonomousSystemNumber();
var asnOrg = asn.getAutonomousSystemOrganization(); var asnOrg = asn.getAutonomousSystemOrganization();
var city = cityReader.city(address); var city = cityReader.city(address);
var cityName = city.getCity().getName(); var cityName = city.getCity().getName();
var province = city.getSubdivisions().stream().map(AbstractNamedRecord::getName).collect(Collectors.joining(","));
var country = cityReader.country(address); var country = cityReader.country(address);
var countryName = country.getCountry().getName(); var countryName = country.getCountry().getName();
var asnStr = asnMap.getOrDefault(asnNum.intValue(), asnOrg); var asnStr = asnMap.getOrDefault(asnNum.intValue(), asnOrg);
return StrUtil.format("Country:{}, City:{}, ASN:{}", countryName, cityName, asnStr); return StrUtil.format("Country:{}, Province:{}, City:{}, ASN:{}", countryName, province, cityName, asnStr);
} catch (Exception e) { } catch (Exception e) {
log.error("获取IP归属地失败:[{}],error:{}", ip, e.getMessage()); log.error("获取IP归属地失败:[{}],error:{}", ip, e.getMessage());
} }
return StrUtil.format("Country:{}, City:{}, ASN:{}", "Unknown", "Unknown", "Unknown"); return StrUtil.format("Country:{}, Province:{}, City:{}, ASN:{}", "Unknown", "Unknown", "Unknown", "Unknown");
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!