tdns/Tesses.Dns/Program.cs

59 lines
1.6 KiB
C#

using Tesses.WebServer;
using Newtonsoft.Json;
using DNS;
using LiteDB;
using DNS.Server;
using (var connection = new LiteDatabase("/data/dns.db"))
{
var entries=connection.GetCollection<DNSEntry>("entries");
StaticServer svr0=new StaticServer("/app/out/site");
MountableServer svr1=new MountableServer(svr0);
RouteServer svr=new RouteServer();
svr1.Mount("/api",svr);
svr.Add("/new",async(e)=>{
entries.Insert(new DNSEntry{TTL=300,Key="",Value="",Type="A"});
await e.SendTextAsync("HELO");
});
svr.Add("/save",async(e)=>{
var ent=await e.ReadJsonAsync<DNSEntry>();
entries.Update(ent);
await e.SendTextAsync("HELO");
},"POST");
svr.Add("/remove",async(e)=>{
string id_str;
if(e.QueryParams.TryGetFirst("id",out id_str))
{
int id;
if(int.TryParse(id_str,out id))
{
entries.Delete(id);
}
}
await e.SendTextAsync("HELO");
});
svr.Add("/entries.json",async(e)=>{
await e.SendJsonAsync(entries.FindAll().ToList());
});
using(DnsServer svr2=new DnsServer(new RequestResolver(entries),"8.8.8.8")){
Thread t2=new Thread(()=>{
svr2.Listen().Wait();
});
t2.Start();
using(var c = new CancellationTokenSource())
{
Console.CancelKeyPress+=(se,e)=>{
c.Cancel();
};
Tesses.WebServer.HttpServerListener svr00=new(new System.Net.IPEndPoint(0,9427),svr1);
svr00.ListenAsync(c.Token).Wait();
}
}
}