| 1 | using Core.Librarys; |
| 2 | using Core.Librarys.Browser; |
| 3 | using Core.Servicers.Interfaces; |
| 4 | using System; |
| 5 | using System.Collections.Generic; |
| 6 | using System.Linq; |
| 7 | using System.Text; |
| 8 | using System.Text.RegularExpressions; |
| 9 | using System.Threading.Tasks; |
| 10 | |
| 11 | namespace Core.Servicers.Instances |
| 12 | { |
| 13 | public class WebFilter : IWebFilter |
| 14 | { |
| 15 | private readonly IAppConfig _appConfig; |
| 16 | private List<string> _ignoreURLCache; |
| 17 | |
| 18 | private readonly string[] _ignoreRegexArr = { "^chrome://(.*)", "^edge://(.*)", "^view-source:(.*)" }; |
| 19 | public WebFilter(IAppConfig appConfig_) |
| 20 | { |
| 21 | _appConfig = appConfig_; |
| 22 | } |
| 23 | |
| 24 | public void Init() |
| 25 | { |
| 26 | _ignoreURLCache = new List<string>(); |
| 27 | _appConfig.ConfigChanged += _appConfig_ConfigChanged; |
| 28 | } |
| 29 | |
| 30 | private void _appConfig_ConfigChanged(Models.Config.ConfigModel oldConfig, Models.Config.ConfigModel newConfig) |
| 31 | { |
| 32 | if (oldConfig.Behavior.IgnoreURLList != newConfig.Behavior.IgnoreURLList) |
| 33 | { |
| 34 | _ignoreURLCache.Clear(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public bool IsIgnore(string url_) |
| 39 | { |
| 40 | if (string.IsNullOrEmpty(url_)) |
| 41 | { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | if (_ignoreURLCache.Contains(url_)) |
| 46 | { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | if (IsDomainIgnore(url_)) |
| 51 | { |
| 52 | _ignoreURLCache.Add(url_); |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | if (IsRegexIgnore(url_)) |
| 57 | { |
| 58 | _ignoreURLCache.Add(url_); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | public bool IsDomainIgnore(string url_) |
| 66 | { |
| 67 | url_ = UrlHelper.GetDomain(url_); |
| 68 | |
| 69 | var config = _appConfig.GetConfig(); |
| 70 | var ignoreURLList = config.Behavior.IgnoreURLList; |
| 71 | |
| 72 | return ignoreURLList.Contains(url_); |
| 73 | } |
| 74 | |
| 75 | public bool IsRegexIgnore(string url_) |
| 76 | { |
| 77 | var config = _appConfig.GetConfig(); |
| 78 | var ignoreURLList = config.Behavior.IgnoreURLList; |
| 79 | |
| 80 | |
| 81 | var regexList = ignoreURLList.Where(m => Regex.IsMatch(m, @"[\*|\$|\!|\(|\)|\{|\\|\[|\^|\|]")).ToList(); |
| 82 | regexList.AddRange(_ignoreRegexArr); |
| 83 | |
| 84 | foreach (string reg in regexList) |
| 85 | { |
| 86 | if (RegexHelper.IsMatch(url_, reg)) |
| 87 | { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | } |