From 2f2e5c327813679b8b6636d6e0491d5d5a7fb5d6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 16 Jan 2017 20:45:24 +1100 Subject: [PATCH] Warn when the user doesn't provide a valid uri to /imgprint and /newlvl heightmap theme --- MCGalaxy/Blocks/Block.Permissions.cs | 6 ++-- MCGalaxy/Chat/Colors.cs | 6 ++-- MCGalaxy/Commands/Moderation/CmdTempRank.cs | 6 ++-- MCGalaxy/Generator/HeightmapGen.cs | 9 ++++-- MCGalaxy/MCGalaxy_.csproj | 1 + MCGalaxy/Server/Server.Fields.cs | 2 +- MCGalaxy/Server/Tasks/ServerTasks.cs | 2 +- MCGalaxy/util/IO/Paths.cs | 33 +++++++++++++++++++++ 8 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 MCGalaxy/util/IO/Paths.cs diff --git a/MCGalaxy/Blocks/Block.Permissions.cs b/MCGalaxy/Blocks/Block.Permissions.cs index 30881f9e4..ac7a70cd9 100644 --- a/MCGalaxy/Blocks/Block.Permissions.cs +++ b/MCGalaxy/Blocks/Block.Permissions.cs @@ -41,8 +41,8 @@ namespace MCGalaxy BlockBehaviour.SetupCoreHandlers(); // Custom permissions set by the user. - if (File.Exists("properties/block.properties")) { - string[] lines = File.ReadAllLines("properties/block.properties"); + if (File.Exists(Paths.BlockPermsFile)) { + string[] lines = File.ReadAllLines(Paths.BlockPermsFile); if (lines.Length > 0 && lines[0] == "#Version 2") { LoadVersion2(lines); } else { @@ -148,7 +148,7 @@ namespace MCGalaxy } static void SaveBlocksCore(IEnumerable givenList) { - using (StreamWriter w = new StreamWriter("properties/block.properties")) { + using (StreamWriter w = new StreamWriter(Paths.BlockPermsFile)) { w.WriteLine("#Version 2"); w.WriteLine("# This file dictates which ranks may use what blocks"); w.WriteLine("# If someone has royally screwed up the ranks, just delete this file and let the server restart"); diff --git a/MCGalaxy/Chat/Colors.cs b/MCGalaxy/Chat/Colors.cs index dca46ce2c..47be838de 100644 --- a/MCGalaxy/Chat/Colors.cs +++ b/MCGalaxy/Chat/Colors.cs @@ -242,7 +242,7 @@ namespace MCGalaxy { } internal static void SaveExtColors() { - using (StreamWriter w = new StreamWriter("text/customcolors.txt")) { + using (StreamWriter w = new StreamWriter(Paths.CustomColorsFile)) { foreach (CustomColor col in ExtColors) { if (col.Undefined) continue; w.WriteLine(col.Code + " " + col.Fallback + " " + col.Name + " " + @@ -252,8 +252,8 @@ namespace MCGalaxy { } internal static void LoadExtColors() { - if (!File.Exists("text/customcolors.txt")) return; - string[] lines = File.ReadAllLines("text/customcolors.txt"); + if (!File.Exists(Paths.CustomColorsFile)) return; + string[] lines = File.ReadAllLines(Paths.CustomColorsFile); CustomColor col = default(CustomColor); for (int i = 0; i < lines.Length; i++) { diff --git a/MCGalaxy/Commands/Moderation/CmdTempRank.cs b/MCGalaxy/Commands/Moderation/CmdTempRank.cs index 59df79fbc..81b1bddf7 100644 --- a/MCGalaxy/Commands/Moderation/CmdTempRank.cs +++ b/MCGalaxy/Commands/Moderation/CmdTempRank.cs @@ -96,7 +96,7 @@ namespace MCGalaxy.Commands.Moderation { StringBuilder all = new StringBuilder(); Player who = PlayerInfo.Find(name); - foreach (string line in File.ReadAllLines("text/tempranks.txt")) { + foreach (string line in File.ReadAllLines(Paths.TempRanksFile)) { if (!line.CaselessStarts(name)) { all.AppendLine(line); continue; } string[] parts = line.Split(' '); @@ -115,7 +115,7 @@ namespace MCGalaxy.Commands.Moderation { } static void Info(Player p, string name) { - foreach (string line in File.ReadAllLines("text/tempranks.txt")) { + foreach (string line in File.ReadAllLines(Paths.TempRanksFile)) { if (!line.CaselessStarts(name)) continue; PrintTempRankInfo(p, line); return; } @@ -124,7 +124,7 @@ namespace MCGalaxy.Commands.Moderation { static void List(Player p) { int count = 0; - foreach (string line in File.ReadAllLines("text/tempranks.txt")) { + foreach (string line in File.ReadAllLines(Paths.TempRanksFile)) { if (count == 0) Player.Message(p, "&ePlayers with a temporary rank assigned:"); PrintTempRankInfo(p, line); diff --git a/MCGalaxy/Generator/HeightmapGen.cs b/MCGalaxy/Generator/HeightmapGen.cs index 5b569110d..f0958510e 100644 --- a/MCGalaxy/Generator/HeightmapGen.cs +++ b/MCGalaxy/Generator/HeightmapGen.cs @@ -26,13 +26,18 @@ namespace MCGalaxy.Generator { public static bool DownloadImage(string url, string dir, Player p) { if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); - if (!url.StartsWith("http://") && !url.StartsWith("https://")) + if (!url.CaselessStarts("http://") && !url.CaselessStarts("https://")) url = "http://" + url; + Uri uri; + if (!Uri.TryCreate(url, UriKind.Absolute, out uri)) { + Player.Message(p, "{0} is not a valid URL.", url); return false; + } + try { using (WebClient client = new WebClient()) { Player.Message(p, "Downloading file from: &f" + url); - client.DownloadFile(url, dir + "tempImage_" + p.name + ".bmp"); + client.DownloadFile(uri, dir + "tempImage_" + p.name + ".bmp"); } Player.Message(p, "Finished downloading image."); return true; diff --git a/MCGalaxy/MCGalaxy_.csproj b/MCGalaxy/MCGalaxy_.csproj index c42188f51..689834330 100644 --- a/MCGalaxy/MCGalaxy_.csproj +++ b/MCGalaxy/MCGalaxy_.csproj @@ -621,6 +621,7 @@ + diff --git a/MCGalaxy/Server/Server.Fields.cs b/MCGalaxy/Server/Server.Fields.cs index f3052c0ec..eb5f3476a 100644 --- a/MCGalaxy/Server/Server.Fields.cs +++ b/MCGalaxy/Server/Server.Fields.cs @@ -221,7 +221,7 @@ namespace MCGalaxy { [ConfigInt("ip-spam-count", "Spam control", null, 25, 0, 1000)] public static int IPSpamCount = 10; [ConfigInt("ip-spam-block-time", "Spam control", null, 30, 0, 1000)] - public static int IPSpamBlockTime = 300; + public static int IPSpamBlockTime = 180; [ConfigInt("ip-spam-interval", "Spam control", null, 1, 0, 1000)] public static int IPSpamInterval = 60; diff --git a/MCGalaxy/Server/Tasks/ServerTasks.cs b/MCGalaxy/Server/Tasks/ServerTasks.cs index 175029263..161c16b96 100644 --- a/MCGalaxy/Server/Tasks/ServerTasks.cs +++ b/MCGalaxy/Server/Tasks/ServerTasks.cs @@ -126,7 +126,7 @@ namespace MCGalaxy.Tasks { internal static void TemprankExpiry(SchedulerTask task) { Player[] players = PlayerInfo.Online.Items; - foreach (string line in File.ReadAllLines("text/tempranks.txt")) + foreach (string line in File.ReadAllLines(Paths.TempRanksFile)) foreach (Player p in players) { if (!line.CaselessStarts(p.name)) continue; diff --git a/MCGalaxy/util/IO/Paths.cs b/MCGalaxy/util/IO/Paths.cs new file mode 100644 index 000000000..552aeae91 --- /dev/null +++ b/MCGalaxy/util/IO/Paths.cs @@ -0,0 +1,33 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + http://www.opensource.org/licenses/ecl2.php + http://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace MCGalaxy { + + public static class Paths { + + public const string CustomColorsFile = "text/customcolors.txt"; + + public const string BlockPermsFile = "properties/block.properties"; + + public const string TempRanksFile = "text/tempranks.txt"; + } +}