Skip to content

Commit

Permalink
Add support for --address any
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Jun 25, 2023
1 parent 930a00f commit 6f65405
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ dotnet serve -o
dotnet serve -o -S
```

... with a specific port (otherwise, it defaults to a random, unused port).
```
dotnet serve --port 8080
```

... with access allowed to remote machines (defaults to loopback only.) Use this if running inside Docker.

```
dotnet serve --address any
```

## Usage

```
Expand All @@ -45,7 +56,10 @@ Options:
-d|--directory <DIR> The root directory to serve. [Current directory]
-o|--open-browser Open a web browser when the server starts. [false]
-p|--port <PORT> Port to use [8080]. Use 0 for a dynamic port.
-a|--address <ADDRESS> Address to use [127.0.0.1]
-a|--address <ADDRESS> Address to use. [Default = localhost].
Accepts IP addresses,
'localhost' for only accept requests from loopback connections, or
'any' to accept requests from any IP address.
--path-base <PATH> The base URL path of postpended to the site url.
--reverse-proxy <MAPPING> Map a path pattern to another url.
Expected format is <SOURCE_PATH_PATTERN>=<DESTINATION_URL_PREFIX>.
Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-serve/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class CommandLineOptions
[Range(0, 65535, ErrorMessage = "Invalid port. Ports must be in the range of 0 to 65535.")]
public int? Port { get; internal set; }

[Option("-a|--address <ADDRESS>", Description = "Address to use [127.0.0.1]")]
[Option("-a|--address <ADDRESS>", Description = "Address to use. [Default = localhost].\nAccepts IP addresses,\n'localhost' for only accept requests from loopback connections, or\n'any' to accept requests from any IP address.")]
public IPAddress[] Addresses { get; }

[Option("--path-base <PATH>", Description = "The base URL path of postpended to the site url.")]
Expand Down
5 changes: 5 additions & 0 deletions src/dotnet-serve/IPAddressParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public IPAddress Parse(string argName, string value, CultureInfo culture)
return IPAddress.Loopback;
}

if (string.Equals("any", value, StringComparison.OrdinalIgnoreCase))
{
return IPAddress.IPv6Any;
}

if (!IPAddress.TryParse(value, out var address))
{
throw new FormatException($"'{value}' is not a valid IP address");
Expand Down
25 changes: 20 additions & 5 deletions src/dotnet-serve/SimpleServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -80,7 +81,14 @@ void ConfigureHttps(ListenOptions options)
{
foreach (var a in _options.Addresses)
{
o.Listen(a, port.GetValueOrDefault(), ConfigureHttps);
if (a == IPAddress.IPv6Any)
{
o.ListenAnyIP(port.GetValueOrDefault(), ConfigureHttps);
}
else
{
o.Listen(a, port.GetValueOrDefault(), ConfigureHttps);
}
}
}
})
Expand All @@ -103,19 +111,21 @@ void ConfigureHttps(ListenOptions options)
}

await host.StartAsync(cancellationToken);
AfterServerStart(host);
var logger = host.Services.GetRequiredService<ILogger<SimpleServer>>();
AfterServerStart(host, logger);
await host.WaitForShutdownAsync(cancellationToken);
return 0;
}

private void AfterServerStart(IWebHost host)
private void AfterServerStart(IWebHost host, ILogger<SimpleServer> logger)
{
var addresses = host.ServerFeatures.Get<IServerAddressesFeature>();
var pathBase = _options.GetPathBase();

_console.WriteLine(GetListeningAddressText(addresses));
foreach (var a in addresses.Addresses)
{
logger.LogDebug("Listening on {address}", a);
_console.WriteLine(ConsoleColor.Green, " " + NormalizeToLoopbackAddress(a) + pathBase);
}

Expand Down Expand Up @@ -158,7 +168,7 @@ static string NormalizeToLoopbackAddress(string url)
}
}

private static void LaunchBrowser(string url)
private void LaunchBrowser(string url)
{
var psi = new ProcessStartInfo();
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Expand All @@ -171,13 +181,18 @@ private static void LaunchBrowser(string url)
psi.FileName = "xdg-open";
psi.ArgumentList.Add(url);
}
else
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
psi.FileName = "cmd";
psi.ArgumentList.Add("/C");
psi.ArgumentList.Add("start");
psi.ArgumentList.Add(url);
}
else
{
_console.Write(ConsoleColor.Red, "Could not determine how to launch the browser for this OS platform.");
return;
}

Process.Start(psi);
}
Expand Down

0 comments on commit 6f65405

Please sign in to comment.