-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
469 lines (398 loc) · 20 KB
/
MainWindow.cs
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Charon_DNS_Changer.Core;
using System.Management;
namespace Charon_DNS_Changer
{
public partial class MainWindow : RadForm
{
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private bool DefaultDNS = false;
private bool CMMode = false;
public string CMprimary, CMsecoundry;
private string DNSChoose = null;
private int DNSIndex;
private CharonPlayerSounds.CharonPlayer ch_player = new CharonPlayerSounds.CharonPlayer();
//private string Interface;
public MainWindow()
{
InitializeComponent();
SetPanelOpacity(0.7f);
DNS_Lists.DropDownListElement.TextBox.TextAlign = HorizontalAlignment.Center;
//this.ChangeDNSBTN.ButtonElement.EnableElementShadow = false;
//this.ChangeDNSBTN.ButtonElement.EnableHighlight = false;
//this.ChangeDNSBTN.ButtonElement.EnableRippleAnimation = false;
//this.ChangeDNSBTN.ButtonElement.EnableFocusBorder = false;
//this.ChangeDNSBTN.ButtonElement.EnableBorderHighlight = false;
this.ChangeDNSBTN.ButtonElement.ButtonFillElement.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
this.ChangeDNSBTN.ButtonElement.BorderElement.ForeColor = Color.DimGray;
this.ChangeDNSBTN.ButtonElement.BorderElement.Visibility = Telerik.WinControls.ElementVisibility.Visible;
this.ChangeDNSBTN.ButtonElement.BorderElement.ForeColor2 = Color.DimGray;
this.ChangeDNSBTN.ButtonElement.BorderElement.ForeColor3 = Color.DimGray;
this.ChangeDNSBTN.ButtonElement.BorderElement.ForeColor4 = Color.DimGray;
Logdata.ReadOnly = true;
Logdata.ForeColor = Color.LimeGreen; // Default text color
Logdata.TabStop = false;
Logdata.KeyPress += (sender, e) => e.Handled = true;
Logdata.Enter += (sender, e) => Logdata.Parent.Focus();
Logdata.Cursor = Cursors.Default;
}
private void AddColoredText(List<object> parts)
{
parts.ForEach(part =>
{
if (part is Color)
{
Logdata.SelectionColor = (Color)part;
}
else if (part is string)
{
Logdata.AppendText((string)part);
}
});
Logdata.SelectionColor = Logdata.ForeColor; // Reset color to default
}
private void MainWindow_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}
private void MainWindow_Paint(object sender, PaintEventArgs e)
{
int cornerRadius = 20; // Adjust as needed
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(new System.Drawing.Rectangle(0, 0, cornerRadius * 2, cornerRadius * 2), 180, 90);
path.AddArc(new System.Drawing.Rectangle(this.Width - cornerRadius * 2, 0, cornerRadius * 2, cornerRadius * 2), -90, 90);
path.AddArc(new System.Drawing.Rectangle(this.Width - cornerRadius * 2, this.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2), 0, 90);
path.AddArc(new System.Drawing.Rectangle(0, this.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2), 90, 90);
path.CloseFigure();
// Apply the rounded rectangle to the form's border
this.Region = new System.Drawing.Region(path);
}
private void btn_close_Click(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
Environment.Exit(0);
}
private void btn_minimize_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void SetPanelOpacity(float opacity)
{
if (opacity < 0 || opacity > 1)
throw new ArgumentOutOfRangeException("opacity", "Opacity must be between 0 and 1.");
Color color = MainPanel.BackColor;
MainPanel.BackColor = Color.FromArgb((int)(opacity * 255), color.R, color.G, color.B);
}
private void radPanel1_Paint(object sender, PaintEventArgs e)
{
int cornerRadius = 20; // Adjust as needed
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(new System.Drawing.Rectangle(0, 0, cornerRadius * 2, cornerRadius * 2), 180, 90);
path.AddArc(new System.Drawing.Rectangle(MainPanel.Width - cornerRadius * 2, 0, cornerRadius * 2, cornerRadius * 2), -90, 90);
path.AddArc(new System.Drawing.Rectangle(MainPanel.Width - cornerRadius * 2, MainPanel.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2), 0, 90);
path.AddArc(new System.Drawing.Rectangle(0, MainPanel.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2), 90, 90);
path.CloseFigure();
// Apply the rounded rectangle to the form's border
MainPanel.Region = new System.Drawing.Region(path);
}
private void CustomMode_ValueChanged(object sender, EventArgs e)
{
if (CustomMode.Value)
{
CustomTB.Visible = true;
CMMode = true;
DefaultDNS = false;
DHCPDefault.Value = false;
DNS_Lists.Enabled = false;
DHCPDefault.Enabled = false;
}
else
{
CMMode = false;
DefaultDNS = false;
DHCPDefault.Value = false;
CustomTB.Visible = false;
DNS_Lists.Enabled = true;
DHCPDefault.Enabled = true;
}
}
private void DHCPDefault_ValueChanged(object sender, EventArgs e)
{
if (DHCPDefault.Value)
{
DefaultDNS = true;
CMMode = false;
CustomMode.Value = false;
DNS_Lists.Enabled = false;
CustomMode.Enabled = false;
CustomTB.Enabled = false;
}
else
{
DefaultDNS = false;
CMMode = false;
CustomMode.Value = false;
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
}
}
private void DNS_Lists_TextChanged(object sender, EventArgs e)
{
DNSChoose = DNS_Lists.Text;
//int selectedIndex = DNS_Lists.SelectedIndex;
//MessageBox.Show(DNSChoose);
}
private void DNS_Lists_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
{
args.VisualItem.TextAlignment = ContentAlignment.MiddleCenter;
}
private void DNS_Lists_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
{
RadDropDownList radDropDownList = sender as RadDropDownList;
int selectedIndex = radDropDownList.SelectedIndex;
DNSIndex = selectedIndex;
}
private void ChangeDNSBTN_Click(object sender, EventArgs e)
{
//waitingBar.Visible = true;
//waitingBar.StartWaiting();
//waitingBar.Text = "Please wait...";
DNS_Lists.Enabled = false;
CustomMode.Enabled = false;
CustomTB.Enabled = false;
DHCPDefault.Enabled = false;
Logdata.Clear();
if (Utils.IsAdministrator())
{
ch_player.PlaySound(Properties.Resources.start);
try
{
string networkAdapterName = Utils.GetFirstConnectedNetworkAdapterName();
if (networkAdapterName == null)
{
AddColoredText(new List<object> { Color.Blue, "[", Color.Red, "-", Color.Blue, "]", Color.Red, " No connected network adapters found.\n" });
ch_player.PlaySound(Properties.Resources.error);
//Console.WriteLine("No connected network adapters found.");
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
}
//Console.WriteLine($"Using network adapter: {networkAdapterName}");
AddColoredText(new List<object> { Color.Blue, "[", Color.LimeGreen, "+", Color.Blue, "]", Color.LimeGreen, $" Using network adapter: {networkAdapterName}\n" });
ManagementObject adapter;
if (Utils.IsConnectionValid(networkAdapterName, out adapter))
{
AddColoredText(new List<object> { Color.Blue, "[", Color.LimeGreen, "+", Color.Blue, "]", Color.LimeGreen, $" Connection is valid for adapter: {networkAdapterName}\n" });
AddColoredText(new List<object> { Color.Blue, "[", Color.LimeGreen, "+", Color.Blue, "]", Color.LimeGreen, $" Adapter Information:\n", Color.Cyan, $"{Utils.GetAdapterInfo(adapter)}\n" });
//;
}
else
{
AddColoredText(new List<object> { Color.Blue, "[", Color.Red, "-", Color.Blue, "]", Color.Red, $" Connection is not valid for adapter: {networkAdapterName}\n" });
ch_player.PlaySound(Properties.Resources.error);
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
//Console.WriteLine($"Connection is not valid for adapter: {networkAdapterName}");
}
if (DefaultDNS)
{
Utils.SetDNSToAutomatic(networkAdapterName);
AddColoredText(new List<object> { Color.Blue, "[", Color.Yellow, "!", Color.Blue, "]", Color.LimeGreen, $" All DNS changed to default (DHCP).\n" });
Notif.NotificationCreator_INFO("", "All DNS changed to default (DHCP).", 2000);
ch_player.PlaySound(Properties.Resources.done);
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
}
else if (CMMode)
{
string[] ipArray = CustomTB.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
List<string> ipList = new List<string>();
Parallel.ForEach(ipArray, ip =>
{
ipList.Add(ip.Trim());
});
if (ipList.Count > 2)
{
AddColoredText(new List<object> { Color.Blue, "[", Color.Red, "-", Color.Blue, "]", Color.Red, $" Please enter only two DNS IPs\n" });
ch_player.PlaySound(Properties.Resources.error);
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
}
else
{
if (ipList.Count > 0)
{
CMprimary = ipList[0];
}
if (ipList.Count > 1)
{
CMsecoundry = ipList[1];
}
string[] dnsServers = { CMprimary, CMsecoundry };
Utils.SetDNSServers(networkAdapterName, dnsServers);
AddColoredText(new List<object> { Color.Blue, "[", Color.Yellow, "!", Color.Blue, "]", Color.LimeGreen, $" Custom DNS servers have been set successfully.\n" });
Notif.NotificationCreator_INFO("", "Custom DNS servers have been set successfully.", 2000);
ch_player.PlaySound(Properties.Resources.done);
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
}
}
else
{
string DNSNameSelected = Utils.GetDns(DNSIndex.ToString());
var NameServer1 = Config.DNS_DICTIONERY[DNSNameSelected]["index1"];
var NameServer2 = Config.DNS_DICTIONERY[DNSNameSelected]["index2"];
//MessageBox.Show($"{DNSNameSelected} => {NameServer1}, {NameServer2}");
string[] dnsServersSelected = { NameServer1, NameServer2 };
Utils.SetDNSServers(networkAdapterName, dnsServersSelected);
AddColoredText(new List<object> { Color.Blue, "[", Color.Yellow, "!", Color.Blue, "]", Color.LimeGreen, $" DNS servers have been set successfully.\n" });
Notif.NotificationCreator_INFO("", "DNS servers have been set successfully.", 2000);
ch_player.PlaySound(Properties.Resources.done);
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
}
}
catch (Exception ex)
{
AddColoredText(new List<object> { Color.Blue, "[", Color.Red, "-", Color.Blue, "]", Color.Red, " Something is wrong\n" });
AddColoredText(new List<object> { Color.Blue, "[", Color.Red, "-", Color.Blue, "]", Color.Red, $" {ex.Message}\n" });
Notif.NotificationCreator_ERROR("", $"Something is wrong: {ex.Message}", 2000);
ch_player.PlaySound(Properties.Resources.error);
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
}
}
else
{
AddColoredText(new List<object> { Color.Blue, "[", Color.Red, "-", Color.Blue, "]", Color.Red, " Please run this program with Administrator user.\n" });
Notif.NotificationCreator_ERROR("", "Please run this program with Administrator user", 2000);
ch_player.PlaySound(Properties.Resources.error);
DNS_Lists.Enabled = true;
CustomMode.Enabled = true;
CustomTB.Enabled = true;
DHCPDefault.Enabled = true;
CustomMode.Value = false;
DHCPDefault.Value = false;
return;
}
}
private void radMenuItem1_Click(object sender, EventArgs e)
{
About_Us ab_us = new About_Us();
ab_us.Show();
}
private void radMenuItem2_Click(object sender, EventArgs e)
{
string url = "https://github.com/Ch4120N/Charon-DNS-Changer-v2";
try
{
// Start the default browser with the specified URL
Process.Start(url);
}
catch (Exception ex)
{
// Handle any errors that occur
MessageBox.Show("An error occurred: " + ex.Message);
}
}
private void Logdata_TextChanged(object sender, EventArgs e)
{
Logdata.SelectionStart = Logdata.Text.Length;
Logdata.ScrollToCaret();
}
//public void OptionSelected(string AdapterName)
//{
// try
// {
// string DNSName = Utils.GetDns(DNSIndex.ToString());
// var NameServer1 = Config.DNS_DICTIONERY[DNSName]["index1"];
// var NameServer2 = Config.DNS_DICTIONERY[DNSName]["index2"];
// string[] dnsServers = { NameServer1, NameServer2 };
// Utils.SetDNSServers(AdapterName, dnsServers);
// }
// catch
// {
// }
//MessageBox.Show(DNSName);
//AddColoredText(new List<object> { Color.Green, "[", Color.White, "+", Color.Green, "] ", Color.LimeGreen, " Message" });
//Interface = Utils.GetInterfaceName();
//Utils.SetDns(DNSName, Interface, "", "");
//MessageBox.Show($"{DNSName} => {NameServer1}, {NameServer2}");
//Parallel.ForEach(Config.DNS_DICTIONERY, outerDic =>
//{
// Parallel.ForEach(outerDic.Value, Inner =>
// {
// MessageBox.Show($"DNS Name: {outerDic.Key} => {Inner.Key} : {Inner.Value}");
// });
//});
//var charonDNS1Index1 = Config.DNS_DICTIONERY["Charon Security Agency DNS1"]["index1"];
//var charonDNS1Index2 = Config.DNS_DICTIONERY["Charon Security Agency DNS1"]["index2"];
//MessageBox.Show($"Charon Security Agency DNS1 => {charonDNS1Index1}, {charonDNS1Index2}");
//if (Config.DNS_DICTIONERY.TryGetValue(select, out var indexs))
//{
// int index1 = -1;
// int index2 = -1;
// return new string[] { index1, index2 };
//}
//else
//{
// // Handle the case where the select key is not found
// return new string[] { "", "" };
//}
}
}