mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-17 07:36:47 +01:00
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:
@@ -94,8 +94,42 @@ message HelloReply {
|
||||
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()
|
||||
{
|
||||
RunOnLocal();
|
||||
return;
|
||||
|
||||
var mappingBuilder = new MappingBuilder();
|
||||
mappingBuilder
|
||||
.Given(Request
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#if USE_ASPNETCORE && !NETSTANDARD1_3
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||
using Microsoft.AspNetCore.Server.Kestrel.Https;
|
||||
@@ -25,7 +27,7 @@ namespace WireMock.Owin
|
||||
{
|
||||
if (urlDetail.IsHttps)
|
||||
{
|
||||
kestrelOptions.ListenAnyIP(urlDetail.Port, listenOptions =>
|
||||
Listen(kestrelOptions, urlDetail, listenOptions =>
|
||||
{
|
||||
listenOptions.UseHttps(options =>
|
||||
{
|
||||
@@ -37,10 +39,11 @@ namespace WireMock.Owin
|
||||
wireMockMiddlewareOptions.X509ThumbprintOrSubjectName,
|
||||
wireMockMiddlewareOptions.X509CertificateFilePath,
|
||||
wireMockMiddlewareOptions.X509CertificatePassword,
|
||||
urlDetail.Host);
|
||||
urlDetail.Host
|
||||
);
|
||||
}
|
||||
|
||||
options.ClientCertificateMode = (ClientCertificateMode) wireMockMiddlewareOptions.ClientCertificateMode;
|
||||
options.ClientCertificateMode = (ClientCertificateMode)wireMockMiddlewareOptions.ClientCertificateMode;
|
||||
if (wireMockMiddlewareOptions.AcceptAnyClientCertificate)
|
||||
{
|
||||
options.ClientCertificateValidation = (_, _, _) => true;
|
||||
@@ -52,20 +55,41 @@ namespace WireMock.Owin
|
||||
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;
|
||||
});
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
kestrelOptions.ListenAnyIP(urlDetail.Port);
|
||||
}
|
||||
|
||||
Listen(kestrelOptions, urlDetail, _ => { });
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
using NFluent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using NFluent;
|
||||
using WireMock.Http;
|
||||
using WireMock.Models;
|
||||
using WireMock.Types;
|
||||
|
||||
Reference in New Issue
Block a user