PowerDns backend

This commit is contained in:
Jöran Malek 2023-12-12 00:59:57 +01:00
parent 0087d2a2cf
commit 11a5549818
7 changed files with 76 additions and 38 deletions

View file

@ -7,6 +7,4 @@ namespace pdns_dhcp.PowerDns;
public interface IPowerDnsFactory : IRequiresFullProxy
{
PowerDnsStreamClient CreateClient(Stream stream);
PowerDnsUnixSocket CreateUnixSocket(string socketPath);
}

View file

@ -0,0 +1,30 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace pdns_dhcp.PowerDns;
public interface IMethod
{
public abstract static string Method { get; }
}
public abstract class Method<TSelf> where TSelf : Method<TSelf>, IMethod
{
[JsonExtensionData]
public Dictionary<string, JsonElement> ExtensionData { get; } = [];
}
public abstract class Method<TSelf, TParam>(TParam parameters) : Method<TSelf> where TSelf : Method<TSelf, TParam>, IMethod
{
public TParam Parameters => parameters;
}
public class InitializeMethod : Method<InitializeMethod>, IMethod
{
public static string Method => "Initialize";
}
public class LookupMethod : Method<LookupMethod>, IMethod
{
public static string Method => "Lookup";
}

View file

@ -1,27 +0,0 @@
using System.Net.Sockets;
using Microsoft.Extensions.Hosting;
namespace pdns_dhcp.PowerDns;
public class PowerDnsUnixSocket : BackgroundService
{
private readonly IPowerDnsFactory _factory;
private readonly Socket _socket;
public PowerDnsUnixSocket(string socketPath, IPowerDnsFactory factory)
{
_factory = factory;
_socket = new(SocketType.Stream, ProtocolType.Unspecified);
_socket.Bind(new UnixDomainSocketEndPoint(socketPath));
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_socket.Listen();
while (await _socket.AcceptAsync(stoppingToken) is { } client)
{
var instance = _factory.CreateClient(new NetworkStream(client, true));
}
}
}

View file

@ -1,8 +1,14 @@
using System.Text.Json;
using Stl.Async;
namespace pdns_dhcp.PowerDns;
public class PowerDnsStreamClient : IDisposable
{
private readonly Stream _stream;
private CancellationTokenSource _cts = new();
private Task _task;
public PowerDnsStreamClient(Stream stream)
{
@ -11,17 +17,29 @@ public class PowerDnsStreamClient : IDisposable
~PowerDnsStreamClient()
{
Dispose(false);
Dispose();
}
public void Dispose()
{
Dispose(true);
using (_cts)
using (_stream)
{
_cts.Cancel();
_task.GetAwaiter().GetResult();
}
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
public void Start(CancellationToken stoppingToken)
{
_stream.Dispose();
using var other = Interlocked.Exchange(ref _cts, CancellationTokenSource.CreateLinkedTokenSource(stoppingToken));
_task = Run(_cts.Token);
other.Cancel();
}
private Task Run(CancellationToken stoppingToken)
{
return Task.CompletedTask;
}
}

View file

@ -0,0 +1,10 @@
namespace pdns_dhcp.PowerDns;
public abstract class Reply;
public abstract class Reply<T>(T result) : Reply
{
public T Result => result;
}
public class BoolReply(bool result) : Reply<bool>(result);

View file

@ -3,7 +3,6 @@ using System.Collections.Immutable;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using pdns_dhcp.Kea;
using pdns_dhcp.Options;
namespace pdns_dhcp.Services;

View file

@ -1,3 +1,5 @@
using System.Net.Sockets;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
@ -8,15 +10,23 @@ namespace pdns_dhcp.Services;
public class PowerDnsBackend : BackgroundService
{
private readonly PowerDnsOptions _options;
private readonly IPowerDnsFactory _factory;
private readonly Socket _socket;
public PowerDnsBackend(IOptions<PowerDnsOptions> options, IPowerDnsFactory socketFactory)
{
_options = options.Value;
_factory = socketFactory;
_socket = new(SocketType.Stream, ProtocolType.Unknown);
_socket.Bind(new UnixDomainSocketEndPoint(options.Value.Listener.Socket));
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
return Task.CompletedTask;
_socket.Listen();
while (await _socket.AcceptAsync(stoppingToken) is { } client)
{
_factory.CreateClient(new NetworkStream(client, true))
.Start(stoppingToken);
}
}
}