Skip to content

Commit 2f3693e

Browse files
committed
optimize uninstall
- Stop service before uninstall - read -list -json
1 parent 35b37ea commit 2f3693e

5 files changed

Lines changed: 118 additions & 38 deletions

File tree

SimpleDnsCrypt/Helper/DnsCryptProxyManager.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.ServiceProcess;
77
using System.Text;
88
using System.Threading;
9+
using Nett;
910
using Newtonsoft.Json;
1011
using SimpleDnsCrypt.Config;
1112
using SimpleDnsCrypt.Models;
@@ -156,7 +157,7 @@ public static string GetVersion()
156157
}
157158

158159
/// <summary>
159-
/// Get the list of available resolvers for the enabled filters.
160+
/// Get the list of available (active) resolvers for the enabled filters.
160161
/// </summary>
161162
/// <returns></returns>
162163
public static List<AvailableResolver> GetAvailableResolvers()
@@ -180,6 +181,51 @@ public static List<AvailableResolver> GetAvailableResolvers()
180181
return resolvers;
181182
}
182183

184+
/// <summary>
185+
/// Get the list of available (inactive) resolvers for the enabled filters.
186+
/// </summary>
187+
/// <returns></returns>
188+
public static List<AvailableResolver> GetAllResolvers()
189+
{
190+
var resolvers = new List<AvailableResolver>();
191+
var dnscryptFolder = Path.Combine(Directory.GetCurrentDirectory(), Global.DnsCryptProxyFolder);
192+
// we use a second config file
193+
var tmpToml = Path.Combine(dnscryptFolder, "_tmp_.toml");
194+
if (File.Exists(tmpToml))
195+
{
196+
File.Delete(tmpToml);
197+
}
198+
File.Copy(Path.Combine(dnscryptFolder, Global.DnsCryptConfigurationFile), Path.Combine(dnscryptFolder, tmpToml));
199+
var config = Toml.ReadFile<DnscryptProxyConfiguration>(tmpToml);
200+
//clear the array
201+
config.server_names = null;
202+
Toml.WriteFile(config, tmpToml);
203+
var result = ExecuteWithArguments("-config _tmp_.toml -list -json");
204+
if (!result.Success) return resolvers;
205+
if (string.IsNullOrEmpty(result.StandardOutput)) return resolvers;
206+
try
207+
{
208+
var res = JsonConvert.DeserializeObject<List<AvailableResolver>>(result.StandardOutput);
209+
if (res.Count > 0)
210+
{
211+
resolvers = res;
212+
}
213+
}
214+
catch (Exception)
215+
{
216+
217+
}
218+
finally
219+
{
220+
if (File.Exists(tmpToml))
221+
{
222+
File.Delete(tmpToml);
223+
}
224+
}
225+
return resolvers;
226+
}
227+
228+
183229
/// <summary>
184230
/// Install the dnscrypt-proxy service.
185231
/// </summary>

SimpleDnsCrypt/ViewModels/MainViewModel.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ public bool IsDnsCryptAutomaticModeEnabled
128128
DnscryptProxyConfiguration.server_names = null;
129129
SaveDnsCryptConfiguration();
130130
GetAvailableResolvers();
131+
HandleService();
131132
}
132133
else
133134
{
134135
if (DnscryptProxyConfiguration.server_names == null || DnscryptProxyConfiguration.server_names.Count == 0)
135136
{
136137
_isDnsCryptAutomaticModeEnabled = true;
138+
_windowManager.ShowMetroMessageBox("At least one server must be selected. Otherwise, dnscrypt-proxy uses all servers corresponding to the selected filters.", "No server selected",
139+
MessageBoxButton.OK, BoxType.Warning);
137140
}
138141
}
139-
140-
141142
NotifyOfPropertyChange(() => IsDnsCryptAutomaticModeEnabled);
142143
}
143144
}
@@ -399,6 +400,8 @@ public void SaveDnsCryptConfiguration()
399400
}
400401
}
401402
}
403+
_isResolverRunning = DnsCryptProxyManager.IsDnsCryptProxyRunning();
404+
NotifyOfPropertyChange(() => IsResolverRunning);
402405
}
403406
catch(Exception) { }
404407
IsSavingConfiguration = false;
@@ -466,7 +469,6 @@ private async void HandleService()
466469
await Task.Delay(Global.ServiceStopTime).ConfigureAwait(false);
467470
_isResolverRunning = DnsCryptProxyManager.IsDnsCryptProxyRunning();
468471
NotifyOfPropertyChange(() => IsResolverRunning);
469-
ResetNetworkCards();
470472
}
471473
else
472474
{
@@ -477,31 +479,21 @@ private async void HandleService()
477479
await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
478480
_isResolverRunning = DnsCryptProxyManager.IsDnsCryptProxyRunning();
479481
NotifyOfPropertyChange(() => IsResolverRunning);
480-
ResetNetworkCards();
481482
}
482483
else
483484
{
484485
//install and start the service
485-
486-
487-
var x = DnsCryptProxyManager.GetAvailableResolvers();
488486
await Task.Run(() => DnsCryptProxyManager.Install()).ConfigureAwait(false);
489487
await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
490488
await Task.Run(() => { DnsCryptProxyManager.Start(); }).ConfigureAwait(false);
491489
await Task.Delay(Global.ServiceStartTime).ConfigureAwait(false);
492490
_isResolverRunning = DnsCryptProxyManager.IsDnsCryptProxyRunning();
493491
NotifyOfPropertyChange(() => IsResolverRunning);
494-
ResetNetworkCards();
495492
}
496493
}
497494
IsWorkingOnService = false;
498495
}
499496

500-
private static void ResetNetworkCards()
501-
{
502-
503-
}
504-
505497
public bool IsWorkingOnService
506498
{
507499
get => _isWorkingOnService;
@@ -597,6 +589,11 @@ public async void NetworkCardClicked(LocalNetworkInterface localNetworkInterface
597589
DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.ToList()));
598590
localNetworkInterface.UseDnsCrypt = status;
599591
}
592+
else
593+
{
594+
_windowManager.ShowMetroMessageBox("You should start the DnsCrypt service first!", "Service not running",
595+
MessageBoxButton.OK, BoxType.Warning);
596+
}
600597
}
601598
await Task.Delay(1000).ConfigureAwait(false);
602599
localNetworkInterface.IsChangeable = true;
@@ -611,6 +608,12 @@ public void SaveLocalServers()
611608
{
612609
IsDnsCryptAutomaticModeEnabled = false;
613610
SaveDnsCryptConfiguration();
611+
HandleService();
612+
}
613+
else
614+
{
615+
_windowManager.ShowMetroMessageBox("At least one server must be selected. Otherwise, dnscrypt-proxy uses all servers corresponding to the selected filters.", "No server selected",
616+
MessageBoxButton.OK, BoxType.Warning);
614617
}
615618
}
616619

@@ -644,12 +647,19 @@ private void GetAvailableResolvers()
644647
_resolvers.Clear();
645648
var resolvers = DnsCryptProxyManager.GetAvailableResolvers();
646649
_resolvers.AddRange(resolvers);
650+
647651
}
648652

649-
/*
650-
private void ReadStamp()
653+
private void GetAllResolvers()
651654
{
652-
var serverNames = new List<string>();
655+
656+
var resolvers = DnsCryptProxyManager.GetAllResolvers();
657+
658+
if (resolvers != null)
659+
{
660+
661+
}
662+
/*var serverNames = new List<string>();
653663
if (DnscryptProxyConfiguration.server_names != null && DnscryptProxyConfiguration.server_names.Count > 0)
654664
{
655665
serverNames = DnscryptProxyConfiguration.server_names.ToList();
@@ -710,10 +720,10 @@ private void ReadStamp()
710720
}
711721
712722
_resolvers.Add(server);
713-
723+
714724
}
715-
}
716-
}*/
725+
}*/
726+
}
717727

718728
#endregion
719729

@@ -731,12 +741,6 @@ public async void UninstallService()
731741
if (result == MessageBoxResult.Yes)
732742
{
733743
IsUninstallingService = true;
734-
await Task.Run(() =>
735-
{
736-
DnsCryptProxyManager.Uninstall();
737-
}).ConfigureAwait(false);
738-
await Task.Delay(Global.ServiceUninstallTime).ConfigureAwait(false);
739-
740744

741745
if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
742746
{
@@ -746,6 +750,11 @@ await Task.Run(() =>
746750
}).ConfigureAwait(false);
747751
await Task.Delay(Global.ServiceStopTime).ConfigureAwait(false);
748752
}
753+
await Task.Run(() =>
754+
{
755+
DnsCryptProxyManager.Uninstall();
756+
}).ConfigureAwait(false);
757+
await Task.Delay(Global.ServiceUninstallTime).ConfigureAwait(false);
749758
_isResolverRunning = DnsCryptProxyManager.IsDnsCryptProxyRunning();
750759
NotifyOfPropertyChange(() => IsResolverRunning);
751760

SimpleDnsCrypt/Views/MainView.xaml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,18 @@
390390
Foreground="#FF8ab329"
391391
HorizontalAlignment="Left"
392392
VerticalAlignment="Center" />
393-
<TextBlock Grid.Column="1" Grid.Row="0"
394-
Text="{lex:Loc Key=resolvers_available_resolvers_header}"
395-
TextWrapping="Wrap" VerticalAlignment="Center"
396-
Foreground="#FF8ab329" FontSize="20"
397-
Opacity="0.7" Margin="5,0,0,3" FontWeight="Bold">
398-
</TextBlock>
393+
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="0" Margin="5,0,0,3">
394+
<TextBlock Text="{lex:Loc Key=resolvers_available_resolvers_header}"
395+
TextWrapping="Wrap" VerticalAlignment="Center"
396+
Foreground="#FF8ab329" FontSize="20"
397+
Opacity="0.7" FontWeight="Bold">
398+
</TextBlock>
399+
<TextBlock Text="{Binding Resolvers.Count, StringFormat={}({0})}"
400+
TextWrapping="Wrap" VerticalAlignment="Center"
401+
Foreground="#FF8ab329" FontSize="20"
402+
Opacity="0.7" FontWeight="Bold" Margin="5,0,0,0">
403+
</TextBlock>
404+
</StackPanel>
399405
<Button x:Name="SaveLocalServers" Grid.Column="2" Grid.Row="0"
400406
Cursor="Hand" IsEnabled="{Binding Resolvers.Count, Converter={StaticResource IntegerToBoolConverter}}"
401407
Content="{lex:Loc Key=default_settings_apply_settings}"
@@ -438,8 +444,8 @@
438444
<Grid.RowDefinitions>
439445
<RowDefinition Height="350" />
440446
</Grid.RowDefinitions>
441-
<ScrollViewer VerticalScrollBarVisibility="Auto">
442-
<ItemsControl ItemsSource="{Binding Resolvers}" IsEnabled="True">
447+
<ScrollViewer VerticalScrollBarVisibility="Auto" Margin="0,5,0,0">
448+
<ItemsControl ItemsSource="{Binding Resolvers}" IsEnabled="True" Margin="0">
443449
<ItemsControl.ItemsPanel>
444450
<ItemsPanelTemplate>
445451
<UniformGrid Columns="3" />

Uninstall/Program.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Net.NetworkInformation;
6+
using System.Threading;
67

78
namespace Uninstall
89
{
@@ -13,6 +14,8 @@ private static void Main(string[] args)
1314
try
1415
{
1516
ClearLocalNetworkInterfaces();
17+
StopService();
18+
Thread.Sleep(500);
1619
UninstallService();
1720
}
1821
finally
@@ -21,18 +24,34 @@ private static void Main(string[] args)
2124
}
2225
}
2326

27+
/// <summary>
28+
/// Stop the dnscrypt-proxy service.
29+
/// </summary>
30+
internal static void StopService()
31+
{
32+
Console.WriteLine("stopping dnscrypt service");
33+
ExecuteWithArguments("-service stop");
34+
}
35+
2436
/// <summary>
2537
/// Uninstall the dnscrypt-proxy service.
2638
/// </summary>
2739
internal static void UninstallService()
40+
{
41+
Console.WriteLine("removing dnscrypt service");
42+
ExecuteWithArguments("-service uninstall");
43+
}
44+
45+
/// <summary>
46+
/// Uninstall the dnscrypt-proxy service.
47+
/// </summary>
48+
internal static void ExecuteWithArguments(string arguments)
2849
{
2950
try
3051
{
3152
const int timeout = 9000;
3253
const string dnsCryptProxyFolder = "dnscrypt-proxy";
3354
const string dnsCryptProxyExecutableName = "dnscrypt-proxy.exe";
34-
const string arguments = "-service uninstall";
35-
Console.WriteLine("removing dnscrypt service");
3655
using (var process = new Process())
3756
{
3857
process.StartInfo.FileName = Path.Combine(Directory.GetCurrentDirectory(), dnsCryptProxyFolder, dnsCryptProxyExecutableName);

Uninstall/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
[assembly: AssemblyCulture("")]
1212
[assembly: ComVisible(false)]
1313
[assembly: Guid("1a9ca4fe-bdd5-4d7c-a86a-7ed503974718")]
14-
[assembly: AssemblyVersion("0.2.0")]
15-
[assembly: AssemblyFileVersion("0.2.0")]
14+
[assembly: AssemblyVersion("0.2.1")]
15+
[assembly: AssemblyFileVersion("0.2.1")]

0 commit comments

Comments
 (0)