Skip to content

Commit

Permalink
v3.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
msasanmh committed May 7, 2024
1 parent a760e19 commit 29b28a0
Show file tree
Hide file tree
Showing 49 changed files with 1,082 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<byte[]> GetResponseAsync()
};

Uri uri = uriBuilder.Uri;

HttpRequest hr = new()
{
CT = CT,
Expand All @@ -65,14 +65,14 @@ public async Task<byte[]> GetResponseAsync()
ProxyPass = ProxyPass,
};
hr.Headers.Add("host", Reader.Host); // In Case Of Using Bootstrap

HttpRequestResponse hrr = await HttpRequest.SendAsync(hr).ConfigureAwait(false);
result = hrr.Data;
}
catch (Exception) { }
});
try { await task.WaitAsync(TimeSpan.FromMilliseconds(TimeoutMS), CT).ConfigureAwait(false); } catch (Exception) { }

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public enum ResponseCode : ushort // 4 Bits
/// </summary>
public enum RRType : ushort
{
Unknown = 0,
/// <summary>
/// A host address
/// </summary>
Expand Down Expand Up @@ -412,12 +413,233 @@ public enum RRType : ushort
DLV = 32769
}

public static RRType ParseRRType(ushort n)
{
return n switch
{
1 => RRType.A,
2 => RRType.NS,
3 => RRType.MD,
4 => RRType.MF,
5 => RRType.CNAME,
6 => RRType.SOA,
7 => RRType.MB,
8 => RRType.MG,
9 => RRType.MR,
10 => RRType.NULL,
11 => RRType.WKS,
12 => RRType.PTR,
13 => RRType.HINFO,
14 => RRType.MINFO,
15 => RRType.MX,
16 => RRType.TEXT,
17 => RRType.RP,
18 => RRType.AFSDB,
19 => RRType.X25,
20 => RRType.ISDN,
21 => RRType.RT,
22 => RRType.NSAP,
23 => RRType.NSAPPTR,
24 => RRType.SIG,
25 => RRType.KEY,
26 => RRType.PX,
27 => RRType.GPOS,
28 => RRType.AAAA,
29 => RRType.LOC,
30 => RRType.NXT,
31 => RRType.EID,
32 => RRType.NIMLOC,
33 => RRType.SRV,
34 => RRType.ATMA,
35 => RRType.NAPTR,
36 => RRType.KX,
37 => RRType.CERT,
38 => RRType.A6,
39 => RRType.DNAME,
40 => RRType.SINK,
41 => RRType.OPT,
42 => RRType.APL,
43 => RRType.DS,
44 => RRType.SSHFP,
45 => RRType.IPSECKEY,
46 => RRType.RRSIG,
47 => RRType.NSEC,
48 => RRType.DNSKEY,
49 => RRType.DHCID,
50 => RRType.NSEC3,
51 => RRType.NSEC3PARAM,
52 => RRType.TLSA,
53 => RRType.SMIMEA,
55 => RRType.HIP,
56 => RRType.NINFO,
57 => RRType.RKEY,
58 => RRType.TALINK,
59 => RRType.CDS,
60 => RRType.CDNSKEY,
61 => RRType.OPENPGPKEY,
62 => RRType.CSYNC,
63 => RRType.ZONEMD,
64 => RRType.SVCB,
65 => RRType.HTTPS,
99 => RRType.SPF,
100 => RRType.UINFO,
101 => RRType.UID,
102 => RRType.GID,
103 => RRType.UNSPEC,
104 => RRType.NID,
105 => RRType.L32,
106 => RRType.L64,
107 => RRType.LP,
108 => RRType.EUI48,
109 => RRType.EUI64,
249 => RRType.TKEY,
250 => RRType.TSIG,
251 => RRType.IXFR,
252 => RRType.AXFR,
253 => RRType.MAILB,
254 => RRType.MAILA,
255 => RRType.ANY,
256 => RRType.URI,
257 => RRType.CAA,
258 => RRType.AVC,
259 => RRType.DOA,
260 => RRType.AMTRELAY,
32768 => RRType.TA,
32769 => RRType.DLV,
_ => RRType.Unknown
};
}

public static RRType ParseRRType(string rrType)
{
rrType = rrType.ToUpper();
return rrType switch
{
nameof(RRType.A) => RRType.A,
nameof(RRType.NS) => RRType.NS,
nameof(RRType.MD) => RRType.MD,
nameof(RRType.MF) => RRType.MF,
nameof(RRType.CNAME) => RRType.CNAME,
nameof(RRType.SOA) => RRType.SOA,
nameof(RRType.MB) => RRType.MB,
nameof(RRType.MG) => RRType.MG,
nameof(RRType.MR) => RRType.MR,
nameof(RRType.NULL) => RRType.NULL,
nameof(RRType.WKS) => RRType.WKS,
nameof(RRType.PTR) => RRType.PTR,
nameof(RRType.HINFO) => RRType.HINFO,
nameof(RRType.MINFO) => RRType.MINFO,
nameof(RRType.MX) => RRType.MX,
nameof(RRType.TEXT) => RRType.TEXT,
nameof(RRType.RP) => RRType.RP,
nameof(RRType.AFSDB) => RRType.AFSDB,
nameof(RRType.X25) => RRType.X25,
nameof(RRType.ISDN) => RRType.ISDN,
nameof(RRType.RT) => RRType.RT,
nameof(RRType.NSAP) => RRType.NSAP,
nameof(RRType.NSAPPTR) => RRType.NSAPPTR,
nameof(RRType.SIG) => RRType.SIG,
nameof(RRType.KEY) => RRType.KEY,
nameof(RRType.PX) => RRType.PX,
nameof(RRType.GPOS) => RRType.GPOS,
nameof(RRType.AAAA) => RRType.AAAA,
nameof(RRType.LOC) => RRType.LOC,
nameof(RRType.NXT) => RRType.NXT,
nameof(RRType.EID) => RRType.EID,
nameof(RRType.NIMLOC) => RRType.NIMLOC,
nameof(RRType.SRV) => RRType.SRV,
nameof(RRType.ATMA) => RRType.ATMA,
nameof(RRType.NAPTR) => RRType.NAPTR,
nameof(RRType.KX) => RRType.KX,
nameof(RRType.CERT) => RRType.CERT,
nameof(RRType.A6) => RRType.A6,
nameof(RRType.DNAME) => RRType.DNAME,
nameof(RRType.SINK) => RRType.SINK,
nameof(RRType.OPT) => RRType.OPT,
nameof(RRType.APL) => RRType.APL,
nameof(RRType.DS) => RRType.DS,
nameof(RRType.SSHFP) => RRType.SSHFP,
nameof(RRType.IPSECKEY) => RRType.IPSECKEY,
nameof(RRType.RRSIG) => RRType.RRSIG,
nameof(RRType.NSEC) => RRType.NSEC,
nameof(RRType.DNSKEY) => RRType.DNSKEY,
nameof(RRType.DHCID) => RRType.DHCID,
nameof(RRType.NSEC3) => RRType.NSEC3,
nameof(RRType.NSEC3PARAM) => RRType.NSEC3PARAM,
nameof(RRType.TLSA) => RRType.TLSA,
nameof(RRType.SMIMEA) => RRType.SMIMEA,
nameof(RRType.HIP) => RRType.HIP,
nameof(RRType.NINFO) => RRType.NINFO,
nameof(RRType.RKEY) => RRType.RKEY,
nameof(RRType.TALINK) => RRType.TALINK,
nameof(RRType.CDS) => RRType.CDS,
nameof(RRType.CDNSKEY) => RRType.CDNSKEY,
nameof(RRType.OPENPGPKEY) => RRType.OPENPGPKEY,
nameof(RRType.CSYNC) => RRType.CSYNC,
nameof(RRType.ZONEMD) => RRType.ZONEMD,
nameof(RRType.SVCB) => RRType.SVCB,
nameof(RRType.HTTPS) => RRType.HTTPS,
nameof(RRType.SPF) => RRType.SPF,
nameof(RRType.UINFO) => RRType.UINFO,
nameof(RRType.UID) => RRType.UID,
nameof(RRType.GID) => RRType.GID,
nameof(RRType.UNSPEC) => RRType.UNSPEC,
nameof(RRType.NID) => RRType.NID,
nameof(RRType.L32) => RRType.L32,
nameof(RRType.L64) => RRType.L64,
nameof(RRType.LP) => RRType.LP,
nameof(RRType.EUI48) => RRType.EUI48,
nameof(RRType.EUI64) => RRType.EUI64,
nameof(RRType.TKEY) => RRType.TKEY,
nameof(RRType.TSIG) => RRType.TSIG,
nameof(RRType.IXFR) => RRType.IXFR,
nameof(RRType.AXFR) => RRType.AXFR,
nameof(RRType.MAILB) => RRType.MAILB,
nameof(RRType.MAILA) => RRType.MAILA,
nameof(RRType.ANY) => RRType.ANY,
nameof(RRType.URI) => RRType.URI,
nameof(RRType.CAA) => RRType.CAA,
nameof(RRType.AVC) => RRType.AVC,
nameof(RRType.DOA) => RRType.DOA,
nameof(RRType.AMTRELAY) => RRType.AMTRELAY,
nameof(RRType.TA) => RRType.TA,
nameof(RRType.DLV) => RRType.DLV,
_ => RRType.Unknown
};
}

public enum CLASS : ushort
{
Unknown = 0,
IN = 1, // The Internet system
CS = 2, // The CSNET class (Obsolete - used only for examples in some obsolete RFCs)
CH = 3, // The Chaos system
HS = 4, // Hesiod [Dyer 87]
}

public static CLASS ParseClass(ushort n)
{
return n switch
{
1 => CLASS.IN,
2 => CLASS.CS,
3 => CLASS.CH,
4 => CLASS.HS,
_ => CLASS.Unknown
};
}

public static CLASS ParseClass(string qClass)
{
qClass = qClass.ToUpper();
return qClass switch
{
nameof(CLASS.IN) => CLASS.IN,
nameof(CLASS.CS) => CLASS.CS,
nameof(CLASS.CH) => CLASS.CH,
nameof(CLASS.HS) => CLASS.HS,
_ => CLASS.Unknown
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override string ToString()
{
result += Additionals.ToString();
}
return result;
return result.Trim();
}

public static DnsMessage Read(byte[] buffer, DnsEnums.DnsProtocol dnsProtocol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ public static Questions Read(byte[] buffer, ref int pos, DnsMessage dnsMessage)

if (!qTypeBool || !qClassBool) return questions;

DnsEnums.RRType typeEnum = (DnsEnums.RRType)Enum.Parse(typeof(DnsEnums.RRType), qType.ToString());
DnsEnums.CLASS classEnum = (DnsEnums.CLASS)Enum.Parse(typeof(DnsEnums.CLASS), qClass.ToString());
DnsEnums.RRType typeEnum = DnsEnums.ParseRRType(qType);
DnsEnums.CLASS classEnum = DnsEnums.ParseClass(qClass);

Question question = new(domain, qNamePosition, typeEnum, classEnum);
questions.QuestionRecords.Add(question);
questions.IsSuccess = true;
if (!typeEnum.Equals(DnsEnums.RRType.Unknown) && !classEnum.Equals(DnsEnums.CLASS.Unknown))
{
Question question = new(domain, qNamePosition, typeEnum, classEnum);
questions.QuestionRecords.Add(question);
questions.IsSuccess = true;
}
}

return questions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,17 @@ public static List<IResourceRecord> Read(byte[] buffer, ref int pos, int resourc
if (currentPos + 2 > buffer.Length) return resourceRecords;
bool rrTypeBool = ByteArrayTool.TryConvertBytesToUInt16(buffer[currentPos..(currentPos + 2)], out ushort rrType);
if (!rrTypeBool) return resourceRecords;
resourceRecord.TYPE = (DnsEnums.RRType)Enum.Parse(typeof(DnsEnums.RRType), rrType.ToString());
resourceRecord.TYPE = DnsEnums.ParseRRType(rrType);
if (resourceRecord.TYPE.Equals(DnsEnums.RRType.Unknown)) return resourceRecords;
currentPos += 2;
pos += 2;

// CLASS
if (currentPos + 2 > buffer.Length) return resourceRecords;
bool rClassBool = ByteArrayTool.TryConvertBytesToUInt16(buffer[currentPos..(currentPos + 2)], out ushort rClass);
if (!rClassBool) return resourceRecords;
resourceRecord.CLASS = (DnsEnums.CLASS)Enum.Parse(typeof(DnsEnums.CLASS), rClass.ToString());
resourceRecord.CLASS = DnsEnums.ParseClass(rClass);
if (resourceRecord.CLASS.Equals(DnsEnums.CLASS.Unknown)) return resourceRecords;
currentPos += 2;
pos += 2;

Expand All @@ -169,8 +171,8 @@ public static List<IResourceRecord> Read(byte[] buffer, ref int pos, int resourc
int rdLengthInt = Convert.ToInt32(rdLength);
currentPos += rdLengthInt;
pos += rdLengthInt;

if (pos <= buffer.Length && rdLengthInt != 0 && resourceRecord.TimeToLive != 0)
if (pos <= buffer.Length && rdLengthInt != 0)
{
resourceRecord.RecordBuffer = buffer[recordStartPos..pos];
resourceRecords.Add(resourceRecord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static async Task Process(AgnosticResult aResult, AgnosticProgram.DnsRule
if (typeQ == DnsEnums.RRType.AAAA)
{
dmR = DnsMessage.CreateResponse(dmQ, 1, 0, 0);
dmR.Answers.AnswerRecords.Clear();
dmR.Answers.AnswerRecords.Add(new AaaaRecord(addressQ, 60, dnsIp));

bool isTryWriteSuccess = DnsMessage.TryWrite(dmR, out byte[] aBuffer);
Expand All @@ -118,6 +119,7 @@ public static async Task Process(AgnosticResult aResult, AgnosticProgram.DnsRule
if (typeQ == DnsEnums.RRType.A)
{
dmR = DnsMessage.CreateResponse(dmQ, 1, 0, 0);
dmR.Answers.AnswerRecords.Clear();
dmR.Answers.AnswerRecords.Add(new ARecord(addressQ, 60, dnsIp));

bool isTryWriteSuccess = DnsMessage.TryWrite(dmR, out byte[] aBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void Start(AgnosticSettings settings)
private void Welcome()
{
// Event
string msgEvent = $"Server Starting On Port:{Settings_.ListenerPort}";
string msgEvent = $"Server Starting On Port: {Settings_.ListenerPort}";
OnRequestReceived?.Invoke(msgEvent, EventArgs.Empty);
OnDebugInfoReceived?.Invoke(msgEvent, EventArgs.Empty);
}
Expand Down Expand Up @@ -262,7 +262,14 @@ private async void AcceptConnections()

// EndPoint
IPEndPoint ipEndPoint = Settings_.ServerEndPoint;


// Make Port Free
if (OperatingSystem.IsWindows())
{
List<int> pids = ProcessManager.GetProcessPidsByUsingPort(Settings_.ListenerPort);
foreach (int pid in pids) ProcessManager.KillProcessByPID(pid);
}

// UDP
UdpSocket_ = new(ipEndPoint.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
if (ipEndPoint.Address.Equals(IPAddress.IPv6Any))
Expand Down Expand Up @@ -406,6 +413,7 @@ async void tcp(TcpListener tcpListener)
Debug.WriteLine(msgEventErr);
OnRequestReceived?.Invoke(msgEventErr, EventArgs.Empty);
TcpListener_?.Stop();
Stop();
}
}
}
Expand Down
Loading

0 comments on commit 29b28a0

Please sign in to comment.