Commit 276eac91 by HlQ

[add] 添加 IP 归属地功能

1 parent 58086984
......@@ -3,4 +3,20 @@
## 销帮帮相关接口
1.[Web 登录页面](https://pfweb.xbongbong.com/login.html)
2.[销帮帮 API](https://open.xbongbong.com/openapi)
\ No newline at end of file
2.[销帮帮 API](https://open.xbongbong.com/openapi)
## IP 归属地查询
[国内IP库](https://github.com/ljxi/GeoCN)
[完整数据](https://github.com/P3TERX/GeoLite.mmdb)
项目中使用的是完整数据。[使用方式](https://maxmind.github.io/GeoIP2-java/index.html)。更新IP数据库,需要从完整数据中下载对应的 mmdb 文件,然后替换服务器上对应的文件。
打包的时候排除 mmdb 文件,避免打包过大。但本地运行时,因会排除 mmdb 文件,导致 **IPUtil** 读取文件失败,本地运行可以把下面的配置注释掉。但因为本地 IP 访问,获取 IP 归属地也没有用:)
```xml
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.mmdb</exclude>
</excludes>
</resource>
</resources>
```
\ No newline at end of file
......@@ -93,10 +93,23 @@
<artifactId>myexcel</artifactId>
<version>${myexcel.version}</version>
</dependency>
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies>
<build>
<finalName>VVAS-Task</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.mmdb</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
......
......@@ -19,6 +19,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import vion.utils.ip.IPUtil;
import vion.vo.UserVO;
/**
......@@ -53,10 +54,12 @@ public class LogAspect {
request.setAttribute("startTime", System.currentTimeMillis());
MDC.put("requestId", (String) request.getAttribute("requestId"));
log.info("Request URL:{}, Method:{}, IP:{}, Operator:{}, Args:{}, Body:{}",
var ip = ServletUtil.getClientIP(request);
log.info("Request URL:{}, Method:{}, IP:{}, {}, Operator:{}, Args:{}, Body:{}",
request.getRequestURI(),
request.getMethod(),
ServletUtil.getClientIP(request),
ip,
IPUtil.getIp(ip),
username,
objectMapper.writeValueAsString(request.getParameterMap()),
StrUtil.replaceChars(ServletUtil.getBody(request), " \r\n", "")
......
......@@ -56,6 +56,7 @@ public class ContractRunner {
@Scheduled(cron = "0 0 8-20 * * *")
@Transactional(rollbackFor = Exception.class)
public void contractSync() {
log.info("【开始】从crm系统同步合同信息");
Long modifyTime = Opt.ofNullable((Long) redissonClient.getBucket(RedisKeyEnum.CONTRACT_SYNC_NEW_TIME.getVal()).get()).orElse(0L);
// 只同步申请日期(原CRM中的签订日期)2024-05-07号(含)之后的合同
......
package vion.utils.ip;
import com.maxmind.db.CHMCache;
import com.maxmind.geoip2.DatabaseReader;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author vion
* @date 2024/8/29
*/
@Component
@Slf4j
public class IPUtil {
private static String asnPath;
private static String cityPath;
@Value("${geo.asn.path}")
public void setAsnPath(String asnPath) {
IPUtil.asnPath = asnPath;
}
@Value("${geo.city.path}")
public void setCityPath(String cityPath) {
IPUtil.cityPath = cityPath;
}
public static Map<Integer, String> asnMap = new HashMap<>() {{
put(9812, "东方有线");
put(9389, "中国长城");
put(17962, "天威视讯");
put(17429, "歌华有线");
put(7497, "科技网");
put(24139, "华数");
put(9801, "中关村");
put(4538, "教育网");
put(24151, "CNNIC");
// 添加中国移动ASN映射
put(38019, "中国移动");
put(139080, "中国移动");
put(9808, "中国移动");
put(24400, "中国移动");
put(134810, "中国移动");
put(24547, "中国移动");
put(56040, "中国移动");
put(56041, "中国移动");
put(56042, "中国移动");
put(56044, "中国移动");
put(132525, "中国移动");
put(56046, "中国移动");
put(56047, "中国移动");
put(56048, "中国移动");
put(59257, "中国移动");
put(24444, "中国移动");
put(24445, "中国移动");
put(137872, "中国移动");
put(9231, "中国移动");
put(58453, "中国移动");
// 添加中国电信ASN映射
put(4134, "中国电信");
put(4812, "中国电信");
put(23724, "中国电信");
put(136188, "中国电信");
put(137693, "中国电信");
put(17638, "中国电信");
put(140553, "中国电信");
put(4847, "中国电信");
put(140061, "中国电信");
put(136195, "中国电信");
put(17799, "中国电信");
put(139018, "中国电信");
put(133776, "中国电信");
put(58772, "中国电信");
put(146966, "中国电信");
put(63527, "中国电信");
put(58539, "中国电信");
put(58540, "中国电信");
put(141998, "中国电信");
put(138169, "中国电信");
put(139203, "中国电信");
put(58563, "中国电信");
put(137690, "中国电信");
put(63838, "中国电信");
put(137694, "中国电信");
put(137698, "中国电信");
put(136167, "中国电信");
put(148969, "中国电信");
put(134764, "中国电信");
put(134770, "中国电信");
put(148981, "中国电信");
put(134774, "中国电信");
put(136190, "中国电信");
put(140647, "中国电信");
put(132225, "中国电信");
put(140485, "中国电信");
put(4811, "中国电信");
put(131285, "中国电信");
put(137689, "中国电信");
put(137692, "中国电信");
put(140636, "中国电信");
put(140638, "中国电信");
put(140345, "中国电信");
put(38283, "中国电信");
put(140292, "中国电信");
put(140903, "中国电信");
put(17897, "中国电信");
put(134762, "中国电信");
put(139019, "中国电信");
put(141739, "中国电信");
put(141771, "中国电信");
put(134419, "中国电信");
put(140276, "中国电信");
put(58542, "中国电信");
put(140278, "中国电信");
put(139767, "中国电信");
put(137688, "中国电信");
put(137691, "中国电信");
put(4809, "中国电信");
put(58466, "中国电信");
put(137687, "中国电信");
put(134756, "中国电信");
put(134760, "中国电信");
put(133774, "中国电信");
put(133775, "中国电信");
put(4816, "中国电信");
put(134768, "中国电信");
put(58461, "中国电信");
put(58519, "中国电信");
put(58520, "中国电信");
put(131325, "中国电信");
// 添加中国联通ASN映射
put(4837, "中国联通");
put(4808, "中国联通");
put(134542, "中国联通");
put(134543, "中国联通");
put(10099, "中国联通");
put(140979, "中国联通");
put(138421, "中国联通");
put(17621, "中国联通");
put(17622, "中国联通");
put(17816, "中国联通");
put(140726, "中国联通");
put(17623, "中国联通");
put(136958, "中国联通");
put(9929, "中国联通");
put(58519, "中国联通");
put(140716, "中国联通");
put(4847, "中国联通");
put(136959, "中国联通");
put(135061, "中国联通");
put(139007, "中国联通");
// 添加其他ASN映射
put(59019, "金山云");
put(135377, "优刻云");
put(45062, "网易云");
put(137718, "火山引擎");
put(37963, "阿里云");
put(45102, "阿里云国际");
put(45090, "腾讯云");
put(132203, "腾讯云国际");
put(55967, "百度云");
put(38365, "百度云");
put(58519, "华为云");
put(55990, "华为云");
put(136907, "华为云");
put(4609, "澳門電訊");
put(134773, "珠江宽频");
put(1659, "台湾教育网");
put(8075, "微软云");
put(17421, "中华电信");
put(3462, "HiNet");
put(13335, "Cloudflare");
put(55960, "亚马逊云");
put(14618, "亚马逊云");
put(16509, "亚马逊云");
put(15169, "谷歌云");
put(396982, "谷歌云");
put(36492, "谷歌云");
}};
public static String getIp(String ip) {
var cityDB = FileUtil.file(cityPath);
var asnDB = FileUtil.file(asnPath);
try (DatabaseReader cityReader = new DatabaseReader.Builder(cityDB)
.withCache(new CHMCache())
.locales(List.of("zh-CN")).build();
DatabaseReader asnReader = new DatabaseReader.Builder(asnDB)
.withCache(new CHMCache())
.locales(List.of("zh-CN")).build()) {
InetAddress address = InetAddress.getByName(ip);
var asn = asnReader.asn(address);
var asnNum = asn.getAutonomousSystemNumber();
var asnOrg = asn.getAutonomousSystemOrganization();
var city = cityReader.city(address);
var cityName = city.getCity().getName();
var country = cityReader.country(address);
var countryName = country.getCountry().getName();
var asnStr = asnMap.getOrDefault(asnNum.intValue(), asnOrg);
return StrUtil.format("Country:{}, City:{}, ASN:{}", countryName, cityName, asnStr);
} catch (Exception e) {
log.error("获取IP归属地失败:[{}],error:{}", ip, e.getMessage());
}
return StrUtil.format("Country:{}, City:{}, ASN:{}", "Unknown", "Unknown", "Unknown");
}
}
No preview for this file type
This file is too large to display.
......@@ -23,4 +23,7 @@ ding.appsecret=Ul5UTZqIost_kEAdfZXidvLoZhzvraYurm_g7PCHg-IrDMLHT7mdSgRS1iCHrDPt
xbongbong.corpid=ding6bb660048f7ae2dcee0f45d8e4f7c288
xbongbong.token=c7f9264e9e1687866c37032295552610
geo.asn.path=./GeoLite2-ASN.mmdb
geo.city.path=./GeoLite2-City.mmdb
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
\ No newline at end of file
......@@ -16,6 +16,12 @@ wx.mp.config-storage.redis.password==RtOTnx2V
wx.mp.config-storage.redis.port=6379
wx.mp.config-storage.redis.database=8
fileurl=/nas-data/work-order
fileUrl=/nas-data/work-order
ding.appkey=dingkrzwks0jpi2di3uo
ding.appsecret=Ul5UTZqIost_kEAdfZXidvLoZhzvraYurm_g7PCHg-IrDMLHT7mdSgRS1iCHrDPt
\ No newline at end of file
ding.appsecret=Ul5UTZqIost_kEAdfZXidvLoZhzvraYurm_g7PCHg-IrDMLHT7mdSgRS1iCHrDPt
xbongbong.corpid=ding6bb660048f7ae2dcee0f45d8e4f7c288
xbongbong.token=c7f9264e9e1687866c37032295552610
geo.asn.path=/mnt/vdb1/vion/work-order/work-order/GeoLite2-ASN.mmdb
geo.city.path=/mnt/vdb1/vion/work-order/work-order/GeoLite2-City.mmdb
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!