Files
dnsmasq-webui/src/DnsmasqWebUI/Infrastructure/Services/HostsFileService.cs
T
alex 1bdd176c18 App logs, client settings, NavMenu layout
- Add real-time app logs via SignalR (log level, category filters)
- Add filters modal with restore defaults; sentinel for empty arrays
- Add client settings: max lines and auto-scroll per log panel
- Improve NavMenu: align settings + hamburger, smaller mobile logo
- LogsService, LogsHub, LoggingController, AppLogsLoggerProvider
- Persist log level and filters to appsettings.Overrides.json
2026-02-10 08:44:30 +10:00

47 lines
1.9 KiB
C#

using DnsmasqWebUI.Infrastructure.Helpers.Config;
using DnsmasqWebUI.Infrastructure.Logging;
using DnsmasqWebUI.Infrastructure.Parsers;
using DnsmasqWebUI.Infrastructure.Services.Abstractions;
using DnsmasqWebUI.Models.Hosts;
using Microsoft.Extensions.Logging;
namespace DnsmasqWebUI.Infrastructure.Services;
public class HostsFileService : IHostsFileService
{
private readonly IDnsmasqConfigSetService _configSetService;
private readonly IHostsCache _hostsCache;
private readonly ILogger<HostsFileService> _logger;
public HostsFileService(IDnsmasqConfigSetService configSetService, IHostsCache hostsCache, ILogger<HostsFileService> logger)
{
_configSetService = configSetService;
_hostsCache = hostsCache;
_logger = logger;
}
public async Task<IReadOnlyList<HostEntry>> ReadAsync(CancellationToken ct = default)
{
var snapshot = await _hostsCache.GetSnapshotAsync(ct);
return snapshot.ManagedEntries;
}
public async Task WriteAsync(IReadOnlyList<HostEntry> entries, CancellationToken ct = default)
{
var set = await _configSetService.GetConfigSetAsync(ct);
var path = set.ManagedHostsFilePath;
if (string.IsNullOrEmpty(path))
throw new InvalidOperationException("No managed hosts path configured. Set Dnsmasq:MainConfigPath and Dnsmasq:ManagedHostsFileName to enable hosts editing.");
var dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
Directory.CreateDirectory(dir);
var tmpPath = path + ".tmp";
var lines = entries.Select(HostsFileLineParser.ToLine).ToList();
await File.WriteAllLinesAsync(tmpPath, lines, DnsmasqFileEncoding.Utf8NoBom, ct);
File.Move(tmpPath, path, overwrite: true);
_hostsCache.NotifyWeWroteManagedHosts(entries);
_logger.LogInformation(LogEvents.HostsWroteManagedFile, "Wrote managed hosts file: {Path}", path);
}
}