Skip to content

Commit

Permalink
TcpCustomServerProtocolSetup and TcpBinaryServerProtocolSetup shouldn…
Browse files Browse the repository at this point in the history
…'t return 0.0.0.0 as a discoverable host, zyanfx#71.
  • Loading branch information
yallie authored and Mikhail Kanygin committed Sep 2, 2020
1 parent 0f3a843 commit bb25449
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
40 changes: 40 additions & 0 deletions source/Zyan.Communication/Protocols/ServerProtocolSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,45 @@ protected virtual bool IsDiscoverableUrl(string url)
return false;
}
}

/// <summary>
/// Gets the current machine IP addresses.
/// TODO: move this method to a helper class.
/// </summary>
internal static IPAddress[] GetIpAddresses()
{
var invalid = new[]
{
IPAddress.Loopback, IPAddress.Any,
IPAddress.IPv6Loopback, IPAddress.IPv6Any
};

return Tcp.DuplexChannel.Manager.GetAddresses()
.Where(ip => !invalid.Contains(ip))
.ToArray();
}

/// <summary>
/// Replaces the host name within the given URL.
/// TODO: move this method to a helper class.
/// </summary>
/// <param name="url">URL to replace the host name.</param>
/// <param name="hostName">New host name.</param>
internal static string TryReplaceHostName(string url, string hostName)
{
try
{
// use Uri class to validate the input
var uri = new Uri(url ?? string.Empty);
var builder = new UriBuilder(uri);
builder.Host = hostName;
return builder.Uri.ToString();
}
catch (UriFormatException)
{
// fail to format the given url
return url;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Linq;
using System.Net.Security;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
Expand Down Expand Up @@ -207,6 +208,19 @@ public override IAuthenticationProvider AuthenticationProvider
}
}

/// <inheritdoc/>
public override string GetDiscoverableUrl(string zyanHostName)
{
var channel = CreateChannel() as IChannelReceiver;
if (channel == null)
{
return null;
}

var url = channel.GetUrlsForUri(zyanHostName).FirstOrDefault();
return TryReplaceHostName(url, GetIpAddresses().FirstOrDefault()?.ToString());
}

#region Versioning settings

private Versioning _versioning = Versioning.Strict;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Linq;
using System.Net;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
Expand Down Expand Up @@ -388,6 +390,19 @@ public override IAuthenticationProvider AuthenticationProvider
}
}

/// <inheritdoc/>
public override string GetDiscoverableUrl(string zyanHostName)
{
var channel = CreateChannel() as IChannelReceiver;
if (channel == null)
{
return null;
}

var url = channel.GetUrlsForUri(zyanHostName).FirstOrDefault();
return TryReplaceHostName(url, GetIpAddresses().FirstOrDefault()?.ToString());
}

#region Versioning settings

private Versioning _versioning = Versioning.Strict;
Expand Down
41 changes: 41 additions & 0 deletions source/Zyan.Tests/ProtocolUrlTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System;
using System.Configuration;
using System.Linq;
using System.Net;
using Zyan.Communication;
using Zyan.Communication.Protocols;
using Zyan.Communication.Protocols.Http;
using Zyan.Communication.Protocols.Ipc;
using Zyan.Communication.Protocols.Null;
Expand Down Expand Up @@ -125,5 +129,42 @@ public void ZyanConnectionDoesntAcceptMalformedUrl()
Assert.Fail("'hello' is not a valid url for the Ipc protocol.");
}
}

[TestMethod]
public void GetIpAddressesDoesntReturnLoopbackOrAnyIpAddresses()
{
var addresses = ServerProtocolSetup.GetIpAddresses();
Assert.IsFalse(addresses.Contains(IPAddress.Any));
Assert.IsFalse(addresses.Contains(IPAddress.IPv6Any));
Assert.IsFalse(addresses.Contains(IPAddress.Loopback));
Assert.IsFalse(addresses.Contains(IPAddress.IPv6Loopback));
}

[TestMethod]
public void TryReplaceHostNameReplacesValidHostNamesAndDoesntThrowOnInvalidInput()
{
var url = ServerProtocolSetup.TryReplaceHostName("http://1.2.3.4/5", "newhost");
Assert.AreEqual("http://newhost/5", url);
Assert.AreEqual("unsupported", ServerProtocolSetup.TryReplaceHostName("unsupported", "example"));
Assert.AreEqual(null, ServerProtocolSetup.TryReplaceHostName(null, null));
}

[TestMethod]
public void TcpCustomServerChannelDoesntReturn0000AsDiscoverableUrl()
{
var proto = new TcpCustomServerProtocolSetup(12346, null);
var url = proto.GetDiscoverableUrl("SomeServer");
Assert.IsFalse(string.IsNullOrEmpty(url));
Assert.AreNotEqual("tcp://0.0.0.0:12346/SomeServer", url);
}

[TestMethod]
public void TcpBinaryServerChannelDoesntReturn0000AsDiscoverableUrl()
{
var proto = new TcpBinaryServerProtocolSetup(12347);
var url = proto.GetDiscoverableUrl("SomeServer");
Assert.IsFalse(string.IsNullOrEmpty(url));
Assert.AreNotEqual("tcp://0.0.0.0:12347/SomeServer", url);
}
}
}

0 comments on commit bb25449

Please sign in to comment.