-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathApprovedRules.cs
More file actions
More file actions
Latest commit
636 lines (535 loc) · 14.5 KB
/
Copy pathApprovedRules.cs
File metadata and controls
636 lines (535 loc) · 14.5 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
internal sealed class ApprovedRules
{
private readonly HashSet<string> exactHostBlocks;
private readonly HashSet<string> pslLabelOverrides;
private ApprovedRules(
HashSet<string> exactHostBlocks,
HashSet<string> pslLabelOverrides)
{
this.exactHostBlocks = exactHostBlocks;
this.pslLabelOverrides = pslLabelOverrides;
}
public int ExactHostBlockCount
{
get { return exactHostBlocks.Count; }
}
public int PslLabelOverrideCount
{
get { return pslLabelOverrides.Count; }
}
public static ApprovedRules Load(
string approvedRulesDirectory)
{
if (string.IsNullOrWhiteSpace(
approvedRulesDirectory))
{
throw new ArgumentException(
"The approved-rules directory is missing.",
nameof(approvedRulesDirectory));
}
string fullDirectory =
Path.GetFullPath(approvedRulesDirectory);
if (!Directory.Exists(fullDirectory))
{
throw new DirectoryNotFoundException(
"The approved-rules directory was not found: " +
fullDirectory);
}
string exactHostPath = Path.Combine(
fullDirectory,
"exact-host-blocks-approved.txt");
string overridePath = Path.Combine(
fullDirectory,
"psl-label-overrides-approved.txt");
HashSet<string> exactHosts =
LoadExactHostnames(exactHostPath);
HashSet<string> overrides =
LoadLabels(overridePath);
return new ApprovedRules(
exactHosts,
overrides);
}
public bool IsExactHostBlocked(
string hostname)
{
string normalized =
NormalizeHostname(hostname);
return normalized.Length != 0 &&
exactHostBlocks.Contains(normalized);
}
public bool IsPslLabelOverride(
string label)
{
string normalized =
NormalizeLabel(label);
return normalized.Length != 0 &&
pslLabelOverrides.Contains(normalized);
}
public string[] GetMatchingPslOverrides(
IReadOnlyList<string> suffixLabels)
{
if (
suffixLabels == null ||
suffixLabels.Count == 0)
{
return Array.Empty<string>();
}
var matches = new HashSet<string>(
StringComparer.OrdinalIgnoreCase);
foreach (string value in suffixLabels)
{
string label =
NormalizeLabel(value);
if (
label.Length != 0 &&
pslLabelOverrides.Contains(label))
{
matches.Add(label);
}
}
string[] output = matches.ToArray();
Array.Sort(
output,
StringComparer.Ordinal);
return output;
}
public string[] GetExactHostBlocks()
{
string[] output =
exactHostBlocks.ToArray();
Array.Sort(
output,
StringComparer.Ordinal);
return output;
}
public string[] GetPslLabelOverrides()
{
string[] output =
pslLabelOverrides.ToArray();
Array.Sort(
output,
}
return builder.ToString();
}
}
internal sealed class ApprovedRulesMetadata
{
public string ApprovedRulesDirectory
{
get;
set;
} = "";
public int ApprovedExactHostCount
{
get;
set;
}
public int ApprovedPslOverrideCount
{
get;
set;
}
public int UsedExactHostCount
{
get;
set;
}
public int UsedPslOverrideCount
{
get;
set;
}
public string ExactHostSource
{
get;
set;
} = "";
public string PslOverrideSource
{
get;
set;
} = "";
public string ExactHostSourceSha256
{
get;
set;
} = "";
public string PslOverrideSourceSha256
{
get;
set;
} = "";
public string[] UsedExactHosts
{
get;
set;
} = Array.Empty<string>();
public string[] UsedPslOverrides
{
get;
set;
} = Array.Empty<string>();
}