forked from DigitalRuby/IPBan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionsToBlockConfigSectionHandler.cs
93 lines (78 loc) · 2.42 KB
/
ExpressionsToBlockConfigSectionHandler.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
#region Imports
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
#endregion Imports
namespace IPBan
{
public class ExpressionToBlock
{
internal Regex RegexObject;
public string XPath { get; set; }
public string Regex { get; set; }
}
public class ExpressionsToBlockGroup
{
/// <summary>
/// Keywords as a ULONG
/// </summary>
public ulong KeywordsULONG { get; set; }
/// <summary>
/// Keywords backing variable, private
/// </summary>
private string keywords;
/// <summary>
/// Keywords as HEX string
/// </summary>
public string Keywords
{
get { return keywords; }
set
{
keywords = value;
// parse, removing any 0x prefix
if (value.StartsWith("0x"))
{
value = value.Substring(2);
}
KeywordsULONG = ulong.Parse(value, NumberStyles.AllowHexSpecifier);
}
}
public string Path;
[XmlArray("Expressions")]
[XmlArrayItem("Expression")]
public ExpressionToBlock[] Expressions { get; set; }
}
public class ExpressionsToBlock
{
[XmlArray("Groups")]
[XmlArrayItem("Group")]
public ExpressionsToBlockGroup[] Groups { get; set; }
}
public class ExpressionsToBlockConfigSectionHandler : IConfigurationSectionHandler
{
private const string sectionName = "ExpressionsToBlock";
public object Create(object parent, object configContext, XmlNode section)
{
string config = section.SelectSingleNode("//" + sectionName).OuterXml;
if (!string.IsNullOrWhiteSpace(config))
{
XmlSerializer serializer = new XmlSerializer(typeof(ExpressionsToBlock));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(config))
{
Position = 0
};
ExpressionsToBlock expressions = serializer.Deserialize(ms) as ExpressionsToBlock;
return expressions;
}
return null;
}
}
}