Exemplo n.º 1
0
 /// <summary>
 ///     Create a Cache Proxy.
 /// </summary>
 /// <param name="cache">
 ///     A <see cref="IBrowsingCache" /> to proxy to.
 /// </param>
 /// <param name="ownCache">
 ///     A boolean flag indicating whether or not the cache proxy takes ownership of <paramref name="cache" />
 ///     and disposes it when the cache proxy itself is disposed. If the cache proxy takes ownership of
 ///     <paramref name="cache" /> and you reference or dispose <paramref name="cache" /> after you create the
 ///     cache proxy, the behavior of the cache proxy and <paramref name="cache" /> is undefined.
 /// </param>
 /// <returns>
 ///     A cache proxy.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="cache" /> is a null reference.
 /// </exception>
 internal static BrowsingCacheProxy Create(IBrowsingCache cache, bool ownCache)
 {
     // ...
     //
     // Throws an exception if the operation fails.
     return(cache is BrowsingCacheProxy cacheProxy ? cacheProxy : new BrowsingCacheProxy(cache, ownCache));
 }
        /// <summary>
        ///     Create a Resilient Cache.
        /// </summary>
        /// <param name="cache">
        ///     A <see cref="IBrowsingCache" /> to proxy to.
        /// </param>
        /// <param name="retryAttempts">
        ///     The number of attempts a failed operation should be retried.
        /// </param>
        /// <param name="ownCache">
        ///     A boolean flag indicating whether or not the resilient cache takes ownership of
        ///     <paramref name="cache" /> and disposes it when the resilient cache itself is disposed. If the resilient
        ///     cache takes ownership of <paramref name="cache" /> and you reference or dispose
        ///     <paramref name="cache" /> after you create the resilient cache, the behavior of the resilient cache and
        ///     <paramref name="cache" /> is undefined.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="cache" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        ///     Thrown if <paramref name="retryAttempts" /> is less than or equal to <c>0</c>.
        /// </exception>
        public ResilientBrowsingCache(IBrowsingCache cache, int retryAttempts, bool ownCache)
        {
            Guard.ThrowIf(nameof(retryAttempts), retryAttempts).LessThanOrEqualTo(0);

            this._cache         = BrowsingCacheProxy.Create(cache, ownCache);
            this._disposed      = false;
            this._ownCache      = ownCache;
            this._retryAttempts = retryAttempts;
            // ...
            //
            // ...
            this._resiliencyPolicy = CreateResiliencyPolicy(this);

            // <summary>
            //      Create Resiliency Policy.
            // </summary>
            IAsyncPolicy CreateResiliencyPolicy(ResilientBrowsingCache @this)
            {
                var cPolicyBuilder = Policy.Handle <BrowsingCacheException>();
                var cPolicy        = cPolicyBuilder.WaitAndRetryAsync(@this._retryAttempts, CreateRetryPolicyTimeout);

                return(cPolicy);
            }

            // <summary>
            //      Create Retry Policy Timeout.
            // </summary>
            TimeSpan CreateRetryPolicyTimeout(int cRetryAttempt)
            {
                var cDelaySeconds = Math.Pow(2, cRetryAttempt);
                var cDelay        = TimeSpan.FromSeconds(cDelaySeconds);

                return(cDelay);
            }
        }
 /// <summary>
 ///     Create a Resilient Cache.
 /// </summary>
 /// <param name="cache">
 ///     A <see cref="IBrowsingCache" /> to proxy to.
 /// </param>
 /// <param name="retryAttempts">
 ///     The number of attempts a failed operation should be retried.
 /// </param>
 /// <param name="ownCache">
 ///     A boolean flag indicating whether or not the resilient cache takes ownership of
 ///     <paramref name="cache" /> and disposes it when the resilient cache itself is disposed. If the resilient
 ///     cache takes ownership of <paramref name="cache" /> and you reference or dispose
 ///     <paramref name="cache" /> after you create the resilient cache, the behavior of the resilient cache and
 ///     <paramref name="cache" /> is undefined.
 /// </param>
 /// <returns>
 ///     A resilient cache.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="cache" /> is a null reference.
 /// </exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 ///     Thrown if <paramref name="retryAttempts" /> is less than or equal to <c>0</c>.
 /// </exception>
 public static ResilientBrowsingCache Create(IBrowsingCache cache, int retryAttempts, bool ownCache)
 {
     // ...
     //
     // Throws an exception if the operation fails.
     return(cache is ResilientBrowsingCache resilientCache ? resilientCache : new ResilientBrowsingCache(cache, retryAttempts, ownCache));
 }
Exemplo n.º 4
0
        /// <summary>
        ///     Put an Unsafe Cache Entry Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="unsafeCacheEntry">
        ///     An <see cref="UnsafeCacheEntry" /> to cache.
        /// </param>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="unsafeCacheEntry" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="this" /> is disposed.
        /// </exception>
        public static Task PutUnsafeCacheEntryAsync(this IBrowsingCache @this, UnsafeCacheEntry unsafeCacheEntry)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            var putUnsafeCacheEntryTask = @this.PutUnsafeCacheEntryAsync(unsafeCacheEntry, CancellationToken.None);

            return(putUnsafeCacheEntryTask);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Set Cache.
        /// </summary>
        /// <param name="value">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="ownCache">
        ///     A boolean flag indicating whether or not the <see cref="BaseBrowsingService" /> takes ownership of the
        ///     cache and disposes it when the service itself is disposed. If the service takes ownership of the cache
        ///     and you reference or dispose the cache after you create the service, the behavior of the service and
        ///     the cache is undefined.
        /// </param>
        /// <returns>
        ///     This service builder.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="value" /> is a null reference.
        /// </exception>
        public T SetCache(IBrowsingCache value, bool ownCache)
        {
            Guard.ThrowIf(nameof(value), value).Null();

            this.Cache    = value;
            this.OwnCache = ownCache;
            return((T)this);
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Create a Cache Proxy.
        /// </summary>
        /// <param name="cache">
        ///     A <see cref="IBrowsingCache" /> to proxy to.
        /// </param>
        /// <param name="ownCache">
        ///     A boolean flag indicating whether or not the cache proxy takes ownership of <paramref name="cache" />
        ///     and disposes it when the cache proxy itself is disposed. If the cache proxy takes ownership of
        ///     <paramref name="cache" /> and you reference or dispose <paramref name="cache" /> after you create the
        ///     cache proxy, the behavior of the cache proxy and <paramref name="cache" /> is undefined.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="cache" /> is a null reference.
        /// </exception>
        private BrowsingCacheProxy(IBrowsingCache cache, bool ownCache)
        {
            Guard.ThrowIf(nameof(cache), cache).Null();

            this._cache    = cache;
            this._disposed = false;
            this._ownCache = ownCache;
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Get an Unsafe Cache Entry Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="threatSha256Hash">
        ///     A full SHA256 hash, formatted as a hexadecimal encoded string, identifying a threat to retrieve from
        ///     the cache.
        /// </param>
        /// <returns>
        ///     The <see cref="UnsafeCacheEntry" /> cached for the threat identified by
        ///     <paramref name="threatSha256Hash" />. A null reference indicates an unsafe cache entry is not cached.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="threatSha256Hash" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="this" /> is disposed.
        /// </exception>
        public static Task <UnsafeCacheEntry> GetUnsafeCacheEntryAsync(this IBrowsingCache @this, string threatSha256Hash)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var getUnsafeCacheEntryTask = @this.GetUnsafeCacheEntryAsync(threatSha256Hash, CancellationToken.None);

            return(getUnsafeCacheEntryTask);
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Remove a Safe Cache Entry Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="threatSha256HashPrefix">
        ///     A SHA256 hash prefix, formatted as a hexadecimal encoded string, identifying a threat to remove from
        ///     the cache.
        /// </param>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="this" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="this" /> is disposed.
        /// </exception>
        public static Task RemoveSafeCacheEntryAsync(this IBrowsingCache @this, string threatSha256HashPrefix)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var removeSafeCacheEntryTask = @this.RemoveSafeCacheEntryAsync(threatSha256HashPrefix, CancellationToken.None);

            return(removeSafeCacheEntryTask);
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Put a Safe Cache Entry Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="safeCacheEntry">
        ///     A <see cref="SafeCacheEntry" /> to cache.
        /// </param>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="safeCacheEntry" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="this" /> is disposed.
        /// </exception>
        public static Task PutSafeCacheEntryAsync(this IBrowsingCache @this, SafeCacheEntry safeCacheEntry)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var putSafeCacheEntryTask = @this.PutSafeCacheEntryAsync(safeCacheEntry, CancellationToken.None);

            return(putSafeCacheEntryTask);
        }
Exemplo n.º 10
0
        /// <summary>
        ///     Put an Unsafe Cache Entry Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="threatSha256Hash">
        ///     A full SHA256 hash, formatted as a hexadecimal encoded string, identifying a threat to cache.
        /// </param>
        /// <param name="unsafeThreats">
        ///     A collection of <see cref="UnsafeThreat" /> to cache.
        /// </param>
        /// <param name="cancellationToken">
        ///     A cancellation token to cancel the asynchronous operation with.
        /// </param>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="this" /> is a null reference, or if <paramref name="threatSha256Hash" /> is a
        ///     null reference, or if <paramref name="unsafeThreats" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="this"/> is disposed.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        ///     Thrown if the asynchronous operation is cancelled.
        /// </exception>
        public static Task PutUnsafeCacheEntryAsync(this IBrowsingCache @this, string threatSha256Hash, IEnumerable <UnsafeThreat> unsafeThreats, CancellationToken cancellationToken)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var unsafeCacheEntry        = new UnsafeCacheEntry(threatSha256Hash, unsafeThreats);
            var putUnsafeCacheEntryTask = @this.PutUnsafeCacheEntryAsync(unsafeCacheEntry, cancellationToken);

            return(putUnsafeCacheEntryTask);
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Put a Safe Cache Entry Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="threatSha256HashPrefix">
        ///     A SHA256 hash prefix, formatted as a hexadecimal encoded string, identifying a threat to cache.
        /// </param>
        /// <param name="expirationDate">
        ///     The date, in Coordinated Universal Time (UTC), the safe cache entry expires and the threat identified
        ///     by <paramref name="threatSha256HashPrefix" /> should be considered safe to. If the date is not in UTC,
        ///     it is converted to it.
        /// </param>
        /// <param name="cancellationToken">
        ///     A cancellation token to cancel the asynchronous operation with.
        /// </param>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="this" /> is a null reference, or if
        ///     <paramref name="threatSha256HashPrefix" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if the object is disposed.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        ///     Thrown if the asynchronous operation is cancelled.
        /// </exception>
        public static Task PutSafeCacheEntryAsync(this IBrowsingCache @this, string threatSha256HashPrefix, DateTime expirationDate, CancellationToken cancellationToken)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var safeCacheEntry        = new SafeCacheEntry(threatSha256HashPrefix, expirationDate);
            var putSafeCacheEntryTask = @this.PutSafeCacheEntryAsync(safeCacheEntry, cancellationToken);

            return(putSafeCacheEntryTask);
        }
Exemplo n.º 12
0
 /// <summary>
 ///     Create a Resilient Cache.
 /// </summary>
 /// <param name="cache">
 ///     A <see cref="IBrowsingCache" /> to proxy to. The resilient cache takes ownership of
 ///     <paramref name="cache" /> and will dispose it when the resilient cache itself is disposed. If you
 ///     reference or dispose <paramref name="cache" /> after you create the resilient cache, the behavior of
 ///     the resilient cache and <paramref name="cache" /> is undefined.
 /// </param>
 /// <param name="retryAttempts">
 ///     The number of attempts a failed operation should be retried.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="cache" /> is a null reference.
 /// </exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 ///     Thrown if <paramref name="retryAttempts" /> is less than or equal to <c>0</c>.
 /// </exception>
 public ResilientBrowsingCache(IBrowsingCache cache, int retryAttempts) : this(cache, retryAttempts, true)
 {
 }
Exemplo n.º 13
0
 /// <summary>
 ///     Create a Resilient Cache.
 /// </summary>
 /// <param name="cache">
 ///     A <see cref="IBrowsingCache" /> to proxy to. The resilient cache takes ownership of
 ///     <paramref name="cache" /> and will dispose it when the resilient cache itself is disposed. If you
 ///     reference or dispose <paramref name="cache" /> after you create the resilient cache, the behavior of
 ///     the resilient cache and <paramref name="cache" /> is undefined.
 /// </param>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="cache" /> is a null reference.
 /// </exception>
 public ResilientBrowsingCache(IBrowsingCache cache) : this(cache, 5)
 {
 }
Exemplo n.º 14
0
 /// <summary>
 ///     Create a Cache Proxy.
 /// </summary>
 /// <param name="cache">
 ///     A <see cref="IBrowsingCache" /> to proxy to. The cache proxy takes ownership of
 ///     <paramref name="cache" /> and will dispose it when the cache proxy itself is disposed. If you reference
 ///     or dispose <paramref name="cache" /> after you create the cache proxy, the behavior of the cache proxy
 ///     and <paramref name="cache" /> is undefined.
 /// </param>
 /// <returns>
 ///     A cache proxy.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="cache" /> is a null reference.
 /// </exception>
 internal static BrowsingCacheProxy Create(IBrowsingCache cache) => BrowsingCacheProxy.Create(cache, true);
Exemplo n.º 15
0
 /// <summary>
 ///     Create a Resilient Cache.
 /// </summary>
 /// <param name="cache">
 ///     A <see cref="IBrowsingCache" /> to proxy to. The resilient cache takes ownership of
 ///     <paramref name="cache" /> and will dispose it when the resilient cache itself is disposed. If you
 ///     reference or dispose <paramref name="cache" /> after you create the resilient cache, the behavior of
 ///     the resilient cache and <paramref name="cache" /> is undefined.
 /// </param>
 /// <param name="retryAttempts">
 ///     The number of attempts a failed operation should be retried.
 /// </param>
 /// <returns>
 ///     A resilient cache.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="cache" /> is a null reference.
 /// </exception>
 /// <exception cref="System.ArgumentOutOfRangeException">
 ///     Thrown if <paramref name="retryAttempts" /> is less than or equal to <c>0</c>.
 /// </exception>
 public static ResilientBrowsingCache Create(IBrowsingCache cache, int retryAttempts) => ResilientBrowsingCache.Create(cache, retryAttempts, true);
Exemplo n.º 16
0
        /// <summary>
        ///     Lookup a URL.
        /// </summary>
        /// <param name="cache">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="client">
        ///     A <see cref="IBrowsingClient" />.
        /// </param>
        /// <param name="database">
        ///     An <see cref="IUnmanagedBrowsingDatabase" />.
        /// </param>
        /// <param name="url">
        ///     A <see cref="Url" /> to lookup.
        /// </param>
        /// <param name="cancellationToken">
        ///     A cancellation token to cancel the asynchronous operation with.
        /// </param>
        /// <returns>
        ///     A <see cref="UrlLookupResult" /> indicating whether <paramref name="url" /> is
        ///     <see cref="UrlLookupResultCode.Safe" /> or <see cref="UrlLookupResultCode.Unsafe" />.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs. If you're not interested in handling this exception, catch
        ///     <see cref="BrowsingException" /> instead.
        /// </exception>
        /// <exception cref="Gee.External.Browsing.Clients.BrowsingClientException">
        ///     Thrown if an error communicating with the Google Safe Browsing API occurs. If you're not interested
        ///     in handling this exception, catch <see cref="BrowsingException" /> instead.
        /// </exception>
        /// <exception cref="Gee.External.Browsing.Databases.BrowsingDatabaseException">
        ///     Thrown if a database error occurs. If you're not interested in handling this exception, catch
        ///     <see cref="BrowsingException" /> instead.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="url" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="cache" /> is disposed, or if <paramref name="client" /> is disposed, or if
        ///     <paramref name="database" /> is disposed.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        ///     Thrown if the asynchronous operation is cancelled.
        /// </exception>
        private protected async Task <UrlLookupResult> LookupAsync(IBrowsingCache cache, IBrowsingClient client, IUnmanagedBrowsingDatabase database, Url url, CancellationToken cancellationToken)
        {
            // ...
            //
            // First, lookup the URL in the local database. Throws an exception if the operation fails.
            var             urlLookupDate = DateTime.UtcNow;
            UrlLookupResult urlLookupResult;
            var             databaseLookupTask = database.LookupAsync(url);
            var             databaseLookResult = await databaseLookupTask.ConfigureAwait(false);

            if (databaseLookResult.IsDatabaseMiss)
            {
                // ...
                //
                // If the URL does not exist in the local database, indicate the URL is safe.
                urlLookupResult = UrlLookupResult.Safe(url, urlLookupDate);
            }
            else if (databaseLookResult.IsDatabaseStale)
            {
                // ...
                //
                // If the local database is expired/out-of-date/stale, indicate the nature of the URL cannot
                // be determined as a result.
                urlLookupResult = UrlLookupResult.DatabaseStale(url, urlLookupDate);
            }
            else
            {
                // ...
                //
                // If the URL exists in the local database, look it up in the cache. Throws an exception if
                // the operation fails.
                var sha256Hash        = databaseLookResult.Sha256Hash;
                var sha256HashPrefix  = databaseLookResult.Sha256HashPrefix;
                var cacheLookupTask   = cache.LookupAsync(sha256Hash, sha256HashPrefix, cancellationToken);
                var cacheLookupResult = await cacheLookupTask.ConfigureAwait(false);

                if (cacheLookupResult.IsCacheSafeHit)
                {
                    // ...
                    //
                    // If we get a cache safe hit, indicate the URL is safe.
                    urlLookupResult = UrlLookupResult.Safe(url, urlLookupDate);
                }
                else if (cacheLookupResult.IsCacheUnsafeHit)
                {
                    // ...
                    //
                    // If we get a cache unsafe hit, indicate the URL is unsafe. If we get a cache unsafe hit,
                    // it is guaranteed we find a URL expression for the threat, since it is essentially the
                    // URL expression that is cached.
                    var unsafeThreatListDescriptors = cacheLookupResult.UnsafeThreatListDescriptors;
                    url.TryGetExpressionForSha256Hash(sha256Hash, out var unsafeUrlExpression);
                    urlLookupResult = UrlLookupResult.Unsafe(url, urlLookupDate, unsafeUrlExpression, unsafeThreatListDescriptors);
                }
                else
                {
                    // ...
                    //
                    // If we get a cache miss, verify the URL with the Google Safe Browsing API. Throws an
                    // exception if the operation fails.
                    var threatLists        = databaseLookResult.ThreatLists;
                    var findFullHashesTask = client.FindFullHashesAsync(sha256HashPrefix, threatLists, cancellationToken);
                    var fullHashResponse   = await findFullHashesTask.ConfigureAwait(false);

                    // ...
                    //
                    // Throws an exception if the operation fails.
                    var safeCacheEntryExpirationDate = fullHashResponse.SafeThreatsExpirationDate;
                    var putSafeCacheEntryTask        = cache.PutSafeCacheEntryAsync(sha256HashPrefix, safeCacheEntryExpirationDate, cancellationToken);
                    await putSafeCacheEntryTask.ConfigureAwait(false);

                    var unsafeThreats = fullHashResponse.UnsafeThreats;
                    if (unsafeThreats.Count != 0)
                    {
                        var unsafeThreatGroups = unsafeThreats.GroupBy(ut => ut.Sha256Hash);
                        foreach (var unsafeThreatGroup in unsafeThreatGroups)
                        {
                            // ...
                            //
                            // Throws an exception if the operation fails.
                            var unsafeThreatSha256Hash  = unsafeThreatGroup.Key;
                            var putUnsafeCacheEntryTask = cache.PutUnsafeCacheEntryAsync(unsafeThreatSha256Hash, unsafeThreatGroup, cancellationToken);
                            await putUnsafeCacheEntryTask.ConfigureAwait(false);
                        }
                    }

                    // ...
                    //
                    // Lookup the URL in the cache again. Throws an exception if the operation fails.
                    cacheLookupTask   = cache.LookupAsync(sha256Hash, sha256HashPrefix, cancellationToken);
                    cacheLookupResult = await cacheLookupTask.ConfigureAwait(false);

                    if (cacheLookupResult.IsCacheSafeHit)
                    {
                        // ...
                        //
                        // If we get a cache safe hit, indicate the URL is safe.
                        urlLookupResult = UrlLookupResult.Safe(url, urlLookupDate);
                    }
                    else if (cacheLookupResult.IsCacheUnsafeHit)
                    {
                        // ...
                        //
                        // If we get a cache unsafe hit, indicate the URL is unsafe. If we get a cache unsafe hit,
                        // it is guaranteed we find a URL expression for the threat, since it is essentially the
                        // URL expression that is cached.
                        var unsafeThreatListDescriptors = cacheLookupResult.UnsafeThreatListDescriptors;
                        url.TryGetExpressionForSha256Hash(sha256Hash, out var unsafeUrlExpression);
                        urlLookupResult = UrlLookupResult.Unsafe(url, urlLookupDate, unsafeUrlExpression, unsafeThreatListDescriptors);
                    }
                    else
                    {
                        urlLookupResult = UrlLookupResult.DatabaseStale(url, urlLookupDate);
                    }
                }
            }

            return(urlLookupResult);
        }
Exemplo n.º 17
0
 private BrowsingCacheProxy(IBrowsingCache cache) : this(cache, true)
 {
 }
Exemplo n.º 18
0
 /// <summary>
 ///     Put a Safe Cache Entry Asynchronously.
 /// </summary>
 /// <param name="this">
 ///     A <see cref="IBrowsingCache" />.
 /// </param>
 /// <param name="threatSha256HashPrefix">
 ///     A SHA256 hash prefix, formatted as a hexadecimal encoded string, identifying a threat to cache.
 /// </param>
 /// <param name="expirationDate">
 ///     The date, in Coordinated Universal Time (UTC), the safe cache entry expires and the threat identified
 ///     by <paramref name="threatSha256HashPrefix" /> should be considered safe to. If the date is not in UTC,
 ///     it is converted to it.
 /// </param>
 /// <returns>
 ///     A task representing the asynchronous operation.
 /// </returns>
 /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
 ///     Thrown if a caching error occurs.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="this" /> is a null reference, or if
 ///     <paramref name="threatSha256HashPrefix" /> is a null reference.
 /// </exception>
 /// <exception cref="System.ObjectDisposedException">
 ///     Thrown if <paramref name="this"/> is disposed.
 /// </exception>
 public static Task PutSafeCacheEntryAsync(this IBrowsingCache @this, string threatSha256HashPrefix, DateTime expirationDate) => @this.PutSafeCacheEntryAsync(threatSha256HashPrefix, expirationDate, CancellationToken.None);
Exemplo n.º 19
0
        /// <summary>
        ///     Lookup a Threat Asynchronously.
        /// </summary>
        /// <param name="this">
        ///     A <see cref="IBrowsingCache" />.
        /// </param>
        /// <param name="threatSha256Hash">
        ///     A full SHA256 hash, formatted as a hexadecimal encoded string, identifying a threat to lookup in the
        ///     cache.
        /// </param>
        /// <param name="threatSha256HashPrefix">
        ///     A SHA256 hash prefix, formatted as a hexadecimal encoded string, identifying a threat to lookup in
        ///     the cache.
        /// </param>
        /// <param name="cancellationToken">
        ///     A cancellation token to cancel the asynchronous operation with.
        /// </param>
        /// <returns>
        ///     A <see cref="CacheLookupResult" /> indicating the nature of the operation.
        /// </returns>
        /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
        ///     Thrown if a caching error occurs.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="this" /> is a null reference, or if <paramref name="threatSha256Hash" /> is
        ///     a null reference, or if <paramref name="threatSha256HashPrefix" /> is a null reference.
        /// </exception>
        /// <exception cref="System.ObjectDisposedException">
        ///     Thrown if <paramref name="this" /> is disposed.
        /// </exception>
        /// <exception cref="System.OperationCanceledException">
        ///     Thrown if the asynchronous operation is cancelled.
        /// </exception>
        public static async Task <CacheLookupResult> LookupAsync(this IBrowsingCache @this, string threatSha256Hash, string threatSha256HashPrefix, CancellationToken cancellationToken)
        {
            Guard.ThrowIf(nameof(@this), @this).Null();

            // ...
            //
            // First, check if the full SHA256 hash identifies a cached unsafe threat. Throws an exception if the
            // operation fails.
            CacheLookupResult cacheLookupResult;
            var getUnsafeCacheEntryTask = @this.GetUnsafeCacheEntryAsync(threatSha256Hash, cancellationToken);
            var unsafeCacheEntry        = await getUnsafeCacheEntryTask.ConfigureAwait(false);

            if (unsafeCacheEntry != null)
            {
                if (!unsafeCacheEntry.Expired)
                {
                    // ..
                    //
                    // If the full SHA256 hash identifies an unexpired cached unsafe threat, indicate a cache unsafe
                    // hit.
                    var unsafeThreats = unsafeCacheEntry.UnsafeThreats;
                    cacheLookupResult = CacheLookupResult.CacheUnsafeHit(threatSha256Hash, threatSha256HashPrefix, unsafeThreats);
                }
                else
                {
                    // ...
                    //
                    // If the full SHA256 hash identifies an expired cached unsafe threat, indicate a cache miss.
                    // Throws an exception if the operation fails.
                    var removeUnsafeCacheEntryTask = @this.RemoveUnsafeCacheEntryAsync(threatSha256Hash, cancellationToken);
                    await removeUnsafeCacheEntryTask.ConfigureAwait(false);

                    cacheLookupResult = CacheLookupResult.CacheMiss(threatSha256Hash, threatSha256HashPrefix);
                }
            }
            else
            {
                // ...
                //
                // If the full SHA256 hash does not identify a cached unsafe threat, check if the SHA256 hash prefix
                // identifies a cached safe threat.
                //
                // Throws an exception if the operation fails.
                var getSafeCacheEntryTask = @this.GetSafeCacheEntryAsync(threatSha256HashPrefix, cancellationToken);
                var safeCacheEntry        = await getSafeCacheEntryTask.ConfigureAwait(false);

                if (safeCacheEntry != null && !safeCacheEntry.Expired)
                {
                    // ...
                    //
                    // If the SHA256 hash prefix identifies an unexpired cache unsafe hit, indicate a cache safe hit.
                    cacheLookupResult = CacheLookupResult.CacheSafeHit(threatSha256Hash, threatSha256HashPrefix);
                }
                else
                {
                    // ...
                    //
                    // If the SHA256 hash prefix does not identify a cached safe threat, indicate a cache miss. Throws
                    // an exception if the operation fails.
                    var removeSafeCacheEntryTask = @this.RemoveSafeCacheEntryAsync(threatSha256HashPrefix, cancellationToken);
                    await removeSafeCacheEntryTask.ConfigureAwait(false);

                    cacheLookupResult = CacheLookupResult.CacheMiss(threatSha256Hash, threatSha256HashPrefix);
                }
            }

            return(cacheLookupResult);
        }
Exemplo n.º 20
0
 /// <summary>
 ///     Lookup a Threat Asynchronously.
 /// </summary>
 /// <param name="this">
 ///     A <see cref="IBrowsingCache" />.
 /// </param>
 /// <param name="threatSha256Hash">
 ///     A full SHA256 hash, formatted as a hexadecimal encoded string, identifying a threat to lookup in the
 ///     cache.
 /// </param>
 /// <param name="threatSha256HashPrefix">
 ///     A SHA256 hash prefix, formatted as a hexadecimal encoded string, identifying a threat to lookup in
 ///     the cache.
 /// </param>
 /// <returns>
 ///     A <see cref="CacheLookupResult" /> indicating the nature of the operation.
 /// </returns>
 /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
 ///     Thrown if a caching error occurs.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="this" /> is a null reference, or if <paramref name="threatSha256Hash" /> is
 ///     a null reference, or if <paramref name="threatSha256HashPrefix" /> is a null reference.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="this" /> is a null reference.
 /// </exception>
 /// <exception cref="System.ObjectDisposedException">
 ///     Thrown if <paramref name="this" /> is disposed.
 /// </exception>
 public static Task <CacheLookupResult> LookupAsync(this IBrowsingCache @this, string threatSha256Hash, string threatSha256HashPrefix) => @this.LookupAsync(threatSha256Hash, threatSha256HashPrefix, CancellationToken.None);
Exemplo n.º 21
0
 /// <summary>
 ///     Create a Resilient Cache.
 /// </summary>
 /// <param name="cache">
 ///     A <see cref="IBrowsingCache" /> to proxy to. The resilient cache takes ownership of
 ///     <paramref name="cache" /> and will dispose it when the resilient cache itself is disposed. If you
 ///     reference or dispose <paramref name="cache" /> after you create the resilient cache, the behavior of
 ///     the resilient cache and <paramref name="cache" /> is undefined.
 /// </param>
 /// <returns>
 ///     A resilient cache.
 /// </returns>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="cache" /> is a null reference.
 /// </exception>
 public static ResilientBrowsingCache Create(IBrowsingCache cache) => ResilientBrowsingCache.Create(cache, 5);
Exemplo n.º 22
0
 /// <summary>
 ///     Put an Unsafe Cache Entry Asynchronously.
 /// </summary>
 /// <param name="this">
 ///     A <see cref="IBrowsingCache" />.
 /// </param>
 /// <param name="threatSha256Hash">
 ///     A full SHA256 hash, formatted as a hexadecimal encoded string, identifying a threat to cache.
 /// </param>
 /// <param name="unsafeThreats">
 ///     A collection of <see cref="UnsafeThreat" /> to cache.
 /// </param>
 /// <returns>
 ///     A task representing the asynchronous operation.
 /// </returns>
 /// <exception cref="Gee.External.Browsing.Cache.BrowsingCacheException">
 ///     Thrown if a caching error occurs.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">
 ///     Thrown if <paramref name="this" /> is a null reference, or if <paramref name="threatSha256Hash" /> is a
 ///     null reference, or if <paramref name="unsafeThreats" /> is a null reference.
 /// </exception>
 /// <exception cref="System.ObjectDisposedException">
 ///     Thrown if <paramref name="this"/> is disposed.
 /// </exception>
 public static Task PutUnsafeCacheEntryAsync(this IBrowsingCache @this, string threatSha256Hash, IEnumerable <UnsafeThreat> unsafeThreats) => @this.PutUnsafeCacheEntryAsync(threatSha256Hash, unsafeThreats, CancellationToken.None);