Skip to content

Commit 45704a0

Browse files
committed
Enable DomainBlockLog
1 parent 2fb68a7 commit 45704a0

10 files changed

Lines changed: 420 additions & 299 deletions

SimpleDnsCrypt/Config/Global.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public static class Global
6868
/// </summary>
6969
public const int ServiceInstallTime = 3000;
7070

71+
public const string DomainBlockLogFileName = "blocked.log";
72+
public const string QueryLogFileName = "query.log";
73+
74+
public const string WhitelistRuleFileName = "domain-whitelist.txt";
75+
public const string BlacklistRuleFileName = "domain-blacklist.txt";
76+
public const string BlacklistFileName = "blacklist.txt";
77+
78+
7179
/// <summary>
7280
/// List of files must exist.
7381
/// </summary>

SimpleDnsCrypt/Models/BlacklistRule.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

SimpleDnsCrypt/SimpleDnsCrypt.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@
175175
<Compile Include="LogMode.cs" />
176176
<Compile Include="Models\AddressBlockLogLine.cs" />
177177
<Compile Include="Models\AvailableResolver.cs" />
178-
<Compile Include="Models\BlacklistRule.cs" />
179178
<Compile Include="Models\BoxType.cs" />
180179
<Compile Include="Models\DomainBlockLogLine.cs" />
181180
<Compile Include="Models\DnscryptProxyConfiguration.cs" />

SimpleDnsCrypt/ViewModels/AddressBlacklistViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class AddressBlacklistViewModel : Screen
1313
private readonly IWindowManager _windowManager;
1414
private readonly IEventAggregator _events;
1515

16-
private ObservableCollection<BlacklistRule> _addressBlacklist;
16+
private BindableCollection<string> _addressBlacklist;
1717
private string _selectedAddressBlacklistEntry;
1818

1919
/// <summary>
@@ -27,7 +27,7 @@ public AddressBlacklistViewModel(IWindowManager windowManager, IEventAggregator
2727
_windowManager = windowManager;
2828
_events = events;
2929
_events.Subscribe(this);
30-
_addressBlacklist = new ObservableCollection<BlacklistRule>();
30+
_addressBlacklist = new BindableCollection<string>();
3131
LoadAddressBlacklist();
3232
}
3333

@@ -42,7 +42,7 @@ public string SelectedAddressBlacklistEntry
4242
}
4343
}
4444

45-
public ObservableCollection<BlacklistRule> AddressBlacklist
45+
public BindableCollection<string> AddressBlacklist
4646
{
4747
get => _addressBlacklist;
4848
set

SimpleDnsCrypt/ViewModels/DomainBlacklistViewModel.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ namespace SimpleDnsCrypt.ViewModels
2424
[Export(typeof(DomainBlacklistViewModel))]
2525
public class DomainBlacklistViewModel : Screen
2626
{
27-
private const string WhitelistRuleFileName = "domain-whitelist.txt";
28-
private const string BlacklistRuleFileName = "domain-blacklist.txt";
29-
private const string BlacklistFileName = "blacklist.txt";
30-
3127
private static readonly ILog Log = LogManagerHelper.Factory();
3228
private readonly IWindowManager _windowManager;
3329
private readonly IEventAggregator _events;
@@ -64,7 +60,7 @@ public DomainBlacklistViewModel(IWindowManager windowManager, IEventAggregator e
6460
else
6561
{
6662
//set default
67-
_domainBlacklistFile = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, BlacklistFileName);
63+
_domainBlacklistFile = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, Global.BlacklistFileName);
6864
Properties.Settings.Default.DomainBlacklistFile = _domainBlacklistFile;
6965
Properties.Settings.Default.Save();
7066
}
@@ -80,7 +76,7 @@ public DomainBlacklistViewModel(IWindowManager windowManager, IEventAggregator e
8076
else
8177
{
8278
//set default
83-
_domainWhitelistRuleFilePath = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, WhitelistRuleFileName);
79+
_domainWhitelistRuleFilePath = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, Global.WhitelistRuleFileName);
8480
Properties.Settings.Default.DomainWhitelistRules = _domainWhitelistRuleFilePath;
8581
Properties.Settings.Default.Save();
8682
}
@@ -96,7 +92,7 @@ public DomainBlacklistViewModel(IWindowManager windowManager, IEventAggregator e
9692
else
9793
{
9894
//set default
99-
_domainBlacklistRuleFilePath = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, BlacklistRuleFileName);
95+
_domainBlacklistRuleFilePath = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, Global.BlacklistRuleFileName);
10096
Properties.Settings.Default.DomainBlacklistRules = _domainBlacklistRuleFilePath;
10197
Properties.Settings.Default.Save();
10298
}
@@ -138,11 +134,10 @@ private async void ManageDnsCryptBlacklist(DnscryptProxyConfiguration dnscryptPr
138134
var saveAndRestartService = false;
139135
if (dnscryptProxyConfiguration.blacklist == null)
140136
{
141-
dnscryptProxyConfiguration.blacklist = new Blacklist()
137+
dnscryptProxyConfiguration.blacklist = new Blacklist
142138
{
143139
blacklist_file = _domainBlacklistFile,
144-
log_format = defaultLogFormat,
145-
log_file = "blocked.log"
140+
log_format = defaultLogFormat
146141
};
147142
saveAndRestartService = true;
148143
}
@@ -227,7 +222,7 @@ public void ChangeBlacklistFilePath()
227222
var result = blacklistFolderDialog.ShowDialog();
228223
if (result == DialogResult.OK)
229224
{
230-
DomainBlacklistFile = Path.Combine(blacklistFolderDialog.SelectedPath, BlacklistFileName);
225+
DomainBlacklistFile = Path.Combine(blacklistFolderDialog.SelectedPath, Global.BlacklistFileName);
231226
}
232227
}
233228
catch (Exception exception)
@@ -334,7 +329,7 @@ public void ChangeWhitelistRulesFilePath()
334329
var result = whitelistFolderDialog.ShowDialog();
335330
if (result == DialogResult.OK)
336331
{
337-
DomainWhitelistRuleFilePath = Path.Combine(whitelistFolderDialog.SelectedPath, WhitelistRuleFileName);
332+
DomainWhitelistRuleFilePath = Path.Combine(whitelistFolderDialog.SelectedPath, Global.WhitelistRuleFileName);
338333
}
339334
}
340335
catch (Exception exception)
@@ -657,7 +652,7 @@ public void ChangeBlacklistRulesFilePath()
657652
var result = blacklistFolderDialog.ShowDialog();
658653
if (result == DialogResult.OK)
659654
{
660-
DomainBlacklistRuleFilePath = Path.Combine(blacklistFolderDialog.SelectedPath, BlacklistRuleFileName);
655+
DomainBlacklistRuleFilePath = Path.Combine(blacklistFolderDialog.SelectedPath, Global.BlacklistRuleFileName);
661656
}
662657
}
663658
catch (Exception exception)

SimpleDnsCrypt/ViewModels/DomainBlockLogViewModel.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace SimpleDnsCrypt.ViewModels
1414
[Export(typeof(DomainBlockLogViewModel))]
1515
public class DomainBlockLogViewModel : Screen
1616
{
17+
private static readonly ILog Log = LogManagerHelper.Factory();
1718
private readonly IWindowManager _windowManager;
1819
private readonly IEventAggregator _events;
1920

@@ -90,7 +91,6 @@ public bool IsDomainBlockLogLogging
9091

9192
private async void DomainBlockLog(DnscryptProxyConfiguration dnscryptProxyConfiguration)
9293
{
93-
const string defaultLogFileName = "blocked.log";
9494
const string defaultLogFormat = "ltsv";
9595
try
9696
{
@@ -103,7 +103,7 @@ private async void DomainBlockLog(DnscryptProxyConfiguration dnscryptProxyConfig
103103
{
104104
dnscryptProxyConfiguration.blacklist = new Blacklist
105105
{
106-
blacklist_file = defaultLogFileName,
106+
log_file = Global.DomainBlockLogFileName,
107107
log_format = defaultLogFormat
108108
};
109109
saveAndRestartService = true;
@@ -116,17 +116,18 @@ private async void DomainBlockLog(DnscryptProxyConfiguration dnscryptProxyConfig
116116
saveAndRestartService = true;
117117
}
118118

119-
if (string.IsNullOrEmpty(dnscryptProxyConfiguration.blacklist.blacklist_file) ||
120-
!dnscryptProxyConfiguration.blacklist.blacklist_file.Equals(defaultLogFileName))
119+
if (string.IsNullOrEmpty(dnscryptProxyConfiguration.blacklist.log_file) ||
120+
!dnscryptProxyConfiguration.blacklist.log_file.Equals(Global.DomainBlockLogFileName))
121121
{
122-
dnscryptProxyConfiguration.blacklist.blacklist_file = defaultLogFileName;
122+
dnscryptProxyConfiguration.blacklist.log_file = Global.DomainBlockLogFileName;
123123
saveAndRestartService = true;
124124
}
125125

126126
if (saveAndRestartService)
127127
{
128128
DnscryptProxyConfigurationManager.DnscryptProxyConfiguration = dnscryptProxyConfiguration;
129129
if (DnscryptProxyConfigurationManager.SaveConfiguration())
130+
{
130131
if (DnsCryptProxyManager.IsDnsCryptProxyInstalled())
131132
{
132133
if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
@@ -142,14 +143,19 @@ private async void DomainBlockLog(DnscryptProxyConfiguration dnscryptProxyConfig
142143
}
143144
else
144145
{
145-
DnsCryptProxyManager.Install();
146-
DnsCryptProxyManager.Start();
147-
await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
146+
await Task.Run(() => DnsCryptProxyManager.Install()).ConfigureAwait(false);
147+
await Task.Delay(Global.ServiceInstallTime).ConfigureAwait(false);
148+
if (DnsCryptProxyManager.IsDnsCryptProxyInstalled())
149+
{
150+
DnsCryptProxyManager.Start();
151+
await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
152+
}
148153
}
154+
}
149155
}
150156

151157
DomainBlockLogFile = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder,
152-
dnscryptProxyConfiguration.blacklist.blacklist_file);
158+
dnscryptProxyConfiguration.blacklist.log_file);
153159

154160
if (!string.IsNullOrEmpty(_domainBlockLogFile))
155161
if (File.Exists(_domainBlockLogFile))
@@ -191,11 +197,26 @@ await Task.Run(() =>
191197
}
192198
else
193199
{
200+
//disable block log again
201+
_isDomainBlockLogLogging = false;
202+
if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
203+
{
204+
if (dnscryptProxyConfiguration.blacklist?.log_file != null)
205+
{
206+
DnscryptProxyConfigurationManager.DnscryptProxyConfiguration = dnscryptProxyConfiguration;
207+
if (DnscryptProxyConfigurationManager.SaveConfiguration())
208+
{
209+
DnsCryptProxyManager.Restart();
210+
await Task.Delay(Global.ServiceRestartTime).ConfigureAwait(false);
211+
}
212+
}
213+
}
194214
Execute.OnUIThread(() => { DomainBlockLogLines.Clear(); });
195215
}
196216
}
197-
catch (Exception)
217+
catch (Exception exception)
198218
{
219+
Log.Error(exception);
199220
}
200221
}
201222
}

SimpleDnsCrypt/ViewModels/MainViewModel.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public void Initialize()
115115
{
116116
QueryLogViewModel.IsQueryLogLogging = true;
117117
}
118+
119+
if (!string.IsNullOrEmpty(DnscryptProxyConfiguration?.blacklist?.log_file))
120+
{
121+
DomainBlockLogViewModel.IsDomainBlockLogLogging = true;
122+
}
123+
124+
if (!string.IsNullOrEmpty(DnscryptProxyConfiguration?.blacklist?.blacklist_file))
125+
{
126+
DomainBlacklistViewModel.IsBlacklistEnabled = true;
127+
}
118128
}
119129

120130
public bool IsDnsCryptAutomaticModeEnabled
@@ -392,7 +402,6 @@ public async void SaveDnsCryptConfiguration()
392402
DnscryptProxyConfigurationManager.DnscryptProxyConfiguration = _dnscryptProxyConfiguration;
393403
if (DnscryptProxyConfigurationManager.SaveConfiguration())
394404
{
395-
//DnscryptProxyConfigurationManager.LoadConfiguration();
396405
_dnscryptProxyConfiguration = DnscryptProxyConfigurationManager.DnscryptProxyConfiguration;
397406
IsWorkingOnService = true;
398407
if (DnsCryptProxyManager.IsDnsCryptProxyInstalled())

SimpleDnsCrypt/ViewModels/QueryLogViewModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class QueryLogViewModel : Screen
1919
private static readonly ILog Log = LogManagerHelper.Factory();
2020
private readonly IWindowManager _windowManager;
2121
private readonly IEventAggregator _events;
22-
private const string DefaultLogFileName = "query.log";
2322

2423
private ObservableCollection<QueryLogLine> _queryLogLines;
2524
private string _queryLogFile;
@@ -42,7 +41,7 @@ public QueryLogViewModel(IWindowManager windowManager, IEventAggregator events)
4241
else
4342
{
4443
//set default
45-
_queryLogFile = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, DefaultLogFileName);
44+
_queryLogFile = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder, Global.QueryLogFileName);
4645
Properties.Settings.Default.QueryLogFile = _queryLogFile;
4746
Properties.Settings.Default.Save();
4847
}
@@ -126,7 +125,7 @@ public void ChangeQueryLogFilePath()
126125
var result = queryLogFolderDialog.ShowDialog();
127126
if (result == DialogResult.OK)
128127
{
129-
QueryLogFile = Path.Combine(queryLogFolderDialog.SelectedPath, DefaultLogFileName);
128+
QueryLogFile = Path.Combine(queryLogFolderDialog.SelectedPath, Global.QueryLogFileName);
130129
}
131130
}
132131
catch (Exception exception)
@@ -173,6 +172,7 @@ private async void QueryLog(DnscryptProxyConfiguration dnscryptProxyConfiguratio
173172
{
174173
DnscryptProxyConfigurationManager.DnscryptProxyConfiguration = dnscryptProxyConfiguration;
175174
if (DnscryptProxyConfigurationManager.SaveConfiguration())
175+
{
176176
if (DnsCryptProxyManager.IsDnsCryptProxyInstalled())
177177
{
178178
if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
@@ -196,6 +196,7 @@ private async void QueryLog(DnscryptProxyConfiguration dnscryptProxyConfiguratio
196196
await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
197197
}
198198
}
199+
}
199200
}
200201

201202
if (!string.IsNullOrEmpty(_queryLogFile))

0 commit comments

Comments
 (0)