-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathApplyApprovedOverrides.cs
More file actions
More file actions
Latest commit
748 lines (658 loc) · 18.9 KB
/
Copy pathApplyApprovedOverrides.cs
File metadata and controls
748 lines (658 loc) · 18.9 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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
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 static class ApplyApprovedOverrides
{
private sealed class Options
{
public string InputBuild = "";
public string ApprovedRulesDirectory = "";
public string OutputDirectory = "";
}
private sealed class Report
{
public string Format { get; set; } =
"leanserp-approved-overrides";
public int Version { get; set; } = 1;
public string CreatedAt { get; set; } = "";
public string SourceBuild { get; set; } = "";
public long OriginalLabelCount { get; set; }
public long FinalLabelCount { get; set; }
public int ApprovedOverrideCount { get; set; }
public int UsedOverrideCount { get; set; }
public int AddedLabelCount { get; set; }
public int ApprovedExactHostCount { get; set; }
public int UsedExactHostCount { get; set; }
public string LabelsSha256 { get; set; } = "";
public string OverridesSha256 { get; set; } = "";
public string ExactHostsSha256 { get; set; } = "";
public string[] UsedOverrides { get; set; } =
Array.Empty<string>();
public string[] UsedExactHosts { get; set; } =
Array.Empty<string>();
}
private static int Main(string[] args)
{
try
{
Options options = ParseOptions(args);
ValidateOptions(options);
string inputLabels = Path.Combine(
options.InputBuild,
"labels.txt"
);
string pslOnlyInput = Path.Combine(
options.InputBuild,
"public-suffix-only.tsv"
);
string approvedOverridesPath = Path.Combine(
options.ApprovedRulesDirectory,
"psl-label-overrides-approved.txt"
);
string approvedExactHostsPath = Path.Combine(
options.ApprovedRulesDirectory,
"exact-host-blocks-approved.txt"
);
var approvedOverrides = LoadLabels(
approvedOverridesPath
);
var approvedExactHosts = LoadHostnames(
approvedExactHostsPath
);
var usedOverrides = new HashSet<string>(
StringComparer.Ordinal
);
var usedExactHosts = new HashSet<string>(
StringComparer.Ordinal
);
ScanPslOnlyHosts(
pslOnlyInput,
approvedOverrides,
approvedExactHosts,
usedOverrides,
usedExactHosts
);
string timestamp =
DateTime.Now.ToString(
"yyyyMMdd-HHmmss",
CultureInfo.InvariantCulture
);
string outputBuild = Path.Combine(
options.OutputDirectory,
"approved-build-" + timestamp
);
Directory.CreateDirectory(outputBuild);
string outputLabels = Path.Combine(
outputBuild,
"labels.txt"
);
string outputOverrides = Path.Combine(
outputBuild,
"psl-label-overrides.txt"
);
string outputExactHosts = Path.Combine(
outputBuild,
"exact-host-blocks.txt"
);
string outputReport = Path.Combine(
outputBuild,
"approved-overrides-report.json"
);
var sortedOverrides = usedOverrides.ToArray();
Array.Sort(
sortedOverrides,
StringComparer.Ordinal
);
var sortedExactHosts = usedExactHosts.ToArray();
Array.Sort(
sortedExactHosts,
StringComparer.Ordinal
);
File.WriteAllLines(
outputOverrides,
sortedOverrides,
new UTF8Encoding(false)
);
File.WriteAllLines(
outputExactHosts,
sortedExactHosts,
new UTF8Encoding(false)
);
(
long originalLabelCount,
long finalLabelCount,
int addedLabelCount
}
}
}
return hostname;
}
private static string RemoveBomArtifacts(
string value
)
{
string text =
(value ?? "").TrimStart('\uFEFF');
while (
text.StartsWith(
"\u00EF\u00BB\u00BF",
StringComparison.Ordinal
)
)
{
text = text.Substring(3);
}
while (
text.StartsWith(
"\u00EF\u00BF\u00BD",
StringComparison.Ordinal
)
)
{
text = text.Substring(3);
}
return text;
}
private static string ComputeSha256(
string path
)
{
using SHA256 sha256 =
SHA256.Create();
using FileStream stream =
new FileStream(
path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
1024 * 1024,
FileOptions.SequentialScan
);
byte[] hash =
sha256.ComputeHash(stream);
var output = new StringBuilder(
hash.Length * 2
);
foreach (byte value in hash)
{
output.Append(
value.ToString(
"x2",
CultureInfo.InvariantCulture
)
);
}
return output.ToString();
}
}