diff --git a/modules/core/src/main/java/com/bytedesk/core/ip/access/IpAccessService.java b/modules/core/src/main/java/com/bytedesk/core/ip/access/IpAccessService.java index 82fc5dabd6..b50ecfdf16 100644 --- a/modules/core/src/main/java/com/bytedesk/core/ip/access/IpAccessService.java +++ b/modules/core/src/main/java/com/bytedesk/core/ip/access/IpAccessService.java @@ -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 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"); } } diff --git a/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistEntity.java b/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistEntity.java index e5577f4551..41f6c16a18 100644 --- a/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistEntity.java +++ b/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistEntity.java @@ -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; + + + } \ No newline at end of file diff --git a/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistRepository.java b/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistRepository.java index 10f62fd3db..57f142b3b4 100644 --- a/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistRepository.java +++ b/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistRepository.java @@ -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 findByIp(String ip); -} \ No newline at end of file + Optional findByIp(String ip); +} diff --git a/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistService.java b/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistService.java index 2a6ddf615e..58127ce8d1 100644 --- a/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistService.java +++ b/modules/core/src/main/java/com/bytedesk/core/ip/black/IpBlacklistService.java @@ -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 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); } diff --git a/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEntity.java b/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEntity.java index a3d7c42416..e771741098 100644 --- a/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEntity.java +++ b/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEntity.java @@ -2,7 +2,7 @@ * @Author: jackning 270580156@qq.com * @Date: 2024-01-29 16:21:24 * @LastEditors: jackning 270580156@qq.com - * @LastEditTime: 2025-01-17 12:25:17 + * @LastEditTime: 2025-01-17 13:46: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. @@ -50,10 +50,7 @@ import lombok.experimental.Accessors; public class VisitorEntity extends BaseEntity { private static final long serialVersionUID = 1L; - - /** - * developers can set basic visitor info - */ + private String nickname; @Builder.Default diff --git a/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEventListener.java b/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEventListener.java index b4122a4a15..0bbbfd45de 100644 --- a/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEventListener.java +++ b/modules/service/src/main/java/com/bytedesk/service/visitor/VisitorEventListener.java @@ -2,7 +2,7 @@ * @Author: jackning 270580156@qq.com * @Date: 2024-09-07 13:16:52 * @LastEditors: jackning 270580156@qq.com - * @LastEditTime: 2025-01-17 11:23:21 + * @LastEditTime: 2025-01-17 13:54:12 * @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,6 +16,7 @@ package com.bytedesk.service.visitor; import java.time.Duration; import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @@ -23,6 +24,7 @@ import org.springframework.stereotype.Component; import com.bytedesk.core.black.BlackEntity; import com.bytedesk.core.black.event.BlackCreateEvent; import com.bytedesk.core.black.event.BlackUpdateEvent; +import com.bytedesk.core.ip.black.IpBlacklistService; import com.bytedesk.core.quartz.event.QuartzFiveMinEvent; import lombok.AllArgsConstructor; @@ -35,23 +37,24 @@ public class VisitorEventListener { private final VisitorRestService visitorService; + private final IpBlacklistService ipBlacklistService; + @EventListener public void onBlackCreateEvent(BlackCreateEvent event) { log.info("IpBlacklistEventListener onBlackCreateEvent: " + event); BlackEntity blackEntity = event.getBlackEntity(); if (blackEntity.isBlockIp()) { - - - // IpBlacklistRequest ipBlacklistRequest = IpBlacklistRequest.builder() - // .ip(blackEntity.getIp()) - // .ipLocation(blackEntity.getIpLocation()) - // .startTime(blackEntity.getStartTime()) - // .endTime(blackEntity.getEndTime()) - // .reason(blackEntity.getReason()) - // .build(); - // ipBlacklistService.createIpBlacklist(ipBlacklistRequest); + Optional visitorEntity = visitorService.findByUid(blackEntity.getUid()); + if (visitorEntity.isPresent()) { + // 添加到黑名单 + ipBlacklistService.addToBlacklist( + visitorEntity.get().getIp(), + visitorEntity.get().getIpLocation(), + blackEntity.getEndTime(), + blackEntity.getReason() + ); + } } - } @EventListener diff --git a/starter/src/main/resources/templates/ftl/dev.ftl b/starter/src/main/resources/templates/ftl/dev.ftl index 382fe9d2d1..1fe370cd4a 100644 --- a/starter/src/main/resources/templates/ftl/dev.ftl +++ b/starter/src/main/resources/templates/ftl/dev.ftl @@ -4,40 +4,106 @@ - 微语 + ByteDesk - - 网站
-

演示

- 管理后台
- 客户端
- 访客端
-

开发

- api
- 监控
- 流式输出测试
- Druid数据库监控(默认账号:admin@email.com,密码:admin)
- + + \ No newline at end of file