Exemplo n.º 1
0
        void procRequest(byte[] data)
        {
            string fullname = string.Join(".", GetName(data).ToArray());

            if (FireEvents && ConnectionRequest != null)
            {
                DnsConnectionRequestEventArgs a = new DnsConnectionRequestEventArgs {
                    Host = endpoint.ToString(), Url = fullname
                };
                ConnectionRequest(a);
            }

            string url     = "";
            bool   treated = false;

            if (dicRules.ContainsKey(fullname))
            {
                if (dicRules[fullname].Mode == HandleMode.Allow)
                {
                    url = fullname;
                }
                else if (dicRules[fullname].Mode == HandleMode.Redirect)
                {
                    url = dicRules[fullname].Address;
                }
                else
                {
                    url = "NXDOMAIN";
                }
                treated = true;
            }

            if (!treated)
            {
                foreach (KeyValuePair <string, DnsSettings> rule in regRules)
                {
                    Regex regex = new Regex(rule.Key);
                    if (!regex.IsMatch(fullname))
                    {
                        continue;
                    }

                    if (rule.Value.Mode == HandleMode.Allow)
                    {
                        url = fullname;
                    }
                    else if (rule.Value.Mode == HandleMode.Redirect)
                    {
                        url = rule.Value.Address;
                    }
                    else
                    {
                        url = "NXDOMAIN";
                    }
                    treated = true;
                    break;
                }
            }

            if (!treated)
            {
                if (!DenyNotInRules)
                {
                    url = fullname;
                }
                else
                {
                    url = "NXDOMAIN";
                }
            }

            IPAddress ip = LocalHostIp;

            if (url != "" && url != "NXDOMAIN")
            {
                try
                {
                    IPAddress address;
                    if (!IPAddress.TryParse(url, out address))
                    {
                        ip = Dns.GetHostEntry(url).AddressList[0];
                    }
                    else
                    {
                        ip = address;
                    }
                }
                catch
                {
                    ip = IPAddress.None;
                }
            }

            byte[] res = MakeResponsePacket(data, ip);

            soc.SendTo(res, endpoint);

            if (FireEvents && ResolvedIp != null)
            {
                DnsEventArgs a = new DnsEventArgs()
                {
                    Host = ip, Url = fullname
                };
                ResolvedIp(a);
            }
        }
Exemplo n.º 2
0
 private static void ResolvedIp(DnsEventArgs e)
 {
     Console.WriteLine("Resolved: " + e.Url + " to: " + ((e.Host == IPAddress.None) ? "NXDOMAIN" : e.Host.ToString()));
 }
Exemplo n.º 3
0
        void procRequest(byte[] data)
        {
            string fullname = string.Join(".", GetName(data).ToArray());

            if (FireEvents && ConnectionRequest != null)
            {
                DnsConnectionRequestEventArgs a = new DnsConnectionRequestEventArgs {
                    Host = endpoint.ToString(), Url = fullname
                };
                ConnectionRequest(a);
            }

            string url = "";

            if (rules.ContainsKey(fullname))
            {
                if (rules[fullname].Mode == HandleMode.Allow)
                {
                    url = fullname;
                }
                else if (rules[fullname].Mode == HandleMode.Redirect)
                {
                    url = rules[fullname].Address;
                }
                else
                {
                    url = "NXDOMAIN";
                }
            }
            else
            {
                if (!DenyNotInRules)
                {
                    url = fullname;
                }
                else
                {
                    url = "NXDOMAIN";
                }
            }

            IPAddress ip = LocalHostIp;

            if (url != "" && url != "NXDOMAIN")
            {
                try
                {
                    IPAddress address;
                    if (!IPAddress.TryParse(url, out address))
                    {
                        ip = Dns.GetHostEntry(url).AddressList[0];
                    }
                    else
                    {
                        ip = address;
                    }
                }
                catch
                {
                    ip = IPAddress.None;
                }
            }

            byte[] res = MakeResponsePacket(data, ip);

            soc.SendTo(res, endpoint);

            if (FireEvents && ResolvedIp != null)
            {
                DnsEventArgs a = new DnsEventArgs()
                {
                    Host = ip, Url = fullname
                };
                ResolvedIp(a);
            }
        }