static void Init() { Hosts = new HostsEditor(GetHostsFileName()); try { // Create default hosts file if not exists var hosts_file = Hosts.FileName; if (!File.Exists(hosts_file)) { File.WriteAllText(hosts_file, new HostsItem("127.0.0.1", "localhost").ToString()); } // Try to create backup on first run var backup_file = hosts_file + ".backup"; if (!File.Exists(backup_file)) { ForceCopy(hosts_file, backup_file); } } catch { throw new Exception("hosts file does not exist"); } }
static void Run(string[] args, bool interactive) { try { ArgsQueue = new Queue <string>(args); string Mode = (ArgsQueue.Count > 0) ? ArgsQueue.Dequeue().ToLower() : "help"; string HostsFile = GetHostsFileName(); string BackupHostsFile = HostsFile + ".backup"; string RollbackHostsFile = HostsFile + ".rollback"; // Check permissions FileIOPermission HostsPermissions = new FileIOPermission(FileIOPermissionAccess.AllAccess, HostsFile); if (!SecurityManager.IsGranted(HostsPermissions)) { throw new Exception("No write permission to the hosts file"); } // Create default hosts file if not exists if (!File.Exists(HostsFile)) { File.WriteAllText(HostsFile, new HostsItem("127.0.0.1", "localhost").ToString()); } switch (Mode) { case "open": if (IsUnix) { break; } var exe = FileAssoc.GetExecutable(".txt") ?? "notepad"; Process.Start(exe, '"' + HostsFile + '"'); return; case "backup": if (ArgsQueue.Count > 0) { BackupHostsFile = HostsFile + "." + ArgsQueue.Dequeue().ToLower(); } File.Copy(HostsFile, BackupHostsFile, true); Console.WriteLine("[OK] Hosts file backed up successfully"); return; case "restore": if (ArgsQueue.Count > 0) { BackupHostsFile = HostsFile + "." + ArgsQueue.Dequeue().ToLower(); } if (!File.Exists(BackupHostsFile)) { throw new Exception("Backup file is not exists"); } File.Copy(HostsFile, RollbackHostsFile, true); File.Copy(BackupHostsFile, HostsFile, true); Console.WriteLine("[OK] Hosts file restored successfully"); return; case "rollback": if (!File.Exists(RollbackHostsFile)) { throw new Exception("Rollback file is not exists"); } if (File.Exists(HostsFile)) { File.Delete(HostsFile); } File.Move(RollbackHostsFile, HostsFile); Console.WriteLine("[OK] Hosts file rolled back successfully"); return; case "empty": case "recreate": File.Copy(HostsFile, RollbackHostsFile, true); File.WriteAllText(HostsFile, new HostsItem("127.0.0.1", "localhost").ToString()); Console.WriteLine("[OK] New hosts file created successfully"); return; case "help": Help(interactive); return; } // Try to create backup on first run if (!File.Exists(BackupHostsFile)) { try { File.Copy(HostsFile, BackupHostsFile); } catch {} } Hosts = new HostsEditor(HostsFile); Hosts.Load(); List <HostsItem> Lines; switch (Mode) { case "print": case "raw": case "file": Console.WriteLine(File.ReadAllText(Hosts.FileName, Hosts.Encoding)); return; case "list": case "view": case "select": case "ls": RunListMode(interactive); return; case "format": Hosts.ResetFormat(); Console.WriteLine("[OK] Hosts file formatted successfully"); break; case "clean": Hosts.RemoveInvalid(); Hosts.ResetFormat(); Console.WriteLine("[OK] Hosts file cleaned successfully"); break; case "add": case "new": RunAddMode(); break; case "set": RunUpdateMode(true); break; case "change": case "update": case "upd": RunUpdateMode(false); break; case "rem": case "rm": case "remove": case "del": case "delete": if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } Lines = Hosts.GetMatched(args[1]); if (Lines.Count == 0) { throw new HostNotFoundException(args[1]); } foreach (HostsItem Line in Lines) { Hosts.Remove(Line); Console.WriteLine("[REMOVED] {0} {1}", Line.IP.ToString(), Line.Aliases.ToString()); } break; case "on": case "enable": if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } Lines = Hosts.GetMatched(args[1]); if (Lines.Count == 0) { throw new HostNotFoundException(args[1]); } foreach (HostsItem Line in Lines) { Line.Enabled = true; Console.WriteLine("[ENABLED] {0} {1}", Line.IP.ToString(), Line.Aliases.ToString()); } break; case "off": case "disable": if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } Lines = Hosts.GetMatched(args[1]); if (Lines.Count == 0) { throw new HostNotFoundException(args[1]); } foreach (HostsItem Line in Lines) { Line.Enabled = false; Console.WriteLine("[DISABLED] {0} {1}", Line.IP.ToString(), Line.Aliases.ToString()); } break; case "hide": if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } Lines = Hosts.GetMatched(args[1]); if (Lines.Count == 0) { throw new HostNotFoundException(args[1]); } foreach (HostsItem Line in Lines) { Line.Hidden = true; Console.WriteLine("[HIDDEN] {0} {1}", Line.IP.ToString(), Line.Aliases.ToString()); } break; case "show": if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } Lines = Hosts.GetMatched(args[1]); if (Lines.Count == 0) { throw new HostNotFoundException(args[1]); } foreach (HostsItem Line in Lines) { Line.Hidden = false; Console.WriteLine("[SHOWN] {0} {1}", Line.IP.ToString(), Line.Aliases.ToString()); } break; default: Console.WriteLine("[ERROR] Unknown command"); Help(interactive); return; } File.Copy(HostsFile, RollbackHostsFile, true); Hosts.Save(); } catch (HostNotSpecifiedException) { Console.WriteLine("[ERROR] Host not specified"); } catch (HostNotFoundException e) { Console.WriteLine("[ERROR] Host '{0}' not found", e.Host); } catch (Exception e) { #if DEBUG Console.WriteLine("[ERROR] " + e.ToString()); #else Console.WriteLine("[ERROR] " + e.Message); #endif } }
public HostsEditor(HostsEditor source) : base(source.Select(item => item.Clone()).ToArray()) { this.FileName = source.FileName; this.Encoding = source.Encoding; }