Пример #1
0
        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
            }
        }
Пример #2
0
        static void Run(List <string> args, bool interactive)
        {
            try
            {
                var args_queue    = new Queue <string>(args);
                var mode          = (args_queue.Count > 0) ? args_queue.Dequeue().ToLower() : "help";
                var hosts_file    = Hosts.FileName;
                var backup_file   = hosts_file + ".backup";
                var rollback_file = hosts_file + ".rollback";

                switch (mode)
                {
                case "open":
                    if (IsUnix)
                    {
                        break;
                    }
                    var exe = FileAssoc.GetExecutable(".txt") ?? "notepad";
                    Process.Start(exe, '"' + hosts_file + '"');
                    return;

                case "apply":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.Count == 0)
                    {
                        throw new Exception("Applied file is not specified");
                    }
                    var apply_file = args_queue.Dequeue();
                    if (!File.Exists(apply_file))
                    {
                        throw new Exception("Applied file does not exist");
                    }
                    ForceCopy(hosts_file, rollback_file);
                    ForceCopy(apply_file, hosts_file);
                    Console.WriteLine("[OK] New hosts file applied successfully");
                    return;

                case "backup":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.Count > 0)
                    {
                        backup_file = hosts_file + "." + args_queue.Dequeue().ToLower();
                    }
                    ForceCopy(hosts_file, backup_file);
                    Console.WriteLine("[OK] Hosts file backed up successfully");
                    return;

                case "restore":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.Count > 0)
                    {
                        backup_file = hosts_file + "." + args_queue.Dequeue().ToLower();
                    }
                    if (!File.Exists(backup_file))
                    {
                        throw new Exception("Backup file does not exist");
                    }
                    ForceCopy(hosts_file, rollback_file);
                    ForceCopy(backup_file, hosts_file);
                    Console.WriteLine("[OK] Hosts file restored successfully");
                    return;

                case "rollback":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (!File.Exists(rollback_file))
                    {
                        throw new Exception("Rollback file does not exist");
                    }
                    if (File.Exists(hosts_file))
                    {
                        File.Delete(hosts_file);
                    }
                    File.Move(rollback_file, hosts_file);
                    Console.WriteLine("[OK] Hosts file rolled back successfully");
                    return;

                case "empty":
                case "recreate":
                case "erase":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    ForceCopy(hosts_file, rollback_file);
                    File.WriteAllText(hosts_file, new HostsItem("127.0.0.1", "localhost").ToString());
                    Console.WriteLine("[OK] New hosts file created successfully");
                    return;

                case "help":
                    Help(interactive);
                    return;
                }

                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(args_queue.ToList(), interactive);
                    return;

                case "format":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    Hosts.ResetFormat();
                    Console.WriteLine("[OK] Hosts file formatted successfully");
                    break;

                case "clean":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    Hosts.RemoveInvalid();
                    Hosts.ResetFormat();
                    Console.WriteLine("[OK] Hosts file cleaned successfully");
                    break;

                case "add":
                case "new":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    RunAddMode(args_queue.ToList());
                    break;

                case "set":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    RunUpdateMode(args_queue.ToList(), true);
                    break;

                case "change":
                case "update":
                case "upd":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    RunUpdateMode(args_queue.ToList(), false);
                    break;

                case "rem":
                case "rm":
                case "remove":
                case "del":
                case "delete":
                case "unset":
                    if (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.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 (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.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 (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.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 (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.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 (!MakeWritable(hosts_file))
                    {
                        throw new NoWritePermissionException();
                    }
                    if (args_queue.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;
                }
                MakeWritable(rollback_file);
                ForceCopy(hosts_file, rollback_file);
                Hosts.Save();
            }
            catch (NoWritePermissionException)
            {
                Console.WriteLine("[ERROR] No write permission to the hosts file");
            }
            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
            }
        }