Listen on HTTP port and PowerDns socket

This commit is contained in:
Jöran Malek 2025-01-27 00:02:07 +01:00
parent dd914cc0c7
commit 2a9d7f46ed
3 changed files with 17 additions and 34 deletions

View file

@ -1,15 +1,10 @@
using System.Net;
using System.Net.Sockets;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using pdns_dhcp.Connections;
using pdns_dhcp.Dhcp;
using pdns_dhcp.Dns;
using pdns_dhcp.Kea;
@ -32,32 +27,6 @@ builder.Services.AddSingleton<DnsRepository>();
builder.Services.AddDhcpWatcherFactory();
builder.Services.AddKeaFactory();
builder.Services.Configure<SocketTransportOptions>(options =>
{
options.CreateBoundListenSocket = endpoint =>
{
Socket socket;
switch (endpoint)
{
case ProtocolSocketIPEndPoint socketIp:
socket = new Socket(socketIp.AddressFamily, socketIp.SocketType, socketIp.ProtocolType);
if (socketIp.Address.Equals(IPAddress.IPv6Any))
{
socket.DualMode = true;
}
break;
default:
return SocketTransportOptions.CreateDefaultBoundListenSocket(endpoint);
}
socket.Bind(endpoint);
return socket;
};
});
builder.WebHost.ConfigureKestrel((context, options) =>
{
bool isSystemd = false;
@ -66,14 +35,14 @@ builder.WebHost.ConfigureKestrel((context, options) =>
isSystemd = true;
options.UseConnectionHandler<PowerDnsHandler>();
});
if (!isSystemd && context.Configuration.GetRequiredSection("PowerDns:Listener").Get<PowerDnsListenerOptions>() is { } pdnsOptions)
{
var path = PathEx.ExpandPath(pdnsOptions.Socket);
FileInfo file = new(path);
file.Directory!.Create();
file.Delete();
options.ListenUnixSocket(path, options =>
options.ConfigurationLoader!.UnixSocketEndpoint(path, options =>
{
options.UseConnectionHandler<PowerDnsHandler>();
});

View file

@ -13,5 +13,12 @@
"Listener": {
"Socket": "${XDG_RUNTIME_DIR}/pdns-dhcp/pdns.sock"
}
},
"Kestrel": {
"Endpoints": {
"Http":{
"Url": "http://[::1]:5000"
}
}
}
}

View file

@ -14,5 +14,12 @@
"Listener": {
"Socket": "/run/pdns-dhcp/pdns.sock"
}
},
"Kestrel": {
"Endpoints": {
"Http":{
"Url": "http://[::]:80"
}
}
}
}