Skip to content

Commit

Permalink
Add support to bind to ip-address instead of only localhost (#1100)
Browse files Browse the repository at this point in the history
* Add support to bind to ip-address instead of only localhost

* .

* .
  • Loading branch information
StefH committed May 17, 2024
1 parent 0b278db commit 2c001f6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
34 changes: 34 additions & 0 deletions examples/WireMock.Net.Console.Net452.Classic/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,42 @@ scalar MyCustomScalar
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
Expand Down
42 changes: 33 additions & 9 deletions src/WireMock.Net/Owin/AspNetCoreSelfHost.NETStandard.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,7 +27,7 @@ private static void SetHttpsAndUrls(KestrelServerOptions kestrelOptions, IWireMo
{
if (urlDetail.IsHttps)
{
kestrelOptions.ListenAnyIP(urlDetail.Port, listenOptions =>
Listen(kestrelOptions, urlDetail, listenOptions =>
{
listenOptions.UseHttps(options =>
{
Expand All @@ -37,10 +39,11 @@ private static void SetHttpsAndUrls(KestrelServerOptions kestrelOptions, IWireMo
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;
Expand All @@ -52,20 +55,41 @@ private static void SetHttpsAndUrls(KestrelServerOptions kestrelOptions, IWireMo
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 2c001f6

Please sign in to comment.