Skip to content

Commit

Permalink
Addresses issue #8, allowing "." as a separator inside a relay name
Browse files Browse the repository at this point in the history
  • Loading branch information
clemensv committed Aug 29, 2024
1 parent 5895733 commit 1a72e7c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions Microsoft.Azure.Relay.Bridge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
global.json = global.json
LICENSE.txt = LICENSE.txt
NuGetPackageVerifier.json = NuGetPackageVerifier.json
OVERVIEW.md = OVERVIEW.md
package-all.cmd = package-all.cmd
package.cmd = package.cmd
package.sh = package.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public string RelayName
{
var val = value != null ? value.Trim('\'', '\"') : value;
if (val != null &&
!new Regex("^[0-9A-Za-z_-]+$").Match(val).Success)
!Constants.RelayNameRegex.Match(val).Success)
{
throw BridgeEventSource.Log.ArgumentOutOfRange(
nameof(RelayName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public string PortName
{
var val = value != null ? value.Trim('\'', '\"') : value;
if (val != null &&
!new Regex("^[0-9A-Za-z_-]+$").Match(val).Success)
!Constants.RelayNameRegex.Match(val).Success)
{
throw BridgeEventSource.Log.ArgumentOutOfRange(
nameof(PortName),
Expand All @@ -180,7 +180,7 @@ public string BindLocalSocket
Uri path;
if (val != null &&
!Uri.TryCreate(val, UriKind.Absolute, out path) &&
!new Regex("^[0-9A-Za-z_-]+$").Match(val).Success)
!Constants.RelayNameRegex.Match(val).Success)
{
throw BridgeEventSource.Log.ArgumentOutOfRange(
nameof(BindLocalSocket),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public string RelayName
{
var val = value != null ? value.Trim('\'', '\"') : value;
if (val != null &&
!new Regex("^[0-9A-Za-z/_-]+$").Match(val).Success)
!Constants.RelayNameRegex.Match(val).Success)
{
throw BridgeEventSource.Log.ArgumentOutOfRange(
nameof(RelayName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public string PortName
{
var val = value != null ? value.Trim('\'', '\"') : value;
if (val != null &&
!new Regex("^[0-9A-Za-z_-]+$").Match(val).Success)
!Constants.RelayNameRegex.Match(val).Success)
{
throw BridgeEventSource.Log.ArgumentOutOfRange(
nameof(PortName),
Expand All @@ -86,7 +86,7 @@ public string LocalSocket
Uri path;
if (val != null &&
!Uri.TryCreate(val, UriKind.Absolute, out path) &&
!new Regex("^[0-9A-Za-z_-]+$").Match(val).Success)
!Constants.RelayNameRegex.Match(val).Success)
{
throw BridgeEventSource.Log.ArgumentOutOfRange(
nameof(LocalSocket),
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.Azure.Relay.Bridge/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// // Copyright (c) Microsoft. All rights reserved.
// // Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Text.RegularExpressions;

namespace Microsoft.Azure.Relay.Bridge
{
static class Constants
public static class Constants
{

public static Regex RelayNameRegex = new Regex(@"^[0-9A-Za-z/_\-\.]+$", RegexOptions.Compiled);
}
}
50 changes: 50 additions & 0 deletions test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,56 @@ public void CommandLineBadLocalForwardTest()
Assert.Contains("BindAddress", ex2.Message);
}

[Fact]
public void CommandLineGoodRemoteForwardTest()
{
CommandLineSettings.Run(new string[] { "-R", "foobar:3000" },
(settings) =>
{
var cfg = Config.LoadConfig(settings);
Assert.Single(cfg.RemoteForward);
Assert.Equal("foobar", cfg.RemoteForward[0].RelayName);
Assert.Equal(3000, cfg.RemoteForward[0].HostPort);
return 0;
});
CommandLineSettings.Run(new string[] { "-R", "foobar_foobar:3000" },
(settings) =>
{
var cfg = Config.LoadConfig(settings);
Assert.Single(cfg.RemoteForward);
Assert.Equal("foobar_foobar", cfg.RemoteForward[0].RelayName);
Assert.Equal(3000, cfg.RemoteForward[0].HostPort);
return 0;
});
CommandLineSettings.Run(new string[] { "-R", "foobar/foobar:3000" },
(settings) =>
{
var cfg = Config.LoadConfig(settings);
Assert.Single(cfg.RemoteForward);
Assert.Equal("foobar/foobar", cfg.RemoteForward[0].RelayName);
Assert.Equal(3000, cfg.RemoteForward[0].HostPort);
return 0;
});
CommandLineSettings.Run(new string[] { "-R", "foobar.foobar:3000" },
(settings) =>
{
var cfg = Config.LoadConfig(settings);
Assert.Single(cfg.RemoteForward);
Assert.Equal("foobar.foobar", cfg.RemoteForward[0].RelayName);
Assert.Equal(3000, cfg.RemoteForward[0].HostPort);
return 0;
});
CommandLineSettings.Run(new string[] { "-R", "foobar-foobar:3000" },
(settings) =>
{
var cfg = Config.LoadConfig(settings);
Assert.Single(cfg.RemoteForward);
Assert.Equal("foobar-foobar", cfg.RemoteForward[0].RelayName);
Assert.Equal(3000, cfg.RemoteForward[0].HostPort);
return 0;
});
}

[Fact]
public void CommandLineBadRemoteForwardTest()
{
Expand Down

0 comments on commit 1a72e7c

Please sign in to comment.