Skip to content

Commit

Permalink
Parsing error for UDP addresses #72
Browse files Browse the repository at this point in the history
  • Loading branch information
clemensv committed Aug 29, 2024
1 parent 744fb70 commit 247b467
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/Microsoft.Azure.Relay.Bridge/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public static Config LoadConfig(CommandLineSettings commandLineSettings)
if (new Regex("^[0-9]+U$").Match(portString).Success)
{
// UDP ports are just negative port numbers
portString = "-" + portString.Substring(0, portString.Length - 2);
portString = "-" + portString.Substring(0, portString.Length - 1);
}
// this is either -L local_socket:relay_name or -L port:relay_name
if (int.TryParse(portString, out var port))
Expand Down Expand Up @@ -668,7 +668,7 @@ public static Config LoadConfig(CommandLineSettings commandLineSettings)
if (new Regex("^[0-9]+U$").Match(portString).Success)
{
// UDP ports are just negative port numbers
portString = "-" + portString.Substring(0, portString.Length - 2);
portString = "-" + portString.Substring(0, portString.Length - 1);
}

// this is -L host:port:relay_name
Expand Down Expand Up @@ -778,7 +778,7 @@ private static void ParseRemoteForward(Config config, string rf, bool newFormat)
if (new Regex("^[0-9]+U$").Match(portString).Success)
{
// UDP ports are just negative port numbers
portString = "-" + portString.Substring(0, portString.Length - 2);
portString = "-" + portString.Substring(0, portString.Length - 1);
}

if (int.TryParse(portString, out var port))
Expand Down Expand Up @@ -846,7 +846,7 @@ private static void ParseRemoteForward(Config config, string rf, bool newFormat)
if (new Regex("^[0-9]+U$").Match(portString).Success)
{
// UDP ports are just negative port numbers
portString = "-" + portString.Substring(0, portString.Length - 2);
portString = "-" + portString.Substring(0, portString.Length - 1);
}
}
else
Expand All @@ -867,7 +867,7 @@ private static void ParseRemoteForward(Config config, string rf, bool newFormat)
if (new Regex("^[0-9]+U$").Match(portString).Success)
{
// UDP ports are just negative port numbers
portString = "-" + portString.Substring(0, portString.Length - 2);
portString = "-" + portString.Substring(0, portString.Length - 1);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.Azure.Relay.Bridge/RemoteForwardBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Dispose()
/// <exception cref="System.Security.SecurityException">Throws a SecurityException if Group Policy prohibits Resource Publishing.</exception>
public async Task Open()
{

if (this.IsOpen)
{
throw new InvalidOperationException();
Expand Down Expand Up @@ -138,7 +138,7 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr
hybridConnectionStream.WriteTimeout = 60000;

// read and write 4-byte header
// we don't do anything with this version preamble just yet; it really
// we don't do anything with this version preamble just yet; it really
// is insurance for when we might have to break protocol.
var versionPreamble = new byte[3];
for (int read = 0; read < versionPreamble.Length;)
Expand All @@ -158,7 +158,7 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr
if (versionPreamble[0] == 1 && versionPreamble[1] == 0 &&
(versionPreamble[2] == 0 || versionPreamble[2] == 1))
{
// For version 1.0, the version preamble is followed by a single byte
// For version 1.0, the version preamble is followed by a single byte
// length indicator and then that number of bytes with of UTF-8 encoded
// port-name string.
var portNameBuffer = new byte[256];
Expand Down Expand Up @@ -193,7 +193,7 @@ async Task HandleRelayConnectionAsync(HybridConnectionStream hybridConnectionStr
return;
}

IRemoteForwarder forwarder = null;
IRemoteForwarder forwarder = null;
if (remoteForwarders.Count == 1 && int.TryParse(portName, out var port))
{
forwarder = remoteForwarders.Values.First();
Expand Down
33 changes: 33 additions & 0 deletions test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ public void CommandLineGoodRemoteForwardTest()
Assert.Equal(3000, cfg.RemoteForward[0].HostPort);
return 0;
});
CommandLineSettings.Run(new string[] { "-R", "foobar:3000U" },
(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) =>
{
Expand Down Expand Up @@ -329,6 +338,30 @@ public void CommandLineGoodRemoteForwardTest()
});
}

[Fact]
public void CommandLineGoodLocalForwardTest()
{
CommandLineSettings.Run(new string[] { "-L", "3000U:foobar" },
(settings) =>
{
var cfg = Config.LoadConfig(settings);
Assert.Single(cfg.LocalForward);
Assert.Equal(-3000, cfg.LocalForward[0].BindPort);
Assert.Equal("foobar", cfg.LocalForward[0].RelayName);
return 0;
});
CommandLineSettings.Run(new string[] { "-L", "3000:foobar" },
(settings) =>
{
var cfg = Config.LoadConfig(settings);
Assert.Single(cfg.LocalForward);
Assert.Equal(3000, cfg.LocalForward[0].BindPort);
Assert.Equal("foobar", cfg.LocalForward[0].RelayName);
return 0;
});
}


[Fact]
public void CommandLineBadRemoteForwardTest()
{
Expand Down

0 comments on commit 247b467

Please sign in to comment.