Skip to content

Commit 34bfe4d

Browse files
committed
Switch from deprecated System.CommandLine.DragonFruit to MakeBuild.CommandLine. It appears to support the same argument handling and is a simple change.
1 parent ca8ce65 commit 34bfe4d

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

Program.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
using System.Net.Sockets;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using DotMake.CommandLine;
11+
12+
Cli.Run(async (string serverPortRange = "1812-1813", string internalPortRange = "32048-62048", IPAddress adminIp = default, int adminPort = 1111, uint clientTimeout = 30, uint targetTimeout = 30, byte defaultTargetWeight = 100, bool unwise = false, ushort statsPeriodMs = 1000, byte defaultGroupId = 0, bool useProxyProtocol = false, string[] proxyProtocolTLV = default) =>
13+
await SimplestLoadBalancer.Program.RunAsync(serverPortRange, internalPortRange, adminIp, adminPort, clientTimeout, targetTimeout, defaultTargetWeight, unwise, statsPeriodMs, defaultGroupId, useProxyProtocol, proxyProtocolTLV));
1014

1115
namespace SimplestLoadBalancer
1216
{
@@ -76,7 +80,7 @@ static class Program
7680
/// <param name="defaultGroupId">Sets the group ID to assign to backends that when a registration packet doesn't include one, and when port isn't assigned a group</param>
7781
/// <param name="useProxyProtocol">When specified packet data will be prepended with a Proxy Protocol v2 header when sent to the backend</param>
7882
/// <param name="proxyProtocolTLV">Use to specify one or more TLVs to add to PPv2 headers (ignored when PPv2 isn't enabled). Example value: "0xDA=smurf".</param>
79-
static async Task Main(string serverPortRange = "1812-1813", string internalPortRange = "32048-62048", IPAddress adminIp = default, int adminPort = 1111, uint clientTimeout = 30, uint targetTimeout = 30, byte defaultTargetWeight = 100, bool unwise = false, ushort statsPeriodMs = 1000, byte defaultGroupId = 0, bool useProxyProtocol = false, string[] proxyProtocolTLV = default)
83+
public static async Task RunAsync(string serverPortRange, string internalPortRange, IPAddress adminIp, int adminPort, uint clientTimeout, uint targetTimeout, byte defaultTargetWeight, bool unwise, ushort statsPeriodMs, byte defaultGroupId, bool useProxyProtocol, string[] proxyProtocolTLV)
8084
{
8185
var ports = serverPortRange.Split("-", StringSplitOptions.RemoveEmptyEntries) switch
8286
{
@@ -91,7 +95,8 @@ static async Task Main(string serverPortRange = "1812-1813", string internalPort
9195
_ => throw new ArgumentException($"Invalid internal port range: {internalPortRange}.", nameof(internalPortRange))
9296
};
9397

94-
if (ports.Intersect(internal_ports).Any()) {
98+
if (ports.Intersect(internal_ports).Any())
99+
{
95100
throw new ArgumentException($"Server and internal port ranges must not overlap and mustn't include the admin port: {serverPortRange}, {internalPortRange} and {adminPort}.", nameof(serverPortRange));
96101
}
97102

@@ -207,11 +212,12 @@ async Task relay()
207212
{
208213
Interlocked.Increment(ref received);
209214

210-
var (_, internal_client, _) = clients.AddOrUpdate((request.RemoteEndPoint, port),
211-
ep => {
215+
var (_, internal_client, _) = clients.AddOrUpdate((request.RemoteEndPoint, port),
216+
ep =>
217+
{
212218
var internal_port = free_internal_ports.Dequeue();
213219
return (internal_port, new UdpClient().Configure(), DateTime.UtcNow);
214-
},
220+
},
215221
(ep, c) => (c.internal_port, c.internal_client, DateTime.UtcNow)
216222
);
217223
if (backend_groups.TryGetValue(port_group_map[port], out var group))
@@ -270,7 +276,7 @@ async Task admin()
270276
(
271277
ip: command switch
272278
{
273-
[0, 0, 0, 0, 0, 0, 0, 0, ..] => packet.RemoteEndPoint.Address,
279+
[0, 0, 0, 0, 0, 0, 0, 0, ..] => packet.RemoteEndPoint.Address,
274280
_ => new IPAddress(command.Slice(0, 8))
275281
},
276282
weight: options switch { [var weight, ..] => weight, _ => defaultTargetWeight },
@@ -281,7 +287,7 @@ async Task admin()
281287
(
282288
ip: command switch
283289
{
284-
[0, 0, 0, 0, ..] => packet.RemoteEndPoint.Address,
290+
[0, 0, 0, 0, ..] => packet.RemoteEndPoint.Address,
285291
_ => new IPAddress(command.Slice(0, 4))
286292
},
287293
weight: options switch { [var weight, ..] => weight, _ => defaultTargetWeight },
@@ -342,14 +348,15 @@ async Task admin()
342348
{
343349
[var client_port_high, var client_port_low, .. var ip_bytes] when ip_bytes.Count == 4 || ip_bytes.Count == 16
344350
=> new IPEndPoint(new IPAddress(ep_bytes[2..]), client_port_low + (client_port_high << 8)),
345-
_ => ep_none
351+
_ => ep_none
346352
}, port_low + (port_high << 8));
347353

348-
if (clients.TryGetValue((client_ep, server_port), out var info)) {
354+
if (clients.TryGetValue((client_ep, server_port), out var info))
355+
{
349356
var internal_port = info.internal_port;
350357
await control.SendAsync([0x2e, 0x12, (byte)(internal_port >> 8), (byte)internal_port, port_high, port_low, .. ep_bytes], 6 + ep_bytes.Count, packet.RemoteEndPoint);
351358
}
352-
}
359+
}
353360
break;
354361

355362
default:

SimplestLoadBalancer.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
<Authors>mlhpdx</Authors>
77
<Product>SimplestLoadBalancer</Product>
88
<Description>Sessionless Load Balancer for UDP</Description>
9+
<PublishAot>true</PublishAot>
910
</PropertyGroup>
1011

1112
<ItemGroup>
13+
<PackageReference Include="DotMake.CommandLine" Version="2.8.0" />
1214
<PackageReference Include="IPNetwork2" Version="3.1.764" />
13-
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
1415
</ItemGroup>
1516

1617
</Project>

0 commit comments

Comments
 (0)