Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
171 lines (162 loc) · 6.56 KB
/
Copy pathProgram.cs
File metadata and controls
171 lines (162 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Net.NetworkInformation;
using System.Threading;
namespace Uninstall
{
internal class Program
{
private const string DnsCryptProxyFolder = "dnscrypt-proxy";
private const string DnsCryptProxyExecutableName = "dnscrypt-proxy.exe";
private const string DnsCryptProxyConfigName = "dnscrypt-proxy.toml";
private static void Main(string[] args)
{
try
{
BackupConfigurationFile();
ClearLocalNetworkInterfaces();
StopService();
Thread.Sleep(500);
UninstallService();
}
finally
{
Environment.Exit(0);
}
}
/// <summary>
/// Copy dnscrypt-proxy.toml to tmp folder.
/// </summary>
internal static void BackupConfigurationFile()
{
try
{
var sdcConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleDnsCrypt.exe.config");
if (!File.Exists(sdcConfig)) return;
var sdcConfigMap = new ExeConfigurationFileMap
{
ExeConfigFilename = sdcConfig
};
var sdcConfigContent =
ConfigurationManager.OpenMappedExeConfiguration(sdcConfigMap, ConfigurationUserLevel.None);
if (!sdcConfigContent.HasFile) return;
var section = (ClientSettingsSection)sdcConfigContent.GetSection("userSettings/SimpleDnsCrypt.Properties.Settings");
var setting = section.Settings.Get("BackupAndRestoreConfigOnUpdate");
var backupAndRestoreConfigOnUpdate = Convert.ToBoolean(setting.Value.ValueXml.LastChild.InnerText);
if (!backupAndRestoreConfigOnUpdate) return;
var config = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DnsCryptProxyFolder,
DnsCryptProxyConfigName);
if (!File.Exists(config)) return;
var tmp = Path.Combine(Path.GetTempPath(), DnsCryptProxyConfigName + ".bak");
Console.WriteLine($"backup configuration to {tmp}");
File.Copy(config, tmp);
}
catch (Exception) { }
}
/// <summary>
/// Stop the dnscrypt-proxy service.
/// </summary>
internal static void StopService()
{
Console.WriteLine("stopping dnscrypt service");
var dnsCryptProxyExecutablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DnsCryptProxyFolder, DnsCryptProxyExecutableName);
ExecuteWithArguments(dnsCryptProxyExecutablePath, "-service stop");
}
/// <summary>
/// Uninstall the dnscrypt-proxy service.
/// </summary>
internal static void UninstallService()
{
Console.WriteLine("removing dnscrypt service");
var dnsCryptProxyExecutablePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DnsCryptProxyFolder, DnsCryptProxyExecutableName);
ExecuteWithArguments(dnsCryptProxyExecutablePath, "-service uninstall");
Registry.LocalMachine.DeleteSubKey(@"SYSTEM\CurrentControlSet\Services\EventLog\Application\dnscrypt-proxy", false);
}
/// <summary>
/// Execute process with arguments
/// </summary>
internal static void ExecuteWithArguments(string filename, string arguments)
{
try
{
const int timeout = 9000;
using (var process = new Process())
{
process.StartInfo.FileName = filename;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
if (process.WaitForExit(timeout))
{
if (process.ExitCode == 0)
{
//do nothing
}
}
else
{
// Timed out.
throw new Exception("Timed out");
}
}
}
catch (Exception) { }
}
/// <summary>
/// Clear all network interfaces.
/// </summary>
internal static void ClearLocalNetworkInterfaces()
{
try
{
string[] networkInterfaceBlacklist =
{
"Microsoft Virtual",
"Hamachi Network",
"VMware Virtual",
"VirtualBox",
"Software Loopback",
"Microsoft ISATAP",
"Microsoft-ISATAP",
"Teredo Tunneling Pseudo-Interface",
"Microsoft Wi-Fi Direct Virtual",
"Microsoft Teredo Tunneling Adapter",
"Von Microsoft gehosteter",
"Microsoft hosted",
"Virtueller Microsoft-Adapter",
"TAP"
};
var networkInterfaces = new List<NetworkInterface>();
foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus != OperationalStatus.Up)
{
continue;
}
foreach (var blacklistEntry in networkInterfaceBlacklist)
{
if (nic.Description.Contains(blacklistEntry) || nic.Name.Contains(blacklistEntry)) continue;
if (!networkInterfaces.Contains(nic))
{
networkInterfaces.Add(nic);
}
}
}
foreach (var networkInterface in networkInterfaces)
{
ExecuteWithArguments("netsh", "interface ipv4 delete dns \"" + networkInterface.Name + "\" all");
ExecuteWithArguments("netsh", "interface ipv6 delete dns \"" + networkInterface.Name + "\" all");
}
}
catch (Exception)
{
}
}
}
}