Esempio n. 1
0
        /// <summary>
        /// Main program method. Read command line arguments.
        /// </summary>
        /// <param name="args">Command line arguments.</param>
        static void Main(string[] args)
        {
            // Read request from args...
            Request request = new Request(args);

            // Help or Error...
            switch (request.getAction())
            {
                case Action.Help:
                    printHelp();
                    return;
                case Action.Error:
                    Console.WriteLine("Bad parameters used.");
                    Console.WriteLine("");
                    printHelp();
                    return;
            }

            // Read hosts file...
            Hosts hosts = new Hosts(Hosts.getHostsFilePath());
            hosts.parse();

            // Run request...
            hosts.performAction(request);
        }
Esempio n. 2
0
        /// <summary>
        /// Perform action from request.
        /// </summary>
        /// <param name="request">request</param>
        public void performAction(Request request)
        {
            // Help and Error action can not be performed...
            if ((request.getAction() == Action.Help) || (request.getAction() == Action.Error))
            {
                throw new Exception("Bad action to make");
            }

            // Save request IP address and hostnames...
            string ip = request.getIp();
            List<string> hostnames = request.getHostnames();

            // Run action...
            switch (request.getAction())
            {
                case Action.Add :
                    // Add only hostnames that does not exists in hosts file...
                    List<string> addHostnames = new List<string>();
                    foreach (string hostname in hostnames)
                    {
                        Host[] hostsHostname = getHostsByHostname(hostname);
                        if (hostsHostname.Length > 0)
                        {
                            foreach (Host hostHostname in hostsHostname)
                            {
                                Console.WriteLine("WARNING: hostname \"" + hostname + "\" is already used with IP address \"" + hostHostname.getIp() + "\"");
                            }
                        }

                        if (contains(ip, hostname))
                        {
                            Console.WriteLine("NOT ADDED: \"" + ip + " - " + hostname + "\" combination already exists in hosts file");
                        }
                        else
                        {
                            addHostnames.Add(hostname);
                        }
                    }

                    // Add new host...
                    hosts.Add(new Host(ip, addHostnames, request.getComment()));

                    break;
                case Action.Remove :
                    // Write non existing combination to console...
                    foreach (string hostname in hostnames)
                    {
                        if (!contains(ip, hostname))
                        {
                            Console.WriteLine("NOT REMOVED: \"" + ip + " - " + hostname + "\" combination not exists in hosts file");
                        }
                    }

                    // Get hosts and removed their hostnames...
                    foreach (Host host in getHostsByIp(ip))
                    {
                        foreach (string hostname in hostnames)
                        {
                            if (host.hasHostname(hostname))
                            {
                                host.removeHostname(hostname);
                            }
                        }
                    }

                    break;
                case Action.Test :
                    // Determine if IP address and hostname exists in hosts file...
                    foreach (string hostname in hostnames)
                    {
                        if (contains(ip, hostname))
                        {
                            Console.WriteLine("EXISTS: \"" + ip + " - " + hostname + "\" combination exists in hosts file");
                        }
                        else
                        {
                            Console.WriteLine("NOT EXISTS: \"" + ip + " - " + hostname + "\" combination not exists in hosts file");
                        }
                    }

                    break;
            }

            // Save (print on DEBUG) while Add or Remove action...
            if ((request.getAction() == Action.Add) || (request.getAction() == Action.Remove))
            {
            #if DEBUG
                print();
            #else
                save();
            #endif
            }
        }