コード例 #1
0
        private string getSHA1ForFilePath(string filePath, bool isEncrypted)
        {
            if (!File.Exists(filePath) || new FileInfo(filePath).Length == 0)
            {
                return(null);
            }

            Stream stream = null;

            try
            {
                using (var fs = File.OpenRead(filePath))
                {
                    if (isEncrypted)
                    {
                        stream = RulesetEncryption.DecryptionStream(fs);
                    }
                    else
                    {
                        stream = fs;
                    }

                    using (SHA1 sec = new SHA1CryptoServiceProvider())
                    {
                        byte[] bt    = sec.ComputeHash(stream);
                        var    lHash = BitConverter.ToString(bt).Replace("-", "");

                        return(lHash.ToLower());
                    }
                }
            }
            catch (Exception ex)
            {
                m_logger.Warn($"Could not calculate SHA1 for {filePath}: {ex}");
                return(null);
            }
            finally
            {
                try
                {
                    if (isEncrypted)
                    {
                        stream?.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    m_logger.Warn("Error occurred while disposing stream: {0}", ex);
                }
            }
        }
コード例 #2
0
        private bool decryptLists(string listFolderPath, string tempFolderPath)
        {
            if (!Directory.Exists(tempFolderPath))
            {
                Directory.CreateDirectory(tempFolderPath);
            }

            if (Configuration == null)
            {
                return(false);
            }

            foreach (var listModel in Configuration.ConfiguredLists)
            {
                string path = getListFilePath(listModel.RelativeListPath, listFolderPath);

                const int bufferSize = 8192;

                try
                {
                    string tempPath = Path.Combine(tempFolderPath, Path.GetFileName(path));

                    using (var encryptedStream = File.OpenRead(path))
                        using (var cs = RulesetEncryption.DecryptionStream(encryptedStream))
                            using (var output = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
                            {
                                cs.CopyTo(output);
                            }
                }
                catch (Exception ex)
                {
                    m_logger.Error($"decryptLists threw exception for {path}: {ex}");
                    return(false);
                }
            }

            return(true);
        }
コード例 #3
0
        public bool?DownloadLists()
        {
            HttpStatusCode code;

            bool?isVerified = VerifyLists();

            if (isVerified == true)
            {
                return(false);
            }

            if (Configuration == null)
            {
                return(null);
            }

            createListFolderIfNotExists();

            m_logger.Info("Updating filtering rules, rules missing or integrity violation.");

            List <FilteringPlainTextListModel> listsToFetch = new List <FilteringPlainTextListModel>();

            foreach (var list in Configuration.ConfiguredLists)
            {
                bool?listIsCurrent = false;

                if (lastFilterListResults != null && lastFilterListResults.TryGetValue(list.RelativeListPath, out listIsCurrent))
                {
                    if (listIsCurrent == false || listIsCurrent == null)
                    {
                        listsToFetch.Add(list);
                    }
                }
            }

            bool responseReceived;

            byte[] listBytes = WebServiceUtil.Default.GetFilterLists(listsToFetch, out code, out responseReceived);

            if (listBytes != null)
            {
                Dictionary <string, string> rulesets = new Dictionary <string, string>();

                using (MemoryStream ms = new MemoryStream(listBytes))
                    using (StreamReader reader = new StreamReader(ms))
                    {
                        string currentList = null;
                        bool   errorList   = false;

                        StringBuilder fileBuilder = new StringBuilder();

                        string line = null;
                        while ((line = reader.ReadLine()) != null)
                        {
                            if (string.IsNullOrWhiteSpace(line))
                            {
                                continue;
                            }

                            if (line.Contains("--startlist"))
                            {
                                currentList = line.Substring("--startlist".Length).TrimStart();
                            }
                            else if (line.StartsWith("--endlist"))
                            {
                                if (errorList)
                                {
                                    errorList = false;
                                }
                                else
                                {
                                    rulesets[currentList] = fileBuilder.ToString();
                                    fileBuilder.Clear();

                                    try
                                    {
                                        byte[] fileBytes = Encoding.UTF8.GetBytes(rulesets[currentList]);
                                        using (FileStream stream = new FileStream(getListFilePath(currentList), FileMode.Create))
                                            using (CryptoStream cs = RulesetEncryption.EncryptionStream(stream))
                                            {
                                                cs.Write(fileBytes, 0, fileBytes.Length);

                                                cs.FlushFinalBlock();
                                            }
                                    }
                                    catch (Exception ex)
                                    {
                                        m_logger.Error($"Failed to write to rule path {getListFilePath(currentList)} {ex}");
                                    }
                                }
                            }
                            else
                            {
                                if (line == "http-result 404")
                                {
                                    m_logger.Error($"404 Error was returned for category {currentList}");
                                    errorList = true;
                                    continue;
                                }
                                fileBuilder.Append($"{line}\n");
                            }
                        }
                    }
            }

            return(true);
        }