PowerDns backend
This commit is contained in:
parent
0087d2a2cf
commit
11a5549818
7 changed files with 76 additions and 38 deletions
|
|
@ -7,6 +7,4 @@ namespace pdns_dhcp.PowerDns;
|
||||||
public interface IPowerDnsFactory : IRequiresFullProxy
|
public interface IPowerDnsFactory : IRequiresFullProxy
|
||||||
{
|
{
|
||||||
PowerDnsStreamClient CreateClient(Stream stream);
|
PowerDnsStreamClient CreateClient(Stream stream);
|
||||||
|
|
||||||
PowerDnsUnixSocket CreateUnixSocket(string socketPath);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
30
src/pdns-dhcp/PowerDns/Methods.cs
Normal file
30
src/pdns-dhcp/PowerDns/Methods.cs
Normal 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";
|
||||||
|
}
|
||||||
|
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +1,14 @@
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
using Stl.Async;
|
||||||
|
|
||||||
namespace pdns_dhcp.PowerDns;
|
namespace pdns_dhcp.PowerDns;
|
||||||
|
|
||||||
public class PowerDnsStreamClient : IDisposable
|
public class PowerDnsStreamClient : IDisposable
|
||||||
{
|
{
|
||||||
private readonly Stream _stream;
|
private readonly Stream _stream;
|
||||||
|
private CancellationTokenSource _cts = new();
|
||||||
|
private Task _task;
|
||||||
|
|
||||||
public PowerDnsStreamClient(Stream stream)
|
public PowerDnsStreamClient(Stream stream)
|
||||||
{
|
{
|
||||||
|
|
@ -11,17 +17,29 @@ public class PowerDnsStreamClient : IDisposable
|
||||||
|
|
||||||
~PowerDnsStreamClient()
|
~PowerDnsStreamClient()
|
||||||
{
|
{
|
||||||
Dispose(false);
|
Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
Dispose(true);
|
using (_cts)
|
||||||
|
using (_stream)
|
||||||
|
{
|
||||||
|
_cts.Cancel();
|
||||||
|
_task.GetAwaiter().GetResult();
|
||||||
|
}
|
||||||
GC.SuppressFinalize(this);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/pdns-dhcp/PowerDns/Replies.cs
Normal file
10
src/pdns-dhcp/PowerDns/Replies.cs
Normal 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);
|
||||||
|
|
@ -3,7 +3,6 @@ using System.Collections.Immutable;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
using pdns_dhcp.Kea;
|
|
||||||
using pdns_dhcp.Options;
|
using pdns_dhcp.Options;
|
||||||
|
|
||||||
namespace pdns_dhcp.Services;
|
namespace pdns_dhcp.Services;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
|
@ -8,15 +10,23 @@ namespace pdns_dhcp.Services;
|
||||||
|
|
||||||
public class PowerDnsBackend : BackgroundService
|
public class PowerDnsBackend : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly PowerDnsOptions _options;
|
private readonly IPowerDnsFactory _factory;
|
||||||
|
private readonly Socket _socket;
|
||||||
|
|
||||||
public PowerDnsBackend(IOptions<PowerDnsOptions> options, IPowerDnsFactory socketFactory)
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue