Ejemplo n.º 1
0
        /// <summary>
        /// validate domain by nslookup
        /// </summary>
        /// <param name="dns"></param>
        /// <param name="fileName"></param>
        /// <param name="invalidDomains"></param>
        public static void ValidateDomains(IPAddress dns, string fileName, string invalidDomains = "invalid_domains.txt")
        {
            if (dns == null)
            {
                dns = IPAddress.Parse("8.8.8.8");
            }

            if (string.IsNullOrEmpty(invalidDomains))
            {
                invalidDomains = "invalid_domains.txt";
            }

            ChinaList     cl      = new ChinaList(fileName);
            List <string> domains = cl.GetDomains();
            //List<string> urls = cl.ParseURLs();
            StringBuilder results = new StringBuilder();
            //StringBuilder fullResult = new StringBuilder();
            List <string> whiteList = new List <string>();

            whiteList.Add("ns1.dnsv2.com");

            Parallel.ForEach(domains, domain =>
            {
                Console.WriteLine("Querying DNS records for domain: {0}", domain);
                QueryResult queryResult = DNSQuery(dns, domain);
                Console.Write(queryResult.ToString());
                //fullResult.Append(queryResult.ToString());
                bool ret = false;

                if (queryResult.NSCount < 1)
                {
                    results.Append(queryResult.ToString());
                    return;
                }

                foreach (var ns in queryResult.NSList)
                {
                    var t = ns;
                    if (ns.Contains("="))
                    {
                        t = ParseNameServer(ns);
                    }

                    try
                    {
                        IPHostEntry ip   = Dns.GetHostEntry(t);
                        QueryResult temp = DNSQuery(ip.AddressList[0], domain);
                        if (temp.NSCount > 0 || whiteList.Contains(t))
                        {
                            ret = true;
                            break;
                        }
                        else
                        {
                            queryResult.Error += string.Format("\n[V]: ns->{0}, Count->{1}", t, temp.NSCount);
                        }
                    }
                    catch (Exception ex)
                    {
                        queryResult.Error += string.Format("\n[V]: ns->{0}, Error->{1}", t, ex.Message);
                        Console.WriteLine("Validate domain: {0}, ns: {1} Error: {2}", domain, t, ex.Message);
                    }
                }

                if (!ret)
                {
                    queryResult.Error += "\n[V]: validate domian fail.";
                    results.Append(queryResult.ToString());
                }
            });

            Save(invalidDomains, results.ToString());
        }
Ejemplo n.º 2
0
        private static void DispatcherTask(Arguments args)
        {
            if (args.IsTrue("help") || args.IsTrue("h"))
            {
                Console.WriteLine(ChinaListConst.HELP_INFO, DateTime.Now.ToString("yyyy"));
            }
            else if (args.IsTrue("version"))
            {
                Console.WriteLine("ABPUtils version: {0}", ChinaLists.GetVersion());
            }
            else if (args.IsTrue("ns") || args.IsTrue("nslookup"))
            {
                var domain = args.Single("d");

                if (string.IsNullOrEmpty(domain))
                {
                    domain = args.Single("domain");
                }

                if (string.IsNullOrEmpty(domain))
                {
                    Console.WriteLine("wrong input domain.");
                    return;
                }

                QueryResult result = null;
                if (string.IsNullOrEmpty(args.Single("dns")))
                {
                    result = ChinaLists.DNSQuery(null, domain);
                }
                else
                {
                    result = ChinaLists.DNSQuery(IPAddress.Parse(args.Single("dns")), domain);
                }

                if (result == null)
                {
                    Console.WriteLine("Query result is null.");
                }
                else
                {
                    Console.Write(result.ToString());
                }
            }
            else if (args.IsTrue("v") || args.IsTrue("validate"))
            {
                var input = args.Single("i");
                if (string.IsNullOrEmpty(input))
                {
                    input = args.Single("input");
                }

                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine("wrong input file.");
                    return;
                }

                var chinaList = new ChinaList(input);
                chinaList.Validate();
            }
            else if (args.IsTrue("u") || args.IsTrue("update"))
            {
                var input = args.Single("i");
                if (string.IsNullOrEmpty(input))
                {
                    input = args.Single("input");
                }

                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine("wrong input file.");
                    return;
                }

                var chinaList = new ChinaList(input);
                chinaList.Update();
                chinaList.Validate();
            }
            else if (args.IsTrue("c") || args.IsTrue("check"))
            {
                var input = args.Single("i");
                if (string.IsNullOrEmpty(input))
                {
                    input = args.Single("input");
                }

                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine("wrong input file.");
                    return;
                }

                var output = args.Single("o");

                if (string.IsNullOrEmpty(output))
                {
                    output = args.Single("output");
                }

                var dns = args.Single("dns");
                if (string.IsNullOrEmpty(dns))
                {
                    ChinaLists.ValidateDomains(null, input, output);
                }
                else
                {
                    ChinaLists.ValidateDomains(IPAddress.Parse(args.Single("dns")), input, output);
                }
            }
            else if (args.IsTrue("m") || args.IsTrue("merge"))
            {
                var input = string.Empty;
                input = args.Single("i");

                if (string.IsNullOrEmpty(input))
                {
                    input = args.Single("input");
                }

                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine("wrong input file.");
                    return;
                }

                WebProxy proxy = null;
                var      p     = args.Single("p");
                if (string.IsNullOrEmpty(p))
                {
                    p = args.Single("proxy");
                }

                if (!string.IsNullOrEmpty(p))
                {
                    var temp = p.Split(':');
                    proxy = new WebProxy(temp[0], int.Parse(temp[1]));
                    proxy.BypassProxyOnLocal = true;
                }

                var output = args.Single("o");
                if (string.IsNullOrEmpty(output))
                {
                    output = args.Single("output");
                }

                ChinaLists.Merge(input, proxy, args.IsTrue("patch"), output);
            }
            else if (args.IsTrue("conf"))
            {
                var input = string.Empty;
                input = args.Single("i");

                if (string.IsNullOrEmpty(input))
                {
                    input = args.Single("input");
                }

                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine("wrong input file.");
                    return;
                }

                ChinaLists.CleanConfigurations(input, null);
                Console.WriteLine("Clean configuration file successful.");
            }
            else
            {
                Console.WriteLine("Wrong args");
            }
        }