diff --git a/MCGalaxy/Chat/ChatTokens.cs b/MCGalaxy/Chat/ChatTokens.cs index a5f9c333c..22515afc4 100644 --- a/MCGalaxy/Chat/ChatTokens.cs +++ b/MCGalaxy/Chat/ChatTokens.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; +using MCGalaxy.Network; using MCGalaxy.Util; namespace MCGalaxy { @@ -95,7 +96,7 @@ namespace MCGalaxy { static string TokenDate(Player p) { return DateTime.Now.ToString("yyyy-MM-dd"); } static string TokenTime(Player p) { return DateTime.Now.ToString("HH:mm:ss"); } static string TokenIP(Player p) { return p.ip; } - static string TokenServerIP(Player p) { return Player.IsLocalIpAddress(p.ip) ? p.ip : Server.IP; } + static string TokenServerIP(Player p) { return HttpUtil.IsLocalIP(p.ip) ? p.ip : Server.IP; } static string TokenColor(Player p) { return p.color; } static string TokenRank(Player p) { return p.group.Name; } static string TokenLevel(Player p) { return p.level == null ? null : p.level.name; } diff --git a/MCGalaxy/Commands/Moderation/CmdLocation.cs b/MCGalaxy/Commands/Moderation/CmdLocation.cs index 1461ac4ce..ac0137c80 100644 --- a/MCGalaxy/Commands/Moderation/CmdLocation.cs +++ b/MCGalaxy/Commands/Moderation/CmdLocation.cs @@ -41,7 +41,7 @@ namespace MCGalaxy.Commands.Moderation { target = match.name; ip = match.ip; } - if (Player.IPInPrivateRange(ip)) { + if (HttpUtil.IsPrivateIP(ip)) { Player.Message(p, Colors.red + "Player has an internal IP, cannot trace"); return; } diff --git a/MCGalaxy/CorePlugin/ConnectingHandler.cs b/MCGalaxy/CorePlugin/ConnectingHandler.cs index 0eec5700f..399d4ac60 100644 --- a/MCGalaxy/CorePlugin/ConnectingHandler.cs +++ b/MCGalaxy/CorePlugin/ConnectingHandler.cs @@ -15,6 +15,7 @@ permissions and limitations under the Licenses. using System; using System.Security.Cryptography; using MCGalaxy.Events; +using MCGalaxy.Network; namespace MCGalaxy.Core { internal static class ConnectingHandler { @@ -82,7 +83,7 @@ namespace MCGalaxy.Core { string hashHex = BitConverter.ToString(hash); if (!mppass.CaselessEq(hashHex.Replace("-", ""))) { - if (!Player.IPInPrivateRange(p.ip)) { + if (!HttpUtil.IsPrivateIP(p.ip)) { p.Leave(null, "Login failed! Close the game and sign in again.", true); return false; } } else { @@ -126,7 +127,7 @@ namespace MCGalaxy.Core { if (Server.vip.Contains(p.name)) return true; Player[] online = PlayerInfo.Online.Items; - if (online.Length >= ServerConfig.MaxPlayers && !Player.IPInPrivateRange(p.ip)) { + if (online.Length >= ServerConfig.MaxPlayers && !HttpUtil.IsPrivateIP(p.ip)) { p.Leave(null, "Server full!", true); return false; } if (group.Permission > LevelPermission.Guest) return true; diff --git a/MCGalaxy/CorePlugin/IPThrottler.cs b/MCGalaxy/CorePlugin/IPThrottler.cs index 02feec454..b308a9778 100644 --- a/MCGalaxy/CorePlugin/IPThrottler.cs +++ b/MCGalaxy/CorePlugin/IPThrottler.cs @@ -17,6 +17,7 @@ */ using System; using System.Collections.Generic; +using MCGalaxy.Network; using MCGalaxy.Tasks; namespace MCGalaxy.Core { @@ -27,7 +28,7 @@ namespace MCGalaxy.Core { static readonly object ipsLock = new object(); internal static bool CheckIP(Player p) { - if (!ServerConfig.IPSpamCheck || Player.IsLocalIpAddress(p.ip)) return true; + if (!ServerConfig.IPSpamCheck || HttpUtil.IsLocalIP(p.ip)) return true; DateTime blockedUntil, now = DateTime.UtcNow; lock (ipsLock) { diff --git a/MCGalaxy/Levels/Level.cs b/MCGalaxy/Levels/Level.cs index ba53ace2f..1227d35c2 100644 --- a/MCGalaxy/Levels/Level.cs +++ b/MCGalaxy/Levels/Level.cs @@ -211,12 +211,6 @@ namespace MCGalaxy { return x >= 0 && y >= 0 && z >= 0 && x < Width && y < Height && z < Length; } - [Obsolete] - public static Level Find(string name) { return LevelInfo.Find(name); } - - [Obsolete] - public static Level FindExact(string name) { return LevelInfo.FindExact(name); } - public static void SaveSettings(Level lvl) { if (lvl.IsMuseum) return; // museums do not save properties diff --git a/MCGalaxy/Levels/LevelInfo.cs b/MCGalaxy/Levels/LevelInfo.cs index 88bf83f93..08cbce336 100644 --- a/MCGalaxy/Levels/LevelInfo.cs +++ b/MCGalaxy/Levels/LevelInfo.cs @@ -26,20 +26,6 @@ namespace MCGalaxy { /// Array of all current loaded levels. /// Note this field is highly volatile, you should cache references to the items array. public static VolatileArray Loaded = new VolatileArray(true); - - [Obsolete("Prefer Matcher.FindLevels() or FindExact()")] - public static Level Find(string name) { - Level match = null; int matches = 0; - Level[] loaded = Loaded.Items; - - foreach (Level lvl in loaded) { - if (lvl.name.CaselessEq(name)) return lvl; - if (lvl.name.CaselessContains(name)) { - match = lvl; matches++; - } - } - return matches == 1 ? match : null; - } public static Level FindExact(string name) { Level[] loaded = Loaded.Items; diff --git a/MCGalaxy/Network/Utils/HttpUtil.cs b/MCGalaxy/Network/Utils/HttpUtil.cs index 1c33be7dc..c93635c29 100644 --- a/MCGalaxy/Network/Utils/HttpUtil.cs +++ b/MCGalaxy/Network/Utils/HttpUtil.cs @@ -1,20 +1,17 @@ /* - 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. - */ +Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/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.Net; @@ -53,5 +50,33 @@ namespace MCGalaxy.Network { if (remoteEndPoint.AddressFamily != localIP.AddressFamily) return null; return new IPEndPoint(localIP, 0); } + + public static bool IsPrivateIP(string ip) { + //range of 172.16.0.0 - 172.31.255.255 + if (ip.StartsWith("172.") && (int.Parse(ip.Split('.')[1]) >= 16 && int.Parse(ip.Split('.')[1]) <= 31)) + return true; + return IPAddress.IsLoopback(IPAddress.Parse(ip)) || ip.StartsWith("192.168.") || ip.StartsWith("10."); + //return IsLocalIpAddress(ip); + } + + public static bool IsLocalIP(string ip) { + try { // get host IP addresses + IPAddress[] hostIPs = Dns.GetHostAddresses(ip); + // get local IP addresses + IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); + + // test if any host IP equals to any local IP or to localhost + foreach ( IPAddress hostIP in hostIPs ) { + // is localhost + if ( IPAddress.IsLoopback(hostIP) ) return true; + // is local address + foreach ( IPAddress localIP in localIPs ) { + if ( hostIP.Equals(localIP) ) return true; + } + } + } + catch { } + return false; + } } } \ No newline at end of file diff --git a/MCGalaxy/Player/Player.cs b/MCGalaxy/Player/Player.cs index bc2601cfd..bf862c939 100644 --- a/MCGalaxy/Player/Player.cs +++ b/MCGalaxy/Player/Player.cs @@ -349,12 +349,6 @@ namespace MCGalaxy { [Obsolete("Use PlayerInfo.Online.Items")] public static List players; - - [Obsolete("Use PlayerInfo.Find(name)")] - public static Player Find(string name) { return PlayerInfo.Find(name); } - - [Obsolete("Use PlayerInfo.FindExact(name)")] - public static Player FindExact(string name) { return PlayerInfo.FindExact(name); } public static bool ValidName(string name) { const string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890._+"; @@ -377,34 +371,6 @@ namespace MCGalaxy { public void RevertBlock(ushort x, ushort y, ushort z) { SendBlockchange(x, y, z, level.GetBlock(x, y, z)); } - - public static bool IPInPrivateRange(string ip) { - //range of 172.16.0.0 - 172.31.255.255 - if (ip.StartsWith("172.") && (int.Parse(ip.Split('.')[1]) >= 16 && int.Parse(ip.Split('.')[1]) <= 31)) - return true; - return IPAddress.IsLoopback(IPAddress.Parse(ip)) || ip.StartsWith("192.168.") || ip.StartsWith("10."); - //return IsLocalIpAddress(ip); - } - - public static bool IsLocalIpAddress(string host) { - try { // get host IP addresses - IPAddress[] hostIPs = Dns.GetHostAddresses(host); - // get local IP addresses - IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName()); - - // test if any host IP equals to any local IP or to localhost - foreach ( IPAddress hostIP in hostIPs ) { - // is localhost - if ( IPAddress.IsLoopback(hostIP) ) return true; - // is local address - foreach ( IPAddress localIP in localIPs ) { - if ( hostIP.Equals(localIP) ) return true; - } - } - } - catch { } - return false; - } public void SetMoney(int amount) { money = amount; diff --git a/MCGalaxy/Player/PlayerInfo.cs b/MCGalaxy/Player/PlayerInfo.cs index c4511d910..f23fc321b 100644 --- a/MCGalaxy/Player/PlayerInfo.cs +++ b/MCGalaxy/Player/PlayerInfo.cs @@ -43,22 +43,6 @@ namespace MCGalaxy { return count; } - - [Obsolete("Prefer FindMatches() or FindExact()")] - public static Player Find(string name) { - Player[] players = PlayerInfo.Online.Items; - Player match = null; int matches = 0; - name = name.ToLower(); - - foreach (Player p in players) { - if (p.name.CaselessEq(name)) return p; - if (p.name.CaselessContains(name)) { - match = p; matches++; - } - } - return matches == 1 ? match : null; - } - public static Player FindMatches(Player pl, string name, bool onlyCanSee = true) { int matches = 0; return FindMatches(pl, name, out matches, onlyCanSee); }