fCraft.PlayerDB.RecoverIPBans C# (CSharp) Method

RecoverIPBans() static private method

static private RecoverIPBans ( ) : void
return void
        internal static void RecoverIPBans() {
            PlayerInfo[] playerInfoListCache = PlayerInfoList;
            for( int i = 0; i < playerInfoListCache.Length; i++ ) {
                PlayerInfo p = playerInfoListCache[i];
                if( p.Banned && p.BanReason.EndsWith( "~BanAll", StringComparison.OrdinalIgnoreCase ) && IPBanList.Get( p.LastIP ) == null ) {
                    IPBanList.Add( new IPBanInfo( p.LastIP, p.Name, p.BannedBy, p.BanReason ) );
                    Logger.Log( "PlayerDB.RecoverIPBans: Banned {0} by association with {1}. Banned by {2}. Reason: {3}", LogType.SystemActivity,
                                p.LastIP, p.Name, p.BannedBy, p.BanReason );
                }
            }
        }

Usage Example

Example #1
0
        internal static void Load()
        {
            if (File.Exists(Paths.IPBanListFileName))
            {
                string headerText;
                using (StreamReader reader = File.OpenText(Paths.IPBanListFileName)) {
                    headerText = reader.ReadLine(); // header
                    if (headerText == null)
                    {
                        Logger.Log("IPBanList.Load: IP ban file is empty.", LogType.Warning);
                    }

                    int version = ParseHeader(headerText);

                    while (!reader.EndOfStream)
                    {
                        string[] fields = reader.ReadLine().Split(',');
                        if (fields.Length == IPBanInfo.FieldCount)
                        {
                            try {
                                IPBanInfo ban;
                                if (version == 0)
                                {
                                    ban = IPBanInfo.LoadOldFormat(fields, true);
                                }
                                else
                                {
                                    ban = IPBanInfo.Load(fields);
                                }

                                if (ban.Address.Equals(IPAddress.Any) || ban.Address.Equals(IPAddress.None))
                                {
                                    Logger.Log("IPBanList.Load: Invalid IP address skipped.", LogType.Warning);
                                }
                                else
                                {
                                    Bans.Add(ban.Address.ToString(), ban);
                                }
                            } catch (IOException ex) {
                                Logger.Log("IPBanList.Load: Error while trying to read from file: {0}", LogType.Error, ex.Message);
                            } catch (Exception ex) {
                                Logger.Log("IPBanList.Load: Could not parse a record: {0}", LogType.Error, ex.Message);
                            }
                        }
                        else
                        {
                            Logger.Log("IPBanList.Load: Corrupt record skipped ({0} fields instead of {1}): {2}", LogType.Error,
                                       fields.Length, IPBanInfo.FieldCount, String.Join(",", fields));
                        }
                    }

                    if (version == 0)
                    {
                        Logger.Log("IPBanList.Load: Attempting to recover IP bans...", LogType.SystemActivity);
                        int oldBanCount = Bans.Count;
                        PlayerDB.RecoverIPBans();
                        Logger.Log("IPBanList.Load: {0} IP bans recovered.", LogType.SystemActivity, Bans.Count - oldBanCount);
                    }
                }

                Logger.Log("IPBanList.Load: Done loading IP ban list ({0} records).", LogType.Debug, Bans.Count);
            }
            else
            {
                Logger.Log("IPBanList.Load: No IP ban file found.", LogType.Warning);
            }
            IsLoaded = true;
        }