public static void Switch(string servername)
        {
            var           data    = JsonConvert.DeserializeObject <Data>(m_client.DownloadString("https://raw.githubusercontent.com/MinisBett/ultimate-osu-server-switcher/master/data/data.json"));
            List <Server> servers = data.Servers.ToList();

            servers.Add(Server.BanchoServer);
            Server server = servers.FirstOrDefault(x => x.ServerName == servername);

            if (server == null)
            {
                MessageBox.Show("The referenced server could not be found.\r\n\r\nPlease try to recreate this shortcut.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            CertificateManager.UninstallAllCertificates(data.Servers.ToList());
            List <string> hosts = HostsUtil.GetHosts().ToList();

            hosts.RemoveAll(x => x.Contains(".ppy.sh"));
            HostsUtil.SetHosts(hosts.ToArray());

            if (!server.IsBancho)
            {
                string[] osu_domains = new string[]
                {
                    //"delta.ppy.sh",
                    "osu.ppy.sh",
                    "c.ppy.sh",
                    "c1.ppy.sh",
                    "c2.ppy.sh",
                    "c3.ppy.sh",
                    "c4.ppy.sh",
                    "c5.ppy.sh",
                    "c6.ppy.sh",
                    "ce.ppy.sh",
                    "a.ppy.sh",
                    //"s.ppy.sh",
                    "i.ppy.sh",
                    //"bm6.ppy.sh",
                };

                hosts = HostsUtil.GetHosts().ToList();
                foreach (string domain in osu_domains)
                {
                    hosts.Add(server.ServerIP + " " + domain);
                }

                HostsUtil.SetHosts(hosts.ToArray());
                CertificateManager.InstallCertificate(server);
            }

            Process.Start(GetOsuExecutablePath());
        }
        private Server GetCurrentServer()
        {
            string[] hosts = HostsUtil.GetHosts();

            for (int i = 0; i < hosts.Length; i++)
            {
                if (hosts[i].Contains(".ppy.sh"))
                {
                    string ip = hosts[i].Replace("\t", " ").Split(' ')[0];
                    return(m_servers.FirstOrDefault(x => x.ServerIP == ip) ?? Server.UnidentifiedServer);
                }
            }

            return(Server.BanchoServer);
        }
        /// <summary>
        /// Returns the server the user is currently connected to
        /// </summary>
        public static Server GetCurrentServer()
        {
            // Get every line of the hosts file
            string[] hosts = HostsUtil.GetHosts();

            // Check for the first line that contains the osu domain
            for (int i = 0; i < hosts.Length; i++)
            {
                if (hosts[i].Contains(".ppy.sh"))
                {
                    // Read the ip that .ppy.sh redirects to
                    string ip = hosts[i].Replace("\t", " ").Split(' ')[0];

                    // Try to identify and return the server
                    return(Servers.FirstOrDefault(x => x.IP == ip) ?? Server.UnidentifiedServer);
                }
            }

            // Because after downloading icon etc the bancho server object is not equal to Server.BanchoServer
            // because the Icon property is set then
            return(Servers.First(x => x.IsBancho));
        }
        /// <summary>
        /// Switch the server
        /// </summary>
        /// <param name="server">The server to switch to</param>
        public static void SwitchServer(Server server)
        {
            // Remember the old server for telemetry
            Server from = GetCurrentServer();

            // Get rid of all uneccessary certificates
            CertificateManager.UninstallAllCertificates(Servers);

            // Clean up the hosts file
            List <string> hosts = HostsUtil.GetHosts().ToList();

            hosts.RemoveAll(x => x.Contains(".ppy.sh"));
            HostsUtil.SetHosts(hosts.ToArray());

            // Edit the hosts file if the server is not bancho
            if (!server.IsBancho)
            {
                // Change the hosts file by adding the server's ip and the osu domain
                hosts = HostsUtil.GetHosts().ToList();
                foreach (string domain in Variables.OsuDomains)
                {
                    hosts.Add(server.IP + " " + domain);
                }
                HostsUtil.SetHosts(hosts.ToArray());

                // Install the certificate if needed (not needed for bancho and localhost)
                if (server.HasCertificate)
                {
                    CertificateManager.InstallCertificate(server);
                }
            }

            // Send telemetry if enabled
            if (m_settings["sendTelemetry"] == "true")
            {
                SendTelemetry(from, server);
            }
        }
        private void BtnConnect_Click(object sender, EventArgs e)
        {
            bool osuWasRunning = m_osuRunning;

            if (m_osuRunning)
            {
                Process p = Process.Start(new ProcessStartInfo()
                {
                    FileName        = "taskkill",
                    Arguments       = "/IM osu!.exe",
                    CreateNoWindow  = true,
                    UseShellExecute = false
                });

                p.WaitForExit();
            }
            lblCurrentServer.Text = "Connecting...";
            Application.DoEvents();
            Server selectedServer = GetSelectedServer();

            CertificateManager.UninstallAllCertificates(m_servers);
            List <string> hosts = HostsUtil.GetHosts().ToList();

            hosts.RemoveAll(x => x.Contains(".ppy.sh"));
            HostsUtil.SetHosts(hosts.ToArray());

            if (!selectedServer.IsBancho)
            {
                string[] osu_domains = new string[]
                {
                    //"delta.ppy.sh",
                    "osu.ppy.sh",
                    "c.ppy.sh",
                    "c1.ppy.sh",
                    "c2.ppy.sh",
                    "c3.ppy.sh",
                    "c4.ppy.sh",
                    "c5.ppy.sh",
                    "c6.ppy.sh",
                    "ce.ppy.sh",
                    "a.ppy.sh",
                    //"s.ppy.sh",
                    "i.ppy.sh",
                    //"bm6.ppy.sh",
                };

                hosts = HostsUtil.GetHosts().ToList();
                foreach (string domain in osu_domains)
                {
                    hosts.Add(selectedServer.ServerIP + " " + domain);
                }
                HostsUtil.SetHosts(hosts.ToArray());

                if (!selectedServer.IsLocalhost)
                {
                    CertificateManager.InstallCertificate(selectedServer);
                }
            }

            if (osuWasRunning)
            {
                Process.Start(QuickSwitch.GetOsuExecutablePath());
            }

            UpdateServerUI();
        }