跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

WebFilter.cs

95 行 · 2.5 KB历史下载
1using Core.Librarys;
2using Core.Librarys.Browser;
3using Core.Servicers.Interfaces;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Text;
8using System.Text.RegularExpressions;
9using System.Threading.Tasks;
10
11namespace 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}