Multipurpose utility for managing Games for Windows - LIVE installs and content. (Mirrored from https://github.com/InvoxiPlayGames/GfWLUtility)
at master 4.0 kB view raw
1using System; 2using System.Collections.Generic; 3using System.IO; 4using System.Linq; 5using System.Text; 6using System.Windows.Forms; 7 8namespace GfWLUtility 9{ 10 internal class DomainBlock 11 { 12 private static string hosts_file_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers\\etc\\hosts"); 13 14 public static bool IsDomainBlocked(string domain) 15 { 16 if (!File.Exists(hosts_file_path)) return false; 17 18 // scan through each entry in the hosts file to see if the block exists 19 string[] hosts_lines = File.ReadAllLines(hosts_file_path); 20 foreach (string entry in hosts_lines) 21 { 22 // ignore any entry starting with a comment, or any blank lines 23 if (entry.Length == 0 || entry[0] == '#') continue; 24 // to check if it's blocked, we see if it's routed nowhere or locally 25 // GfWL doesn't support IPv6 so we don't care for that either 26 if (entry.StartsWith("0.0.0.0") || entry.StartsWith("127.0.0.1")) 27 { 28 // would split but splitting is hard so we just see if the line contains the domain 29 if (entry.Contains(domain)) return true; 30 } 31 } 32 33 return false; 34 } 35 36 public static void BlockDomain(string domain) 37 { 38 if (!Program.Elevated) 39 throw new Exception("Blocking domains requires admin."); 40 41 // scan through each entry in the hosts file to see if the block exists 42 string[] hosts_lines = File.ReadAllLines(hosts_file_path); 43 List<string> hosts_lines_list = hosts_lines.ToList(); 44 foreach (string entry in hosts_lines) 45 { 46 // ignore any entry starting with a comment, or any blank lines 47 if (entry.Length == 0 || entry[0] == '#') continue; 48 // to check if it's blocked, we see if it's routed nowhere or locally 49 // GfWL doesn't support IPv6 so we don't care for that either 50 if (entry.StartsWith("0.0.0.0") || entry.StartsWith("127.0.0.1")) 51 { 52 // would split but splitting is hard so we just see if the line contains the domain 53 if (entry.Contains(domain)) return; 54 } 55 } 56 57 // if we got here the domain isn't blocked already so add it 58 hosts_lines_list.Add($"0.0.0.0 {domain}"); 59 File.WriteAllLines(hosts_file_path, hosts_lines_list.ToArray()); 60 } 61 62 public static void UnblockDomain(string domain) 63 { 64 if (!Program.Elevated) 65 throw new Exception("Unblocking domains requires admin."); 66 67 // scan through each entry in the hosts file to see if the block exists 68 string[] hosts_lines = File.ReadAllLines(hosts_file_path); 69 List<string> hosts_lines_list = hosts_lines.ToList(); 70 foreach (string entry in hosts_lines) 71 { 72 // ignore any entry starting with a comment, or any blank lines 73 if (entry.Length == 0 || entry[0] == '#') continue; 74 // to check if it's blocked, we see if it's routed nowhere or locally 75 // GfWL doesn't support IPv6 so we don't care for that either 76 if (entry.StartsWith("0.0.0.0") || entry.StartsWith("127.0.0.1")) 77 { 78 // would split but splitting is hard so we just see if the line contains the domain 79 // and then we remove the line from the hosts file 80 if (entry.Contains(domain)) 81 hosts_lines_list.Remove(entry); 82 } 83 } 84 // write the newly modified hosts file back to the hosts file 85 File.WriteAllLines(hosts_file_path, hosts_lines_list.ToArray()); 86 } 87 } 88}