mirror of
https://github.com/fergalmoran/DnsServer.git
synced 2026-02-09 01:13:58 +00:00
Simplified config
This commit is contained in:
@@ -104,6 +104,7 @@ namespace LogExporter
|
||||
{
|
||||
_dnsServer = dnsServer;
|
||||
_config = BufferManagementConfig.Deserialize(config);
|
||||
|
||||
if (_config == null)
|
||||
{
|
||||
throw new DnsClientException("Invalid application configuration.");
|
||||
@@ -160,7 +161,7 @@ namespace LogExporter
|
||||
{
|
||||
var logs = new List<LogEntry>(BULK_INSERT_COUNT);
|
||||
|
||||
while (true)
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
while ((logs.Count < BULK_INSERT_COUNT) && _logBuffer.TryTake(out LogEntry? log))
|
||||
{
|
||||
@@ -184,30 +185,22 @@ namespace LogExporter
|
||||
|
||||
private void RegisterExportTargets()
|
||||
{
|
||||
|
||||
var fileTarget = _config.FileTarget;
|
||||
if (fileTarget != null && fileTarget.Enabled)
|
||||
// Helper function to register an export strategy if the target is enabled
|
||||
void RegisterIfEnabled<TTarget, TStrategy>(TTarget target, Func<TTarget, TStrategy> strategyFactory)
|
||||
where TTarget : TargetBase
|
||||
where TStrategy : IExportStrategy
|
||||
{
|
||||
var strategy = new FileExportStrategy(fileTarget.Path);
|
||||
_exportManager.RegisterStrategy(strategy);
|
||||
|
||||
if (target?.Enabled == true)
|
||||
{
|
||||
var strategy = strategyFactory(target);
|
||||
_exportManager.AddOrReplaceStrategy(strategy);
|
||||
}
|
||||
}
|
||||
|
||||
var httpTarget = _config.HttpTarget;
|
||||
if (httpTarget != null && httpTarget.Enabled)
|
||||
{
|
||||
var strategy = new HttpExportStrategy(httpTarget.Endpoint, httpTarget.Method, httpTarget.Headers);
|
||||
_exportManager.RegisterStrategy(strategy);
|
||||
|
||||
}
|
||||
|
||||
var syslogTarget = _config.SyslogTarget;
|
||||
if (syslogTarget != null && syslogTarget.Enabled)
|
||||
{
|
||||
var strategy = new SyslogExportStrategy(syslogTarget.Address, syslogTarget.Port, syslogTarget.Protocol);
|
||||
_exportManager.RegisterStrategy(strategy);
|
||||
|
||||
}
|
||||
// Register the different strategies using the helper
|
||||
RegisterIfEnabled(_config.FileTarget, target => new FileExportStrategy(target.Path));
|
||||
RegisterIfEnabled(_config.HttpTarget, target => new HttpExportStrategy(target.Endpoint, target.Method, target.Headers));
|
||||
RegisterIfEnabled(_config.SyslogTarget, target => new SyslogExportStrategy(target.Address, target.Port, target.Protocol));
|
||||
}
|
||||
|
||||
#endregion private
|
||||
|
||||
@@ -43,11 +43,15 @@ namespace LogExporter
|
||||
return JsonSerializer.Deserialize<BufferManagementConfig>(json);
|
||||
}
|
||||
}
|
||||
public class SyslogTarget
|
||||
|
||||
public class TargetBase
|
||||
{
|
||||
[JsonPropertyName("enabled")]
|
||||
public bool Enabled { get; set; }
|
||||
}
|
||||
|
||||
public class SyslogTarget : TargetBase
|
||||
{
|
||||
[JsonPropertyName("address")]
|
||||
public string Address { get; set; }
|
||||
|
||||
@@ -58,20 +62,14 @@ namespace LogExporter
|
||||
public string? Protocol { get; set; }
|
||||
}
|
||||
|
||||
public class FileTarget
|
||||
public class FileTarget : TargetBase
|
||||
{
|
||||
[JsonPropertyName("enabled")]
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
[JsonPropertyName("path")]
|
||||
public string Path { get; set; }
|
||||
}
|
||||
|
||||
public class HttpTarget
|
||||
public class HttpTarget : TargetBase
|
||||
{
|
||||
[JsonPropertyName("enabled")]
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
[JsonPropertyName("endpoint")]
|
||||
public string Endpoint { get; set; }
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -27,7 +28,7 @@ namespace LogExporter.Strategy
|
||||
{
|
||||
#region variables
|
||||
|
||||
private readonly List<IExportStrategy> _exportStrategies;
|
||||
private readonly Dictionary<Type, IExportStrategy> _exportStrategies;
|
||||
|
||||
#endregion variables
|
||||
|
||||
@@ -35,24 +36,30 @@ namespace LogExporter.Strategy
|
||||
|
||||
public ExportManager()
|
||||
{
|
||||
_exportStrategies = new List<IExportStrategy>();
|
||||
_exportStrategies = new Dictionary<Type, IExportStrategy>();
|
||||
}
|
||||
|
||||
#endregion constructor
|
||||
|
||||
#region public
|
||||
|
||||
public IExportStrategy? GetStrategy<T>() where T : IExportStrategy
|
||||
{
|
||||
_exportStrategies.TryGetValue(typeof(T), out var strategy);
|
||||
return strategy;
|
||||
}
|
||||
|
||||
public async Task ImplementStrategyForAsync(List<LogEntry> logs, CancellationToken cancellationToken = default)
|
||||
{
|
||||
foreach (var strategy in _exportStrategies)
|
||||
foreach (var strategy in _exportStrategies.Values)
|
||||
{
|
||||
await strategy.ExportLogsAsync(logs, cancellationToken).ConfigureAwait(false);
|
||||
await strategy.ExportLogsAsync(logs, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
public void RegisterStrategy(IExportStrategy strategy)
|
||||
public void AddOrReplaceStrategy(IExportStrategy strategy)
|
||||
{
|
||||
_exportStrategies.Add(strategy);
|
||||
_exportStrategies[strategy.GetType()] = strategy;
|
||||
}
|
||||
|
||||
#endregion public
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace LogExporter.Strategy
|
||||
port ??= DEFAULT_PORT;
|
||||
protocol ??= DEFAUL_PROTOCOL;
|
||||
|
||||
_sender = protocol switch
|
||||
_sender = protocol.ToLowerInvariant() switch
|
||||
{
|
||||
"tls" => new SyslogEncryptedTcpSender(address, port.Value),
|
||||
"tcp" => new SyslogTcpSender(address, port.Value),
|
||||
|
||||
@@ -1,26 +1,21 @@
|
||||
{
|
||||
"maxLogEntries": 10000,
|
||||
"targets": [
|
||||
{
|
||||
"type": "file",
|
||||
"enabled": true,
|
||||
"path": "/var/log/dns_logs.json"
|
||||
},
|
||||
{
|
||||
"type": "http",
|
||||
"enabled": false,
|
||||
"endpoint": "http://example.com/logs",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Authorization": "Bearer abc123"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "syslog",
|
||||
"enabled": false,
|
||||
"address": "127.0.0.1",
|
||||
"port": 514,
|
||||
"protocol": "udp"
|
||||
}
|
||||
]
|
||||
}
|
||||
"maxLogEntries": 1000,
|
||||
"file": {
|
||||
"path": "/var/log/dns_logs.json",
|
||||
"enabled": true
|
||||
},
|
||||
"http": {
|
||||
"endpoint": "http://example.com/logs",
|
||||
"method": "POST",
|
||||
"headers": {
|
||||
"Authorization": "Bearer abc123"
|
||||
},
|
||||
"enabled": true
|
||||
},
|
||||
"syslog": {
|
||||
"address": "127.0.0.1",
|
||||
"port": 514,
|
||||
"protocol": "UDP",
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user