コード例 #1
0
ファイル: Program.cs プロジェクト: moeart/ntr
        static void Main(string[] args)
        {
            var options = new Option();

            /*
             * PARSER OPTIONS
             */
            // Parser options via CommandLinePaser
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                Timeout     = options.Timeout * 1000;
                Interval    = options.Interval * 1000;
                MaxHop      = options.MaxHop;
                EnableASN   = options.EnableASN;
                UseIPIPGeo  = options.UseIPIPGeo;
                DomainCheck = options.DomainCheck;
            }
            else
            {
                return; // if invalid option, end application
            }

            // Timeout or Interval MUST greater than 0
            if (Timeout == 0 || Interval == 0)
            {
                Console.WriteLine(options.GetUsage());
                return;
            }



            /*
             * DOMAIN OR IP PROCCESS
             */
            // Parser the first parameter is IP or Domain
            if (args.Count() <= 0) // LESS ONE PARAMETER
            {
                Console.WriteLine(options.GetUsage());
                return;
            }
            else if (!(ConsoleHelper.IsValidIPAddress(args[0]) || // IS IP ADDRESS
                       ConsoleHelper.IsValidDomainName(args[0]) // OR DOMAIN
                       ))
            {
                Console.WriteLine("ERROR: {0} is unknown IP address or domain.\n", args[0]);
                Console.WriteLine(options.GetUsage());
                return;
            }

            // save hostname
            NTR_HOSTNAME = args[0];

            // resolving ip address
            string NTR_DESTIPADDR;

            if (ConsoleHelper.IsValidDomainName(NTR_HOSTNAME))
            {
                try
                {
                    NTR_DESTIPADDR = Dns.GetHostAddresses(NTR_HOSTNAME)[0].ToString();
                }
                catch
                {
                    Console.WriteLine($"ERROR: cannot resolve domain '{NTR_HOSTNAME}' to IP address!\n");
                    return;
                }
            }
            else
            {
                NTR_DESTIPADDR = NTR_HOSTNAME;
            }

            // is ipv6 or ipv4
            ipv6 = ConsoleHelper.IsIPv6(NTR_DESTIPADDR);



            /*
             * INITIZATION
             */
            // to load ip location database when ipv4 only
            if (ipv6 == false)
            {
                try
                {
                    QQWry = new QQWryLocator(AppDomain.CurrentDomain.BaseDirectory + "\\qqwry.dat");
                }
                catch { }
            }

            // SetUp ASN Paser
            AsnHelper.BgpQueryServer = BgpQueryServer;

            // SetUp Tracer
            var NtrResult = new NtrResultItem[MaxHop];

            Trace.Timeout  = Timeout;
            Trace.HostName = NTR_DESTIPADDR;

            // Create a new List used to store results
            for (int i = 0; i < MaxHop; i++)
            {
                NtrResultList.Add(new NtrResultItem());
            }

            // Route Tracer Timer
            System.Timers.Timer TracerTimer = new System.Timers.Timer();
            TracerTimer.Elapsed += new ElapsedEventHandler(Tracer);
            TracerTimer.Interval = Interval;
            TracerTimer.Enabled  = true;

            // Display Timer
            System.Timers.Timer DisplayTimer = new System.Timers.Timer();
            DisplayTimer.Elapsed += new ElapsedEventHandler(Display);
            DisplayTimer.Interval = Interval;
            DisplayTimer.Enabled  = true;

            //Console.ReadLine();
            Console.ReadKey();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: moeart/ntr
        private static void Display(object source, ElapsedEventArgs e)
        {
            Console.Title = $"Ntr {NTR_HOSTNAME}  -  MoeArt OpenSource  http://lab.acgdraw.com";
            Console.Clear();
            int MaxLength = Console.WindowWidth - 1;

            // Print Tool Information
            string ToolName      = "NTR - MoeArt's Network Traceroute";
            string ToolCopyright = "(c)2020 MoeArt OpenSource, www.acgdraw.com";

            ConsoleHelper.WriteCenter(ToolName, MaxLength);
            ConsoleHelper.WriteCenter(ToolCopyright, MaxLength);

            string InfoLeft  = $"Dest: {NTR_HOSTNAME}";
            string InfoRight = "ST: " + NTR_STARTTIME.ToShortDateString() + " " + NTR_STARTTIME.ToLongTimeString();

            Console.WriteLine("{0}{1}", InfoLeft, InfoRight.PadLeft(MaxLength - InfoLeft.Length));

            // Print Title Bar
            string Formatv4 = "{0,3}  {1,-17} {2,5} {3,5} {4,5} {5,5} {6,5} {7,5}  {8,-7} {9}";
            string Formatv6 = "{0,3}  {1,-40} {2,5} {3,5} {4,5} {5,5} {6,5} {7,5}  {8,-7} {9}";
            string Format   = ipv6 ? Formatv6 : Formatv4;
            string Title    = String.Format(Format,
                                            "#",
                                            "DESTINATION",
                                            "LOSS%",
                                            "SENT",
                                            "LAST",
                                            "BEST",
                                            "AVG",
                                            "WRST",
                                            "ASN",
                                            "LOCATION").PadRight(MaxLength);

            // Reserve color for Title bar
            ConsoleColor OriginalForeColor = Console.ForegroundColor;
            ConsoleColor OriginalBackColor = Console.BackgroundColor;

            Console.BackgroundColor = OriginalForeColor;
            Console.ForegroundColor = OriginalBackColor;
            Console.WriteLine(Title.Substring(0, MaxLength));
            Console.ResetColor();

            // Print Items from Result List
            for (int i = 0; i < NtrResultList.Count(); i++)
            {
                NtrResultItem Item          = NtrResultList[i];
                string        FormattedItem = String.Format(Format,
                                                            Item.Hop == 0 ? "?" : Item.Hop.ToString(),
                                                            Item.Host == string.Empty ? "Request timed out" : Item.Host,
                                                            Item.Loss,
                                                            Item.Sent,
                                                            Item.Last == NTR_TIMEDOUT ? "*" : Item.Last.ToString(),
                                                            Item.Best == NTR_TIMEDOUT ? "*" : Item.Best.ToString(),
                                                            Item.Avg == NTR_TIMEDOUT ? "*" : Item.Avg.ToString(),
                                                            Item.Wrst == NTR_TIMEDOUT ? "*" : Item.Wrst.ToString(),
                                                            Item.ASN,
                                                            Item.Geo).PadRight(MaxLength);
                Console.WriteLine(ChineseSubStr.GetSubString(FormattedItem, 0, MaxLength));

                // Break at the End, This hop same as Hostname means the End
                if (Item.Host == Trace.HostName)
                {
                    MaxHop = i + 1; // Set Max Hop to the End Hop
                    break;          // Break Displaying
                }
            }
        }