static async Task ExecutePortsCommandAsync(NamespaceDetails namespaceDetails, int gatewayCount)
        {
            TraceCommandHeader("Ports");
            if (string.IsNullOrEmpty(namespaceDetails.ServiceNamespace))
            {
                TraceMissingArgument(NamespaceOrConnectionStringArgumentDescription);
                return;
            }

            RelayTraceSource.TraceInfo(await NetworkUtility.VerifyRelayPortsAsync(namespaceDetails.ServiceNamespace, Enumerable.Range(9400, gatewayCount)));

            var tasks = new List <Task <string> >();

            for (int i = 0; i < gatewayCount; i++)
            {
                // Build the ILPIP DNS name and run it for G0 through G63
                var task = NetworkUtility.VerifyRelayPortsAsync(string.Format(namespaceDetails.GatewayDnsFormat, i));
                tasks.Add(task);
            }

            foreach (Task <string> task in tasks)
            {
                string result = await task;
                RelayTraceSource.TraceInfo(result);
            }
        }
        static void ExecuteNamespaceCommand(NamespaceDetails namespaceDetails)
        {
            TraceCommandHeader("Namespace Details");

            bool foundAny = false;

            void OutputLineIf(bool condition, string name, Func <string> valueSelector)
            {
                if (condition)
                {
                    RelayTraceSource.TraceInfo(OutputFormat, name + ":", valueSelector());
                    foundAny = true;
                }
            }

            OutputLineIf(!string.IsNullOrEmpty(namespaceDetails.ServiceNamespace), "ServiceNamespace", () => namespaceDetails.ServiceNamespace);
            OutputLineIf(namespaceDetails.AddressList?.Length > 0, "Address(VIP)", () => string.Join(",", (IEnumerable <IPAddress>)namespaceDetails.AddressList));
            OutputLineIf(!string.IsNullOrEmpty(namespaceDetails.Deployment), "Deployment", () => namespaceDetails.Deployment);
            OutputLineIf(!string.IsNullOrEmpty(namespaceDetails.HostName), "HostName", () => namespaceDetails.HostName);
            OutputLineIf(!string.IsNullOrEmpty(namespaceDetails.GatewayDnsFormat), "GatewayDnsFormat", () => namespaceDetails.GatewayDnsFormat);

            if (!foundAny)
            {
                TraceMissingArgument(NamespaceOrConnectionStringArgumentDescription);
            }
        }
        static void ConfigureListCommand(CommandLineApplication hcCommand)
        {
            hcCommand.RelayCommand("list", (listCmd) =>
            {
                listCmd.Description          = "List HybridConnection(s)";
                var connectionStringArgument = listCmd.Argument("connectionString", "Relay ConnectionString");

                listCmd.OnExecute(async() =>
                {
                    string connectionString = ConnectionStringUtility.ResolveConnectionString(connectionStringArgument);
                    if (string.IsNullOrEmpty(connectionString))
                    {
                        TraceMissingArgument(connectionStringArgument.Name);
                        listCmd.ShowHelp();
                        return(1);
                    }

                    var connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
                    RelayTraceSource.TraceInfo($"Listing HybridConnections for {connectionStringBuilder.Endpoint.Host}");
                    RelayTraceSource.TraceInfo($"{"Path",-38} {"ListenerCount",-15} {"RequiresClientAuth",-20}");
                    var namespaceManager = new RelayNamespaceManager(connectionString);
                    IEnumerable <HybridConnectionDescription> hybridConnections = await namespaceManager.GetHybridConnectionsAsync();
                    foreach (var hybridConnection in hybridConnections)
                    {
                        RelayTraceSource.TraceInfo($"{hybridConnection.Path,-38} {hybridConnection.ListenerCount,-15} {hybridConnection.RequiresClientAuthorization}");
                    }

                    return(0);
                });
            });
        }
        static void ConfigureCreateCommand(CommandLineApplication hcCommand)
        {
            hcCommand.RelayCommand("create", (createCmd) =>
            {
                createCmd.Description = "Create a HybridConnection";

                var pathArgument             = createCmd.Argument("path", "HybridConnection path");
                var connectionStringArgument = createCmd.Argument("connectionString", "Relay ConnectionString");

                var requireClientAuthOption = createCmd.Option(
                    CommandStrings.RequiresClientAuthTemplate, CommandStrings.RequiresClientAuthDescription, CommandOptionType.SingleValue);

                createCmd.OnExecute(async() =>
                {
                    string connectionString = ConnectionStringUtility.ResolveConnectionString(connectionStringArgument);
                    if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(pathArgument.Value))
                    {
                        TraceMissingArgument(string.IsNullOrEmpty(connectionString) ? connectionStringArgument.Name : pathArgument.Name);
                        createCmd.ShowHelp();
                        return(1);
                    }

                    var connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
                    RelayTraceSource.TraceInfo($"Creating HybridConnection '{pathArgument.Value}' in {connectionStringBuilder.Endpoint.Host}...");
                    var hcDescription = new HybridConnectionDescription(pathArgument.Value);
                    hcDescription.RequiresClientAuthorization = GetBoolOption(requireClientAuthOption, true);
                    var namespaceManager = new RelayNamespaceManager(connectionString);
                    await namespaceManager.CreateHybridConnectionAsync(hcDescription);
                    RelayTraceSource.TraceInfo($"Creating HybridConnection '{pathArgument.Value}' in {connectionStringBuilder.Endpoint.Host} succeeded");
                    return(0);
                });
            });
        }
        static void ConfigureCountCommand(CommandLineApplication hcCommand)
        {
            hcCommand.RelayCommand("count", (countCommand) =>
            {
                countCommand.Description = "Get HybridConnection Count";

                var connectionStringArgument = countCommand.Argument("connectionString", "Relay ConnectionString");

                countCommand.OnExecute(async() =>
                {
                    string connectionString = ConnectionStringUtility.ResolveConnectionString(connectionStringArgument);
                    if (string.IsNullOrEmpty(connectionString))
                    {
                        TraceMissingArgument(connectionStringArgument.Name);
                        countCommand.ShowHelp();
                        return(1);
                    }

                    var namespaceManager = Microsoft.ServiceBus.NamespaceManager.CreateFromConnectionString(connectionString);
                    Uri namespaceUri     = namespaceManager.Address;
                    string namespaceHost = namespaceUri.Host;
                    var tokenProvider    = namespaceManager.Settings.TokenProvider;

                    RelayTraceSource.TraceVerbose($"Getting HybridConnection count for '{namespaceUri}");

                    int count = await NamespaceUtility.GetEntityCountAsync(namespaceUri, tokenProvider, "HybridConnections");
                    RelayTraceSource.TraceInfo(string.Format($"{{0,-{namespaceHost.Length}}}  {{1}}", "Namespace", "HybridConnectionCount"));
                    RelayTraceSource.TraceInfo(string.Format($"{{0,-{namespaceHost.Length}}}  {{1}}", namespaceHost, count));

                    return(0);
                });
            });
        }
        static void ConfigureDeleteCommand(CommandLineApplication hcCommand)
        {
            hcCommand.RelayCommand("delete", (deleteCmd) =>
            {
                deleteCmd.Description        = "Delete a HybridConnection";
                var pathArgument             = deleteCmd.Argument("path", "HybridConnection path");
                var connectionStringArgument = deleteCmd.Argument("connectionString", "Relay ConnectionString");

                deleteCmd.OnExecute(async() =>
                {
                    string connectionString = ConnectionStringUtility.ResolveConnectionString(connectionStringArgument);
                    if (string.IsNullOrEmpty(connectionString) || string.IsNullOrEmpty(pathArgument.Value))
                    {
                        TraceMissingArgument(string.IsNullOrEmpty(connectionString) ? connectionStringArgument.Name : pathArgument.Name);
                        deleteCmd.ShowHelp();
                        return(1);
                    }

                    var connectionStringBuilder = new RelayConnectionStringBuilder(connectionString);
                    RelayTraceSource.TraceInfo($"Deleting HybridConnection '{pathArgument.Value}' in {connectionStringBuilder.Endpoint.Host}...");
                    var namespaceManager = new RelayNamespaceManager(connectionString);
                    await namespaceManager.DeleteHybridConnectionAsync(pathArgument.Value);
                    RelayTraceSource.TraceInfo($"Deleting HybridConnection '{pathArgument.Value}' in {connectionStringBuilder.Endpoint.Host} succeeded");
                    return(0);
                });
            });
        }
        static void ConfigureCreateCommand(CommandLineApplication sasCommand)
        {
            sasCommand.RelayCommand("create", (createCmd) =>
            {
                createCmd.Description = "Create a Sas Token";

                var pathArgument             = createCmd.Argument("path", "Entity path");
                var connectionStringArgument = createCmd.Argument("connectionString", "The ConnectionString to use");

                createCmd.OnExecute(async() =>
                {
                    string connectionString = ConnectionStringUtility.ResolveConnectionString(connectionStringArgument);
                    if (string.IsNullOrEmpty(connectionString))
                    {
                        TraceMissingArgument(connectionStringArgument.Name);
                        createCmd.ShowHelp();
                        return(1);
                    }

                    var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

                    var audience = new UriBuilder(namespaceManager.Address);
                    if (!string.IsNullOrEmpty(pathArgument.Value))
                    {
                        audience.Path = pathArgument.Value;
                    }

                    string token = await namespaceManager.Settings.TokenProvider.GetWebTokenAsync(audience.Uri.AbsoluteUri, string.Empty, true, TimeSpan.FromMinutes(20));
                    RelayTraceSource.TraceInfo($"Token:\r\n{token}");
                    return(0);
                });
            });
        }
 static void ExecuteNetStatCommand()
 {
     TraceCommandHeader("NetStat.exe");
     ExecuteProcess(
         "netstat.exe",
         "-ano -p tcp",
         TimeSpan.FromSeconds(30),
         (s) => RelayTraceSource.TraceInfo(s),
         (s) => RelayTraceSource.TraceError("ERROR: " + s),
         throwOnNonZero: true);
 }
 static async Task GetCloudServiceTimeAsync(string serviceNamespace)
 {
     try
     {
         var webRequest = WebRequest.CreateHttp(new Uri($"https://{serviceNamespace}"));
         webRequest.Method = "GET";
         using (var response = await webRequest.GetResponseAsync())
         {
             RelayTraceSource.TraceInfo(OutputFormat, "Azure Time:", response.Headers["Date"]); // RFC1123
         }
     }
     catch (Exception exception)
     {
         RelayTraceSource.TraceException(exception, "Getting current time from Relay cloud service");
     }
 }
        static async Task ExecutePlatformCommandAsync(NamespaceDetails namespaceDetails)
        {
            TraceCommandHeader("OS/Platform");
            RelayTraceSource.TraceInfo(OutputFormat, "OSVersion:", Environment.OSVersion);
            RelayTraceSource.TraceInfo(OutputFormat, "ProcessorCount:", Environment.ProcessorCount);
            RelayTraceSource.TraceInfo(OutputFormat, "Is64BitOperatingSystem:", Environment.Is64BitOperatingSystem);
            RelayTraceSource.TraceInfo(OutputFormat, "CLR Version:", Environment.Version);
            RelayTraceSource.TraceInfo(OutputFormat, "mscorlib AssemblyVersion:", typeof(object).Assembly.GetName().Version);
            RelayTraceSource.TraceInfo(OutputFormat, "mscorlib FileVersion:", FileVersionInfo.GetVersionInfo(typeof(object).Assembly.Location).FileVersion);

            if (!string.IsNullOrEmpty(namespaceDetails.ServiceNamespace))
            {
                await GetCloudServiceTimeAsync(namespaceDetails.ServiceNamespace);
            }

            var utcNow = DateTime.UtcNow;

            RelayTraceSource.TraceInfo(OutputFormat, "Machine Time(UTC):", utcNow.ToString(DateTimeFormatInfo.InvariantInfo.RFC1123Pattern));
            RelayTraceSource.TraceInfo(OutputFormat, "Machine Time(Local):", utcNow.ToLocalTime().ToString("ddd, dd MMM yyyy HH':'mm':'ss '('zzz')'")); // Like RFC1123Pattern but with zzz for timezone offset
        }