Skip to content

Commit 86f50b2

Browse files
committed
Additional NetLimiter Functionality
1 parent de4cf69 commit 86f50b2

File tree

5 files changed

+156
-164
lines changed

5 files changed

+156
-164
lines changed

BHD-ServerManager/Classes/Services/NetLimiter/NetLimiterClient.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,47 @@ public static async Task<bool> EndProgramAsync()
174174
return response.Success;
175175
}
176176

177+
public static async Task<List<string>> GetFilterIpAddressesAsync(string filterName)
178+
{
179+
var command = new Command
180+
{
181+
Action = "getfilterips",
182+
Parameters = new Dictionary<string, string> { { "filterName", filterName } }
183+
};
184+
185+
var response = await SendCommandAsync(command);
186+
if (response.Success && response.Data != null)
187+
{
188+
// Deserialize JsonElement to List of IP range objects
189+
var jsonElement = (JsonElement)response.Data;
190+
var ipRanges = JsonSerializer.Deserialize<List<IpRange>>(jsonElement.GetRawText(), JsonOptions);
191+
192+
// Convert to list of IP strings (for single IPs, Start == End)
193+
var ipList = new List<string>();
194+
foreach (var range in ipRanges!)
195+
{
196+
if (range.Start == range.End)
197+
{
198+
ipList.Add(range.Start);
199+
}
200+
else
201+
{
202+
ipList.Add($"{range.Start}-{range.End}");
203+
}
204+
}
205+
206+
return ipList;
207+
}
208+
209+
return new List<string>();
210+
}
211+
212+
private class IpRange
213+
{
214+
public string Start { get; set; }
215+
public string End { get; set; }
216+
}
217+
177218
public static void StartBridgeProcess(string hostname = "localhost", ushort port = 11111, string username = "", string password = "")
178219
{
179220

@@ -231,7 +272,24 @@ public static void StartBridgeProcess(string hostname = "localhost", ushort port
231272
throw;
232273
}
233274
}
234-
275+
public static async Task<List<string>> GetFilterNamesAsync()
276+
{
277+
var command = new Command
278+
{
279+
Action = "getfilternames",
280+
Parameters = new Dictionary<string, string>()
281+
};
282+
283+
var response = await SendCommandAsync(command);
284+
if (response.Success && response.Data != null)
285+
{
286+
// Deserialize JsonElement to List<string>
287+
var jsonElement = (JsonElement)response.Data;
288+
return JsonSerializer.Deserialize<List<string>>(jsonElement.GetRawText(), JsonOptions)!;
289+
}
290+
291+
return new List<string>();
292+
}
235293
public static void StopBridgeProcess()
236294
{
237295
if (_bridgeProcess != null && !_bridgeProcess.HasExited)

NetLimiterBridge/MainClient.cs

Lines changed: 0 additions & 98 deletions
This file was deleted.

NetLimiterBridge/NetLimiterBridge.cs

Lines changed: 97 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,47 +129,110 @@ private async Task ListenForCommandsAsync()
129129
}
130130
}
131131

132-
private Response ProcessCommand(Command command)
132+
private Response ProcessCommand(Command command)
133+
{
134+
Console.WriteLine($"Processing command: {command.Action}");
135+
try
136+
{
137+
switch (command.Action.ToLower())
138+
{
139+
case "getappid":
140+
return GetAppId(command.Parameters["appPath"]);
141+
case "getfilternames":
142+
return GetFilterNames();
143+
case "getconnections":
144+
return GetConnections(command.Parameters["appId"]);
145+
146+
case "getfilterobject":
147+
return GetFilterObject(command.Parameters["filterName"]);
148+
case "getfilterips":
149+
return GetFilterIpAddresses(command.Parameters["filterName"]);
150+
case "addiptofilter":
151+
return AddIpToFilter(command.Parameters["filterName"], command.Parameters["ipAddress"]);
152+
153+
case "removeipfromfilter":
154+
return RemoveIpFromFilter(command.Parameters["filterName"], command.Parameters["ipAddress"]);
155+
156+
case "setconnectionlimit":
157+
return SetConnectionLimit(int.Parse(command.Parameters["limit"]));
158+
159+
case "enableconnectionlimit":
160+
return EnableConnectionLimit(bool.Parse(command.Parameters["enabled"]));
161+
162+
case "endprogram":
163+
return EndProgram();
164+
165+
default:
166+
return new Response { Success = false, Message = "Unknown command" };
167+
}
168+
}
169+
catch (Exception ex)
170+
{
171+
return new Response { Success = false, Message = ex.Message };
172+
}
173+
}
174+
175+
private Response GetFilterNames()
133176
{
134-
Console.WriteLine($"Processing command: {command.Action}");
135177
try
136-
{
137-
switch (command.Action.ToLower())
138-
{
139-
case "getappid":
140-
return GetAppId(command.Parameters["appPath"]);
141-
142-
case "getconnections":
143-
return GetConnections(command.Parameters["appId"]);
144-
145-
case "getfilterobject":
146-
return GetFilterObject(command.Parameters["filterName"]);
147-
148-
case "addiptofilter":
149-
return AddIpToFilter(command.Parameters["filterName"], command.Parameters["ipAddress"]);
150-
151-
case "removeipfromfilter":
152-
return RemoveIpFromFilter(command.Parameters["filterName"], command.Parameters["ipAddress"]);
178+
{
179+
var filters = _client.Filters;
180+
var filterNames = filters.Select(f => f.Name).ToList();
153181

154-
case "setconnectionlimit":
155-
return SetConnectionLimit(int.Parse(command.Parameters["limit"]));
182+
return new Response
183+
{
184+
Success = true,
185+
Data = filterNames,
186+
Message = $"Found {filterNames.Count} filter(s)"
187+
};
188+
}
189+
catch (Exception ex)
190+
{
191+
return new Response { Success = false, Message = ex.Message };
192+
}
193+
}
156194

157-
case "enableconnectionlimit":
158-
return EnableConnectionLimit(bool.Parse(command.Parameters["enabled"]));
195+
private Response GetFilterIpAddresses(string filterName)
196+
{
197+
try
198+
{
199+
var filters = _client.Filters;
200+
var filter = _client.Filters.FirstOrDefault(f => f.Name == filterName);
159201

160-
case "endprogram":
161-
return EndProgram();
202+
if (filter != null)
203+
{
204+
var remoteAddressFilter = filter.Functions
205+
.OfType<FFRemoteAddressInRange>()
206+
.FirstOrDefault();
207+
208+
if (remoteAddressFilter == null)
209+
{
210+
return new Response { Success = false, Message = "No existing 'Remote address in range' function found." };
211+
}
212+
213+
var ipAddresses = remoteAddressFilter.Values
214+
.Select(range => new
215+
{
216+
Start = range.Range.Start.ToString(),
217+
End = range.Range.End.ToString()
218+
})
219+
.ToList();
220+
221+
return new Response
222+
{
223+
Success = true,
224+
Data = ipAddresses,
225+
Message = $"Found {ipAddresses.Count} IP address range(s)"
226+
};
227+
}
162228

163-
default:
164-
return new Response { Success = false, Message = "Unknown command" };
165-
}
166-
}
167-
catch (Exception ex)
168-
{
169-
return new Response { Success = false, Message = ex.Message };
170-
}
229+
return new Response { Success = false, Message = "Filter not found" };
230+
}
231+
catch (Exception ex)
232+
{
233+
return new Response { Success = false, Message = ex.Message };
234+
}
171235
}
172-
173236
private Response GetAppId(string appPath)
174237
{
175238
try

NetLimiterBridge/NetLimiterBridge.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
<StartupObject>NetLimiterBridge.Program</StartupObject>
5656
</PropertyGroup>
5757
<ItemGroup>
58-
<Compile Include="MainClient.cs" />
5958
<Compile Include="Messages.cs" />
6059
<Compile Include="NetLimiterBridge.cs" />
6160
<Compile Include="Program.cs" />
@@ -65,7 +64,6 @@
6564
<Content Include="NLInterop.dll">
6665
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
6766
</Content>
68-
<Content Include="ReadMe.md" />
6967
</ItemGroup>
7068
<ItemGroup>
7169
<None Include="App.config" />

NetLimiterBridge/ReadMe.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)