Пример #1
0
        public IPAddress[] ResolveHost()
        {
            var entry = _entry;

            if (entry == null || entry.Expired)
            {
                entry = _entry = GetEntry(Host);
            }

            return((entry == null) ? EmptyAddresses :
                   (entry.IPAddresses ?? EmptyAddresses));
        }
Пример #2
0
 /// <summary>
 /// Get ip address entry from the database
 /// </summary>
 /// <param name="ipAddress">IP address to lookup</param>
 /// <param name="entry">IP address entry or default if not found</param>
 /// <returns>True if ip address found, false if not</returns>
 public bool TryGetIPAddress(string ipAddress, out IPAddressEntry entry, object transaction = null)
 {
     if (IPAddress.TryParse(ipAddress, out IPAddress ipAddressObj))
     {
         byte[]             ipBytes = ipAddressObj.GetAddressBytes();
         IPBanDBTransaction tran    = transaction as IPBanDBTransaction;
         using (SqliteDataReader reader = ExecuteReader("SELECT IPAddressText, LastFailedLogin, FailedLoginCount, BanDate, State, BanEndDate FROM IPAddresses WHERE IPAddress = @Param0",
                                                        tran?.DBConnection, tran?.DBTransaction, ipBytes))
         {
             if (reader.Read())
             {
                 entry = ParseIPAddressEntry(reader);
                 return(true);
             }
         }
     }
     entry = null;
     return(false);
 }
Пример #3
0
 /// <summary>
 /// Get ip address entry from the database
 /// </summary>
 /// <param name="ipAddress">IP address to lookup</param>
 /// <param name="entry">IP address entry or default if not found</param>
 /// <returns>True if ip address found, false if not</returns>
 public bool TryGetIPAddress(string ipAddress, out IPAddressEntry entry, object transaction = null)
 {
     if (IPAddress.TryParse(ipAddress, out IPAddress ipAddressObj))
     {
         ipAddressObj = ipAddressObj.Clean();
         byte[] ipBytes = ipAddressObj.GetAddressBytes();
         SqliteDBTransaction tran = transaction as SqliteDBTransaction;
         using SqliteDataReader reader = ExecuteReader("SELECT IPAddressText, LastFailedLogin, FailedLoginCount, BanDate," +
             "State, BanEndDate, UserName, Source FROM IPAddresses WHERE IPAddress = @Param0",
             tran?.DBConnection, tran?.DBTransaction, ipBytes);
         if (reader.Read())
         {
             entry = ParseIPAddressEntry(reader);
             return true;
         }
     }
     entry = null;
     return false;
 }
Пример #4
0
        private static IPAddressEntry GetEntry(string host)
        {
            if (host.IsEmpty())
            {
                return(null);
            }

            if (s_DnsEntries.TryGetValue(host, out IPAddressEntry entry) && !entry.Expired)
            {
                return(entry);
            }

            lock (((ICollection)s_DnsEntries).SyncRoot)
            {
                if (s_DnsEntries.TryGetValue(host, out entry) && !entry.Expired)
                {
                    return(entry);
                }

                var isIp = false;

                IPAddress[] ipAddresses = null;
                if (host.Equals(NetConstants.LocalHost, StringComparison.OrdinalIgnoreCase))
                {
                    if (Socket.OSSupportsIPv4)
                    {
                        isIp        = true;
                        ipAddresses = new[] { IPAddress.Parse(NetConstants.IP4Loopback) };
                    }
                    else if (Socket.OSSupportsIPv6)
                    {
                        isIp        = true;
                        ipAddresses = new[] { IPAddress.Parse(NetConstants.IP6Loopback) };
                    }
                }

                if (!isIp)
                {
                    isIp = IPAddress.TryParse(host, out IPAddress ipAddress);

                    ipAddresses = isIp ? new[] { ipAddress } :
                    NetAsyncEx.GetHostAddressesAsync(host).Result;

                    if (!ipAddresses.IsEmpty())
                    {
                        isIp = isIp ||
                               ipAddresses.All(ip => IPAddress.IsLoopback(ip) || LocalIPs.Contains(ip));

                        if (ipAddresses.Length > 1)
                        {
                            ipAddresses = ipAddresses
                                          .OrderBy((addr) =>
                                                   { return(addr.AddressFamily == AddressFamily.InterNetwork ? -1 : 1); })
                                          .ToArray();
                        }
                    }
                }

                if (entry != null)
                {
                    entry.SetIPAddresses(ipAddresses ?? EmptyAddresses, isIp);
                }
                else
                {
                    s_DnsEntries[host] = entry = new IPAddressEntry(host, ipAddresses ?? EmptyAddresses, isIp);
                }
            }
            return(entry);
        }