Skip to content

Commit

Permalink
Only listen on 127.0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ShortDevelopment committed Dec 5, 2024
1 parent 43a1976 commit 3e74617
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/ShortDev.Microsoft.ConnectedDevices/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Net;

namespace ShortDev.Microsoft.ConnectedDevices;

internal sealed class TestUtils
{
public static IPAddress ListenAddress { get; private set; } = IPAddress.Any;

internal static void ListenLocalOnly()
{
ListenAddress = IPAddress.Loopback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public EndpointInfo GetEndpoint()
public event DeviceConnectedEventHandler? DeviceConnected;
public async Task Listen(CancellationToken cancellationToken)
{
var listener = _listener ??= new(IPAddress.Any, TcpPort);
var listener = _listener ??= new(TestUtils.ListenAddress, TcpPort);
listener.Start();

try
Expand Down Expand Up @@ -88,7 +88,7 @@ static UdpClient CreateUdpClient(int port)
}

client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.Client.Bind(new IPEndPoint(IPAddress.Any, port));
client.Client.Bind(new IPEndPoint(TestUtils.ListenAddress, port));

return client;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ ConnectedDevicesPlatform CreateDevice(DeviceContainer network, string name, stri

static void UseTcp(ConnectedDevicesPlatform cdp, int tcpPort, int udpPort)
{
TestUtils.ListenLocalOnly();

NetworkHandler networkHandler = new(IPAddress.Loopback);
NetworkTransport networkTransport = new(networkHandler, tcpPort, udpPort);
cdp.AddTransport(networkTransport);
Expand Down

0 comments on commit 3e74617

Please sign in to comment.