mirror of
https://github.com/alexhopeoconnor/dnsmasq-webui.git
synced 2026-10-21 13:22:35 +10:00
Refactor parsers to Superpower; config set; EnsureManagedConfig; options doc
- Parsers: Superpower for all (Directive, FileLine, Include, DhcpHost, Leases, Hosts) - Shared ConfParserHelpers (Token, OptionalCommentPrefix) - DnsmasqConfOptionRegistry + DnsmasqOptionKind: typed options (ConfFile, ConfDir, Domain, DhcpRange, Server, Local, Address, DhcpOption, DhcpHost, Path, Flag, etc.) - EnsureManagedConfigHostedService: append conf-dir= to main config if missing, then create managed file; DnsmasqOptions + Dockerfile comments for permissions - Renames: ConfigLine->DnsmasqConfLine, ConfigSet->DnsmasqConfigSet, etc. - Test harness: entrypoint in scripts/, prepare-test-mount improvements - Status/Reload API and UI: stdout+stderr, exit code
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Diagnostics;
|
||||
using DnsmasqWebUI.Options;
|
||||
using DnsmasqWebUI.Services.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
@@ -10,31 +11,44 @@ namespace DnsmasqWebUI.Controllers;
|
||||
public class StatusController : ControllerBase
|
||||
{
|
||||
private readonly DnsmasqOptions _options;
|
||||
private readonly IDnsmasqConfigSetService _configSetService;
|
||||
|
||||
public StatusController(IOptions<DnsmasqOptions> options)
|
||||
public StatusController(IOptions<DnsmasqOptions> options, IDnsmasqConfigSetService configSetService)
|
||||
{
|
||||
_options = options.Value;
|
||||
_configSetService = configSetService;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Get()
|
||||
public async Task<IActionResult> Get(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dnsmasqStatus = GetDnsmasqServiceStatus();
|
||||
return Ok(new
|
||||
var set = await _configSetService.GetConfigSetAsync(ct);
|
||||
var (dnsmasqStatus, statusCommandExitCode, statusCommandStdout, statusCommandStderr) = GetDnsmasqServiceStatus(_options.StatusCommand);
|
||||
var effectiveLeasesPath = _configSetService.GetLeasesPath();
|
||||
var payload = new Dictionary<string, object?>
|
||||
{
|
||||
hostsPath = _options.HostsPath,
|
||||
configPath = _options.ConfigPath,
|
||||
leasesPath = _options.LeasesPath,
|
||||
hostsPathExists = !string.IsNullOrEmpty(_options.HostsPath) && System.IO.File.Exists(_options.HostsPath),
|
||||
configPathExists = !string.IsNullOrEmpty(_options.ConfigPath) && System.IO.File.Exists(_options.ConfigPath),
|
||||
leasesPathConfigured = !string.IsNullOrEmpty(_options.LeasesPath),
|
||||
leasesPathExists = !string.IsNullOrEmpty(_options.LeasesPath) && System.IO.File.Exists(_options.LeasesPath),
|
||||
reloadCommandConfigured = !string.IsNullOrWhiteSpace(_options.ReloadCommand),
|
||||
statusCommandConfigured = !string.IsNullOrWhiteSpace(_options.StatusCommand),
|
||||
dnsmasqStatus
|
||||
});
|
||||
["hostsPath"] = _options.HostsPath,
|
||||
["mainConfigPath"] = _options.MainConfigPath,
|
||||
["managedFilePath"] = set.ManagedFilePath,
|
||||
["leasesPath"] = effectiveLeasesPath,
|
||||
["hostsPathExists"] = !string.IsNullOrEmpty(_options.HostsPath) && System.IO.File.Exists(_options.HostsPath),
|
||||
["mainConfigPathExists"] = !string.IsNullOrEmpty(_options.MainConfigPath) && System.IO.File.Exists(_options.MainConfigPath),
|
||||
["managedFilePathExists"] = !string.IsNullOrEmpty(set.ManagedFilePath) && System.IO.File.Exists(set.ManagedFilePath),
|
||||
["leasesPathConfigured"] = !string.IsNullOrEmpty(effectiveLeasesPath),
|
||||
["leasesPathExists"] = !string.IsNullOrEmpty(effectiveLeasesPath) && System.IO.File.Exists(effectiveLeasesPath),
|
||||
["reloadCommandConfigured"] = !string.IsNullOrWhiteSpace(_options.ReloadCommand),
|
||||
["statusCommandConfigured"] = !string.IsNullOrWhiteSpace(_options.StatusCommand),
|
||||
["dnsmasqStatus"] = dnsmasqStatus
|
||||
};
|
||||
if (dnsmasqStatus != "active" && statusCommandExitCode.HasValue)
|
||||
payload["statusCommandExitCode"] = statusCommandExitCode.Value;
|
||||
if (dnsmasqStatus != "active" && !string.IsNullOrEmpty(statusCommandStdout))
|
||||
payload["statusCommandStdout"] = statusCommandStdout;
|
||||
if (dnsmasqStatus != "active" && !string.IsNullOrEmpty(statusCommandStderr))
|
||||
payload["statusCommandStderr"] = statusCommandStderr;
|
||||
return Ok(payload);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -42,11 +56,11 @@ public class StatusController : ControllerBase
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Runs StatusCommand if configured; returns "active" | "inactive" | "unknown" | "notConfigured".</summary>
|
||||
private string GetDnsmasqServiceStatus()
|
||||
/// <summary>Runs StatusCommand if configured; returns status and optional exit code/stdout/stderr for UI.</summary>
|
||||
private static (string Status, int? ExitCode, string? Stdout, string? Stderr) GetDnsmasqServiceStatus(string? statusCommand)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_options.StatusCommand))
|
||||
return "notConfigured";
|
||||
if (string.IsNullOrWhiteSpace(statusCommand))
|
||||
return ("notConfigured", null, null, null);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -55,7 +69,7 @@ public class StatusController : ControllerBase
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "/bin/sh",
|
||||
Arguments = "-c \"" + _options.StatusCommand.Replace("\"", "\\\"") + "\"",
|
||||
Arguments = "-c \"" + statusCommand.Replace("\"", "\\\"") + "\"",
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
@@ -63,14 +77,18 @@ public class StatusController : ControllerBase
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
process.StandardOutput.ReadToEnd();
|
||||
process.StandardError.ReadToEnd();
|
||||
var stdout = process.StandardOutput.ReadToEnd();
|
||||
var stderr = process.StandardError.ReadToEnd();
|
||||
process.WaitForExit(TimeSpan.FromSeconds(5));
|
||||
return process.ExitCode == 0 ? "active" : "inactive";
|
||||
var exitCode = process.HasExited ? process.ExitCode : -1;
|
||||
var status = exitCode == 0 ? "active" : "inactive";
|
||||
return (status, exitCode,
|
||||
string.IsNullOrWhiteSpace(stdout) ? null : stdout.Trim(),
|
||||
string.IsNullOrWhiteSpace(stderr) ? null : stderr.Trim());
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
return "unknown";
|
||||
return ("unknown", null, null, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user