/// <summary> /// Runs when Exit or Cancel event fires (normally when Ctrl-C) /// is pressed. Used to clean up and stop operations when exiting /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private static void ExitHandler(object sender, ConsoleCancelEventArgs args) { // Cancel termination args.Cancel = true; // Stop ping p.Dispose(); // Stop graph if it is running if (g != null) { g.Dispose(); } // Reset console colour Display.ResetColor(); Console.CursorVisible = true; }
/// <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) { } }
/// <summary> /// Main entry point of PowerPing /// Parses arguments and runs operations /// </summary> /// <param name="args">Program arguments</param> static void Main(string[] args) { // Local variables PingAttributes attributes = new PingAttributes(); attributes.Address = ""; // 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 attributes)) { Helper.ErrorAndExit("Problem parsing arguments, use \"PowerPing /help\" or \"PowerPing /?\" for help."); } // Find address/host in arguments if (attributes.Operation != PingOperation.Whoami && attributes.Operation != PingOperation.Listen) { if (!CommandLine.FindAddress(args, ref attributes)) { Helper.ErrorAndExit("Could not find correctly formatted address, please check and try again"); } } // Add Control C event handler if (attributes.Operation != PingOperation.Whoami && attributes.Operation != PingOperation.Location && attributes.Operation != PingOperation.Whois) { Console.CancelKeyPress += new ConsoleCancelEventHandler(ExitHandler); } // Select correct function using opMode Ping p = new Ping(m_CancellationTokenSource.Token); Graph g; switch (attributes.Operation) { case PingOperation.Listen: Listen.Start(m_CancellationTokenSource.Token); break; case PingOperation.Location: Console.WriteLine(Lookup.GetAddressLocationInfo(attributes.InputtedAddress, false)); Helper.WaitForUserInput(); break; case PingOperation.Whoami: Console.WriteLine(Lookup.GetAddressLocationInfo("", true)); Helper.WaitForUserInput(); break; case PingOperation.Whois: Lookup.QueryWhoIs(attributes.InputtedAddress); break; case PingOperation.Graph: g = new Graph(attributes.InputtedAddress, m_CancellationTokenSource.Token); g.Start(); break; case PingOperation.CompactGraph: g = new Graph(attributes.InputtedAddress, m_CancellationTokenSource.Token); g.CompactGraph = true; g.Start(); break; case PingOperation.Flood: Flood.Start(attributes.InputtedAddress, m_CancellationTokenSource.Token); break; case PingOperation.Scan: Scan.Start(attributes.InputtedAddress, m_CancellationTokenSource.Token); break; case PingOperation.Normal: // Send ping normally p.Send(attributes); break; default: Helper.ErrorAndExit("Could not determine ping operation"); break; } // Reset console colour Display.ResetColor(); Console.CursorVisible = true; }