From 85636fdabf1e835b47969d8cace0eca9b9783e64 Mon Sep 17 00:00:00 2001 From: gaojie Date: Thu, 25 Apr 2019 18:29:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8D=E5=AD=97=E5=90=88=E6=B3=95=E6=80=A7?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jieling/logic/player/PlayerLogic.java | 60 +++++++++++++++++-- .../java/com/ljsd/jieling/util/EmojiUtil.java | 39 ++++++++++++ .../ljsd/jieling/util/ShieldedWordUtils.java | 42 +++++++++++++ .../main/java/network/client/NettyClient.java | 21 +++++-- 4 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 serverlogic/src/main/java/com/ljsd/jieling/util/EmojiUtil.java create mode 100644 serverlogic/src/main/java/com/ljsd/jieling/util/ShieldedWordUtils.java diff --git a/serverlogic/src/main/java/com/ljsd/jieling/logic/player/PlayerLogic.java b/serverlogic/src/main/java/com/ljsd/jieling/logic/player/PlayerLogic.java index 3fcf18b3d..8a287e4cb 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/logic/player/PlayerLogic.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/logic/player/PlayerLogic.java @@ -8,18 +8,19 @@ import com.ljsd.jieling.logic.dao.PlayerManager; import com.ljsd.jieling.logic.dao.TeamPosManager; import com.ljsd.jieling.logic.dao.UserManager; import com.ljsd.jieling.logic.dao.root.User; +import com.ljsd.jieling.logic.mail.MailLogic; import com.ljsd.jieling.network.session.ISession; import com.ljsd.jieling.protocols.MessageTypeProto; -import com.ljsd.jieling.util.MathUtils; -import com.ljsd.jieling.util.MessageUtil; -import com.ljsd.jieling.util.SensitivewordFilter; +import com.ljsd.jieling.util.*; import org.luaj.vm2.ast.Str; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; public class PlayerLogic { - + private static final Logger LOGGER = LoggerFactory.getLogger(MailLogic.class); private PlayerLogic(){} public static PlayerLogic getInstance(){ @@ -63,6 +64,9 @@ public class PlayerLogic { User user = UserManager.getUser(uid); PlayerManager playerInfoManager = user.getPlayerInfoManager(); TeamPosManager teamPosManager = user.getTeamPosManager(); +// if (checkName(iSession, name, msgId)){ +// return; +// } if (type == 1) { // 检查名字是否被占用 if (PlayerLogic.getInstance().isExistName(name)) { @@ -82,9 +86,57 @@ public class PlayerLogic { MessageUtil.sendMessage(iSession,1,msgId,null,true); } + private boolean checkName(ISession iSession, String name, int msgId) throws Exception { + int length = getStrLength(name); + if (length == -1) { + MessageUtil.sendErrorResponse(iSession, 0, msgId, ""); + return true; + } + if (length > 14 || length < 1) { + MessageUtil.sendErrorResponse(iSession, 0, msgId, ""); + return true; + } + boolean result = ShieldedWordUtils.checkName( name); + if (!result) { + MessageUtil.sendErrorResponse(iSession, 0, msgId, ""); + return true; + } + return false; + } + private boolean isExistName(String name) { String key = RedisKey.getKey(RedisKey.C_User_Name_Key, name, false); Object myUid = RedisUtil.getInstence().get(key); return myUid != null ; } + + /** + * 获取字符串的长度,对双字符(包括汉字)按两位计数 + * + * @param value + * @return + */ + private int getStrLength(String value) { + int valueLength = 0; + String chinese = "[\u0391-\uFFE5]"; + for (int i = 0; i < value.length(); i++) { + String temp = value.substring(i, i + 1); + + if (EmojiUtil.isContainEmoji(temp)) { + LOGGER.error("==========> error , because of Emoji..."); + return -1;// 如果含有敏感词汇 -1 + } + + if (temp.matches(chinese)) { + valueLength += 2; + } else { + valueLength += 1; + } + + if (valueLength > 12) { + return valueLength; + } + } + return valueLength; + } } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/EmojiUtil.java b/serverlogic/src/main/java/com/ljsd/jieling/util/EmojiUtil.java new file mode 100644 index 000000000..512da4fb2 --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/EmojiUtil.java @@ -0,0 +1,39 @@ +package com.ljsd.jieling.util; + +/** + * Created by Administrator on 2016/4/26. + */ +public class EmojiUtil { + + public static String getFinalStr(String scource){ + for (int i = 0; i < scource.length(); i++) { + char c = scource.charAt(i); + if (!isEmojiCharacter(c)) { + scource = scource.replace(c,'*'); + } + } + return scource; + } + + public static boolean isContainEmoji(String scource){ + for (int i = 0; i < scource.length(); i++) { + char c = scource.charAt(i); + if (!isEmojiCharacter(c)) { + return true; + } + } + return false; + } + + + private static boolean isEmojiCharacter(char codePoint) { + return (codePoint == 0x0) || + (codePoint == 0x9) || + (codePoint == 0xA) || + (codePoint == 0xD) || + ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || + ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || + ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)); + } + +} diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/ShieldedWordUtils.java b/serverlogic/src/main/java/com/ljsd/jieling/util/ShieldedWordUtils.java new file mode 100644 index 000000000..8afb92147 --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/ShieldedWordUtils.java @@ -0,0 +1,42 @@ +package com.ljsd.jieling.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class ShieldedWordUtils { + + public static boolean checkName(String name){ + boolean result = validateUserName(name); + if(!result){ + return false; + } + result = isContaintSensitiveWord(name); + if(result){ + return false; + } + return true; + } + + /** + * 验证用户名,支持中英文(包括全角字符)、数字、下划线和减号 (全角及汉字算两位),长度为4-20位,中文按二位计数 + * + * @param userName + * @return + * @author www.zuidaima.com + */ + public static boolean validateUserName(String userName) { + String validateStr = "^[\\\\w\\\\--_[A-Za-z0-9]\\u4e00-\\u9fa5\\uFF21-\\uFF3A\\uFF41-\\uFF5A]+$"; + return matcher(validateStr, userName); + } + + private static boolean matcher(String reg, String string) { + Pattern pattern = Pattern.compile(reg); + Matcher matcher = pattern.matcher(string); + return matcher.matches(); + } + + public static boolean isContaintSensitiveWord(String userName){ + boolean hasDirtyWords = SensitivewordFilter.isContaintSensitiveWord(userName, 1); + return hasDirtyWords; + } +} diff --git a/test/src/main/java/network/client/NettyClient.java b/test/src/main/java/network/client/NettyClient.java index acfda4620..41efd1034 100644 --- a/test/src/main/java/network/client/NettyClient.java +++ b/test/src/main/java/network/client/NettyClient.java @@ -21,7 +21,7 @@ public class NettyClient { private static final Logger LOGGER = LoggerFactory.getLogger(NettyClient.class); - private static int uid = 40000072; + private static int uid = 10003124; private static int msgIndex = 1; private static int token=543242; @@ -45,6 +45,10 @@ public class NettyClient { //--------------------------------------------------------------------------------------------------------- handler.sendRequest_jieling(makeFinalMessage(gameMessage, MessageTypeProto.MessageType.LOGIN_REQUEST_VALUE)); +// for (int i = 0; i < 100; i++){ +// handler.sendRequest_jieling(makeFinalMessage(gameMessage, MessageTypeProto.MessageType.RANDOMNAME_REQUEST_VALUE)); +// } + handler.sendRequest_jieling(makeFinalMessage(reName(), MessageTypeProto.MessageType.RENAME_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(getHero(), MessageTypeProto.MessageType.GET_HEROINFO_REQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(getRandomHero(), MessageTypeProto.MessageType.HERO_RAND_REQQUEST_VALUE)); // handler.sendRequest_jieling(makeFinalMessage(gmRequest(11001), MessageTypeProto.MessageType.GM_REQUEST_VALUE)); @@ -63,9 +67,18 @@ public class NettyClient { } catch (Exception e) { e.printStackTrace(); }*/ - handler.sendRequest_jieling(makeFinalMessage(createEquip(3001), MessageTypeProto.MessageType.WORKSHOP_EQUIP_CREATE_REQUEST_VALUE)); +// handler.sendRequest_jieling(makeFinalMessage(createEquip(3001), MessageTypeProto.MessageType.WORKSHOP_EQUIP_CREATE_REQUEST_VALUE)); + } + + private static MessageLite reName() { + PlayerInfoProto.ReNameRequest reNameRequest = PlayerInfoProto.ReNameRequest.newBuilder() + .setType(1) + .setName("哈哈哈哈") + .build(); + return reNameRequest; + } private static MessageLite heroComperRequest(int itemId, int itemNum) { @@ -99,7 +112,7 @@ public class NettyClient { Tea.intToByte(packetData, 0, uid); //uid Tea.intToByte(packetData, 4, token); //token Tea.intToByte(packetData, 8,msgNum); //msgId - Tea.intToByte(packetData, 12, msgIndex); // msgIndex + Tea.intToByte(packetData, 12, msgIndex++); // msgIndex System.arraycopy(messageData, 0, packetData, 16, messageData.length); int[] secretKey = Tea.KEY; @@ -108,7 +121,7 @@ public class NettyClient { } public static CommonProto.GMCommand gmRequest(int itemId){ - return CommonProto.GMCommand.newBuilder().setCommand("2#"+itemId+"#1").build(); + return CommonProto.GMCommand.newBuilder().setCommand("8#"+0+"#1").build(); } public HeroInfoProto.UpHeroStarRequest upHeroStar(){