Add support to bind to ip-address instead of only localhost (#1100)

* Add support to bind to ip-address instead of only localhost

* .

* .
This commit is contained in:
Stef Heyenrath
2024-05-17 21:46:07 +02:00
committed by GitHub
parent 0b278dbbbb
commit 2c001f661d
3 changed files with 68 additions and 11 deletions

View File

@@ -94,8 +94,42 @@ message HelloReply {
fullName:String fullName:String
}"; }";
private static void RunOnLocal()
{
try
{
var serverOnPrivateIPAddress192_168_1 = WireMockServer.Start(new WireMockServerSettings
{
Urls = new[] { "http://192.168.1.166:8102" }
});
System.Console.WriteLine($"{string.Join(", ", serverOnPrivateIPAddress192_168_1.Urls)}");
serverOnPrivateIPAddress192_168_1.Stop();
}
catch (Exception e)
{
System.Console.WriteLine("serverOnPrivateIPAddress192: " + e);
}
try
{
var serverOnPrivateIPAddress172_19 = WireMockServer.Start(new WireMockServerSettings
{
Urls = new[] { "https://172.19.80.1:8103" }
});
System.Console.WriteLine($"{string.Join(", ", serverOnPrivateIPAddress172_19.Urls)}");
serverOnPrivateIPAddress172_19.Stop();
}
catch (Exception e)
{
System.Console.WriteLine("serverOnPrivateIPAddress172_19: " + e);
}
}
public static void Run() public static void Run()
{ {
RunOnLocal();
return;
var mappingBuilder = new MappingBuilder(); var mappingBuilder = new MappingBuilder();
mappingBuilder mappingBuilder
.Given(Request .Given(Request

View File

@@ -1,5 +1,7 @@
#if USE_ASPNETCORE && !NETSTANDARD1_3 #if USE_ASPNETCORE && !NETSTANDARD1_3
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https; using Microsoft.AspNetCore.Server.Kestrel.Https;
@@ -25,7 +27,7 @@ namespace WireMock.Owin
{ {
if (urlDetail.IsHttps) if (urlDetail.IsHttps)
{ {
kestrelOptions.ListenAnyIP(urlDetail.Port, listenOptions => Listen(kestrelOptions, urlDetail, listenOptions =>
{ {
listenOptions.UseHttps(options => listenOptions.UseHttps(options =>
{ {
@@ -37,10 +39,11 @@ namespace WireMock.Owin
wireMockMiddlewareOptions.X509ThumbprintOrSubjectName, wireMockMiddlewareOptions.X509ThumbprintOrSubjectName,
wireMockMiddlewareOptions.X509CertificateFilePath, wireMockMiddlewareOptions.X509CertificateFilePath,
wireMockMiddlewareOptions.X509CertificatePassword, wireMockMiddlewareOptions.X509CertificatePassword,
urlDetail.Host); urlDetail.Host
);
} }
options.ClientCertificateMode = (ClientCertificateMode) wireMockMiddlewareOptions.ClientCertificateMode; options.ClientCertificateMode = (ClientCertificateMode)wireMockMiddlewareOptions.ClientCertificateMode;
if (wireMockMiddlewareOptions.AcceptAnyClientCertificate) if (wireMockMiddlewareOptions.AcceptAnyClientCertificate)
{ {
options.ClientCertificateValidation = (_, _, _) => true; options.ClientCertificateValidation = (_, _, _) => true;
@@ -52,20 +55,41 @@ namespace WireMock.Owin
listenOptions.Protocols = HttpProtocols.Http2; listenOptions.Protocols = HttpProtocols.Http2;
} }
}); });
continue;
} }
else if (urlDetail.IsHttp2)
if (urlDetail.IsHttp2)
{ {
kestrelOptions.ListenAnyIP(urlDetail.Port, listenOptions => Listen(kestrelOptions, urlDetail, listenOptions =>
{ {
listenOptions.Protocols = HttpProtocols.Http2; listenOptions.Protocols = HttpProtocols.Http2;
}); });
continue;
} }
else
{ Listen(kestrelOptions, urlDetail, _ => { });
kestrelOptions.ListenAnyIP(urlDetail.Port);
}
} }
} }
private static void Listen(KestrelServerOptions kestrelOptions, HostUrlDetails urlDetail, Action<ListenOptions> configure)
{
// Listens on ::1 and 127.0.0.1 with the given port.
if (urlDetail is { Port: > 0, Host: "localhost" or "127.0.0.1" or "0.0.0.0" or "::1" })
{
kestrelOptions.ListenLocalhost(urlDetail.Port, configure);
return;
}
// Try to parse the host as a valid IP address and bind to the given IP address and port.
if (IPAddress.TryParse(urlDetail.Host, out var ipAddress))
{
kestrelOptions.Listen(ipAddress, urlDetail.Port, configure);
return;
}
// Otherwise, listen on all IPs.
kestrelOptions.ListenAnyIP(urlDetail.Port, configure);
}
} }
internal static class IWebHostBuilderExtensions internal static class IWebHostBuilderExtensions

View File

@@ -1,9 +1,8 @@
using NFluent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using FluentAssertions; using FluentAssertions;
using NFluent;
using WireMock.Http; using WireMock.Http;
using WireMock.Models; using WireMock.Models;
using WireMock.Types; using WireMock.Types;