Count9 #7

Merged
tygozwolle merged 20 commits from count9 into master 2026-08-03 12:17:13 +02:00
2 changed files with 14 additions and 14 deletions
Showing only changes of commit 926d2a1fc5 - Show all commits
@@ -107,24 +107,24 @@ public class AccountEnforcementService : BackgroundService
// Find active sessions to disconnect them instantly via RADIUS CoA (PoD)
var activeSessions = await db.RadAcct
.Where(ra => ra.Username == profile.Username && ra.AcctStopTime == null)
.Select(ra => ra.NasIpAddress)
.Distinct()
.Select(ra => new { ra.NasIpAddress, ra.CallingStationId, ra.AcctSessionId })
.ToListAsync(ct);
var radiusSecret = Environment.GetEnvironmentVariable("RADIUS_SECRET") ?? "radpass";
foreach (var nasIp in activeSessions)
foreach (var session in activeSessions)
{
if (string.IsNullOrWhiteSpace(nasIp)) continue;
if (string.IsNullOrWhiteSpace(session.NasIpAddress)) continue;
try
{
var payload = $"User-Name=\\\"{profile.Username}\\\"\\nCalling-Station-Id=\\\"{session.CallingStationId}\\\"\\nAcct-Session-Id=\\\"{session.AcctSessionId}\\\"\\n";
var process = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "sh",
Arguments = $"-c \"echo 'User-Name={profile.Username}' | radclient -x {nasIp}:3799 disconnect '{radiusSecret}'\"",
Arguments = $"-c \"printf '{payload}' | radclient -x {session.NasIpAddress}:3799 disconnect '{radiusSecret}'\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
@@ -133,11 +133,11 @@ public class AccountEnforcementService : BackgroundService
};
process.Start();
await process.WaitForExitAsync(ct);
_logger.LogInformation("Sent RADIUS Disconnect-Request to NAS {NasIp} for user {Username}", nasIp, profile.Username);
_logger.LogInformation("Sent RADIUS Disconnect-Request to NAS {NasIp} for user {Username} (MAC: {Mac})", session.NasIpAddress, profile.Username, session.CallingStationId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send RADIUS Disconnect-Request to NAS {NasIp}", nasIp);
_logger.LogError(ex, "Failed to send RADIUS Disconnect-Request to NAS {NasIp}", session.NasIpAddress);
}
}
+7 -7
View File
@@ -379,24 +379,24 @@ public class RadiusService : IRadiusService
{
var activeSessions = await _db.RadAcct
.Where(ra => ra.Username == username && ra.AcctStopTime == null)
.Select(ra => ra.NasIpAddress)
.Distinct()
.Select(ra => new { ra.NasIpAddress, ra.CallingStationId, ra.AcctSessionId })
.ToListAsync();
var radiusSecret = Environment.GetEnvironmentVariable("RADIUS_SECRET") ?? "radpass";
foreach (var nasIp in activeSessions)
foreach (var session in activeSessions)
{
if (string.IsNullOrWhiteSpace(nasIp)) continue;
if (string.IsNullOrWhiteSpace(session.NasIpAddress)) continue;
try
{
var payload = $"User-Name=\\\"{username}\\\"\\nCalling-Station-Id=\\\"{session.CallingStationId}\\\"\\nAcct-Session-Id=\\\"{session.AcctSessionId}\\\"\\n";
var process = new System.Diagnostics.Process
{
StartInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "sh",
Arguments = $"-c \"echo 'User-Name={username}' | radclient -x {nasIp}:3799 disconnect '{radiusSecret}'\"",
Arguments = $"-c \"printf '{payload}' | radclient -x {session.NasIpAddress}:3799 disconnect '{radiusSecret}'\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
@@ -405,11 +405,11 @@ public class RadiusService : IRadiusService
};
process.Start();
await process.WaitForExitAsync();
_logger.LogInformation("Sent manual RADIUS Disconnect-Request to NAS {NasIp} for user {Username}", nasIp, username);
_logger.LogInformation("Sent manual RADIUS Disconnect-Request to NAS {NasIp} for user {Username} (MAC: {Mac})", session.NasIpAddress, username, session.CallingStationId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to send manual RADIUS Disconnect-Request to NAS {NasIp}", nasIp);
_logger.LogError(ex, "Failed to send manual RADIUS Disconnect-Request to NAS {NasIp}", session.NasIpAddress);
}
}
}