/// <summary> /// Returns the server the user is currently connected to /// </summary> public static Server GetCurrentServer() { // Get every line of the hosts file string[] hosts = HostsUtils.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 // replace \t because sometimes the ips are seperated by \t instead of a space 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> /// <returns>If the switch was successful</returns> public static bool SwitchServer(Server server) { // Remember the old server for telemetry Server from = GetCurrentServer(); // Get the hosts file to edit it List <string> hosts = HostsUtils.GetHosts().ToList(); // Check if reading the hosts file was successful if (hosts == null) { return(false); } hosts.RemoveAll(x => x.Contains(Variables.HostsIdentifier)); // Edit the hosts file if the server has a custom ip if (server.HasIP) { // Change the hosts file by adding the server's ip and the osu domain foreach (string domain in Variables.OsuDomains) { hosts.Add(server.IP + " " + domain); } } // Apply the changes to the hosts file bool successful = HostsUtils.SetHosts(hosts.ToArray()); // check if writing to the hosts file was successful if (!successful) { return(false); } try { // Get rid of all uneccessary certificates CertificateUtils.UninstallAllCertificates(Servers); } catch (Exception ex) { // Show error MessageBox.Show($"Error whilst trying to uninstall certificates.\n\n{ex.Message}\n\nPlease switch back to bancho and try again.\nIf the issue persists, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Install the certificate if needed (not needed for bancho and localhost) if (server.HasCertificate) { try { CertificateUtils.InstallCertificate(server); } catch (Exception ex) { // Show error MessageBox.Show($"Error whilst trying to install certificates.\n\n{ex.Message}\n\nPlease switch back to bancho and try again.\nOtherwise you will experience desynchronizations between your hosts file and your installed certificates.\n\nIf the issue persists, please visit our Discord server.", "Ultimate Osu Server Switcher", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // switch the account in the osu config file if that option is enabled if (m_settings["switchAccount"] == "true") { // Only switch when an account is saved for that server List <Account> accounts = JsonConvert.DeserializeObject <List <Account> >(m_accounts["accounts"]); // get the account that belongs to the server the user switched to Account account = accounts.FirstOrDefault(x => x.ServerUID == server.UID); if (account != null) { // Try to change the account details in the osu config file OsuConfigFile.SetAccount(account); } } // Add the switch to the history by getting the history, adding the element and re-saving it List <HistoryElement> history = JsonConvert.DeserializeObject <List <HistoryElement> >(m_history["history"]); history.Add(new HistoryElement(DateTime.Now, from.UID, server.UID)); string json = JsonConvert.SerializeObject(history.ToArray()); m_history["history"] = json; // Send telemetry if enabled if (m_settings["sendTelemetry"] == "true") { SendTelemetry(from, server); } return(true); }