diff --git a/Barotrauma/BarotraumaShared/Source/Networking/Client.cs b/Barotrauma/BarotraumaShared/Source/Networking/Client.cs index 80965f69a..f190311fb 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/Client.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/Client.cs @@ -134,17 +134,19 @@ namespace Barotrauma.Networking JobPreferences = new List(JobPrefab.List.GetRange(0, Math.Min(JobPrefab.List.Count, 3))); } - public static bool IsValidName(string name) + public static bool IsValidName(string name, GameServer server) { if (name.Contains("\n") || name.Contains("\r")) return false; + if (name.Any(c => c == ';' || c == ',' || c == '<' || c == '/')) return false; - return (name.All(c => - c != ';' && - c != ',' && - c != '<' && - c != '/')); + foreach (char character in name) + { + if (!server.AllowedClientNameChars.Any(charRange => (int)character >= charRange.First && (int)character <= charRange.Second)) return false; + } + + return true; } - + public static string SanitizeName(string name) { name = name.Trim(); @@ -155,16 +157,8 @@ namespace Barotrauma.Networking string rName = ""; for (int i = 0; i < name.Length; i++) { - if (name[i] < 32) - { - rName += '?'; - } - else - { - rName += name[i]; - } + rName += name[i] < 32 ? '?' : name[i]; } - return rName; } diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServerLogin.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServerLogin.cs index 56d21c33d..317b21583 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServerLogin.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServerLogin.cs @@ -165,7 +165,7 @@ namespace Barotrauma.Networking DebugConsole.NewMessage(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (not in whitelist)", Color.Red); return; } - if (!Client.IsValidName(clName)) + if (!Client.IsValidName(clName, this)) { DisconnectUnauthClient(inc, unauthClient, "Your name contains illegal symbols."); Log(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (invalid name)", ServerLog.MessageType.Error); diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs index 087259b39..3e3708352 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs @@ -281,6 +281,15 @@ namespace Barotrauma.Networking private set; } + /// + /// A list of int pairs that represent the ranges of UTF-16 codes allowed in client names + /// + public List> AllowedClientNameChars + { + get; + private set; + } = new List>(); + private void SaveSettings() { XDocument doc = new XDocument(new XElement("serversettings")); @@ -302,6 +311,8 @@ namespace Barotrauma.Networking doc.Root.SetAttributeValue("AllowedRandomMissionTypes", string.Join(",", AllowedRandomMissionTypes)); + doc.Root.SetAttributeValue("AllowedClientNameChars", string.Join(",", AllowedClientNameChars.Select(c => c.First + "-" + c.Second))); + #if SERVER doc.Root.SetAttributeValue("password", password); #endif @@ -361,6 +372,36 @@ namespace Barotrauma.Networking TraitorsEnabled = traitorsEnabled; GameMain.NetLobbyScreen.SetTraitorsEnabled(traitorsEnabled); + //"65-90", "97-122", "48-59" = upper and lower case english alphabet and numbers + string[] allowedClientNameCharsStr = doc.Root.GetAttributeStringArray("AllowedClientNameChars", new string[] { "65-90", "97-122", "48-59" }); + foreach (string allowedClientNameCharRange in allowedClientNameCharsStr) + { + string[] splitRange = allowedClientNameCharRange.Split('-'); + if (splitRange.Length == 0 || splitRange.Length > 2) + { + DebugConsole.ThrowError("Error in server settings - "+ allowedClientNameCharRange+" is not a valid range for characters allowed in client names."); + continue; + } + + int min = -1; + if (!int.TryParse(splitRange[0], out min)) + { + DebugConsole.ThrowError("Error in server settings - " + allowedClientNameCharRange + " is not a valid range for characters allowed in client names."); + continue; + } + int max = min; + if (splitRange.Length == 2) + { + if (!int.TryParse(splitRange[1], out max)) + { + DebugConsole.ThrowError("Error in server settings - " + allowedClientNameCharRange + " is not a valid range for characters allowed in client names."); + continue; + } + } + + if (min > -1 && max > -1) AllowedClientNameChars.Add(Pair.Create(min, max)); + } + AllowedRandomMissionTypes = doc.Root.GetAttributeStringArray( "AllowedRandomMissionTypes", MissionPrefab.MissionTypes.ToArray()).ToList(); diff --git a/Barotrauma/BarotraumaShared/serversettings.xml b/Barotrauma/BarotraumaShared/serversettings.xml index 1823f3dcf..7bcf40f11 100644 --- a/Barotrauma/BarotraumaShared/serversettings.xml +++ b/Barotrauma/BarotraumaShared/serversettings.xml @@ -32,4 +32,5 @@ TraitorsEnabled="No" autobantime="60" maxautobantime="360" + AllowedClientNameChars="32-33,38-46,48-57,65-90,91,93,95-122,192-255,384-591,1024-1279" />