LogExporterApp: fixed multiple issues with the app. Code refactoring changes done.

This commit is contained in:
Shreyas Zare
2025-01-18 13:02:31 +05:30
parent c0ae6dbf35
commit 7bd0b7ca6b
10 changed files with 294 additions and 319 deletions

View File

@@ -1,4 +1,24 @@
using System;
/*
Technitium DNS Server
Copyright (C) 2025 Shreyas Zare (shreyas@technitium.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using DnsServerCore.ApplicationCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
@@ -11,16 +31,14 @@ namespace LogExporter
{
public class LogEntry
{
public DateTime Timestamp { get; set; }
public string ClientIp { get; set; }
public int ClientPort { get; set; }
public bool DnssecOk { get; set; }
public DnsTransportProtocol Protocol { get; set; }
public DnsResponseCode ResponseCode { get; set; }
public List<Question> Questions { get; set; }
public List<Answer> Answers { get; set; }
public object? RequestTag { get; set; }
public object? ResponseTag { get; set; }
public DateTime Timestamp { get; private set; }
public string ClientIp { get; private set; }
public DnsTransportProtocol Protocol { get; private set; }
public DnsServerResponseType ResponseType { get; private set; }
public double? ResponseRtt { get; private set; }
public DnsResponseCode ResponseCode { get; private set; }
public DnsQuestion? Question { get; private set; }
public List<DnsResourceRecord> Answers { get; private set; }
public LogEntry(DateTime timestamp, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram request, DnsDatagram response)
{
@@ -29,65 +47,57 @@ namespace LogExporter
// Extract client information
ClientIp = remoteEP.Address.ToString();
ClientPort = remoteEP.Port;
DnssecOk = request.DnssecOk;
Protocol = protocol;
ResponseType = response.Tag == null ? DnsServerResponseType.Recursive : (DnsServerResponseType)response.Tag;
if ((ResponseType == DnsServerResponseType.Recursive) && (response.Metadata is not null))
ResponseRtt = response.Metadata.RoundTripTime;
ResponseCode = response.RCODE;
// Extract request information
Questions = new List<Question>(request.Question.Count);
if (request.Question?.Count > 0)
if (request.Question.Count > 0)
{
Questions.AddRange(request.Question.Select(questionRecord => new Question
DnsQuestionRecord query = request.Question[0];
Question = new DnsQuestion
{
QuestionName = questionRecord.Name,
QuestionType = questionRecord.Type,
QuestionClass = questionRecord.Class,
Size = questionRecord.UncompressedLength,
}));
QuestionName = query.Name,
QuestionType = query.Type,
QuestionClass = query.Class,
};
}
// Convert answer section into a simple string summary (comma-separated for multiple answers)
Answers = new List<Answer>(response.Answer.Count);
if (response.Answer?.Count > 0)
Answers = new List<DnsResourceRecord>(response.Answer.Count);
if (response.Answer.Count > 0)
{
Answers.AddRange(response.Answer.Select(record => new Answer
Answers.AddRange(response.Answer.Select(record => new DnsResourceRecord
{
Name = record.Name,
RecordType = record.Type,
RecordData = record.RDATA.ToString(),
RecordClass = record.Class,
RecordTtl = record.TTL,
Size = record.UncompressedLength,
RecordData = record.RDATA.ToString(),
DnssecStatus = record.DnssecStatus,
}));
}
if (request.Tag != null)
{
RequestTag = request.Tag;
}
if (response.Tag != null)
{
ResponseTag = response.Tag;
}
}
public class Question
public class DnsQuestion
{
public string QuestionName { get; set; }
public DnsResourceRecordType? QuestionType { get; set; }
public DnsClass? QuestionClass { get; set; }
public int Size { get; set; }
public DnsResourceRecordType QuestionType { get; set; }
public DnsClass QuestionClass { get; set; }
}
public class Answer
public class DnsResourceRecord
{
public string Name { get; set; }
public DnsResourceRecordType RecordType { get; set; }
public string RecordData { get; set; }
public DnsClass RecordClass { get; set; }
public uint RecordTtl { get; set; }
public int Size { get; set; }
public string RecordData { get; set; }
public DnssecStatus DnssecStatus { get; set; }
}
@@ -101,7 +111,7 @@ namespace LogExporter
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var dts = reader.GetString();
string? dts = reader.GetString();
return dts == null ? DateTime.MinValue : DateTime.Parse(dts);
}