-
Notifications
You must be signed in to change notification settings - Fork 16
/
EditSet.cs
314 lines (285 loc) · 10.6 KB
/
EditSet.cs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.Collections.Generic;
using static WebOne.Program;
namespace WebOne
{
/// <summary>
/// Set of edits for particular pages
/// </summary>
class EditSet
{
/// <summary>
/// List of masks of URLs on which the Set whould be used [title and OnUrl]
/// </summary>
public List<string> UrlMasks { get; set; }
/// <summary>
/// List of masks of URLs on which the Set would not be used [IgnoreUrl]
/// </summary>
public List<string> UrlIgnoreMasks { get; set; }
/// <summary>
/// List of masks of MIME Content-Types on which the Set would be used [OnContentType]
/// </summary>
public List<string> ContentTypeMasks { get; set; }
/// <summary>
/// Mask (exact) of HTTP status code where the Set would be used [OnCode]
/// </summary>
public int? OnCode { get; set; }
/// <summary>
/// Flag indicating that the edit set should be used only on plain HTTP requests
/// </summary>
public bool HttpOnly { get; set; }
/// <summary>
/// Flag indicating that the edit set should be used only on HTTPS requests
/// </summary>
public bool HttpsOnly { get; set; }
/// <summary>
/// List of masks of HTTP request headers on which the Set would not be used [OnHeader]
/// </summary>
public List<string> HeaderMasks { get; set; }
/// <summary>
/// Flag that indicates that the edits can be performed at time of HTTP Request (before get of response)
/// </summary>
public bool IsForRequest { get; private set; }
/// <summary>
/// Limitation by proxy server operating system [OnHostOS]<br/>
/// True if OS is good, False if is bad.
/// </summary>
public bool CorrectHostOS { get; private set; }
/// <summary>
/// List of edits that would be performed on the need content
/// </summary>
public List<EditSetRule> Edits { get; set; }
/// <summary>
/// Create a Set of edits from a section from webone.conf
/// </summary>
/// <param name="Section">The webone.conf section</param>
public EditSet(ConfigFileSection Section)
{
UrlMasks = new List<string>();
UrlIgnoreMasks = new List<string>();
ContentTypeMasks = new List<string>();
HeaderMasks = new List<string>();
CorrectHostOS = true;
Edits = new List<EditSetRule>();
IsForRequest = false;
bool MayBeForResponse = false; //does this set containing tasks for HTTP response processing?
if (Section.Mask != null)
{
Program.CheckRegExp(Section.Mask, Section.Location);
UrlMasks.Add(Section.Mask);
}
foreach (var Line in Section.Options)
{
if (!Line.HaveKeyValue) continue;
switch (Line.Key)
{
//detection rules
case "OnUrl":
CheckRegExp(Line);
UrlMasks.Add(Line.Value);
continue;
case "OnCode":
OnCode = int.Parse(Line.Value);
continue;
case "IgnoreUrl":
CheckRegExp(Line);
UrlIgnoreMasks.Add(Line.Value);
continue;
case "OnContentType":
CheckRegExp(Line);
ContentTypeMasks.Add(Line.Value);
continue;
case "OnHeader":
CheckRegExp(Line);
HeaderMasks.Add(Line.Value);
continue;
case "OnHostOS":
switch (Line.Value.ToLower())
{
case "windows":
CorrectHostOS = OperatingSystem.IsWindows();
continue;
case "linux":
CorrectHostOS = OperatingSystem.IsLinux();
continue;
case "macos":
CorrectHostOS = OperatingSystem.IsMacOS();
continue;
default:
new LogWriter().WriteLine(true, false, "Warning: unknown host OS \"{0}\", edit set disabled.", Line.Value);
CorrectHostOS = false;
continue;
}
case "OnHttpOnly":
HttpOnly = ToBoolean(Line.Value);
continue;
case "OnHttpsOnly":
HttpsOnly = ToBoolean(Line.Value);
continue;
//editing rules (can contain regular expressions)
case "AddRedirect":
case "AddInternalRedirect":
case "AddFind":
case "AddReplace":
CheckRegExp(Line);
Edits.Add(new EditSetRule(Line.Key, Line.Value));
break;
//editing rules (cannot contain regular expressions)
case "AddHeader":
case "AddResponseHeader":
case "AddConvert":
case "AddConvertDest":
case "AddConvertArg1":
case "AddConvertArg2":
case "AddRequestHeaderFind":
case "AddRequestHeaderReplace":
case "AddResponseHeaderFind":
case "AddResponseHeaderReplace":
case "AddHeaderDumping":
case "AddRequestDumping":
case "AddDumping":
case "AddOutputEncoding":
case "AddTranslit":
Edits.Add(new EditSetRule(Line.Key, Line.Value));
break;
default:
if (Line.Key.StartsWith("Add"))
new LogWriter().WriteLine(true, false, "Warning: unknown editing rule \"{0}\".", Line.Key);
else
new LogWriter().WriteLine(true, false, "Warning: unknown detection rule \"{0}\".", Line.Key);
break;
}
if (Line.Key.StartsWith("AddConvert")) MayBeForResponse = true;
if (Line.Key == "AddContentType") MayBeForResponse = true;
if (Line.Key == "AddFind") MayBeForResponse = true;
if (Line.Key == "AddReplace") MayBeForResponse = true;
if (Line.Key == "AddInternalRedirect") MayBeForResponse = false;
}
ProcessComplexRules(Section.Location);
//check if the edit set can be runned on HTTP-request time
if (ContentTypeMasks.Count == 0 && !MayBeForResponse) IsForRequest = true;
if (UrlMasks.Count == 0) UrlMasks.Add(".*");
}
/// <summary>
/// Process all multi-line rules (like AddFind+AddReplace) to virtual rules (e.g. AddFindReplace)
/// </summary>
/// <param name="EditSetLocation">Edit Set's location (for error message if need)</param>
private void ProcessComplexRules(string EditSetLocation)
{
/* List of virtual editing rules, not listed at https://github.com/atauenis/webone/wiki/Sets-of-edits
* AddFind + AddReplace = AddFindReplace (FindReplaceEditSetRule)
* AddConvert + AddConvertDest + AddConvertArg1 + AddConvertArg2 = AddConverting (ConvertEditSetRule)
* AddHeaderFind + AddHeaderReplace = AddRequestHeaderFindReplace (FindReplaceEditSetRule)
* AddResponseHeaderFind + AddResponseHeaderReplace = AddResponseHeaderFindReplace (FindReplaceEditSetRule)
*/
//load all original lines
List<string> Finds = new();
List<string> Replacions = new();
List<string> RequestHeaderFinds = new();
List<string> RequestHeaderReplacions = new();
List<string> ResponseHeaderFinds = new();
List<string> ResponseHeaderReplacions = new();
string Converter = null;
string ConvertDest = "";
string ConvertArg1 = "";
string ConvertArg2 = "";
foreach (EditSetRule Rule in Edits)
{
switch (Rule.Action)
{
case "AddFind":
Finds.Add(Rule.Value);
break;
case "AddReplace":
Replacions.Add(Rule.Value);
break;
case "AddRequestHeaderFind":
RequestHeaderFinds.Add(Rule.Value);
break;
case "AddRequestHeaderReplace":
RequestHeaderReplacions.Add(Rule.Value);
break;
case "AddResponseHeaderFind":
ResponseHeaderFinds.Add(Rule.Value);
break;
case "AddResponseHeaderReplace":
ResponseHeaderReplacions.Add(Rule.Value);
break;
case "AddConvert":
Converter = Rule.Value;
break;
case "AddConvertDest":
ConvertDest = Rule.Value;
break;
case "AddConvertArg1":
ConvertArg1 = Rule.Value;
break;
case "AddConvertArg2":
ConvertArg2 = Rule.Value;
break;
}
}
//process AddFind, AddReplace -> AddFindReplace
if (Finds.Count != Replacions.Count)
Log.WriteLine(true, false, "Warning: Invalid amount of finds/replaces in {0}.", EditSetLocation);
else if (Finds.Count > 0)
for (int i = 0; i < Finds.Count; i++)
{
Edits.Add(new FindReplaceEditSetRule("AddFindReplace", Finds[i], Replacions[i]));
}
//process AddHeaderFind, AddHeaderReplace -> AddRequestHeaderFindReplace
if (RequestHeaderFinds.Count != RequestHeaderReplacions.Count)
Log.WriteLine(true, false, "Warning: Invalid amount of request header finds/replaces in {0}.", EditSetLocation);
else if (RequestHeaderFinds.Count > 0)
for (int i = 0; i < RequestHeaderFinds.Count; i++)
{
Edits.Add(new FindReplaceEditSetRule("AddRequestHeaderFindReplace", RequestHeaderFinds[i], RequestHeaderReplacions[i]));
}
//process AddResponseHeaderFind, AddResponseHeaderReplace -> AddResponseHeaderFindReplace
if (ResponseHeaderFinds.Count != ResponseHeaderReplacions.Count)
Log.WriteLine(true, false, "Warning: Invalid amount of response header finds/replaces in {0}.", EditSetLocation);
else if (ResponseHeaderFinds.Count > 0)
for (int i = 0; i < ResponseHeaderFinds.Count; i++)
{
Edits.Add(new FindReplaceEditSetRule("AddResponseHeaderFindReplace", ResponseHeaderFinds[i], ResponseHeaderReplacions[i]));
}
//process AddConvert, AddConvertDest, AddConvertArg1, AddConvertArg2 -> AddConverting
if (!string.IsNullOrEmpty(Converter))
{
//check converter presence and warn if there is no such
bool CorrectConverter = false;
foreach (var conv in ConfigFile.Converters)
{
if (conv.Executable == Converter)
{
Edits.Add(new ConvertEditSetRule("AddConverting", Converter, ConvertDest, ConvertArg1, ConvertArg2));
CorrectConverter = true;
break;
}
}
if (!CorrectConverter) Log.WriteLine(true, false, @"Warning: Converter ""{1}"" in Edit Set at {0} is not present in lists of converters.", EditSetLocation, Converter);
}
else if (ConvertDest != "" || ConvertArg1 != "" || ConvertArg2 != "")
{
Log.WriteLine(true, false, "Warning: Please add AddConvert rule to Edit Set starting at {0} to use converting.", EditSetLocation);
}
}
/// <summary>
/// Test validness of a Regular Expression pattern in a rule line
/// </summary>
/// <param name="RegExpLine">Rule line</param>
/// <returns><see cref="True"/> if RegExp is valid or <see cref="False"/> if it is invalid</returns>
private bool CheckRegExp(ConfigFileOption RegExpLine) { return Program.CheckRegExp(RegExpLine.Value, RegExpLine.Location); }
//test function
public override string ToString()
{
string Str = "[Edit:" + UrlMasks[0] + "]\n";
if (UrlMasks.Count > 1) for (int i = 1; i < UrlMasks.Count; i++) Str += "OnUrl=" + UrlMasks[i] + "\n";
foreach (var imask in UrlIgnoreMasks) Str += "IgnoreUrl=" + "=" + imask + "\n";
foreach (var ctmask in ContentTypeMasks) Str += "OnContentType=" + "=" + ctmask + "\n";
foreach (var hmask in HeaderMasks) Str += "OnHeader=" + "=" + hmask + "\n";
foreach (var edit in Edits) Str += edit.Action + "=" + edit.Value + "\n";
return Str;
}
}
}