|
@@ -60,32 +60,45 @@ namespace DnsForwarder
|
|
|
|
|
|
private static void Callback(IAsyncResult ar)
|
|
private static void Callback(IAsyncResult ar)
|
|
{
|
|
{
|
|
|
|
+ var flagNextStarted = false;
|
|
EndPoint from = new IPEndPoint(IPAddress.Any, 0);
|
|
EndPoint from = new IPEndPoint(IPAddress.Any, 0);
|
|
- var count = _listener.EndReceiveFrom(ar, ref from);
|
|
|
|
- var buf = (byte[])ar.AsyncState;
|
|
|
|
|
|
+ string domain = null;
|
|
|
|
+ string target = null;
|
|
|
|
|
|
- StartHandleRequest();
|
|
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ var count = _listener.EndReceiveFrom(ar, ref from);
|
|
|
|
|
|
- var domain = ExtractDomainName(buf, count);
|
|
|
|
|
|
+ StartHandleRequest();
|
|
|
|
+ flagNextStarted = true;
|
|
|
|
|
|
- var target = MatchServer(domain);
|
|
|
|
|
|
+ var buf = (byte[])ar.AsyncState;
|
|
|
|
+
|
|
|
|
+ domain = ExtractDomainName(buf);
|
|
|
|
+
|
|
|
|
+ target = MatchServer(domain);
|
|
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
var dnsResponse = GetDnsResponse(buf, count, target);
|
|
var dnsResponse = GetDnsResponse(buf, count, target);
|
|
if (dnsResponse != null) _listener.SendTo(dnsResponse, from);
|
|
if (dnsResponse != null) _listener.SendTo(dnsResponse, from);
|
|
_consoleOutout.Add($"{DateTime.Now:yyyyMMdd HH:mm:ss} {from} [{target}]\t{domain}");
|
|
_consoleOutout.Add($"{DateTime.Now:yyyyMMdd HH:mm:ss} {from} [{target}]\t{domain}");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
{
|
|
{
|
|
- _consoleOutout.Add($"{DateTime.Now:yyyyMMdd HH:mm:ss} {from} [{target}]\t{domain} ERR:{e.Message}");
|
|
|
|
|
|
+ _consoleOutout.Add($"{DateTime.Now:yyyyMMdd HH:mm:ss} {from} [{target ?? "Unknown"}]\t{domain ?? "Unknown"} Err:{e.Message}");
|
|
|
|
+ }
|
|
|
|
+ catch
|
|
|
|
+ {
|
|
|
|
+ _consoleOutout.Add($"{DateTime.Now:yyyyMMdd HH:mm:ss} {from} [{target ?? "Unknown"}]\t{domain ?? "Unknown"} Err:Unknown");
|
|
|
|
+ }
|
|
|
|
+ finally
|
|
|
|
+ {
|
|
|
|
+ if (flagNextStarted == false) StartHandleRequest();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- protected static string ExtractDomainName(byte[] buf, int count)
|
|
|
|
|
|
+ protected static string ExtractDomainName(byte[] buf)
|
|
{
|
|
{
|
|
- var lst = new List<byte[]>();
|
|
|
|
-
|
|
|
|
|
|
+ //seeking for end of domain
|
|
var ptr = 12;
|
|
var ptr = 12;
|
|
while (buf[ptr] != 0)
|
|
while (buf[ptr] != 0)
|
|
{
|
|
{
|
|
@@ -95,7 +108,7 @@ namespace DnsForwarder
|
|
var bufDomain = new byte[ptr - 12];
|
|
var bufDomain = new byte[ptr - 12];
|
|
Array.Copy(buf, 12, bufDomain, 0, bufDomain.Length);
|
|
Array.Copy(buf, 12, bufDomain, 0, bufDomain.Length);
|
|
|
|
|
|
- //dot fill
|
|
|
|
|
|
+ //fill dots
|
|
ptr = 0;
|
|
ptr = 0;
|
|
while (ptr < bufDomain.Length)
|
|
while (ptr < bufDomain.Length)
|
|
{
|
|
{
|