Skip to content

Commit 73199f7

Browse files
committed
Implemented DNSCryptQueryMonitor and Improved DNSConfigManager Reader
Introduces DNSCryptQueryMonitor for real-time monitoring of blocked DNS queries. Adds registry-based detection of DNSCrypt installation directory in ServiceManager. Updates Enums and TraceLogger for improved logging and log management. TODO: Need to add the results of the block history to a listbox instead of the TraceLogger.
1 parent 00e44c6 commit 73199f7

File tree

10 files changed

+445
-72
lines changed

10 files changed

+445
-72
lines changed

SinkDNS/Modules/DNSCrypt/DNSCryptConfigParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class DNSCryptConfigParser(string SinkDNSconfigFilePath, string DNSCryptC
2828
{
2929
private readonly string _SinkDNSconfigFilePath = SinkDNSconfigFilePath ?? throw new ArgumentNullException(nameof(SinkDNSconfigFilePath));
3030

31-
private readonly DNSCryptConfigurationWriter _DNSCryptConfigWriter = new(DNSCryptConfigPath);
31+
private readonly DNSCryptConfigurationManager _DNSCryptConfigWriter = new(DNSCryptConfigPath);
3232

3333
private static readonly char[] separator = ['='];
3434

SinkDNS/Modules/DNSCrypt/DNSCryptConfigurationWriter.cs renamed to SinkDNS/Modules/DNSCrypt/DNSCryptConfigurationManager.cs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace SinkDNS.Modules.DNSCrypt
2727
{
2828
//This class is responsible for parsing and writing configuration data to and from DNSCrypt.
2929
//It handles reading configuration files, interpreting settings, and converting them into usable formats for the application.
30-
internal class DNSCryptConfigurationWriter(string configFilePath)
30+
public class DNSCryptConfigurationManager(string configFilePath)
3131
{
3232
private readonly string _configFilePath = configFilePath ?? throw new ArgumentNullException(nameof(configFilePath));
3333
private List<string> _configLines = [];
@@ -43,7 +43,8 @@ public void ChangeSetting(string settingName, string value)
4343

4444
if (!File.Exists(_configFilePath))
4545
{
46-
TraceLogger.Log("Configuration file not found!", Enums.StatusSeverityType.Error);
46+
TraceLogger.Log($"Configuration file not found {_configFilePath}", Enums.StatusSeverityType.Error);
47+
MessageBox.Show("A SinkDNS module error has occurred. Configuration file not found!\n" + _configFilePath, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
4748
return;
4849
}
4950

@@ -59,7 +60,8 @@ private void LoadConfiguration()
5960
{
6061
if (!File.Exists(_configFilePath))
6162
{
62-
TraceLogger.Log("Configuration file not found!", Enums.StatusSeverityType.Error);
63+
TraceLogger.Log($"Configuration file not found {_configFilePath}", Enums.StatusSeverityType.Error);
64+
MessageBox.Show("A SinkDNS module error has occurred. Configuration file not found!\n" + _configFilePath, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
6365
return;
6466
}
6567
TraceLogger.Log("Loading configuration file: " + _configFilePath);
@@ -127,74 +129,103 @@ private static string FormatValue(string value)
127129

128130
public string? GetSetting(string section, string settingName)
129131
{
132+
//does the config file exists? check.
130133
if (!File.Exists(_configFilePath))
131-
throw new FileNotFoundException("Configuration file not found", _configFilePath);
132-
134+
{
135+
TraceLogger.Log($"Configuration file not found {_configFilePath}", Enums.StatusSeverityType.Error);
136+
MessageBox.Show("A SinkDNS module error has occurred. Configuration file not found!\n" + _configFilePath, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
137+
return null;
138+
}
133139
if (!_configLoaded)
134140
{
135141
LoadConfiguration();
136142
}
137-
138143
int startLine = -1;
139144
int endLine = -1;
140-
141145
// Find section if specified
142146
if (!string.IsNullOrEmpty(section))
143147
{
148+
TraceLogger.Log($"Searching for section '{section}' in configuration.");
144149
for (int i = 0; i < _configLines.Count; i++)
145150
{
146151
if (_configLines[i].Trim() == section)
147152
{
153+
TraceLogger.Log($"Section '{section}' found at line {i + 1}.");
148154
startLine = i;
149155
break;
150156
}
151157
}
158+
TraceLogger.Log($"Section '{section}' search completed. STARTLINE: {startLine}");
152159
if (startLine == -1)
153160
return null; // Section not found
154-
161+
TraceLogger.Log($"Locating end of section '{section}'.");
155162
// Find end of section (next section or end of file)
156163
for (int i = startLine + 1; i < _configLines.Count; i++)
157164
{
158165
if (_configLines[i].Trim().StartsWith("[") && _configLines[i].Trim().EndsWith("]"))
159166
{
167+
TraceLogger.Log($"End of section '{section}' found at line {i + 1}.");
160168
endLine = i;
161169
break;
162170
}
163171
}
172+
TraceLogger.Log($"End of section '{section}' search completed. ENDLINE: {endLine}");
164173
if (endLine == -1)
165174
endLine = _configLines.Count;
166175
}
167176
else
168177
{
178+
TraceLogger.Log("No section specified. Searching entire configuration.");
169179
startLine = 0;
170180
endLine = _configLines.Count;
171181
}
172-
173182
// Search for setting within section
174183
for (int i = startLine; i < endLine; i++)
175184
{
176185
var line = _configLines[i].Trim();
177186
if (line.StartsWith(settingName + "=") || line.StartsWith(settingName + " ="))
178187
{
179-
var value = line[(settingName.Length + 1)..].Trim();
188+
TraceLogger.Log($"Setting '{settingName}' found at line {i + 1}. Extracting value.");
189+
// Extract everything after the equals sign
190+
var value = line.Substring(settingName.Length + 1).Trim();
191+
192+
// Remove leading spaces and equals sign if present
193+
while (value.StartsWith("=") || value.StartsWith(" "))
194+
{
195+
value = value.Substring(1).Trim();
196+
}
197+
180198
// Remove quotes if present
181199
if (value.StartsWith("\"") && value.EndsWith("\""))
182200
{
201+
TraceLogger.Log($"Value for setting '{settingName}' is quoted. Removing quotes.");
183202
return value[1..^1];
184203
}
204+
205+
// Remove any trailing comments
206+
int commentIndex = value.IndexOf('#');
207+
if (commentIndex >= 0)
208+
{
209+
value = value.Substring(0, commentIndex).Trim();
210+
}
211+
value = value.Replace("'", "");
212+
TraceLogger.Log($"Value for setting '{settingName}' extracted: {value}");
185213
return value;
186214
}
187215
}
216+
TraceLogger.Log($"Setting '{settingName}' not found in the specified section.");
188217
return null;
189218
}
219+
190220
public void WriteToConfigFile()
191221
{
192222
if (!_configLoaded || !_hasChanges)
193223
return;
194224

195225
if (!File.Exists(_configFilePath))
196226
{
197-
TraceLogger.Log("Configuration file not found!", Enums.StatusSeverityType.Error);
227+
TraceLogger.Log($"Configuration file not found {_configFilePath}", Enums.StatusSeverityType.Error);
228+
MessageBox.Show("A SinkDNS module error has occurred. Configuration file not found!\n" + _configFilePath, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
198229
return;
199230
}
200231

0 commit comments

Comments
 (0)