From 2d7f344b1ad67887515a84457439e0c98296652d Mon Sep 17 00:00:00 2001 From: heavyrian2012 Date: Tue, 5 Aug 2025 17:11:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E7=B3=BB=E7=BB=9F=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=87=8C=E8=AE=BE=E7=BD=AE=E9=82=A3=E4=B9=88=E5=90=8D?= =?UTF-8?q?=E5=AD=97=E4=B8=8D=E8=83=BD=E8=A2=AB=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../h2/V50__update_group_member_index.sql | 2 +- .../h2/V51__add_not_allow_name_setting.sql | 1 + .../V58__reupdate_group_member_index.sql | 2 + .../mysql/V59__add_no_allow_name_setting.sql | 1 + .../imhandler/CreateGroupHandler.java | 14 ++++-- .../imhandler/ModifyGroupAliasHandler.java | 14 ++++-- .../ModifyGroupMemberAliasHandler.java | 14 ++++-- .../persistence/MemoryMessagesStore.java | 48 +++++++++++++++---- .../java/io/moquette/spi/IMessagesStore.java | 2 + .../cn/wildfirechat/proto/ProtoConstants.java | 1 + .../main/java/cn/wildfirechat/sdk/Main.java | 30 ++++++++---- 11 files changed, 100 insertions(+), 29 deletions(-) create mode 100755 broker/migrate/h2/V51__add_not_allow_name_setting.sql create mode 100755 broker/migrate/mysql/V58__reupdate_group_member_index.sql create mode 100755 broker/migrate/mysql/V59__add_no_allow_name_setting.sql diff --git a/broker/migrate/h2/V50__update_group_member_index.sql b/broker/migrate/h2/V50__update_group_member_index.sql index df34b7a1..670f30a6 100755 --- a/broker/migrate/h2/V50__update_group_member_index.sql +++ b/broker/migrate/h2/V50__update_group_member_index.sql @@ -1 +1 @@ -CREATE INDEX `user_gid_mid_dt_index` ON `t_group_member` (`_gid`, `_mid`, `_dt`); +CREATE INDEX `user_gid_dt_index` ON `t_group_member` (`_gid`, `_dt`); diff --git a/broker/migrate/h2/V51__add_not_allow_name_setting.sql b/broker/migrate/h2/V51__add_not_allow_name_setting.sql new file mode 100755 index 00000000..ee4a2920 --- /dev/null +++ b/broker/migrate/h2/V51__add_not_allow_name_setting.sql @@ -0,0 +1 @@ +insert into t_settings(`id`, `_value`,`_desc`) values (2, '文件传输助手,客服,官方', '禁止使用名称,如果有多个,以英文逗号隔开。直接修改数据库,不用重启IM服务'); diff --git a/broker/migrate/mysql/V58__reupdate_group_member_index.sql b/broker/migrate/mysql/V58__reupdate_group_member_index.sql new file mode 100755 index 00000000..db450f81 --- /dev/null +++ b/broker/migrate/mysql/V58__reupdate_group_member_index.sql @@ -0,0 +1,2 @@ +DROP INDEX `user_gid_mid_dt_index` ON `t_group_member`; +CREATE INDEX `user_gid_dt_index` ON `t_group_member` (`_gid`, `_dt`); diff --git a/broker/migrate/mysql/V59__add_no_allow_name_setting.sql b/broker/migrate/mysql/V59__add_no_allow_name_setting.sql new file mode 100755 index 00000000..ee4a2920 --- /dev/null +++ b/broker/migrate/mysql/V59__add_no_allow_name_setting.sql @@ -0,0 +1 @@ +insert into t_settings(`id`, `_value`,`_desc`) values (2, '文件传输助手,客服,官方', '禁止使用名称,如果有多个,以英文逗号隔开。直接修改数据库,不用重启IM服务'); diff --git a/broker/src/main/java/io/moquette/imhandler/CreateGroupHandler.java b/broker/src/main/java/io/moquette/imhandler/CreateGroupHandler.java index 0b90c87d..fb11902a 100644 --- a/broker/src/main/java/io/moquette/imhandler/CreateGroupHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/CreateGroupHandler.java @@ -69,10 +69,16 @@ public class CreateGroupHandler extends GroupHandler matched = m_messagesStore.handleSensitiveWord(request.getGroup().getGroupInfo().getName()); - if (matched != null && !matched.isEmpty()) { - return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + if(!isAdmin && !StringUtil.isNullOrEmpty(request.getGroup().getGroupInfo().getName())) { + if(!m_messagesStore.isSensitiveOnlyMessage()) { + Set matched = m_messagesStore.handleSensitiveWord(request.getGroup().getGroupInfo().getName()); + if (matched != null && !matched.isEmpty()) { + return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + } + } + + if(!m_messagesStore.isAllowName(request.getGroup().getGroupInfo().getName())) { + return ErrorCode.ERROR_CODE_NOT_RIGHT; } } diff --git a/broker/src/main/java/io/moquette/imhandler/ModifyGroupAliasHandler.java b/broker/src/main/java/io/moquette/imhandler/ModifyGroupAliasHandler.java index b26b13b7..a8f6a27e 100644 --- a/broker/src/main/java/io/moquette/imhandler/ModifyGroupAliasHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/ModifyGroupAliasHandler.java @@ -34,10 +34,16 @@ public class ModifyGroupAliasHandler extends GroupHandler matched = m_messagesStore.handleSensitiveWord(request.getAlias()); - if (matched != null && !matched.isEmpty()) { - return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + if (!isAdmin && !StringUtil.isNullOrEmpty(request.getAlias())) { + if(!m_messagesStore.isSensitiveOnlyMessage()) { + Set matched = m_messagesStore.handleSensitiveWord(request.getAlias()); + if (matched != null && !matched.isEmpty()) { + return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + } + } + + if(!m_messagesStore.isAllowName(request.getAlias())) { + return ErrorCode.ERROR_CODE_NOT_RIGHT; } } diff --git a/broker/src/main/java/io/moquette/imhandler/ModifyGroupMemberAliasHandler.java b/broker/src/main/java/io/moquette/imhandler/ModifyGroupMemberAliasHandler.java index 93a1c219..628a8138 100644 --- a/broker/src/main/java/io/moquette/imhandler/ModifyGroupMemberAliasHandler.java +++ b/broker/src/main/java/io/moquette/imhandler/ModifyGroupMemberAliasHandler.java @@ -32,10 +32,16 @@ public class ModifyGroupMemberAliasHandler extends GroupHandler 0 && requestSourceType == ProtoConstants.RequestSourceType.Request_From_Robot && !m_messagesStore.isAllowRobotCustomGroupNotification()) { return ErrorCode.ERROR_CODE_NOT_RIGHT; } - if (!m_messagesStore.isSensitiveOnlyMessage() && !isAdmin && !StringUtil.isNullOrEmpty(request.getAlias())) { - Set matched = m_messagesStore.handleSensitiveWord(request.getAlias()); - if (matched != null && !matched.isEmpty()) { - return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + if (!isAdmin && !StringUtil.isNullOrEmpty(request.getAlias())) { + if(!m_messagesStore.isSensitiveOnlyMessage()) { + Set matched = m_messagesStore.handleSensitiveWord(request.getAlias()); + if (matched != null && !matched.isEmpty()) { + return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + } + } + + if(!m_messagesStore.isAllowName(request.getAlias())) { + return ErrorCode.ERROR_CODE_NOT_RIGHT; } } diff --git a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java index 03e60102..a990ec3e 100755 --- a/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java +++ b/broker/src/main/java/io/moquette/persistence/MemoryMessagesStore.java @@ -1980,10 +1980,16 @@ public class MemoryMessagesStore implements IMessagesStore { return ErrorCode.ERROR_CODE_NOT_RIGHT; } - if (!mSensitiveOnlyMessage && modifyType == Modify_Group_Name && !StringUtil.isNullOrEmpty(value)) { - Set matched = handleSensitiveWord(value); - if (matched != null && !matched.isEmpty()) { - return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + if (modifyType == Modify_Group_Name && !StringUtil.isNullOrEmpty(value)) { + if(!mSensitiveOnlyMessage) { + Set matched = handleSensitiveWord(value); + if (matched != null && !matched.isEmpty()) { + return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + } + } + + if(!isAllowName(value)) { + return ErrorCode.ERROR_CODE_NOT_RIGHT; } } } @@ -2822,12 +2828,19 @@ public class MemoryMessagesStore implements IMessagesStore { ) { switch (entry.getType()) { case Modify_DisplayName: - if(!mSensitiveOnlyMessage) { - Set matched = handleSensitiveWord(entry.getValue()); - if (matched != null && !matched.isEmpty()) { - return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + if(!StringUtil.isNullOrEmpty(entry.getValue())) { + if (!mSensitiveOnlyMessage) { + Set matched = handleSensitiveWord(entry.getValue()); + if (matched != null && !matched.isEmpty()) { + return ErrorCode.ERROR_CODE_SENSITIVE_MATCHED; + } + } + + if(!isAllowName(entry.getValue())) { + return ErrorCode.ERROR_CODE_NOT_RIGHT; } } + builder.setDisplayName(entry.getValue()); modified = true; break; @@ -3919,6 +3932,25 @@ public class MemoryMessagesStore implements IMessagesStore { return ErrorCode.ERROR_CODE_SUCCESS; } + @Override + public boolean isAllowName(String name) { + if(StringUtil.isNullOrEmpty(name)) { + return true; + } + + name = name.trim(); + SystemSettingPojo notAllowNameSetting = getSystemSetting(ProtoConstants.SystemSettingType.NOT_ALLOW_USER_NAMES); + if (notAllowNameSetting != null && !StringUtil.isNullOrEmpty(notAllowNameSetting.value)) { + String[] ss = notAllowNameSetting.value.split(","); + for (String s : ss) { + if(name.contains(s.trim())) { + return false; + } + } + } + return true; + } + @Override public ErrorCode handleJoinChatroom(String userId, String clientId, String chatroomId) { IMap chatroomInfoMap = m_Server.getHazelcastInstance().getMap(CHATROOMS); diff --git a/broker/src/main/java/io/moquette/spi/IMessagesStore.java b/broker/src/main/java/io/moquette/spi/IMessagesStore.java index 73807150..5af7270f 100755 --- a/broker/src/main/java/io/moquette/spi/IMessagesStore.java +++ b/broker/src/main/java/io/moquette/spi/IMessagesStore.java @@ -190,6 +190,8 @@ public interface IMessagesStore { ErrorCode setFriendAliasRequest(String fromUser, String targetUserId, String alias, long[] head); ErrorCode setFriendExtraRequest(String fromUser, String targetUserId, String extra, long[] head); + boolean isAllowName(String name); + ErrorCode handleJoinChatroom(String userId, String clientId, String chatroomId); ErrorCode handleQuitChatroom(String userId, String clientId, String chatroomId); diff --git a/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java b/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java index 840ec552..45b74b67 100644 --- a/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java +++ b/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java @@ -226,6 +226,7 @@ public class ProtoConstants { public interface SystemSettingType { int Group_Max_Member_Count = 1; + int NOT_ALLOW_USER_NAMES = 2; } public interface SearchUserType { diff --git a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java index 75b76631..50df5a90 100644 --- a/sdk/src/main/java/cn/wildfirechat/sdk/Main.java +++ b/sdk/src/main/java/cn/wildfirechat/sdk/Main.java @@ -58,6 +58,9 @@ public class Main { //测试解析数据库中消息内容 testReadMessageContentFromDB(); + //计算消息分表 + testMessageSharding(); + //Robot和Channel都是使用的80端口,第三方可以创建或者为第三方创建,第三方可以使用robot或者channel与IM系统进行对接。 testRobot(); testChannel(); @@ -616,7 +619,7 @@ public class Main { System.exit(-1); } - voidIMResult = GroupAdmin.setGroupMemberAlias("user1", groupInfo.getTarget_id(), "user3", "test user3", null, null); + voidIMResult = GroupAdmin.setGroupMemberAlias("user1", groupInfo.getTarget_id(), "user2", "test user2", null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("set group member alias success"); } else { @@ -1003,13 +1006,24 @@ public class Main { testStreamingText(sender, conversation); } - static void testReadMessageContentFromDB() throws Exception { - //需要注意几点: - // 1 WFCMessage类不能混淆,如果混淆 WFCMessage.MessageContent.parseFrom(data) 就会失败。 - // 2 MessageContentFactory方法会自动查找所有的消息类,包括文本/图片/语音等。也有可能在某些环境下找不到这些类。可以检查MessageContentFactory - // 类的源码,修改registerAllMessageContent方法,确保能找到所有的内容类。也可以显式调用注册的方法把所有已知消息都注册一遍,比如: - // MessageContentFactory.registerCustomMessageContent(TextMessageContent.class); //这里示例只写了文本的,其他所有已知消息都需要注册一遍。 + //消息相关表分为2类,分表是:t_messages_x和t_user_messages_y,并且是分表存储的。表内存储数据和分表规则请参考 https://docs.wildfirechat.cn/faq/server.html 问题2 + static void testMessageSharding() { + String userId = "user1"; + int hashId = Math.abs(userId.hashCode())%128; + String userMessageTable = "t_user_messages_" + hashId; + System.out.println("user:" + userId + " user messages table is " + userMessageTable); + Calendar calendar = Calendar.getInstance(); + Date date = new Date(); + calendar.setTime(date); + int month = calendar.get(Calendar.MONTH); + int year = calendar.get(Calendar.YEAR); + year %= 3; + String messageTable = "t_messages_" + (year * 12 + month); + System.out.println("This month save message to table " + messageTable); + } + + static void testReadMessageContentFromDB() throws Exception { //从数据库t_messages_x表中读取到消息内容字段_data的二进制数据为 byte[] data = {8,1,18,5,72,101,108,108,111,64,3}; //1. 先把二进制数据转化为协议栈消息内容。 @@ -1605,7 +1619,7 @@ public class Main { System.exit(-1); } - voidIMResult = robotService.setGroupMemberAlias(groupInfo.getTarget_id(), "user3", "test user3", null, null); + voidIMResult = robotService.setGroupMemberAlias(groupInfo.getTarget_id(), "user2", "test user2", null, null); if (voidIMResult != null && voidIMResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { System.out.println("set group member alias success"); } else {