/// <summary> /// Checks the HOSTS file on startup, detecting existing redirects to the bf2web.gamespy /// or gpcm/gpsp.gamespy urls /// </summary> private void DoHOSTSCheck() { // Make sure we can read and write if (!HostsFile.CanRead) { UpdateStatus("- Cannot open HOSTS file for reading!"); } else if (!HostsFile.CanWrite) { UpdateStatus("- Cannot open HOSTS file for writing!"); } else { bool MatchFound = false; // Login server redirect if (HostsFile.HasEntry("gpcm.gamespy.com")) { MatchFound = true; GpcmCheckbox.Checked = true; GpcmAddress.Text = HostsFile.Get("gpcm.gamespy.com"); } // Stat server redirect if (HostsFile.HasEntry("bf2web.gamespy.com")) { MatchFound = true; Bf2webCheckbox.Checked = true; Bf2webAddress.Text = HostsFile.Get("bf2web.gamespy.com"); } // Did we find any matches? if (MatchFound) { UpdateStatus("- Found old redirect data in HOSTS file."); RedirectsEnabled = true; LockGroups(); RedirectButton.Enabled = true; RedirectButton.Text = "Remove HOSTS Redirect"; UpdateStatus("- Locking HOSTS file"); HostsFile.Lock(); UpdateStatus("- All Done!"); } } }
/// <summary> /// Removes HOSTS file redirects. /// </summary> private void UndoRedirects() { // Tell the writter to restore the HOSTS file to its // original state UpdateStatus("- Unlocking HOSTS file"); HostsFile.UnLock(); // Restore the original hosts file contents UdpateStatus("- Restoring HOSTS file... ", false); try { HostsFile.Remove("bf2web.gamespy.com"); HostsFile.Remove("gpcm.gamespy.com"); HostsFile.Remove("gpsp.gamespy.com"); HostsFile.Remove("motd.gamespy.com"); HostsFile.Remove("master.gamespy.com"); HostsFile.Remove("battlefield2.ms14.gamespy.com"); HostsFile.Remove("battlefield2.available.gamespy.com"); HostsFile.Save(); UpdateStatus("Success!"); } catch { UpdateStatus("Failed!"); MessageBox.Show( "Unable to RESTORE to HOSTS file! Unfortunatly this error can only be fixed by manually removing the HOSTS file," + " and replacing it with a new one :( . If possible, you may also try changing the permissions yourself.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } // Flush the DNS! FlushDNS(); // Update status UpdateStatus("- All Done!"); // Reset form data RedirectsEnabled = false; RedirectButton.Text = "Begin HOSTS Redirect"; UnlockGroups(); }
/// <summary> /// Preforms the pings required to fill the dns cache, and locks the HOSTS file. /// The reason we ping, is because once the HOSTS file is locked, any request /// made to a url (when the DNS cache is empty), will skip the hosts file, because /// it cant be read. If we ping first, then the DNS cache fills up with the IP /// addresses in the hosts file. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RebuildDNSCache(object sender, DoWorkEventArgs e) { // Update status window UdpateStatus("- Rebuilding DNS Cache... ", false); foreach (KeyValuePair <String, String> IP in HostsFile.GetLines()) { // Cancel if we have a cancelation request if (HostsWorker.CancellationPending) { UpdateStatus("Cancelled!"); e.Cancel = true; return; } Ping p = new Ping(); PingReply reply = p.Send(IP.Key); } UpdateStatus("Done"); // Lock the hosts file UpdateStatus("- Locking HOSTS file"); HostsFile.Lock(); UpdateStatus("- All Done!"); }
/// <summary> /// Event fired when the UI is closing, Unlocks the HostsFile /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Launcher_FormClosing(object sender, FormClosingEventArgs e) { HostsFile.UnLock(); }
/// <summary> /// This is the main HOSTS file button event handler. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RedirectButton_Click(object sender, EventArgs e) { // Clear the output window LogBox.Clear(); // Show exception message on button push if we cant read or write if (!HostsFile.CanRead) { string message = "Unable to READ the HOST file! Please make sure this program is being ran as an administrator, or " + "modify your HOSTS file permissions, allowing this program to read/modify it."; MessageBox.Show(message, "Hosts file Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (!HostsFile.CanWrite) { string message = "HOSTS file is not WRITABLE! Please make sure this program is being ran as an administrator, or " + "modify your HOSTS file permissions, allowing this program to read/modify it."; MessageBox.Show(message, "Hosts file Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } // If we do not have a redirect in the hosts file... else if (!RedirectsEnabled) { // Make sure we are going to redirect something... if (!Bf2webCheckbox.Checked && !GpcmCheckbox.Checked) { MessageBox.Show( "Please select at least 1 redirect option", "Select an Option", MessageBoxButtons.OK, MessageBoxIcon.Information ); return; } // Lock button and groupboxes LockGroups(); // First, lets determine what the user wants to redirect if (Bf2webCheckbox.Checked) { // Make sure we have a valid IP address in the address box! string text = Bf2webAddress.Text.Trim(); if (text.Length < 8) { MessageBox.Show( "You must enter an IP address or Hostname in the Address box!", "Invalid Address", MessageBoxButtons.OK, MessageBoxIcon.Warning ); UnlockGroups(); Bf2webAddress.Focus(); return; } // Convert Localhost to the Loopback Address if (text.ToLower().Trim() == "localhost") { text = IPAddress.Loopback.ToString(); } // Check if this is an IP address or hostname IPAddress BF2Web; try { UpdateStatus("- Resolving Hostname: " + text); BF2Web = GetIpAddress(text); UpdateStatus("- Found IP: " + BF2Web); } catch { MessageBox.Show( "Stats server redirect address is invalid, or doesnt exist. Please enter a valid, and existing IPv4/6 or Hostname.", "Invalid Address", MessageBoxButtons.OK, MessageBoxIcon.Warning ); UpdateStatus("- Failed to Resolve Hostname!"); UnlockGroups(); return; } // Append line, and update status HostsFile.Set("bf2web.gamespy.com", BF2Web.ToString()); Config.LastStatsServerAddress = Bf2webAddress.Text.Trim(); UpdateStatus("- Adding bf2web.gamespy.com redirect to hosts file"); } // First, lets determine what the user wants to redirect if (GpcmCheckbox.Checked) { // Make sure we have a valid IP address in the address box! string text2 = GpcmAddress.Text.Trim(); if (text2.Length < 8) { MessageBox.Show( "You must enter an IP address or Hostname in the Address box!", "Invalid Address", MessageBoxButtons.OK, MessageBoxIcon.Warning ); UnlockGroups(); GpcmAddress.Focus(); return; } // Convert Localhost to the Loopback Address if (text2.ToLower().Trim() == "localhost") { text2 = IPAddress.Loopback.ToString(); } // Make sure the IP address is valid! IPAddress GpcmA; try { UpdateStatus("- Resolving Hostname: " + text2); GpcmA = GetIpAddress(text2); UpdateStatus("- Found IP: " + GpcmA); } catch { MessageBox.Show( "Login Server redirect address is invalid, or doesnt exist. Please enter a valid, and existing IPv4/6 or Hostname.", "Invalid Address", MessageBoxButtons.OK, MessageBoxIcon.Warning ); UpdateStatus("- Failed to Resolve Hostname!"); UnlockGroups(); return; } // Update status UpdateStatus("- Adding gpcm.gamespy.com redirect to hosts file"); UpdateStatus("- Adding gpsp.gamespy.com redirect to hosts file"); // Append lines to hosts file HostsFile.Set("gpcm.gamespy.com", GpcmA.ToString()); HostsFile.Set("gpsp.gamespy.com", GpcmA.ToString()); Config.LastLoginServerAddress = GpcmAddress.Text.Trim(); } // Save last used addresses Config.Save(); // Create new instance of the background worker HostsWorker = new BackgroundWorker(); HostsWorker.WorkerSupportsCancellation = true; // Write the lines to the hosts file UdpateStatus("- Writting to hosts file... ", false); try { // Save lines to hosts file HostsFile.Set("motd.gamespy.com", IPAddress.Loopback.ToString()); HostsFile.Set("master.gamespy.com", IPAddress.Loopback.ToString()); HostsFile.Set("battlefield2.ms14.gamespy.com", IPAddress.Loopback.ToString()); HostsFile.Set("battlefield2.available.gamespy.com", IPAddress.Loopback.ToString()); HostsFile.Save(); UpdateStatus("Success!"); // Flush DNS Cache FlushDNS(); // Do pings, And lock hosts file. We do this in // a background worker so the User can imediatly start // the BF2 client while the HOSTS redirect finishes HostsWorker.DoWork += new DoWorkEventHandler(RebuildDNSCache); HostsWorker.RunWorkerAsync(); // Set form data RedirectsEnabled = true; RedirectButton.Text = "Remove HOSTS Redirect"; RedirectButton.Enabled = true; } catch { UpdateStatus("Failed!"); UnlockGroups(); MessageBox.Show( "Unable to WRITE to HOSTS file! Please make sure to replace your HOSTS file with " + "the one provided in the release package, or remove your current permissions from the HOSTS file. " + "It may also help to run this program as an administrator.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning ); } } else { // Lock the button RedirectButton.Enabled = false; // Create new instance of the background worker if (HostsWorker == null) { HostsWorker = new BackgroundWorker(); HostsWorker.WorkerSupportsCancellation = true; } // Stop worker if its busy if (HostsWorker.IsBusy) { LoadingForm.ShowScreen(this); this.Enabled = false; HostsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(HostsWorker_RunWorkerCompleted); HostsWorker.CancelAsync(); return; } UndoRedirects(); } }