|
1 | | -using System; |
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Net.NetworkInformation; |
| 7 | +using System.Threading; |
2 | 8 |
|
3 | 9 | namespace Uninstall |
4 | 10 | { |
5 | | - class Program |
| 11 | + internal class Program |
6 | 12 | { |
| 13 | + private const string DnsCryptProxyFolder = "dnscrypt-proxy"; |
| 14 | + private const string DnsCryptProxyExecutableName = "dnscrypt-proxy.exe"; |
| 15 | + |
7 | 16 | static void Main(string[] args) |
8 | 17 | { |
9 | | - Console.WriteLine("Uninstall"); |
| 18 | + try |
| 19 | + { |
| 20 | + ClearLocalNetworkInterfaces(); |
| 21 | + StopService(); |
| 22 | + Thread.Sleep(500); |
| 23 | + UninstallService(); |
| 24 | + } |
| 25 | + finally |
| 26 | + { |
| 27 | + Environment.Exit(0); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Clear all network interfaces. |
| 33 | + /// </summary> |
| 34 | + internal static void ClearLocalNetworkInterfaces() |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + string[] networkInterfaceBlacklist = |
| 39 | + { |
| 40 | + "Microsoft Virtual", |
| 41 | + "Hamachi Network", |
| 42 | + "VMware Virtual", |
| 43 | + "VirtualBox", |
| 44 | + "Software Loopback", |
| 45 | + "Microsoft ISATAP", |
| 46 | + "Microsoft-ISATAP", |
| 47 | + "Teredo Tunneling Pseudo-Interface", |
| 48 | + "Microsoft Wi-Fi Direct Virtual", |
| 49 | + "Microsoft Teredo Tunneling Adapter", |
| 50 | + "Von Microsoft gehosteter", |
| 51 | + "Microsoft hosted", |
| 52 | + "Virtueller Microsoft-Adapter", |
| 53 | + "TAP" |
| 54 | + }; |
| 55 | + |
| 56 | + var networkInterfaces = new List<NetworkInterface>(); |
| 57 | + foreach (var nic in NetworkInterface.GetAllNetworkInterfaces()) |
| 58 | + { |
| 59 | + if (nic.OperationalStatus != OperationalStatus.Up) |
| 60 | + { |
| 61 | + continue; |
| 62 | + } |
| 63 | + foreach (var blacklistEntry in networkInterfaceBlacklist) |
| 64 | + { |
| 65 | + if (nic.Description.Contains(blacklistEntry) || nic.Name.Contains(blacklistEntry)) continue; |
| 66 | + if (!networkInterfaces.Contains(nic)) |
| 67 | + { |
| 68 | + networkInterfaces.Add(nic); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + foreach (var networkInterface in networkInterfaces) |
| 74 | + { |
| 75 | + using var process = new Process(); |
| 76 | + Console.WriteLine("clearing {0}", networkInterface.Name); |
| 77 | + ExecuteWithArguments("netsh", "interface ipv4 delete dns \"" + networkInterface.Name + "\" all"); |
| 78 | + ExecuteWithArguments("netsh", "interface ipv6 delete dns \"" + networkInterface.Name + "\" all"); |
| 79 | + } |
| 80 | + } |
| 81 | + catch (Exception) |
| 82 | + { |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Stop the dnscrypt-proxy service. |
| 88 | + /// </summary> |
| 89 | + internal static void StopService() |
| 90 | + { |
| 91 | + Console.WriteLine("stopping dnscrypt service"); |
| 92 | + var dnsCryptProxy = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DnsCryptProxyFolder, DnsCryptProxyExecutableName); |
| 93 | + ExecuteWithArguments(dnsCryptProxy, "-service stop"); |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Uninstall the dnscrypt-proxy service. |
| 98 | + /// </summary> |
| 99 | + internal static void UninstallService() |
| 100 | + { |
| 101 | + Console.WriteLine("removing dnscrypt service"); |
| 102 | + var dnsCryptProxy = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DnsCryptProxyFolder, DnsCryptProxyExecutableName); |
| 103 | + ExecuteWithArguments(dnsCryptProxy, "-service uninstall"); |
| 104 | + Registry.LocalMachine.DeleteSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog\Application\dnscrypt-proxy", false); |
| 105 | + } |
| 106 | + |
| 107 | + private static void ExecuteWithArguments(string filename, string arguments) |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + const int timeout = 9000; |
| 112 | + using var process = new Process(); |
| 113 | + process.StartInfo.FileName = filename; |
| 114 | + process.StartInfo.Arguments = arguments; |
| 115 | + process.StartInfo.UseShellExecute = false; |
| 116 | + process.StartInfo.CreateNoWindow = true; |
| 117 | + process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
| 118 | + process.StartInfo.RedirectStandardOutput = true; |
| 119 | + process.StartInfo.RedirectStandardError = true; |
| 120 | + process.Start(); |
| 121 | + |
| 122 | + if (process.WaitForExit(timeout)) |
| 123 | + { |
| 124 | + if (process.ExitCode == 0) |
| 125 | + { |
| 126 | + //do nothing |
| 127 | + } |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + // Timed out. |
| 132 | + throw new Exception("Timed out"); |
| 133 | + } |
| 134 | + } |
| 135 | + catch (Exception) |
| 136 | + { |
| 137 | + |
| 138 | + } |
10 | 139 | } |
11 | 140 | } |
12 | 141 | } |
0 commit comments