예제 #1
0
        /// <summary>
        /// Main entry point of PowerPing
        /// Parses arguments and runs operations
        /// </summary>
        /// <param name="args">Program arguments</param>
        static void Main(string[] args)
        {
            // User inputted attributes
            PingAttributes inputtedAttributes = new PingAttributes();

            // Setup console
            Display.DefaultForegroundColor = Console.ForegroundColor;
            Display.DefaultBackgroundColor = Console.BackgroundColor;

            // Show current version info
            //Display.Version();

            // Check if no arguments
            if (args.Length == 0)
            {
                Display.Help();
                Helper.WaitForUserInput();
                return;
            }

            // Parse command line arguments
            if (!CommandLine.Parse(args, ref inputtedAttributes))
            {
                Helper.ErrorAndExit("Problem parsing arguments, use \"PowerPing /help\" or \"PowerPing /?\" for help.");
            }

            // Find address/host in arguments
            if (inputtedAttributes.Operation != PingOperation.Whoami &&
                inputtedAttributes.Operation != PingOperation.Listen)
            {
                if (!CommandLine.FindAddress(args, ref inputtedAttributes))
                {
                    Helper.ErrorAndExit("Could not find correctly formatted address, please check and try again");
                }
            }

            // Perform DNS lookup on inputted address
            // inputtedAttributes.ResolvedAddress = Lookup.QueryDNS(inputtedAttributes.InputtedAddress, inputtedAttributes.UseICMPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6);

            // Add Control C event handler
            if (inputtedAttributes.Operation != PingOperation.Whoami &&
                inputtedAttributes.Operation != PingOperation.Location &&
                inputtedAttributes.Operation != PingOperation.Whois)
            {
                Console.CancelKeyPress += new ConsoleCancelEventHandler(ExitHandler);
            }

            // Select correct function using opMode
            Ping  p;
            Graph g;

            switch (inputtedAttributes.Operation)
            {
            case PingOperation.Listen:
                // If we find an address then pass it to listen, otherwise start it without one
                if (CommandLine.FindAddress(args, ref inputtedAttributes))
                {
                    Listen.Start(m_CancellationTokenSource.Token, inputtedAttributes.InputtedAddress);
                }
                else
                {
                    Listen.Start(m_CancellationTokenSource.Token);
                }
                break;

            case PingOperation.Location:
                Console.WriteLine(Lookup.GetAddressLocationInfo(inputtedAttributes.InputtedAddress, false));
                Helper.WaitForUserInput();
                break;

            case PingOperation.Whoami:
                Console.WriteLine(Lookup.GetAddressLocationInfo("", true));
                Helper.WaitForUserInput();
                break;

            case PingOperation.Whois:
                Lookup.QueryWhoIs(inputtedAttributes.InputtedAddress);
                break;

            case PingOperation.Graph:
                g = new Graph(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token);
                g.Start();
                break;

            case PingOperation.CompactGraph:
                g = new Graph(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token);
                g.CompactGraph = true;
                g.Start();
                break;

            case PingOperation.Flood:
                Flood.Start(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token);
                break;

            case PingOperation.Scan:
                Scan.Start(inputtedAttributes.InputtedAddress, m_CancellationTokenSource.Token);
                break;

            case PingOperation.Normal:
                // Send ping normally
                p = new Ping(inputtedAttributes, m_CancellationTokenSource.Token);
                PingResults results = p.Send();
                Display.PingResults(inputtedAttributes, results);
                break;

            default:
                Helper.ErrorAndExit("Could not determine ping operation");
                break;
            }

            // Reset console colour
            Display.ResetColor();
            try { Console.CursorVisible = true; } catch (Exception) { }
        }