update modules/service: mod 5 files

This commit is contained in:
jack ning
2025-03-06 12:48:55 +08:00
parent b209b3a2b9
commit 4fef8eae10
5 changed files with 27 additions and 112 deletions

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2022-03-10 14:41:11
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2024-10-29 21:34:58
* @LastEditTime: 2025-03-06 12:35:41
* @Description: bytedesk.com https://github.com/Bytedesk/bytedesk
* Please be aware of the BSL license restrictions before installing Bytedesk IM
* selling, reselling, or hosting Bytedesk IM as a service is a breach of the terms and automatically terminates your rights under the license.
@@ -14,13 +14,13 @@
*/
package com.bytedesk.core.ip;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import lombok.extern.slf4j.Slf4j;
@@ -33,49 +33,27 @@ import lombok.extern.slf4j.Slf4j;
@Configuration
public class IP2RegionConfig {
// @Value("${bytedesk.ip2region-db-path}")
// private String dbPath;
private static final String DB_FILE_NAME = "ip2region.xdb";
@Bean
public Searcher searcher() {
// 获取当前Java进程的工作目录Working Directory
// String currentPath = System.getProperty("user.dir");
String dbPath = IP2RegionConfig.class.getClassLoader().getResource("ip2region.xdb").getPath();
// String dbPath = currentPath + "/ip2region.xdb";
log.info("ip2region path {}", dbPath);
// 打成 jar 包后无法正常读取jar包内的ip2region.xdb文件需要将ip2region.xdb文件复制到临时目录
File file = new File(dbPath);
if (!file.exists()) {
// classpath:ip2region.xdb
log.error("Error: Invalid ip2region.xdb file");
String tmpDir = System.getProperties().getProperty("java.io.tmpdir");
dbPath = tmpDir + "ip.db";
log.info("temp dir {}", dbPath);
file = new File(dbPath);
try {
FileUtils.copyInputStreamToFile(IP2RegionConfig.class.getClassLoader().getResourceAsStream("ip2region.xdb"), file);
} catch (IOException e) {
e.printStackTrace();
try {
// 1. 直接从 classpath 读取文件到内存
ClassPathResource resource = new ClassPathResource(DB_FILE_NAME);
try (InputStream inputStream = resource.getInputStream()) {
// 2. 将文件内容读入字节数组
byte[] cBuff = StreamUtils.copyToByteArray(inputStream);
if (cBuff == null || cBuff.length == 0) {
log.error("Failed to read ip2region.xdb content");
return null;
}
// 3. 创建查询对象
return Searcher.newWithBuffer(cBuff);
}
}
// 1、从 dbPath 加载整个 xdb 到内存。
byte[] cBuff = null;
try {
cBuff = Searcher.loadContentFromFile(dbPath);
} catch (Exception e) {
log.error("failed to load content from `%s`: %s\n", dbPath, e);
log.error("Failed to initialize ip2region searcher: {}", e.getMessage());
return null;
}
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
Searcher searcher = null;
try {
searcher = Searcher.newWithBuffer(cBuff);
} catch (Exception e) {
log.error("failed to create content cached searcher: %s\n", e);
}
return searcher;
}
}

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2024-03-16 13:28:03
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-15 14:30:59
* @LastEditTime: 2025-03-06 12:40:50
* @Description: bytedesk.com https://github.com/Bytedesk/bytedesk
* Please be aware of the BSL license restrictions before installing Bytedesk IM
* selling, reselling, or hosting Bytedesk IM as a service is a breach of the terms and automatically terminates your rights under the license.
@@ -68,67 +68,6 @@ public class IpService {
return getIpLocation(ip);
}
// 封禁IP, TODO: cache区分org
// @CachePut(value = "ip", key = "#ipRequest.ip")
// public IpResponse blockIp(IpRequest ipRequest) {
// //
// Optional<IpEntity> ipOptional = findByOrgUid(ipRequest.getOrgUid());
// if (ipOptional.isPresent()) {
// // 更新
// if (StringUtils.hasText(ipRequest.getIp())) {
// ipOptional.get().getIps().add(ipRequest.getIp());
// }
// ipOptional.get().setIpRangeStart(ipRequest.getIpRangeStart());
// ipOptional.get().setIpRangeEnd(ipRequest.getIpRangeEnd());
// ipOptional.get().setType(IpTypeEnum.fromValue(ipRequest.getType()).name());
// ipOptional.get().setUntilDate(DateUtils.formatStringToDateTime(ipRequest.getUntilDate()));
// ipOptional.get().setReason(ipRequest.getReason());
// IpEntity savedIp = save(ipOptional.get());
// if (savedIp == null) {
// throw new RuntimeException("failed to update ip");
// }
// return convertToResponse(ipOptional.get());
// }
// //
// IpEntity ip = modelMapper.map(ipRequest, IpEntity.class);
// ip.setUid(uidUtils.getCacheSerialUid());
// if (StringUtils.hasText(ipRequest.getIp())) {
// ip.getIps().add(ipRequest.getIp());
// }
// ip.setUntilDate(DateUtils.formatStringToDateTime(ipRequest.getUntilDate()));
// //
// IpEntity savedIp = save(ip);
// if (savedIp == null) {
// throw new RuntimeException("failed to save ip");
// }
// //
// return convertToResponse(ip);
// }
// public IpResponse unblockIp(IpRequest ipRequest) {
// Optional<IpEntity> ipOptional = findByOrgUid(ipRequest.getOrgUid());
// if (ipOptional.isPresent()) {
// ipOptional.get().getIps().remove(ipRequest.getIp());
// IpEntity savedIp = save(ipOptional.get());
// if (savedIp == null) {
// throw new RuntimeException("failed to unblock ip");
// }
// return convertToResponse(ipOptional.get());
// }
// throw new RuntimeException("failed to unblock ip");
// }
// public Boolean isBlocked(HttpServletRequest request) {
// String ip = IpUtils.getIp(request);
// return isBlocked(ip, "");
// }
// TODO: cache区分org
@Cacheable(value = "ip", key = "#ip-#orgUid")
public Boolean isBlocked(String ip, String orgUid) {