コード例 #1
0
        public Task <DNS.Protocol.IResponse> Resolve(DNS.Protocol.IRequest request, CancellationToken cancellationToken = default)
        {
            DNS.Protocol.IResponse response = DNS.Protocol.Response.FromRequest(request);

            foreach (var question in response.Questions)
            {
                if (question.Type == DNS.Protocol.RecordType.A)
                {
                    var client = new DnsClient.LookupClient(DnsClient.NameServer.GooglePublicDns);
                    client.UseTcpOnly = true;
                    client.UseCache   = true;

                    try
                    {
                        var result = client.Query(question.Name.ToString(), DnsClient.QueryType.A);
                        foreach (var item in result.Answers.ARecords())
                        {
                            response.AnswerRecords.Add(new DNS.Protocol.ResourceRecords.IPAddressResourceRecord(question.Name, item.Address));
                        }
                    }
                    catch (Exception)
                    {
                        // 跳过
                    }
                }
                else if (question.Type == DNS.Protocol.RecordType.AAAA)
                {
                    var client = new DnsClient.LookupClient(DnsClient.NameServer.GooglePublicDns);
                    client.UseTcpOnly = true;
                    client.UseCache   = true;

                    try
                    {
                        var result = client.Query(question.Name.ToString(), DnsClient.QueryType.AAAA);
                        foreach (var item in result.Answers.AaaaRecords())
                        {
                            response.AnswerRecords.Add(new DNS.Protocol.ResourceRecords.IPAddressResourceRecord(question.Name, item.Address));
                        }
                    }
                    catch (Exception)
                    {
                        // 跳过
                    }
                }
                else
                {
                    // 跳过
                }
            }

            return(Task.FromResult(response));
        }
コード例 #2
0
        /// <summary>
        /// Applies the custom host resolver rules to override DNS requests.
        /// </summary>
        /// <param name="value">The given value for the host-resolver-rules parameter</param>
        /// <param name="settings"></param>
        private void handleHostResolverRules(string value, ref CefSettings settings)
        {
            if (value == null)
            {
                return;
            }

            // resolve domains with set DNS server if necessary
            List <string> parts = new List <string>();
            var           rules = value.Split(',');

            foreach (var rule in rules)
            {
                if (!rule.Contains("{{"))
                {
                    parts.Add(rule);
                    continue;
                }

                string r = rule.Trim().ToLower();
                if (!r.StartsWith("map"))
                {
                    parts.Add(rule);
                    continue;
                }

                string domain = Common.GetTextBetween(rule.ToLower(), "map ", " {{").Trim();

                string dnsAddress = Tools.Common.GetTextBetween(rule, "{{", "}}");

                int    port      = 53; // default DNS port
                string ip_string = null;
                if (dnsAddress.Contains(":"))
                {
                    var addressParts = dnsAddress.Split(':');
                    ip_string = addressParts[0].Trim();
                    string port_string = addressParts[1];
                    if (!int.TryParse(port_string, out port))
                    {
                        Logger.Log("Ignored invalid value for parameter host-resolver-rules, a port was not a valid number");
                        return;
                    }
                }
                else
                {
                    ip_string = dnsAddress.Trim();
                }

                IPAddress ipAddress = null;
                if (!IPAddress.TryParse(ip_string, out ipAddress))
                {
                    Logger.Log("Ignored invalid value for parameter host-resolver-rules, a port was not a valid number");
                    return;
                }

                var endpoint = new IPEndPoint(ipAddress, port);
                var lookup   = new DnsClient.LookupClient(endpoint);
                var result   = lookup.Query(domain, DnsClient.QueryType.A);
                if (result.Answers.Count <= 0)
                {
                    Logger.Log("No ip address found for domain '" + domain + "', ignoring setting for it");
                    continue;
                }

                string replace     = Common.GetTextBetween(rule, "{{", "}}");
                string replacement = (result.Answers[0] as DnsClient.Protocol.ARecord).Address.ToString();

                string finalRule = rule.Replace("{{" + replace + "}}", replacement);
                parts.Add(finalRule);
            }

            // rebuild setting with replacements
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < parts.Count; i++)
            {
                var part = parts[i];
                sb.Append(part);
                if (i < parts.Count - 1)
                {
                    sb.Append(", ");
                }
            }

            string setting = sb.ToString();

            settings.CefCommandLineArgs.Add("host-resolver-rules", setting);
        }