Provides utility methods for working with IP addresses and ranges.
Beispiel #1
0
        public static PlayerInfo[] FindPlayersCidr([NotNull] IPAddress address, byte range, int limit)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            if (range > 32)
            {
                throw new ArgumentOutOfRangeException("range");
            }
            if (limit < 0)
            {
                throw new ArgumentOutOfRangeException("limit");
            }
            CheckIfLoaded();
            List <PlayerInfo> result = new List <PlayerInfo>();
            int  count      = 0;
            uint addressInt = address.AsUInt();
            uint netMask    = IPAddressUtil.NetMask(range);

            PlayerInfo[] cache = PlayerInfoList;
            for (int i = 0; i < cache.Length; i++)
            {
                if (cache[i].LastIP.Match(addressInt, netMask))
                {
                    result.Add(cache[i]);
                    count++;
                    if (count >= limit)
                    {
                        return(result.ToArray());
                    }
                }
            }
            return(result.ToArray());
        }
        static void ImportBans(Player player, CommandReader cmd)
        {
            string serverName = cmd.Next();
            string fileName   = cmd.Next();

            // Make sure all parameters are specified
            if (serverName == null || fileName == null)
            {
                CdImport.PrintUsage(player);
                return;
            }

            // Check if file exists
            if (!File.Exists(fileName))
            {
                player.Message("File not found: {0}", fileName);
                return;
            }

            string[] names;

            switch (serverName.ToLower())
            {
            case "mcsharp":
            case "mczall":
            case "mclawl":
                try {
                    names = File.ReadAllLines(fileName);
                } catch (Exception ex) {
                    Logger.Log(LogType.Error,
                               "Could not open \"{0}\" to import bans: {1}",
                               fileName, ex);
                    return;
                }
                break;

            default:
                player.Message("fCraft does not support importing from {0}", serverName);
                return;
            }

            if (!cmd.IsConfirmed)
            {
                player.Confirm(cmd, "Import {0} bans from \"{1}\"?",
                               names.Length, Path.GetFileName(fileName));
                return;
            }

            string reason = "(import from " + serverName + ")";

            foreach (string name in names)
            {
                if (Player.IsValidName(name))
                {
                    PlayerInfo info = PlayerDB.FindExact(name) ??
                                      PlayerDB.AddUnrecognizedPlayer(name, RankChangeType.Default);
                    info.Ban(player, reason, true, true);
                }
                else
                {
                    IPAddress ip;
                    if (IPAddressUtil.IsIP(name) && IPAddress.TryParse(name, out ip))
                    {
                        ip.BanIP(player, reason, true, true);
                    }
                    else
                    {
                        player.Message("Could not parse \"{0}\" as either name or IP. Skipping.", name);
                    }
                }
            }

            PlayerDB.Save();
            IPBanList.Save();
        }