<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace dnscrypt_winclient
{
	public partial class ApplicationForm : Form
	{
		public const int SW_SHOWMINIMIZED = 2;
		public const int SW_HIDE = 0;

		private ProcessStartInfo CryptProc = null;
		private Process CryptHandle = null;
		private bool CryptProcRunning = false;

		public ApplicationForm()
		{
			InitializeComponent();
			GetNICs(false);
			this.portBox.SelectedIndex = 1;	//Port 443
		}

		/// <summary>
		/// Fills the ListBox with information about each NIC
		/// </summary>
		private void GetNICs(Boolean showHidden)
		{
			this.ipv6Radio.Enabled = false;

			NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
			foreach (NetworkInterface adapter in adapters)
			{
				IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
				List<string> dnsServers = new List<string>();	//More accurate than IPInterfaceProperties.DnsAddresses
				List<string> dnsServersv6 = new List<string>();

				//Determine if the DNS addresses are obtained automatically or specified
				//We don't want to add them when the program is closed if they weren't actually set
				//Unfortunately, neither WMI nor the NetworkInterface class provide information about this setting
				dnsServers = NetworkManager.getDNS(adapter.Id);
				dnsServersv6 = NetworkManager.getDNSv6(adapter.Id);

				NetworkListItem item = new NetworkListItem(adapter.Name, adapter.Description, dnsServers, dnsServersv6, adapter.Supports(NetworkInterfaceComponent.IPv4), adapter.Supports(NetworkInterfaceComponent.IPv6));
				if (!item.hidden || showHidden)
				{
					DNSlistbox.Items.Add(item);

					//See if the device supports IPv6 and enable the option if so
					if (item.IPv6)
					{
						this.ipv6Radio.Enabled = true;
					}
				}

				/*
				IPAddressCollection dnsServers2 = adapterProperties.DnsAddresses;
				if (dnsServers2.Count > 0)
				{
					//NetworkListItem item = new NetworkListItem(adapter.Description, dnsServers2);
					//DNSlistbox.Items.Add(item);

					Console.WriteLine(adapter.Description + ": " + adapter.Id);
					foreach (IPAddress dns in dnsServers2)
					{
					    Console.WriteLine("  DNS Servers ............................. : {0}", dns.ToString());
					}
					Console.WriteLine();
				}*/
			}
		}

		/// <summary>
		/// This callback is triggered when a checkbox's state changes in the NIC list box
		/// When the box is checked, the target NIC's DNS settings are changed to 127.0.0.1
		/// When the box is unchecked, the settings are restored to their original value
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DNSlisbox_itemcheck_statechanged(object sender, ItemCheckEventArgs e)
		{
			try
			{
				//Restore the user's old settings
				if (e.CurrentValue == CheckState.Checked)
				{
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).IPv4)
					{
						NetworkManager.setDNS(((NetworkListItem)DNSlistbox.Items[e.Index]).NICDescription, ((NetworkListItem)DNSlistbox.Items[e.Index]).DNSservers);
					}

					//Since we don't set an IPv6 server, don't run this if they didn't have any to begin with
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).DNSserversv6.Count > 0)
					{
						NetworkManager.setDNSv6(((NetworkListItem)DNSlistbox.Items[e.Index]).NICName, ((NetworkListItem)DNSlistbox.Items[e.Index]).DNSserversv6);
					}
				}
				else //Set it to loopback for DNSCrypt
				{
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).IPv4)
					{
						NetworkManager.setDNS(((NetworkListItem)DNSlistbox.Items[e.Index]).NICDescription, "127.0.0.1");
					}

					//Only do this if there were custom servers set to begin with
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).DNSserversv6.Count > 0)
					{
						List<string> ip = new List<string>();
						//ip.Add("::1");
						NetworkManager.setDNSv6(((NetworkListItem)DNSlistbox.Items[e.Index]).NICName, ip);
					}
				}
			}
			catch (Exception exception)
			{
				MessageBox.Show("There was an error changing the device's DNS server: " + exception.Message);
			}
		}

		/// <summary>
		/// Button to start and stop the DNSCrypt Proxy application
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void service_button_Click(object sender, EventArgs e)
		{
			// Start the DNSCrypt process
			if (!this.CryptProcRunning)
			{
				// Make sure the file exists before trying to launch it
				if (!File.Exists(Directory.GetCurrentDirectory() + "\\dnscrypt-proxy.exe"))
				{
					MessageBox.Show("dnscrypt-proxy.exe was not found. It should be placed in the same directory as this program. If you do not have this file, you can download it from https://github.com/opendns/dnscrypt-proxy/downloads", "File not found");
					return;
				}

				this.CryptProc = new ProcessStartInfo();
				this.CryptProc.FileName = "dnscrypt-proxy.exe";
				this.CryptProc.WindowStyle = ProcessWindowStyle.Minimized;

				if (this.protoTCP.Checked)
				{
					this.CryptProc.Arguments = "-T";
				}

				this.CryptProc.Arguments += " -t " + this.portBox.SelectedItem.ToString();

				if (this.ipv6Radio.Checked)
				{
					this.CryptProc.Arguments += " --resolver-address=fc00:db20:35b:7399::5";
				}
				else if (this.parentalControlsRadio.Checked)
				{
					this.CryptProc.Arguments += " --resolver-address=192.168.3.11";
				}

				if (this.gatewayCheckbox.Checked)
				{
					this.CryptProc.Arguments += " --local-address=0.0.0.0";
				}<commit_msg>The -t switch has been deprecated for a while and is gone in 1.1.0<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace dnscrypt_winclient
{
	public partial class ApplicationForm : Form
	{
		public const int SW_SHOWMINIMIZED = 2;
		public const int SW_HIDE = 0;

		private ProcessStartInfo CryptProc = null;
		private Process CryptHandle = null;
		private bool CryptProcRunning = false;

		public ApplicationForm()
		{
			InitializeComponent();
			GetNICs(false);
			this.portBox.SelectedIndex = 1;	//Port 443
		}

		/// <summary>
		/// Fills the ListBox with information about each NIC
		/// </summary>
		private void GetNICs(Boolean showHidden)
		{
			this.ipv6Radio.Enabled = false;

			NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
			foreach (NetworkInterface adapter in adapters)
			{
				IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
				List<string> dnsServers = new List<string>();	//More accurate than IPInterfaceProperties.DnsAddresses
				List<string> dnsServersv6 = new List<string>();

				//Determine if the DNS addresses are obtained automatically or specified
				//We don't want to add them when the program is closed if they weren't actually set
				//Unfortunately, neither WMI nor the NetworkInterface class provide information about this setting
				dnsServers = NetworkManager.getDNS(adapter.Id);
				dnsServersv6 = NetworkManager.getDNSv6(adapter.Id);

				NetworkListItem item = new NetworkListItem(adapter.Name, adapter.Description, dnsServers, dnsServersv6, adapter.Supports(NetworkInterfaceComponent.IPv4), adapter.Supports(NetworkInterfaceComponent.IPv6));
				if (!item.hidden || showHidden)
				{
					DNSlistbox.Items.Add(item);

					//See if the device supports IPv6 and enable the option if so
					if (item.IPv6)
					{
						this.ipv6Radio.Enabled = true;
					}
				}

				/*
				IPAddressCollection dnsServers2 = adapterProperties.DnsAddresses;
				if (dnsServers2.Count > 0)
				{
					//NetworkListItem item = new NetworkListItem(adapter.Description, dnsServers2);
					//DNSlistbox.Items.Add(item);

					Console.WriteLine(adapter.Description + ": " + adapter.Id);
					foreach (IPAddress dns in dnsServers2)
					{
					    Console.WriteLine("  DNS Servers ............................. : {0}", dns.ToString());
					}
					Console.WriteLine();
				}*/
			}
		}

		/// <summary>
		/// This callback is triggered when a checkbox's state changes in the NIC list box
		/// When the box is checked, the target NIC's DNS settings are changed to 127.0.0.1
		/// When the box is unchecked, the settings are restored to their original value
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void DNSlisbox_itemcheck_statechanged(object sender, ItemCheckEventArgs e)
		{
			try
			{
				//Restore the user's old settings
				if (e.CurrentValue == CheckState.Checked)
				{
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).IPv4)
					{
						NetworkManager.setDNS(((NetworkListItem)DNSlistbox.Items[e.Index]).NICDescription, ((NetworkListItem)DNSlistbox.Items[e.Index]).DNSservers);
					}

					//Since we don't set an IPv6 server, don't run this if they didn't have any to begin with
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).DNSserversv6.Count > 0)
					{
						NetworkManager.setDNSv6(((NetworkListItem)DNSlistbox.Items[e.Index]).NICName, ((NetworkListItem)DNSlistbox.Items[e.Index]).DNSserversv6);
					}
				}
				else //Set it to loopback for DNSCrypt
				{
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).IPv4)
					{
						NetworkManager.setDNS(((NetworkListItem)DNSlistbox.Items[e.Index]).NICDescription, "127.0.0.1");
					}

					//Only do this if there were custom servers set to begin with
					if (((NetworkListItem)DNSlistbox.Items[e.Index]).DNSserversv6.Count > 0)
					{
						List<string> ip = new List<string>();
						//ip.Add("::1");
						NetworkManager.setDNSv6(((NetworkListItem)DNSlistbox.Items[e.Index]).NICName, ip);
					}
				}
			}
			catch (Exception exception)
			{
				MessageBox.Show("There was an error changing the device's DNS server: " + exception.Message);
			}
		}

		/// <summary>
		/// Button to start and stop the DNSCrypt Proxy application
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void service_button_Click(object sender, EventArgs e)
		{
			// Start the DNSCrypt process
			if (!this.CryptProcRunning)
			{
				// Make sure the file exists before trying to launch it
				if (!File.Exists(Directory.GetCurrentDirectory() + "\\dnscrypt-proxy.exe"))
				{
					MessageBox.Show("dnscrypt-proxy.exe was not found. It should be placed in the same directory as this program. If you do not have this file, you can download it from https://github.com/opendns/dnscrypt-proxy/downloads", "File not found");
					return;
				}

				this.CryptProc = new ProcessStartInfo();
				this.CryptProc.FileName = "dnscrypt-proxy.exe";
				this.CryptProc.WindowStyle = ProcessWindowStyle.Minimized;

				if (this.protoTCP.Checked)
				{
					this.CryptProc.Arguments = "-T";
				}

				if (this.ipv6Radio.Checked)
				{
					this.CryptProc.Arguments += " --resolver-address=[2620:0:ccd::2]";
				}
				else if (this.parentalControlsRadio.Checked)
				{
					this.CryptProc.Arguments += " --resolver-address=192.168.3.11";
				}
				else
				{
					this.CryptProc.Arguments += " --resolver-address=208.67.220.220";
				}
				this.CryptProc.Arguments += ":" + this.portBox.SelectedItem.ToString();

				if (this.gatewayCheckbox.Checked)
				{
					this.CryptProc.Arguments += " --local-address=0.0.0.0";
				}
{'fraction_non_alphanumeric': 0.12431539598365644, 'fraction_numerical': 0.012953142658436929, 'mean_word_length': 3.2560118386977432, 'pattern_counts': {'":': 5, '<': 43, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 9, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7232846', 'n_tokens_mistral': 4432, 'n_tokens_neox': 3523, 'n_words': 859}