This commit is contained in:
jack ning
2025-01-17 13:55:59 +08:00
parent 1b28859a21
commit cc0bb170c0
7 changed files with 146 additions and 57 deletions

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2024-12-24 17:44:03
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 12:20:00
* @LastEditTime: 2025-01-17 13:53: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.
@@ -16,8 +16,8 @@ package com.bytedesk.core.ip.access;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bytedesk.core.ip.IpService;
import com.bytedesk.core.ip.black.IpBlacklistEntity;
import com.bytedesk.core.ip.black.IpBlacklistRepository;
import com.bytedesk.core.ip.black.IpBlacklistService;
import com.bytedesk.core.ip.white.IpWhitelistRepository;
import com.bytedesk.core.uid.UidUtils;
@@ -25,6 +25,7 @@ import com.bytedesk.core.uid.UidUtils;
import lombok.AllArgsConstructor;
import java.time.LocalDateTime;
import java.util.Optional;
@Service
@Transactional
@@ -32,15 +33,16 @@ import java.time.LocalDateTime;
public class IpAccessService {
private static final int MAX_REQUESTS_PER_MINUTE = 60;
private static final int BLOCK_HOURS = 24;
private final IpAccessRepository ipAccessRepository;
private final IpBlacklistRepository blacklistRepository;
private final IpWhitelistRepository whitelistRepository;
private final IpBlacklistService ipBlacklistService;
private final IpService ipService;
private final UidUtils uidUtils;
public boolean isIpBlocked(String ip) {
@@ -50,8 +52,8 @@ public class IpAccessService {
}
// 检查是否在黑名单中且未过期
IpBlacklistEntity blacklist = blacklistRepository.findByIp(ip);
if (blacklist != null && blacklist.getEndTime().isAfter(LocalDateTime.now())) {
Optional<IpBlacklistEntity> blacklist = ipBlacklistService.findByIp(ip);
if (blacklist.isPresent() && blacklist.get().getEndTime().isAfter(LocalDateTime.now())) {
return true;
}
@@ -87,7 +89,10 @@ public class IpAccessService {
ipAccessRepository.save(access);
// 检查是否需要加入黑名单
if (access.getAccessCount() > MAX_REQUESTS_PER_MINUTE) {
ipBlacklistService.addToBlacklist(ip);
//
String ipLocation = ipService.getIpLocation(ip);
LocalDateTime endTime = LocalDateTime.now().plusHours(BLOCK_HOURS);
ipBlacklistService.addToBlacklist(ip, ipLocation, endTime, "Exceeded maximum request rate");
}
}

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2024-12-24 17:43:52
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 11:11:26
* @LastEditTime: 2025-01-17 13:55:02
* @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.
@@ -39,4 +39,7 @@ public class IpBlacklistEntity extends BaseEntity {
private LocalDateTime startTime;
private LocalDateTime endTime;
private String reason;
}

View File

@@ -1,8 +1,23 @@
/*
* @Author: jackning 270580156@qq.com
* @Date: 2024-12-24 17:49:12
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 13:48:52
* @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.
* Business Source License 1.1: https://github.com/Bytedesk/bytedesk/blob/main/LICENSE
* contact: 270580156@qq.com
*
* Copyright (c) 2025 by bytedesk.com, All Rights Reserved.
*/
package com.bytedesk.core.ip.black;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
public interface IpBlacklistRepository extends JpaRepository<IpBlacklistEntity, Long> {
IpBlacklistEntity findByIp(String ip);
}
Optional<IpBlacklistEntity> findByIp(String ip);
}

View File

@@ -2,7 +2,7 @@
* @Author: jackning 270580156@qq.com
* @Date: 2024-12-24 22:19:09
* @LastEditors: jackning 270580156@qq.com
* @LastEditTime: 2025-01-17 10:52:50
* @LastEditTime: 2025-01-17 13:52:59
* @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,6 +14,7 @@
package com.bytedesk.core.ip.black;
import java.time.LocalDateTime;
import java.util.Optional;
import org.springframework.stereotype.Service;
@@ -29,19 +30,18 @@ public class IpBlacklistService {
private final UidUtils uidUtils;
private static final int BLOCK_HOURS = 24;
public IpBlacklistEntity findByIp(String ip) {
public Optional<IpBlacklistEntity> findByIp(String ip) {
return ipBlacklistRepository.findByIp(ip);
}
public void addToBlacklist(String ip) {
public void addToBlacklist(String ip, String ipLocation, LocalDateTime endTime, String reason) {
IpBlacklistEntity blacklist = new IpBlacklistEntity();
blacklist.setIp(ip);
blacklist.setUid(uidUtils.getUid());
blacklist.setStartTime(LocalDateTime.now());
blacklist.setEndTime(LocalDateTime.now().plusHours(BLOCK_HOURS));
blacklist.setReason("Exceeded maximum request rate");
blacklist.setEndTime(endTime);
blacklist.setReason(reason);
blacklist.setIpLocation(ipLocation);
save(blacklist);
}