/// <summary> /// Queries the DNS Cache for the Gamespy hosts, and verifies that the /// IP addresses in the DNS Cache match that of the desired redirect IP /// </summary> public static bool VerifyDNSCache(IProgress <DnsCacheResult> Progress = null) { // Nothing to do here if redirects are disabled if (!RedirectsEnabled) { return(false); } // Grab our saved hosts file IP addresses if (RedirectMethod != RedirectMode.DnsServer) { // Grab hosts file base HostsFile hFile = (RedirectMethod == RedirectMode.HostsFile) ? HostsFileSys as HostsFile : HostsFileIcs as HostsFile; // Fetch our saved IPAddresses GamespyServerAddress = (hFile.HasEntry("master.gamespy.com")) ? hFile.Get("master.gamespy.com") : null; StatsServerAddress = (hFile.HasEntry(Bf2StatsHost)) ? hFile.Get(Bf2StatsHost) : null; } // Flush our cache report DnsCacheReport.Entries.Clear(); // Verify Gamespy Server IP addresses if (GamespyServerAddress != null) { foreach (string address in GamespyHosts) { // Create new report DnsCacheResult Result = new DnsCacheResult(address, GamespyServerAddress); // Add the result to the report DnsCacheReport.AddOrUpdate(Result); // Report progress if we have a progress object if (Progress != null) { Progress.Report(Result); } } } // Verify Stats Server address if (StatsServerAddress != null) { // Create new report DnsCacheResult Result = new DnsCacheResult(Bf2StatsHost, StatsServerAddress); // Add the result to the report DnsCacheReport.AddOrUpdate(Result); // Report progress if we have a progress object if (Progress != null) { Progress.Report(Result); } } // Set internal DnsCacheReport.LastRefresh = DateTime.Now; return(DnsCacheReport.ErrorFree); }
/// <summary> /// The main entry point for the redirector /// </summary> /// <returns>Returns wether the DNS cache results match the selected IPAddresses</returns> public static Task <bool> Initialize() { return(Task.Run <bool>(() => { // Only initialize once if (!IsInitialized) { IsInitialized = true; // Set the System.Net DNS Cache refresh timeout to 1 millisecond ServicePointManager.DnsRefreshTimeout = 1; // Get config options RedirectMethod = Program.Config.RedirectMode; // Create new Instances HostsFileSys = new SysHostsFile(); HostsFileIcs = new HostsFileIcs(); // Detect redirects bool IcsHasRedirects = HostsFileIcs.HasAnyEntry(GamespyHosts) || HostsFileIcs.HasEntry(Bf2StatsHost); bool HostsHasRedirects = HostsFileSys.HasAnyEntry(GamespyHosts) || HostsFileSys.HasEntry(Bf2StatsHost); // Both files cannot have redirects! if (IcsHasRedirects && HostsHasRedirects) { try { // Remove all redirects RemoveRedirects(HostsFileIcs); RemoveRedirects(HostsFileSys); } catch { } } else if (IcsHasRedirects || HostsHasRedirects) { // Do the settings match? bool match = (RedirectMethod == RedirectMode.HostsFile && HostsHasRedirects) || (RedirectMethod == RedirectMode.HostsIcsFile && IcsHasRedirects); // Perform switch if the settings don't match if (!match) { RedirectMethod = (RedirectMethod == RedirectMode.HostsFile) ? RedirectMode.HostsIcsFile : RedirectMode.HostsFile; } // === Fetch our redirect data // Grab hosts file base HostsFile hFile = (RedirectMethod == RedirectMode.HostsFile) ? HostsFileSys as HostsFile : HostsFileIcs as HostsFile; // Parse Stats Address if (hFile.HasEntry("bf2web.gamespy.com")) { StatsServerAddress = hFile.Get("bf2web.gamespy.com"); } // Parse Gamespy Address if (hFile.HasEntry("master.gamespy.com")) { GamespyServerAddress = hFile.Get("master.gamespy.com"); } // Lock hosts file if (RedirectMethod == RedirectMode.HostsFile && Program.Config.LockHostsFile) { HostsFileSys.Lock(); } } else if (RedirectMethod == RedirectMode.DnsServer) { IPAddress addy; // Parse last used Stats Server Address if (Networking.TryGetIpAddress(Program.Config.LastStatsServerAddress, out addy)) { StatsServerAddress = addy; } // Parse gamespy saved address if (Networking.TryGetIpAddress(Program.Config.LastLoginServerAddress, out addy)) { GamespyServerAddress = addy; } } } // Verify cache return (RedirectsEnabled) ? VerifyDNSCache() : true; })); }