Skip to content

Commit f2c7f0f

Browse files
feat(dotnet-utils): add HttpAllowAnyPort() and HttpAllowAnything() methods to JmuxClaims (#1440)
Issue: DGW-244
1 parent 5a5c065 commit f2c7f0f

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

utils/dotnet/Devolutions.Gateway.Utils.Tests/JsonSerializationTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@ public void JmuxClaimsHttpAllowAnyAdditional()
8282
Assert.Equal(EXPECTED, result);
8383
}
8484

85+
[Fact]
86+
public void JmuxClaimsHttpAllowAnyPort()
87+
{
88+
const string EXPECTED = """{"dst_hst":"http://hello.world:0","dst_addl":["https://example.com:0","tcp://test.com:0"],"jet_ap":"http","jet_aid":"3e7c1854-f1eb-42d2-b9cb-9303036e50da","jet_gw_id":"ccbaad3f-4627-4666-8bb5-cb6a1a7db815"}""";
89+
90+
var claims = new JmuxClaims(gatewayId, "http://hello.world:80", ApplicationProtocol.Http, sessionId);
91+
claims.AdditionalDestinations = new List<TargetAddr> { "https://example.com:443", "tcp://test.com:8080" };
92+
claims.HttpAllowAnyPort();
93+
string result = JsonSerializer.Serialize(claims);
94+
Assert.Equal(EXPECTED, result);
95+
}
96+
97+
[Fact]
98+
public void JmuxClaimsHttpAllowAnything()
99+
{
100+
const string EXPECTED = """{"dst_hst":"http://hello.world:80","dst_addl":["http://*:0","https://*:0"],"jet_ap":"http","jet_aid":"3e7c1854-f1eb-42d2-b9cb-9303036e50da","jet_gw_id":"ccbaad3f-4627-4666-8bb5-cb6a1a7db815"}""";
101+
102+
var claims = new JmuxClaims(gatewayId, "http://hello.world", ApplicationProtocol.Http, sessionId);
103+
claims.HttpAllowAnything();
104+
string result = JsonSerializer.Serialize(claims);
105+
Assert.Equal(EXPECTED, result);
106+
}
107+
85108
[Fact]
86109
public void AssociationClaims()
87110
{

utils/dotnet/Devolutions.Gateway.Utils/src/JmuxClaims.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,35 @@ public void HttpExpandAdditionals()
7878
}
7979
}
8080

81+
/// <summary>Modify all destinations to set the port to 0 (wildcard port).</summary>
82+
public void HttpAllowAnyPort()
83+
{
84+
// Set the main destination port to 0
85+
this.Destination = new TargetAddr(this.Destination.Scheme, this.Destination.Host, 0);
86+
87+
// Set all additional destinations ports to 0
88+
if (this.AdditionalDestinations != null)
89+
{
90+
for (int i = 0; i < this.AdditionalDestinations.Count; i++)
91+
{
92+
var dest = this.AdditionalDestinations[i];
93+
this.AdditionalDestinations[i] = new TargetAddr(dest.Scheme, dest.Host, 0);
94+
}
95+
}
96+
}
97+
98+
/// <summary>Insert *:0 to allow any port for any destination.</summary>
99+
public void HttpAllowAnything()
100+
{
101+
if (this.AdditionalDestinations is null)
102+
{
103+
this.AdditionalDestinations = new List<TargetAddr>();
104+
}
105+
106+
this.AdditionalDestinations.Add(new TargetAddr("http", "*", 0));
107+
this.AdditionalDestinations.Add(new TargetAddr("https", "*", 0));
108+
}
109+
81110
public string GetContentType()
82111
{
83112
return "JMUX";

0 commit comments

Comments
 (0)