Skip to content

Commit

Permalink
Purge the DNS cache on service start
Browse files Browse the repository at this point in the history
  • Loading branch information
instantsc committed Jan 22, 2022
1 parent b5f4b7e commit 38b569e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions SimpleDnsCrypt/Helper/DnsCryptProxyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public static async Task<bool> Restart()
{
var dnscryptService = new ServiceController { ServiceName = DnsCryptProxyServiceName };
await dnscryptService.StopAsync(TimeSpan.FromMilliseconds(Global.ServiceStopTime));
return await dnscryptService.StartAsync(TimeSpan.FromMilliseconds(Global.ServiceStartTime));
var result = await dnscryptService.StartAsync(TimeSpan.FromMilliseconds(Global.ServiceStartTime));
await FlushSystemDnsCache();
return result;
}
catch (Exception exception)
{
Expand Down Expand Up @@ -142,16 +144,15 @@ public static async Task<bool> Start()
var dnscryptService = new ServiceController { ServiceName = DnsCryptProxyServiceName };

var proxyStatus = dnscryptService.Status;
switch (proxyStatus)
var result = proxyStatus switch
{
case ServiceControllerStatus.ContinuePending:
case ServiceControllerStatus.Paused:
case ServiceControllerStatus.PausePending:
case ServiceControllerStatus.Stopped:
case ServiceControllerStatus.StopPending:
return await dnscryptService.StartAsync(TimeSpan.FromMilliseconds(Global.ServiceStartTime));
}
return dnscryptService.Status == ServiceControllerStatus.Running;
ServiceControllerStatus.StartPending =>
await dnscryptService.WaitForStatusAsync(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds(Global.ServiceStartTime)),
ServiceControllerStatus.Running => true,
_ => await dnscryptService.StartAsync(TimeSpan.FromMilliseconds(Global.ServiceStartTime))
};
await FlushSystemDnsCache();
return result;
}
catch (Exception exception)
{
Expand All @@ -160,6 +161,11 @@ public static async Task<bool> Start()
}
}

public static async Task FlushSystemDnsCache()
{
await ProcessHelper.ExecuteWithArgumentsAsync("ipconfig", "/flushdns");
}

/// <summary>
/// Get the version of the dnscrypt-proxy.exe.
/// </summary>
Expand Down

0 comments on commit 38b569e

Please sign in to comment.