-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathConfig.cs
168 lines (123 loc) · 4.77 KB
/
Config.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
namespace HttpReverseProxy.Plugin.InjectFile
{
using HttpReverseProxy.Plugin.InjectFile.DataTypes;
using HttpReverseProxyLib;
using HttpReverseProxyLib.DataTypes.Enum;
using HttpReverseProxyLib.Exceptions;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
public class Config
{
#region PROPERTIES
public static string PluginName { get; private set; } = "InjectFile";
public static int PluginPriority { get; private set; } = 4;
public static string ConfigFileName { get; private set; } = "plugin.config";
public static string PluginVersion { get; private set; } = "0.1";
public static List<InjectFileConfigRecord> InjectFileRecords { get; set; } = new List<InjectFileConfigRecord>();
#endregion
#region PUBLIC
/// <summary>
///
/// </summary>
/// <param name="configFilePath"></param>
public void ParseConfigurationFile(string configFilePath)
{
if (string.IsNullOrEmpty(configFilePath))
{
throw new ProxyWarningException("Config file path is invalid");
}
if (!File.Exists(configFilePath))
{
throw new ProxyWarningException("Config file does not exist");
}
var configFileLines = File.ReadAllLines(configFilePath);
InjectFileRecords.Clear();
foreach (var tmpLine in configFileLines)
{
try
{
InjectFileConfigRecord newRecord = this.VerifyRecordParameters(tmpLine);
// Make regex from hostname/path
newRecord.HostnameStr = this.RegexifyHostname(newRecord.HostnameStr);
newRecord.HostnameRegex = new Regex(newRecord.HostnameStr, RegexOptions.Compiled | RegexOptions.IgnoreCase);
newRecord.PathStr = this.RegexifyPath(newRecord.PathStr);
newRecord.PathRegex = new Regex(newRecord.PathStr, RegexOptions.Compiled | RegexOptions.IgnoreCase);
InjectFileRecords.Add(newRecord);
}
catch (ProxyWarningException pwex)
{
Logging.Instance.LogMessage("CONFIG", ProxyProtocol.Undefined, Loglevel.Debug, @"InjectFile.VerifyRecordParameters(EXCEPTION) : {0}", pwex.Message);
}
catch (ProxyErrorException peex)
{
Logging.Instance.LogMessage("CONFIG", ProxyProtocol.Undefined, Loglevel.Debug, @"InjectFile.VerifyRecordParameters(EXCEPTION) : {0}", peex.Message);
}
}
}
#endregion
#region PROTECTED
private string RegexifyHostname(string wildcardBuffer)
{
if (string.IsNullOrEmpty(wildcardBuffer) ||
wildcardBuffer.Contains("*") == false)
{
return wildcardBuffer;
}
var tmpRegex = wildcardBuffer.Replace("*", "ASTERISK");
tmpRegex = Regex.Escape(tmpRegex);
wildcardBuffer = tmpRegex.Replace("ASTERISK", @"[\d\w\-_\.]*?");
return wildcardBuffer;
}
private string RegexifyPath(string wildcardBuffer)
{
if (string.IsNullOrEmpty(wildcardBuffer) ||
wildcardBuffer.Contains("*") == false)
{
return wildcardBuffer;
}
var tmpRegex = wildcardBuffer.Replace("*", "ASTERISK");
tmpRegex = Regex.Escape(tmpRegex);
wildcardBuffer = tmpRegex.Replace("ASTERISK", @"[\d\w\-_\/\.\+\~\=\&\?]*?");
return wildcardBuffer;
}
protected InjectFileConfigRecord VerifyRecordParameters(string configFileLine)
{
var hostRegex = string.Empty;
var pathRegex = string.Empty;
var replacementResource = string.Empty;
if (string.IsNullOrEmpty(configFileLine))
{
throw new ProxyWarningException("Configuration line is invalid");
}
var splitter = Regex.Split(configFileLine, @"\|\|");
if (splitter.Length != 3)
{
throw new ProxyWarningException("Wrong numbers of configuration parameters");
}
hostRegex = splitter[0]?.ToLower();
pathRegex = splitter[1];
replacementResource = splitter[2];
if (string.IsNullOrEmpty(hostRegex) == true)
{
throw new ProxyWarningException($"Host parameter is invalid: {hostRegex}");
}
if (string.IsNullOrEmpty(pathRegex) == true)
{
throw new ProxyWarningException($"Path parameter is invalid: {pathRegex}");
}
if (string.IsNullOrEmpty(replacementResource) == true ||
File.Exists(replacementResource) == false)
{
throw new ProxyWarningException($"Replacement resource parameter is invalid: {replacementResource}");
}
if (InjectFileRecords.Exists(elem => elem.HostnameStr == hostRegex &&
elem.PathStr == pathRegex))
{
throw new ProxyWarningException("Record already exists");
}
return new InjectFileConfigRecord(hostRegex, pathRegex, replacementResource);
}
#endregion
}
}