/// <summary>
        ///     Create a Base JSON Database.
        /// </summary>
        /// <param name="databaseFilePath">
        ///     An absolute file path to a database file. If the file does not exist, it will be created.
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="databaseFilePath" /> is a null reference.
        /// </exception>
        private protected BaseJsonBrowsingDatabase(string databaseFilePath)
        {
            this._database           = new MemoryBrowsingDatabase();
            this._disposed           = false;
            this.DatabaseFileManager = new JsonFileManager(databaseFilePath);
            this.DatabaseFilePath    = databaseFilePath;
            // ...
            //
            // ...
            LoadDatabase(this);

            // <summary>
            //      Load Database.
            // </summary>
            void LoadDatabase(BaseJsonBrowsingDatabase @this)
            {
                try {
                    // ...
                    //
                    // Throws an exception if the operation fails.
                    var cFileModel = @this.DatabaseFileManager.Read();
                    if (cFileModel.ThreatLists != null)
                    {
                        foreach (var cThreatListModel in cFileModel.ThreatLists)
                        {
                            var cPlatformType    = cThreatListModel.PlatformType;
                            var cThreatEntryType = cThreatListModel.ThreatEntryType;
                            var cThreatType      = cThreatListModel.ThreatType;
                            var cThreatList      = ThreatList.Build()
                                                   .SetDescriptor(cThreatType, cPlatformType, cThreatEntryType)
                                                   .SetRetrieveDate(cThreatListModel.RetrieveDate)
                                                   .SetState(cThreatListModel.State)
                                                   .SetWaitToDate(cThreatListModel.WaitToDate)
                                                   .Build();

                            // ...
                            //
                            // Throws an exception if the operation fails.
                            var cThreatSha256HashPrefixes = cThreatListModel.ThreatSha256HashPrefixes;
                            var cStoreThreatListTask      = @this._database.StoreThreatListAsync(cThreatList, cThreatSha256HashPrefixes);
                            cStoreThreatListTask.Wait();
                        }
                    }
                }
                catch {
                    // ...
                    //
                    // We don't care if we are unable to load the database.
                }
            }
        }
Пример #2
0
        /// <summary>
        ///     Set Retrieved Threat List.
        /// </summary>
        /// <param name="valueAction">
        ///     An action to create the <see cref="ThreatList" /> retrieved from the Google Safe Browsing API.
        /// </param>
        /// <returns>
        ///     This threat list update result builder.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        ///     Thrown if <paramref name="valueAction" /> is a null reference.
        /// </exception>
        public ThreatListUpdateResultBuilder SetRetrievedThreatList(Func <ThreatListBuilder, ThreatList> valueAction)
        {
            Guard.ThrowIf(nameof(valueAction), valueAction).Null();

            // ...
            //
            // Throws an exception if the operation fails.
            var threatListBuilder = ThreatList.Build();
            var threatList        = valueAction(threatListBuilder);

            this.SetRetrievedThreatList(threatList);

            return(this);
        }
Пример #3
0
        /// <summary>
        ///     Synchronize Database File Asynchronously.
        /// </summary>
        /// <returns>
        ///     A task representing the asynchronous operation.
        /// </returns>
        private async Task SyncDatabaseFileAsync()
        {
            var cancellationToken = this._syncTaskCancellationTokenSource.Token;

            while (!cancellationToken.IsCancellationRequested)
            {
                try {
                    // ...
                    //
                    // Throws an exception if the operation fails.
                    var fileModel = this.DatabaseFileManager.Read();
                    if (fileModel.ThreatLists != null)
                    {
                        foreach (var threatListModel in fileModel.ThreatLists)
                        {
                            var platformType    = threatListModel.PlatformType;
                            var threatEntryType = threatListModel.ThreatEntryType;
                            var threatType      = threatListModel.ThreatType;
                            var threatList      = ThreatList.Build()
                                                  .SetDescriptor(threatType, platformType, threatEntryType)
                                                  .SetRetrieveDate(threatListModel.RetrieveDate)
                                                  .SetState(threatListModel.State)
                                                  .SetWaitToDate(threatListModel.WaitToDate)
                                                  .Build();

                            // ...
                            //
                            // Throws an exception if the operation fails.
                            var threatSha256HashPrefixes = threatListModel.ThreatSha256HashPrefixes;
                            var storeThreatListTask      = this.StoreThreatListAsync(threatList, threatSha256HashPrefixes, cancellationToken);
                            await storeThreatListTask.ConfigureAwait(false);
                        }
                    }
                }
                catch {
                    // ...
                    //
                    // We don't care if we failed to synchronize the database file. We'll try again on the next
                    // iteration.
                }
                finally {
                    var delayTask = DelayAsync(this);
                    await delayTask.ConfigureAwait(false);
                }
            }

            // <summary>
            //      Delay Asynchronously.
            // </summary>
            async Task DelayAsync(UnmanagedJsonBrowsingDatabase @this)
            {
                try {
                    var cCancellationToken = @this._syncTaskCancellationTokenSource.Token;
                    var cDelayTask         = Task.Delay(UnmanagedJsonBrowsingDatabase.SyncInterval, cCancellationToken);
                    await cDelayTask.ConfigureAwait(false);
                }
                catch (OperationCanceledException) {
                    // ...
                    //
                    // We don't care if the cancellation token is cancelled.
                }
            }
        }