diff --git a/radiuscontroller/Services/AccountEnforcementService.cs b/radiuscontroller/Services/AccountEnforcementService.cs index 53dc6b2..d1940a6 100644 --- a/radiuscontroller/Services/AccountEnforcementService.cs +++ b/radiuscontroller/Services/AccountEnforcementService.cs @@ -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); } } diff --git a/radiuscontroller/Services/RadiusService.cs b/radiuscontroller/Services/RadiusService.cs index 425f89b..8741fea 100644 --- a/radiuscontroller/Services/RadiusService.cs +++ b/radiuscontroller/Services/RadiusService.cs @@ -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); } } }