From 4ca9a26bddb170eccf7f0add8cbb9ad1f674a711 Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 13 Jun 2018 11:38:12 -0400 Subject: [PATCH] Added name validation --- .../src/io/anuke/annotations/Annotations.java | 2 +- core/assets/bundles/bundle.properties | 1 + core/src/io/anuke/mindustry/Vars.java | 2 + .../io/anuke/mindustry/core/NetServer.java | 50 +++++++++++++++++++ .../src/io/anuke/mindustry/net/NetEvents.java | 9 +++- core/src/io/anuke/mindustry/net/Packets.java | 2 +- .../mindustry/ui/dialogs/JoinDialog.java | 3 +- .../mindustry/ui/fragments/MenuFragment.java | 4 +- 8 files changed, 67 insertions(+), 6 deletions(-) diff --git a/annotations/src/io/anuke/annotations/Annotations.java b/annotations/src/io/anuke/annotations/Annotations.java index e267cdd8d3..8fb07932b6 100644 --- a/annotations/src/io/anuke/annotations/Annotations.java +++ b/annotations/src/io/anuke/annotations/Annotations.java @@ -19,7 +19,7 @@ public class Annotations { Loc targets() default Loc.server; /**Specifies which methods are generated. Only affects server-to-client methods.*/ Variant variants() default Variant.all; - /**The local locations where this method is called.*/ + /**The local locations where this method is called locally, when invoked.*/ Loc called() default Loc.none; /**Whether to forward this packet to all other clients upon recieval. Server only.*/ boolean forward() default false; diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index b589c25abd..cff5faecb2 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -50,6 +50,7 @@ text.server.kicked.serverOutdated=Outdated server! Ask the host to update! text.server.kicked.banned=You are banned on this server. text.server.kicked.recentKick=You have been kicked recently.\nWait before connecting again. text.server.kicked.nameInUse=There is someone with that name\nalready on this server. +text.server.kicked.nameEmpty=Your name must contain at least one character or number. text.server.kicked.idInUse=You are already on this server! Connecting with two accounts is not permitted. text.server.connected={0} has joined. text.server.disconnected={0} has disconnected. diff --git a/core/src/io/anuke/mindustry/Vars.java b/core/src/io/anuke/mindustry/Vars.java index 6747c6279d..c61258ff5d 100644 --- a/core/src/io/anuke/mindustry/Vars.java +++ b/core/src/io/anuke/mindustry/Vars.java @@ -73,6 +73,8 @@ public class Vars{ public static boolean showBlockDebug = false; public static final int maxTextLength = 150; + public static final int maxNameLength = 40; + public static final int maxCharNameLength = 20; public static boolean headless = false; diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index 54fcf74e45..62600f0697 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -1,5 +1,7 @@ package io.anuke.mindustry.core; +import com.badlogic.gdx.graphics.Color; +import com.badlogic.gdx.graphics.Colors; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Base64Coder; import com.badlogic.gdx.utils.IntMap; @@ -98,6 +100,13 @@ public class NetServer extends Module{ } } + packet.name = fixName(packet.name); + + if(packet.name.trim().length() <= 0){ + kick(id, KickReason.nameEmpty); + return; + } + Log.info("Recieved connect packet for player '{0}' / UUID {1} / IP {2}", packet.name, uuid, trace.ip); String ip = Net.getConnection(id).address; @@ -201,6 +210,47 @@ public class NetServer extends Module{ return connections.get(connectionID).uuid; } + String fixName(String name){ + + for(int i = 0; i < name.length(); i ++){ + if(name.charAt(i) == '[' && i != name.length() - 1 && name.charAt(i + 1) != '[' && (i == 0 || name.charAt(i - 1) != '[')){ + String prev = name.substring(0, i); + String next = name.substring(i); + String result = checkColor(next); + + name = prev + result; + } + } + + return name.substring(0, Math.min(name.length(), maxNameLength)); + } + + String checkColor(String str){ + + for(int i = 1; i < str.length(); i ++){ + if(str.charAt(i) == ']'){ + String color = str.substring(1, i); + + if(Colors.get(color.toUpperCase()) != null || Colors.get(color.toLowerCase()) != null){ + Color result = (Colors.get(color.toLowerCase()) == null ? Colors.get(color.toUpperCase()) : Colors.get(color.toLowerCase())); + if(result.a <= 0.8f){ + return str.substring(i + 1); + } + }else{ + try{ + Color result = Color.valueOf(color); + if(result.a <= 0.8f){ + return str.substring(i + 1); + } + }catch (Exception e){ + return str; + } + } + } + } + return str; + } + void sync(){ try { diff --git a/core/src/io/anuke/mindustry/net/NetEvents.java b/core/src/io/anuke/mindustry/net/NetEvents.java index 8aeb9a524c..749513a3a4 100644 --- a/core/src/io/anuke/mindustry/net/NetEvents.java +++ b/core/src/io/anuke/mindustry/net/NetEvents.java @@ -6,18 +6,23 @@ import io.anuke.annotations.Annotations.Variant; import io.anuke.mindustry.Vars; import io.anuke.mindustry.entities.Player; +import static io.anuke.mindustry.Vars.maxTextLength; import static io.anuke.mindustry.Vars.playerGroup; public class NetEvents { - @Remote(called = Loc.both, targets = Loc.both) + @Remote(called = Loc.server, targets = Loc.both, forward = true) public static void sendMessage(Player player, String message){ + if(message.length() > maxTextLength){ + throw new ValidateException(player, "Player has sent a message above the text limit."); + } + if(Vars.ui != null){ Vars.ui.chatfrag.addMessage(message, player == null ? null : colorizeName(player.id, player.name)); } } - @Remote(called = Loc.both, variants = Variant.both) + @Remote(called = Loc.server, variants = Variant.both, forward = true) public static void sendMessage(String message){ if(Vars.ui != null){ Vars.ui.chatfrag.addMessage(message, null); diff --git a/core/src/io/anuke/mindustry/net/Packets.java b/core/src/io/anuke/mindustry/net/Packets.java index 91f992529d..4088610683 100644 --- a/core/src/io/anuke/mindustry/net/Packets.java +++ b/core/src/io/anuke/mindustry/net/Packets.java @@ -137,7 +137,7 @@ public class Packets { } public enum KickReason{ - kick, invalidPassword, clientOutdated, serverOutdated, banned, gameover(true), recentKick, nameInUse, idInUse, fastShoot; + kick, invalidPassword, clientOutdated, serverOutdated, banned, gameover(true), recentKick, nameInUse, idInUse, fastShoot, nameEmpty; public final boolean quiet; KickReason(){ quiet = false; } diff --git a/core/src/io/anuke/mindustry/ui/dialogs/JoinDialog.java b/core/src/io/anuke/mindustry/ui/dialogs/JoinDialog.java index 7294c62b5a..99c66c0414 100644 --- a/core/src/io/anuke/mindustry/ui/dialogs/JoinDialog.java +++ b/core/src/io/anuke/mindustry/ui/dialogs/JoinDialog.java @@ -22,6 +22,7 @@ import io.anuke.ucore.util.Bundles; import io.anuke.ucore.util.Log; import io.anuke.ucore.util.Strings; +import static io.anuke.mindustry.Vars.maxNameLength; import static io.anuke.mindustry.Vars.players; import static io.anuke.mindustry.Vars.ui; @@ -211,7 +212,7 @@ public class JoinDialog extends FloatingDialog { player.name = text; Settings.put("name", text); Settings.save(); - }).grow().pad(8).get().setMaxLength(40); + }).grow().pad(8).get().setMaxLength(maxNameLength); ImageButton button = t.addImageButton("white", 40, () -> { new ColorPickDialog().show(color -> { diff --git a/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java b/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java index a89a723f5e..7d6429c41d 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/MenuFragment.java @@ -58,7 +58,9 @@ public class MenuFragment implements Fragment{ new imagebutton("icon-play-2", isize, ui.levels::show).text("$text.play").padTop(4f); - new imagebutton("icon-tutorial", isize, () -> {}).text("$text.tutorial").padTop(4f); + new imagebutton("icon-tutorial", isize, () -> { + + }).text("$text.tutorial").padTop(4f); new imagebutton("icon-load", isize, ui.load::show).text("$text.load").padTop(4f);