Initial commit

This commit is contained in:
Michael Nolan 2022-07-24 15:09:33 -05:00
commit 2020fbaf16
11 changed files with 482 additions and 0 deletions

133
.gitignore vendored Normal file
View File

@ -0,0 +1,133 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.sln.docstates
# Build results
[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Oo]bj/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.svclog
*.scc
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile
# Visual Studio profiler
*.psess
*.vsp
*.vspx
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# Click-Once directory
publish/
# Publish Web Output
*.Publish.xml
*.pubxml
*.azurePubxml
# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
packages/
## TODO: If the tool you use requires repositories.config, also uncomment the next line
!packages/repositories.config
# Windows Azure Build Output
csx/
*.build.csdef
# Windows Store app package directory
AppPackages/
# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
![Ss]tyle[Cc]op.targets
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.publishsettings
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
App_Data/*.mdf
App_Data/*.ldf
# =========================
# Windows detritus
# =========================
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Mac desktop service store files
.DS_Store
_NCrunch*

18
Dockerfile Normal file
View File

@ -0,0 +1,18 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
RUN git clone https://gitlab.tesses.net/tesses50/tdns.git .
WORKDIR /app/Tesses.Dns
# Restore as distinct layers
RUN dotnet restore
# Build and publish a release
RUN dotnet publish -c Release -o /app/out
RUN mkdir /app/out/site && cp -r /app/Site/* /app/out/site/
# Build runtime image
FROM mcr.microsoft.com/dotnet/runtime:6.0
WORKDIR /app
EXPOSE 53
EXPOSE 9427
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Tesses.Dns.dll","--docker"]

7
Site/css/bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

60
Site/index.html Normal file
View File

@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tesses DNS</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<h1>Tesses DNS</h1>
<template id="dns">
<div class="row">
<div class="col">
<input class="form-control" type="text" id="key" placeholder="Domain Name or Key">
</div>
<div class="col">
<select class="form-select" id="type">
<option selected value="A">A</option>
<option value="CNAME">CNAME</option>
</select>
</div>
<div class="col">
<input class="form-control" type="text" id="value" placeholder="IP, Domain Or Value">
</div>
<div class="col">
<input class="form-control" value="300" type="number" id="ttl" placeholder="TTL" min="0" max="1000000">
</div>
<div class="col">
<button type="button" id="save" class="btn btn-success">Save</button>
<button type="button" id="delete" class="btn btn-danger">Delete</button>
</div>
</div>
<br>
</template>
<div id="dns_items">
</div>
<button type="button" id="add" onclick="add()" class="btn btn-primary">Add</button>
<button type="button" id="save" onclick="saveAll()" class="btn btn-success">Save</button>
</div>
<script src="js/dns.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>

7
Site/js/bootstrap.min.js vendored Normal file

File diff suppressed because one or more lines are too long

92
Site/js/dns.js Normal file
View File

@ -0,0 +1,92 @@
var list=[];
const dns_template = document.getElementById('dns');
const dns_items = document.getElementById('dns_items');
function delete_item(item)
{
dns_items.removeChild(item.root);
for( var i = 0; i < arr.length; i++){
if ( list[i] === item) {
list.splice(i, 1);
break;
}
}
fetch(`api/remove?id=${item.id}`).then(e=>e.text()).then(e=>{
//removed entry
});
}
function save(item)
{
var data={
Id: item.id,
Key: item.key.value,
Value: item.key.value,
Type: item.type.value,
TTL: item.ttl.value,
};
fetch("api/save",{
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
referrer: "no-referrer",
body: JSON.stringify(data)
}).then(e=>e.text()).then(e=>{});
}
function saveAll()
{
list.forEach(e=>{
save(e);
});
}
function add()
{
fetch('api/new').then(e=e.text()).then(e=>{
load();
});
}
function load()
{
list=[];
dns_items.innerHTML="";
fetch("api/entries.json").then(e=>e.json()).this(e=>{
var dns_entry = dns_template.content.cloneNode(true);
var key = dns_entry.getElementById('key');
key.value = e.Key;
var value = dns_entry.getElementById('value');
value.value= e.Value;
var type = dns_entry.getElementById('type');
type.value = e.Type;
var ttl = dns_entry.getElementById('ttl');
ttl.value = e.TTL;
var saveBtn = dns_entry.getElementById('save');
var deleteBtn = dns_entry.getElementById('delete');
var item={
id: e.Id,
root: dns_entry,
key: key,
value: value,
type: type,
ttl: ttl,
saveBtn: saveBtn,
deleteBtn: deleteBtn
};
list.push(item);
saveBtn.onclick += (sender,e)=>{
save(item);
};
deleteBtn.onclick += (sender,e)=>{
delete_item(item);
};
dns_items.appendChild(dns_entry);
});
}
load();

46
Tesses.Dns/Entry.cs Normal file
View File

@ -0,0 +1,46 @@
using System.Net;
using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
public class DNSEntry
{
public DNSEntry()
{
Key="";
Value="";
Type="A";
}
public int Id {get;set;}
public string Key {get;set;}
public string Value {get;set;}
public string Type {get;set;}
public int TTL {get;set;}
public static IResourceRecord? GetDNSEntry(IEnumerable<DNSEntry> entry,Question q)
{
foreach(var item in entry)
{
if(q.Type == RecordType.A && item.Type=="A")
{
if(item.Key == q.Name.ToString())
{
IResourceRecord record = new IPAddressResourceRecord(
q.Name, IPAddress.Parse(item.Value),TimeSpan.FromSeconds(item.TTL));
return record;
}
}
if(q.Type == RecordType.CNAME && item.Type=="CNAME")
{
if(item.Key == q.Name.ToString())
{
IResourceRecord record = new CanonicalNameResourceRecord(
q.Name,new Domain(item.Value),TimeSpan.FromSeconds(item.TTL));
return record;
}
}
}
return null;
}
}

59
Tesses.Dns/Program.cs Normal file
View File

@ -0,0 +1,59 @@
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();
}
}
}

31
Tesses.Dns/Server.cs Normal file
View File

@ -0,0 +1,31 @@
using System.Net;
using DNS.Client.RequestResolver;
using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
public class RequestResolver : IRequestResolver {
public RequestResolver(LiteDB.ILiteCollection<DNSEntry> db)
{
_db=db;
}
LiteDB.ILiteCollection<DNSEntry> _db;
public Task<IResponse> Resolve(IRequest request,CancellationToken token) {
IResponse response = Response.FromRequest(request);
foreach (Question question in response.Questions) {
var res= DNSEntry.GetDNSEntry(_db.FindAll(),question);
if(res != null)
{
response.AnswerRecords.Add(res);
}
}
return Task.FromResult(response);
}
}
// All dns requests received will be handled by the localhost request resolver

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dns" Version="7.0.0" />
<PackageReference Include="LiteDb" Version="5.0.12" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Tesses.WebServer" Version="1.0.3.5" />
</ItemGroup>
</Project>

12
docker-compose.yaml Normal file
View File

@ -0,0 +1,12 @@
version: '2'
services:
tdns:
build: Dockerfile
image: tdns
ports:
- 9427:9427
- 53:53
volumes:
- tdns-data:/data
volumes:
tdns-data: