/// <summary> /// Calls goproxy initalization and loads certificate and key from the specified files. /// </summary> /// <param name="portNumber"></param> /// <param name="certFile">string containing a path to a PEM-encoded certificate file.</param> /// <param name="keyFile">string containing a path to a PEM-encoded key file.</param> public void Init(short httpPortNumber, short httpsPortNumber, string certFile, string keyFile) { onBeforeRequestDelegate = new ProxyNativeWrapper.CallbackDelegate(onBeforeRequest); onBeforeResponseDelegate = new ProxyNativeWrapper.CallbackDelegate(onBeforeResponse); onBlacklistDelegate = new AdBlockMatcherApi.InternalAdBlockCallbackDelegate(onBlacklist); onWhitelistDelegate = new AdBlockMatcherApi.InternalAdBlockCallbackDelegate(onWhitelist); ProxyNativeWrapper.SetOnBeforeRequestCallback(onBeforeRequestDelegate); ProxyNativeWrapper.SetOnBeforeResponseCallback(onBeforeResponseDelegate); AdBlockMatcherApi.SetBlacklistCallback(onBlacklistDelegate); AdBlockMatcherApi.SetWhitelistCallback(onWhitelistDelegate); ProxyNativeWrapper.Init(httpPortNumber, httpsPortNumber, GoString.FromString(certFile), GoString.FromString(keyFile)); }
private RelaxedPolicyStatus getRelaxedPolicyStatus() { bool relaxedInEffect = AdBlockMatcherApi.GetBypassEnabled(); if (relaxedInEffect) { return(RelaxedPolicyStatus.Activated); } else { if (policyConfiguration.Configuration != null && policyConfiguration.Configuration.BypassesPermitted - policyConfiguration.Configuration.BypassesUsed == 0) { return(RelaxedPolicyStatus.AllUsed); } else { return(RelaxedPolicyStatus.Deactivated); } } }
private void disableRelaxedPolicy() { logger.Info("disableRelaxedPolicy"); AdBlockMatcherApi.DisableBypass(); }
private void enableRelaxedPolicy() { logger.Info("enableRelaxedPolicy"); AdBlockMatcherApi.EnableBypass(); }
public bool LoadLists() { try { m_policyLock.EnterWriteLock(); var listFolderPath = getListFolder(); if (Directory.Exists(listFolderPath)) { // Recreate our filter collection and reset all categories to be disabled. AdBlockMatcherApi.Initialize(); // Recreate our triggers container. if (m_textTriggers != null) { m_textTriggers.Dispose(); } m_categoryIndex.SetAll(false); // XXX TODO - Maybe make it a compiler flag to toggle if this is going to // be an in-memory DB or not. m_textTriggers = new BagOfTextTriggers(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "t.dat"), true, true, m_logger); // Now clear all generated categories. These will be re-generated as needed. m_generatedCategoriesMap.Clear(); uint totalFilterRulesLoaded = 0; uint totalFilterRulesFailed = 0; uint totalTriggersLoaded = 0; // Load all configured list files. string tempFolder = getTempFolder(); decryptLists(getListFolder(), tempFolder); var rulePath = s_paths.GetPath("rules.dat"); if (File.Exists(rulePath)) { // AdBlockMatcherApi.Load(rulePath); } foreach (var listModel in Configuration.ConfiguredLists) { var rulesetPath = getListFilePath(listModel.RelativeListPath, tempFolder); if (File.Exists(rulesetPath)) { var thisListCategoryName = listModel.RelativeListPath.Substring(0, listModel.RelativeListPath.LastIndexOfAny(new[] { '/', '\\' }) + 1) + Path.GetFileNameWithoutExtension(listModel.RelativeListPath); MappedFilterListCategoryModel categoryModel = null; switch (listModel.ListType) { case PlainTextFilteringListType.Blacklist: { if (TryFetchOrCreateCategoryMap(thisListCategoryName, listModel.ListType, out categoryModel)) { AdBlockMatcherApi.ParseRuleFile(rulesetPath, categoryModel.CategoryId, ListType.Blacklist); m_categoryIndex.SetIsCategoryEnabled(categoryModel.CategoryId, true); } } break; case PlainTextFilteringListType.BypassList: { MappedBypassListCategoryModel bypassCategoryModel = null; // Must be loaded twice. Once as a blacklist, once as a whitelist. if (TryFetchOrCreateCategoryMap(thisListCategoryName, listModel.ListType, out bypassCategoryModel)) { AdBlockMatcherApi.ParseRuleFile(rulesetPath, bypassCategoryModel.CategoryId, ListType.BypassList); m_categoryIndex.SetIsCategoryEnabled(bypassCategoryModel.CategoryId, true); GC.Collect(); } } break; case PlainTextFilteringListType.TextTrigger: { // Always load triggers as blacklists. if (TryFetchOrCreateCategoryMap(thisListCategoryName, listModel.ListType, out categoryModel)) { using (var listStream = File.OpenRead(rulesetPath)) { try { var triggersLoaded = m_textTriggers.LoadStoreFromStream(listStream, categoryModel.CategoryId).Result; totalTriggersLoaded += (uint)triggersLoaded; if (triggersLoaded > 0) { m_categoryIndex.SetIsCategoryEnabled(categoryModel.CategoryId, true); } } catch (Exception ex) { m_logger.Info($"Error on LoadStoresFromStream {ex}"); } } } GC.Collect(); } break; case PlainTextFilteringListType.Whitelist: { if (TryFetchOrCreateCategoryMap(thisListCategoryName, listModel.ListType, out categoryModel)) { AdBlockMatcherApi.ParseRuleFile(rulesetPath, categoryModel.CategoryId, ListType.Whitelist); m_categoryIndex.SetIsCategoryEnabled(categoryModel.CategoryId, true); } GC.Collect(); } break; } } } if (Configuration != null && Configuration.CustomTriggerBlacklist != null && Configuration.CustomTriggerBlacklist.Count > 0) { MappedFilterListCategoryModel categoryModel = null; // Always load triggers as blacklists. if (TryFetchOrCreateCategoryMap("/user/trigger_blacklist", PlainTextFilteringListType.TextTrigger, out categoryModel)) { var triggersLoaded = m_textTriggers.LoadStoreFromList(Configuration.CustomTriggerBlacklist, categoryModel.CategoryId).Result; totalTriggersLoaded += (uint)triggersLoaded; if (triggersLoaded > 0) { m_categoryIndex.SetIsCategoryEnabled(categoryModel.CategoryId, true); } m_logger.Info("Number of triggers loaded for CustomTriggerBlacklist {0}", triggersLoaded); } } if (Configuration != null && Configuration.CustomWhitelist != null && Configuration.CustomWhitelist.Count > 0) { List <string> sanitizedCustomWhitelist = new List <string>(); // As we are importing directly into an Adblock Plus-style rule engine, we need to make sure // that the user can't whitelist sites by adding something with a "@@" in front of it. // The easiest way to do this is to limit the characters to 'safe' characters. Regex isCleanRule = new Regex(@"^[a-zA-Z0-9\-_\:\.\/]+$", RegexOptions.Compiled); string rulesetPath = Path.Combine(tempFolder, ".user.custom_whitelist.rules.txt"); using (var rulesetStream = File.OpenWrite(rulesetPath)) using (var writer = new StreamWriter(rulesetStream)) { foreach (string site in Configuration.CustomWhitelist) { if (site == null) { continue; } if (isCleanRule.IsMatch(site)) { writer.WriteLine($"||{site}"); } } } MappedFilterListCategoryModel categoryModel = null; if (TryFetchOrCreateCategoryMap("/user/custom_whitelist", PlainTextFilteringListType.Whitelist, out categoryModel)) { AdBlockMatcherApi.ParseRuleFile(rulesetPath, categoryModel.CategoryId, ListType.Whitelist); m_categoryIndex.SetIsCategoryEnabled(categoryModel.CategoryId, true); } } if (Configuration != null && Configuration.SelfModeration != null && Configuration.SelfModeration.Count > 0) { List <string> sanitizedSelfModerationSites = new List <string>(); // As we are importing directly into an Adblock Plus-style rule engine, we need to make sure // that the user can't whitelist sites by adding something with a "@@" in front of it. // The easiest way to do this is to limit the characters to 'safe' characters. Regex isCleanRule = new Regex(@"^[a-zA-Z0-9\-_\:\.\/]+$", RegexOptions.Compiled); string rulesetPath = Path.Combine(tempFolder, ".user.self_moderation.rules.txt"); using (var rulesetStream = File.OpenWrite(rulesetPath)) using (var writer = new StreamWriter(rulesetStream)) { foreach (string site in Configuration.SelfModeration) { if (site == null) { continue; } if (isCleanRule.IsMatch(site)) { writer.WriteLine($"||{site}"); } } } MappedFilterListCategoryModel categoryModel = null; if (TryFetchOrCreateCategoryMap("/user/self_moderation", PlainTextFilteringListType.Blacklist, out categoryModel)) { AdBlockMatcherApi.ParseRuleFile(rulesetPath, categoryModel.CategoryId, ListType.Blacklist); m_categoryIndex.SetIsCategoryEnabled(categoryModel.CategoryId, true); } } //m_filterCollection.FinalizeForRead(); //m_filterCollection.InitializeBloomFilters(); m_textTriggers.FinalizeForRead(); m_textTriggers.InitializeBloomFilters(); // AdBlockMatcherApi.Save(s_paths.GetPath("rules.dat")); ListsReloaded?.Invoke(this, new EventArgs()); m_logger.Info("Loaded {0} rules, {1} rules failed most likely due to being malformed, and {2} text triggers loaded.", totalFilterRulesLoaded, totalFilterRulesFailed, totalTriggersLoaded); } return(true); } catch (Exception ex) { LoggerUtil.RecursivelyLogException(m_logger, ex); return(false); } finally { m_policyLock.ExitWriteLock(); deleteTemporaryLists(); } }