-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkHelper.cs
More file actions
336 lines (295 loc) · 15.8 KB
/
NetworkHelper.cs
File metadata and controls
336 lines (295 loc) · 15.8 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using static GateSwitchWay.NetworkHelper;
namespace GateSwitchWay
{
public static class NetworkHelper
{
public struct NetworkInfo
{
public bool Gateway4_enable;
public string Gateway4;
public bool Gateway6_enable;
public string Gateway6;
public bool Dns4_enable;
public string Dns4;
public bool Dns6_enable;
public string Dns6;
}
public static NetworkInfo GetCurrentNetworkInfo()
{
NetworkInfo currentNetworkInfo = new NetworkInfo
{
Gateway4 = "N/A",
Gateway6 = "N/A",
Dns4 = "N/A",
Dns6 = "N/A",
Gateway4_enable = false,
Gateway6_enable = false,
Dns4_enable = false,
Dns6_enable = false
};
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.OperationalStatus == OperationalStatus.Up && ni.NetworkInterfaceType != NetworkInterfaceType.Loopback)
{
IPInterfaceProperties ipProps = ni.GetIPProperties();
// Get the gateway addresses
var gateway4 = ipProps.GatewayAddresses
.FirstOrDefault(g => g.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
var gateway6 = ipProps.GatewayAddresses
.FirstOrDefault(g => g.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
currentNetworkInfo.Gateway4 = gateway4?.Address.ToString() ?? "N/A";
currentNetworkInfo.Gateway6 = gateway6?.Address.ToString() ?? "N/A";
currentNetworkInfo.Gateway4_enable = gateway4 != null;
currentNetworkInfo.Gateway6_enable = gateway6 != null;
// Get the DNS addresses
var dnsAddress4 = ipProps.DnsAddresses
.FirstOrDefault(d => d.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
var dnsAddress6 = ipProps.DnsAddresses
.FirstOrDefault(d => d.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
currentNetworkInfo.Dns4 = dnsAddress4?.ToString() ?? "N/A";
currentNetworkInfo.Dns6 = dnsAddress6?.ToString() ?? "N/A";
currentNetworkInfo.Dns4_enable = dnsAddress4 != null;
currentNetworkInfo.Dns6_enable = dnsAddress6 != null;
break; // Use the first active network interface
}
}
return currentNetworkInfo;
}
public static void UpdateTaskbarNetworkInfo(NetworkInfo networkInfo, NotifyIcon notifyIcon, bool isSwitchedOn)
{
notifyIcon.Text = $"{(isSwitchedOn ? "AlterNative GW" : "Native GW")}\rGW4: {networkInfo.Gateway4}\rGW6: {networkInfo.Gateway6}\rDNS4: {networkInfo.Dns4}\rDNS6: {networkInfo.Dns6}";
}
public static void PopulateNetworkInfoTextBoxes(NetworkInfo networkInfo, TextBox textBoxGw4, TextBox textBoxGw6, TextBox textBoxDns4, TextBox textBoxDns6)
{
textBoxGw4.Text = networkInfo.Gateway4;
textBoxGw6.Text = networkInfo.Gateway6;
textBoxDns4.Text = networkInfo.Dns4;
textBoxDns6.Text = networkInfo.Dns6;
}
public static void LoadAlterNativeSettings(TextBox textBoxGw4, TextBox textBoxGw6, TextBox textBoxDns4, TextBox textBoxDns6, CheckBox checkBoxGw4, CheckBox checkBoxGw6, CheckBox checkBoxDns4, CheckBox checkBoxDns6)
{
textBoxGw4.Text = Settings.Default.AlterNativeGw4;
textBoxGw6.Text = Settings.Default.AlterNativeGw6;
textBoxDns4.Text = Settings.Default.AlterNativeDns4;
textBoxDns6.Text = Settings.Default.AlterNativeDns6;
checkBoxGw4.Checked = Settings.Default.AlterNativeGw4Enabled;
checkBoxGw6.Checked = Settings.Default.AlterNativeGw6Enabled;
checkBoxDns4.Checked = Settings.Default.AlterNativeDns4Enabled;
checkBoxDns6.Checked = Settings.Default.AlterNativeDns6Enabled;
}
public static void SaveAlterNativeSettings(TextBox textBoxGw4, TextBox textBoxGw6, TextBox textBoxDns4, TextBox textBoxDns6, CheckBox checkBoxGw4, CheckBox checkBoxGw6, CheckBox checkBoxDns4, CheckBox checkBoxDns6)
{
Settings.Default.AlterNativeGw4 = textBoxGw4.Text;
Settings.Default.AlterNativeGw6 = textBoxGw6.Text;
Settings.Default.AlterNativeDns4 = textBoxDns4.Text;
Settings.Default.AlterNativeDns6 = textBoxDns6.Text;
Settings.Default.AlterNativeGw4Enabled = checkBoxGw4.Checked;
Settings.Default.AlterNativeGw6Enabled = checkBoxGw6.Checked;
Settings.Default.AlterNativeDns4Enabled = checkBoxDns4.Checked;
Settings.Default.AlterNativeDns6Enabled = checkBoxDns6.Checked;
Settings.Default.Save();
}
public static void TemporaryModifyNetworkInfo(NetworkInfo networkInfo)
{
var currentNetworkInfo = GetCurrentNetworkInfo();
// Modify IPv4 Gateway
if (networkInfo.Gateway4_enable && !string.IsNullOrEmpty(networkInfo.Gateway4) && networkInfo.Gateway4 != "N/A" && networkInfo.Gateway4 != currentNetworkInfo.Gateway4)
{
ExecutePowerShellCommand($"New-NetRoute -DestinationPrefix \"0.0.0.0/0\" -NextHop {networkInfo.Gateway4} -InterfaceIndex (Get-NetAdapter -Physical | Where-Object {{ $_.Status -eq 'Up' }}).ifIndex -PolicyStore \"ActiveStore\"");
if (currentNetworkInfo.Gateway4 != "N/A")
{
ExecutePowerShellCommand($"Remove-NetRoute -NextHop {currentNetworkInfo.Gateway4} -Confirm:$False");
}
}
// Modify IPv6 Gateway
if (networkInfo.Gateway6_enable && !string.IsNullOrEmpty(networkInfo.Gateway6) && networkInfo.Gateway6 != "N/A" && networkInfo.Gateway6 != currentNetworkInfo.Gateway6)
{
ExecutePowerShellCommand($"New-NetRoute -DestinationPrefix \"::/0\" -NextHop {networkInfo.Gateway6} -InterfaceIndex (Get-NetAdapter -Physical | Where-Object {{ $_.Status -eq 'Up' }}).ifIndex -PolicyStore \"ActiveStore\"");
if (currentNetworkInfo.Gateway6 != "N/A")
{
ExecutePowerShellCommand($"Remove-NetRoute -NextHop {currentNetworkInfo.Gateway6} -Confirm:$False");
}
}
// Modify IPv4 DNS
if (networkInfo.Dns4_enable && !string.IsNullOrEmpty(networkInfo.Dns4) && networkInfo.Dns4 != "N/A" && networkInfo.Dns4 != currentNetworkInfo.Dns4)
{
ExecutePowerShellCommand($"Set-DnsClientServerAddress -InterfaceIndex (Get-NetAdapter -Physical | Where-Object {{ $_.Status -eq 'Up' }}).ifIndex -ServerAddresses {networkInfo.Dns4}");
}
// Modify IPv6 DNS
if (networkInfo.Dns6_enable && !string.IsNullOrEmpty(networkInfo.Dns6) && networkInfo.Dns6 != "N/A" && networkInfo.Dns6 != currentNetworkInfo.Dns6)
{
ExecutePowerShellCommand($"Set-DnsClientServerAddress -InterfaceIndex (Get-NetAdapter -Physical | Where-Object {{ $_.Status -eq 'Up' }}).ifIndex -ServerAddresses {networkInfo.Dns6}");
}
}
private static void ExecutePowerShellCommand(string command)
{
// If adding a new route, check if it exists first and use different approach
if (command.StartsWith("New-NetRoute"))
{
// Extract destination prefix and next hop from the command
string destinationPrefix = ExtractParameter(command, "-DestinationPrefix");
string nextHop = ExtractParameter(command, "-NextHop");
if (!string.IsNullOrEmpty(destinationPrefix) && !string.IsNullOrEmpty(nextHop))
{
// Create a modified command that checks if the route exists first
string modifiedCommand = $@"
$route = Get-NetRoute -DestinationPrefix '{destinationPrefix}' -ErrorAction SilentlyContinue
if ($route) {{
# Route exists, remove it first
Remove-NetRoute -DestinationPrefix '{destinationPrefix}' -Confirm:$False -ErrorAction SilentlyContinue
}}
# Now add the new route using the original parameters
{command}
";
ExecuteRawPowerShellCommand(modifiedCommand);
return;
}
}
// For other commands, execute normally
ExecuteRawPowerShellCommand(command);
}
private static string ExtractParameter(string command, string paramName)
{
// Simple parameter extraction
int startIndex = command.IndexOf(paramName) + paramName.Length;
if (startIndex > paramName.Length)
{
startIndex = command.IndexOf("\"", startIndex);
if (startIndex > 0)
{
int endIndex = command.IndexOf("\"", startIndex + 1);
if (endIndex > startIndex)
{
return command.Substring(startIndex + 1, endIndex - startIndex - 1);
}
}
else
{
// Try without quotes
startIndex = command.IndexOf(" ", command.IndexOf(paramName) + paramName.Length) + 1;
if (startIndex > 0)
{
int endIndex = command.IndexOf(" ", startIndex);
if (endIndex < 0) endIndex = command.Length;
return command.Substring(startIndex, endIndex - startIndex);
}
}
}
return string.Empty;
}
private static void ExecuteRawPowerShellCommand(string command)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-Command \"{command}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process process = new Process())
{
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
if (!string.IsNullOrEmpty(error) &&
!error.Contains("Instance MSFT_NetRoute already exists") &&
!error.Contains("ObjectNotFound")) // Ignore errors when removing non-existent routes
{
Debug.WriteLine($"PowerShell error: {error}");
throw new InvalidOperationException($"Error executing PowerShell command: {error}");
}
}
}
public static void SaveNativeSettings(NetworkInfo networkInfo)
{
Settings.Default.NativeGw4 = networkInfo.Gateway4;
Settings.Default.NativeGw6 = networkInfo.Gateway6;
Settings.Default.NativeDns4 = networkInfo.Dns4;
Settings.Default.NativeDns6 = networkInfo.Dns6;
Settings.Default.NativeGw4Enabled = networkInfo.Gateway4_enable;
Settings.Default.NativeGw6Enabled = networkInfo.Gateway6_enable;
Settings.Default.NativeDns4Enabled = networkInfo.Dns4_enable;
Settings.Default.NativeDns6Enabled = networkInfo.Dns6_enable;
Settings.Default.Save();
}
public static void SaveNativeSettingsFromUI(TextBox textBoxGw4, TextBox textBoxGw6, TextBox textBoxDns4, TextBox textBoxDns6, CheckBox checkBoxGw4, CheckBox checkBoxGw6, CheckBox checkBoxDns4, CheckBox checkBoxDns6)
{
Settings.Default.NativeGw4 = textBoxGw4.Text;
Settings.Default.NativeGw6 = textBoxGw6.Text;
Settings.Default.NativeDns4 = textBoxDns4.Text;
Settings.Default.NativeDns6 = textBoxDns6.Text;
Settings.Default.NativeGw4Enabled = checkBoxGw4.Checked;
Settings.Default.NativeGw6Enabled = checkBoxGw6.Checked;
Settings.Default.NativeDns4Enabled = checkBoxDns4.Checked;
Settings.Default.NativeDns6Enabled = checkBoxDns6.Checked;
Settings.Default.Save();
}
public static void LoadNativeSettingsToUI(TextBox textBoxGw4, TextBox textBoxGw6, TextBox textBoxDns4, TextBox textBoxDns6, CheckBox checkBoxGw4, CheckBox checkBoxGw6, CheckBox checkBoxDns4, CheckBox checkBoxDns6)
{
textBoxGw4.Text = Settings.Default.NativeGw4;
textBoxGw6.Text = Settings.Default.NativeGw6;
textBoxDns4.Text = Settings.Default.NativeDns4;
textBoxDns6.Text = Settings.Default.NativeDns6;
checkBoxGw4.Checked = Settings.Default.NativeGw4Enabled;
checkBoxGw6.Checked = Settings.Default.NativeGw6Enabled;
checkBoxDns4.Checked = Settings.Default.NativeDns4Enabled;
checkBoxDns6.Checked = Settings.Default.NativeDns6Enabled;
}
public static NetworkInfo GetAlterNativeSettings()
{
NetworkInfo alternativeNetworkInfo = new NetworkInfo
{
Gateway4 = Settings.Default.AlterNativeGw4,
Gateway6 = Settings.Default.AlterNativeGw6,
Dns4 = Settings.Default.AlterNativeDns4,
Dns6 = Settings.Default.AlterNativeDns6,
Gateway4_enable = Settings.Default.AlterNativeGw4Enabled,
Gateway6_enable = Settings.Default.AlterNativeGw6Enabled,
Dns4_enable = Settings.Default.AlterNativeDns4Enabled,
Dns6_enable = Settings.Default.AlterNativeDns6Enabled
};
return alternativeNetworkInfo;
}
public static bool AreSettingsEqual(NetworkInfo networkInfo1, NetworkInfo networkInfo2)
{
// Check if none of the fields are enabled
if (!networkInfo2.Gateway4_enable && !networkInfo2.Gateway6_enable &&
!networkInfo2.Dns4_enable && !networkInfo2.Dns6_enable)
{
return false;
}
if (networkInfo2.Gateway4_enable && networkInfo1.Gateway4 != networkInfo2.Gateway4)
return false;
if (networkInfo2.Gateway6_enable && networkInfo1.Gateway6 != networkInfo2.Gateway6)
return false;
if (networkInfo2.Dns4_enable && networkInfo1.Dns4 != networkInfo2.Dns4)
return false;
if (networkInfo2.Dns6_enable && networkInfo1.Dns6 != networkInfo2.Dns6)
return false;
return true;
}
public static NetworkInfo LoadNativeSettings()
{
NetworkInfo nativeNetworkInfo = new NetworkInfo
{
Gateway4 = Settings.Default.NativeGw4,
Gateway6 = Settings.Default.NativeGw6,
Dns4 = Settings.Default.NativeDns4,
Dns6 = Settings.Default.NativeDns6,
Gateway4_enable = Settings.Default.NativeGw4Enabled,
Gateway6_enable = Settings.Default.NativeGw6Enabled,
Dns4_enable = Settings.Default.NativeDns4Enabled,
Dns6_enable = Settings.Default.NativeDns6Enabled
};
return nativeNetworkInfo;
}
}
}