custom binding and port number (#195)

* allow custom bindings

* reorganize

* cleanup
This commit is contained in:
Jason Dove
2021-05-20 20:09:14 -05:00
committed by GitHub
parent 5fd0cc5469
commit c0b5ecd388
11 changed files with 90 additions and 17 deletions
@@ -0,0 +1,63 @@
using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using ErsatzTV.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ErsatzTV.Services.RunOnce
{
public class EndpointValidatorService : IHostedService
{
private readonly IConfiguration _configuration;
private readonly ILogger<EndpointValidatorService> _logger;
public EndpointValidatorService(IConfiguration configuration, ILogger<EndpointValidatorService> logger)
{
_configuration = configuration;
_logger = logger;
}
public Task StartAsync(CancellationToken cancellationToken)
{
string urls = _configuration.GetValue<string>("Kestrel:Endpoints:Http:Url");
if (urls.Split(";").Length > 1)
{
throw new NotSupportedException($"Multiple endpoints are not supported: {urls}");
}
const string PATTERN = @"http:\/\/(.*):(\d+)";
Match match = Regex.Match(urls, PATTERN);
if (match.Success)
{
string hostname = match.Groups[1].Value;
Settings.ListenPort = int.Parse(match.Groups[2].Value);
// IP address must be 0.0.0.0 or 127.0.0.1
if (IPAddress.TryParse(hostname, out IPAddress address))
{
if (!address.Equals(IPAddress.Parse("0.0.0.0")) && !IPAddress.IsLoopback(address))
{
throw new NotSupportedException($"Endpoint MUST include loopback: {urls}");
}
}
}
else
{
throw new NotSupportedException($"Invalid endpoint format: {urls}");
}
_logger.LogInformation(
"Server will listen on port {Port} - try UI at {UI}",
Settings.ListenPort,
$"http://localhost:{Settings.ListenPort}");
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}