Ejemplo n.º 1
0
        public static void ReadConfig(string path)
        {
            string    configStr  = File.ReadAllText(path);
            JsonValue configJson = Json.Parse(configStr);

            if (configStr.Contains("\"SecondDns\""))
            {
                SecondDnsIp = IPAddress.Parse(configJson.AsObjectGetString("SecondDns"));
            }
            if (configStr.Contains("\"SecondHttpsDns\""))
            {
                SecondHttpsDnsUrl = configJson.AsObjectGetString("SecondHttpsDns");
            }
            if (configStr.Contains("\"EnableDnsMessage\""))
            {
                DnsMsgEnable = configJson.AsObjectGetBool("EnableDnsMessage");
            }
            if (configStr.Contains("\"EnableDnsCache\""))
            {
                DnsCacheEnable = configJson.AsObjectGetBool("EnableDnsCache");
            }
            if (configStr.Contains("\"EnableHttp2\""))
            {
                Http2Enable = configJson.AsObjectGetBool("EnableHttp2");
            }
            if (configStr.Contains("\"EnableAutoCleanLog\""))
            {
                AutoCleanLogEnable = configJson.AsObjectGetBool("EnableAutoCleanLog");
            }
            if (configStr.Contains("\"Port\""))
            {
                ListenPort = configJson.AsObjectGetInt("Port", 53);
            }
            if (configStr.Contains("\"ChinaList\""))
            {
                ChinaListEnable = configJson.AsObjectGetBool("ChinaList");
            }

            ListenIp        = IPAddress.Parse(configJson.AsObjectGetString("Listen"));
            BlackListEnable = configJson.AsObjectGetBool("BlackList");
            WhiteListEnable = configJson.AsObjectGetBool("RewriteList");
            ProxyEnable     = configJson.AsObjectGetBool("ProxyEnable");
            EDnsCustomize   = configJson.AsObjectGetBool("EDnsCustomize");
            DebugLog        = configJson.AsObjectGetBool("DebugLog");
            HttpsDnsUrl     = configJson.AsObjectGetString("HttpsDns").Trim();

            if (EDnsCustomize)
            {
                EDnsIp = IPAddress.Parse(configJson.AsObjectGetString("EDnsClientIp"));
            }
            if (ProxyEnable)
            {
                WProxy = new WebProxy(configJson.AsObjectGetString("Proxy"));
            }
        }
Ejemplo n.º 2
0
        public static void ReadConfig(string path)
        {
            string    configStr  = File.ReadAllText(path);
            JsonValue configJson = Json.Parse(configStr);

            if (configStr.Contains("\"SecondDns\""))
            {
                SecondDnsIp = IPAddress.Parse(configJson.AsObjectGetString("SecondDns"));
            }
            if (configStr.Contains("\"SecondHttpsDns\""))
            {
                SecondHttpsDnsUrl = configJson.AsObjectGetString("SecondHttpsDns");
            }
            if (configStr.Contains("\"EnableDnsMessage\""))
            {
                DnsMsgEnable = configJson.AsObjectGetBool("EnableDnsMessage");
            }
            if (configStr.Contains("\"EnableDnsCache\""))
            {
                DnsCacheEnable = configJson.AsObjectGetBool("EnableDnsCache");
            }
            if (configStr.Contains("\"EnableHttp2\""))
            {
                Http2Enable = configJson.AsObjectGetBool("EnableHttp2");
            }
            if (configStr.Contains("\"EnableAutoCleanLog\""))
            {
                AutoCleanLogEnable = configJson.AsObjectGetBool("EnableAutoCleanLog");
            }
            if (configStr.Contains("\"Port\""))
            {
                ListenPort = configJson.AsObjectGetInt("Port", 53);
            }
            if (configStr.Contains("\"ChinaList\""))
            {
                ChinaListEnable = configJson.AsObjectGetBool("ChinaList");
            }
            if (configStr.Contains("\"StartupOverDoH\""))
            {
                StartupOverDoH = configJson.AsObjectGetBool("StartupOverDoH");
            }
            if (configStr.Contains("\"AllowSelfSignedCert\""))
            {
                AllowSelfSignedCert = configJson.AsObjectGetBool("AllowSelfSignedCert");
            }
            if (configStr.Contains("\"AllowAutoRedirect\""))
            {
                AllowAutoRedirect = configJson.AsObjectGetBool("AllowAutoRedirect");
            }
            if (configStr.Contains("\"HTTPStatusNotify\""))
            {
                HTTPStatusNotify = configJson.AsObjectGetBool("HTTPStatusNotify");
            }

            if (configStr.Contains("\"Ipv6Disable\""))
            {
                Ipv6Disable = configJson.AsObjectGetBool("Ipv6Disable");
            }
            if (configStr.Contains("\"Ipv4Disable\""))
            {
                Ipv4Disable = configJson.AsObjectGetBool("Ipv4Disable");
            }

            if (configStr.Contains("\"TTLRewrite\""))
            {
                TtlRewrite = configJson.AsObjectGetBool("TTLRewrite");
            }
            if (configStr.Contains("\"TTLMinTime\""))
            {
                TtlMinTime = configJson.AsObjectGetInt("TTLMinTime");
            }

            ListenIp        = IPAddress.Parse(configJson.AsObjectGetString("Listen"));
            BlackListEnable = configJson.AsObjectGetBool("BlackList");
            WhiteListEnable = configJson.AsObjectGetBool("RewriteList");
            ProxyEnable     = configJson.AsObjectGetBool("ProxyEnable");
            EDnsCustomize   = configJson.AsObjectGetBool("EDnsCustomize");
            DebugLog        = configJson.AsObjectGetBool("DebugLog");
            HttpsDnsUrl     = configJson.AsObjectGetString("HttpsDns").Trim();

            if (EDnsCustomize)
            {
                EDnsIp = IPAddress.Parse(configJson.AsObjectGetString("EDnsClientIp"));
            }
            if (ProxyEnable)
            {
                WProxy = new WebProxy(configJson.AsObjectGetString("Proxy"));
            }
        }
Ejemplo n.º 3
0
        public static (List <DnsRecordBase> list, ReturnCode statusCode) ResolveOverHttpsByDnsJson(string clientIpAddress,
                                                                                                   string domainName, string dohUrl,
                                                                                                   bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A)
        {
            string dnsStr;
            List <DnsRecordBase> recordList = new List <DnsRecordBase>();

            try
            {
                dnsStr = MyCurl.GetString(dohUrl + @"?ct=application/dns-json&" +
                                          $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}",
                                          DnsSettings.Http2Enable, proxyEnable, wProxy, DnsSettings.AllowAutoRedirect);
            }
            catch (WebException e)
            {
                HttpWebResponse response = (HttpWebResponse)e.Response;
                try
                {
                    BackgroundLog($@"| - Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode} | {e.Status} | {domainName} | {response.ResponseUri}");
                    if (DnsSettings.HTTPStatusNotify)
                    {
                        MainWindow.NotifyIcon.ShowBalloonTip(360, "AuroraDNS - 错误",
                                                             $"异常 :{Convert.ToInt32(response.StatusCode)} {response.StatusCode} {Environment.NewLine} {domainName}", ToolTipIcon.Warning);
                    }
                    if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        DnsSettings.DnsMsgEnable = true;
                    }
                }
                catch (Exception exception)
                {
                    BackgroundLog($@"| - Catch WebException : {exception.Message} | {e.Status} | {domainName} | {dohUrl}" + @"?ct=application/dns-json&" +
                                  $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}");
                    if (DnsSettings.HTTPStatusNotify)
                    {
                        MainWindow.NotifyIcon.ShowBalloonTip(360, "AuroraDNS - 错误",
                                                             $"异常 : {exception.Message} {Environment.NewLine} {domainName}", ToolTipIcon.Warning);
                    }
                }

                if (dohUrl != DnsSettings.HttpsDnsUrl)
                {
                    return(new List <DnsRecordBase>(), ReturnCode.ServerFailure);
                }
                BackgroundLog($@"| -- SecondDoH : {DnsSettings.SecondHttpsDnsUrl}");
                return(ResolveOverHttpsByDnsJson(clientIpAddress, domainName, DnsSettings.SecondHttpsDnsUrl,
                                                 proxyEnable, wProxy, type));
            }

            JsonValue dnsJsonValue = Json.Parse(dnsStr);

            int statusCode = dnsJsonValue.AsObjectGetInt("Status");

            if (statusCode != 0)
            {
                return(new List <DnsRecordBase>(), (ReturnCode)statusCode);
            }

            if (dnsStr.Contains("\"Answer\""))
            {
                var dnsAnswerJsonList = dnsJsonValue.AsObjectGetArray("Answer");

                foreach (var itemJsonValue in dnsAnswerJsonList)
                {
                    string answerAddr       = itemJsonValue.AsObjectGetString("data");
                    string answerDomainName = itemJsonValue.AsObjectGetString("name");
                    int    answerType       = itemJsonValue.AsObjectGetInt("type");
                    int    ttl = itemJsonValue.AsObjectGetInt("TTL");

                    switch (type)
                    {
                    case RecordType.A when Convert.ToInt32(RecordType.A) == answerType && !DnsSettings.Ipv4Disable:
                    {
                        ARecord aRecord = new ARecord(
                            DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr));

                        recordList.Add(aRecord);
                        break;
                    }

                    case RecordType.A:
                    {
                        if (Convert.ToInt32(RecordType.CName) == answerType)
                        {
                            CNameRecord cRecord = new CNameRecord(
                                DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));

                            recordList.Add(cRecord);

                            //recordList.AddRange(ResolveOverHttps(clientIpAddress,answerAddr));
                            //return recordList;
                        }

                        break;
                    }

                    case RecordType.Aaaa when Convert.ToInt32(RecordType.Aaaa) == answerType && !DnsSettings.Ipv6Disable:
                    {
                        AaaaRecord aaaaRecord = new AaaaRecord(
                            DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr));
                        recordList.Add(aaaaRecord);
                        break;
                    }

                    case RecordType.Aaaa:
                    {
                        if (Convert.ToInt32(RecordType.CName) == answerType)
                        {
                            CNameRecord cRecord = new CNameRecord(
                                DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                            recordList.Add(cRecord);
                        }

                        break;
                    }

                    case RecordType.CName when answerType == Convert.ToInt32(RecordType.CName):
                    {
                        CNameRecord cRecord = new CNameRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(cRecord);
                        break;
                    }

                    case RecordType.Ns when answerType == Convert.ToInt32(RecordType.Ns):
                    {
                        NsRecord nsRecord = new NsRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(nsRecord);
                        break;
                    }

                    case RecordType.Mx when answerType == Convert.ToInt32(RecordType.Mx):
                    {
                        MxRecord mxRecord = new MxRecord(
                            DomainName.Parse(answerDomainName), ttl,
                            ushort.Parse(answerAddr.Split(' ')[0]),
                            DomainName.Parse(answerAddr.Split(' ')[1]));
                        recordList.Add(mxRecord);
                        break;
                    }

                    case RecordType.Txt when answerType == Convert.ToInt32(RecordType.Txt):
                    {
                        TxtRecord txtRecord = new TxtRecord(DomainName.Parse(answerDomainName), ttl, answerAddr);
                        recordList.Add(txtRecord);
                        break;
                    }

                    case RecordType.Ptr when answerType == Convert.ToInt32(RecordType.Ptr):
                    {
                        PtrRecord ptrRecord = new PtrRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(ptrRecord);
                        break;
                    }

                    default:
                        statusCode = Convert.ToInt32(ReturnCode.ServerFailure);
                        break;
                    }
                }
            }

            return(recordList, (ReturnCode)statusCode);
        }
Ejemplo n.º 4
0
        private static (List <dynamic> list, ReturnCode statusCode) ResolveOverHttps(string clientIpAddress, string domainName,
                                                                                     bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A)
        {
            string         dnsStr;
            List <dynamic> recordList = new List <dynamic>();

            using (MWebClient webClient = new MWebClient())
            {
                webClient.Headers["User-Agent"] = "AuroraDNSC/0.1";

//                webClient.AllowAutoRedirect = false;

                if (proxyEnable)
                {
                    webClient.Proxy = wProxy;
                }

                try
                {
                    dnsStr = webClient.DownloadString(
                        DnsSettings.HttpsDnsUrl +
                        @"?ct=application/dns-json&" +
                        $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}");
                }
                catch (WebException e)
                {
                    HttpWebResponse response = (HttpWebResponse)e.Response;
                    try
                    {
                        BgwLog($@"| - Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode} | {domainName}");
                    }
                    catch (Exception exception)
                    {
                        BgwLog($@"| - Catch WebException : {exception.Message} | {domainName}");

                        //MainWindow.NotifyIcon.ShowBalloonTip(360, "AuroraDNS - 错误",
                        //    $"异常 : {exception.Message} {Environment.NewLine} {domainName}", ToolTipIcon.Warning);
                    }
                    return(new List <dynamic>(), ReturnCode.ServerFailure);
                }
            }

            JsonValue dnsJsonValue = Json.Parse(dnsStr);

            int statusCode = dnsJsonValue.AsObjectGetInt("Status");

            if (statusCode != 0)
            {
                return(new List <dynamic>(), (ReturnCode)statusCode);
            }

            if (dnsStr.Contains("\"Answer\""))
            {
                var dnsAnswerJsonList = dnsJsonValue.AsObjectGetArray("Answer");

                foreach (var itemJsonValue in dnsAnswerJsonList)
                {
                    string answerAddr       = itemJsonValue.AsObjectGetString("data");
                    string answerDomainName = itemJsonValue.AsObjectGetString("name");
                    int    answerType       = itemJsonValue.AsObjectGetInt("type");
                    int    ttl = itemJsonValue.AsObjectGetInt("TTL");

                    switch (type)
                    {
                    case RecordType.A:
                    {
                        if (Convert.ToInt32(RecordType.A) == answerType)
                        {
                            ARecord aRecord = new ARecord(
                                DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr));

                            recordList.Add(aRecord);
                        }
                        else if (Convert.ToInt32(RecordType.CName) == answerType)
                        {
                            CNameRecord cRecord = new CNameRecord(
                                DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));

                            recordList.Add(cRecord);

                            //recordList.AddRange(ResolveOverHttps(clientIpAddress,answerAddr));
                            //return recordList;
                        }

                        break;
                    }

                    case RecordType.Aaaa:
                    {
                        if (Convert.ToInt32(RecordType.Aaaa) == answerType)
                        {
                            AaaaRecord aaaaRecord = new AaaaRecord(
                                DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr));
                            recordList.Add(aaaaRecord);
                        }
                        else if (Convert.ToInt32(RecordType.CName) == answerType)
                        {
                            CNameRecord cRecord = new CNameRecord(
                                DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                            recordList.Add(cRecord);
                        }
                        break;
                    }

                    case RecordType.CName when answerType == Convert.ToInt32(RecordType.CName):
                    {
                        CNameRecord cRecord = new CNameRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(cRecord);
                        break;
                    }

                    case RecordType.Ns when answerType == Convert.ToInt32(RecordType.Ns):
                    {
                        NsRecord nsRecord = new NsRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(nsRecord);
                        break;
                    }

                    case RecordType.Mx when answerType == Convert.ToInt32(RecordType.Mx):
                    {
                        MxRecord mxRecord = new MxRecord(
                            DomainName.Parse(answerDomainName), ttl,
                            ushort.Parse(answerAddr.Split(' ')[0]),
                            DomainName.Parse(answerAddr.Split(' ')[1]));
                        recordList.Add(mxRecord);
                        break;
                    }

                    case RecordType.Txt when answerType == Convert.ToInt32(RecordType.Txt):
                    {
                        TxtRecord txtRecord = new TxtRecord(DomainName.Parse(answerDomainName), ttl, answerAddr);
                        recordList.Add(txtRecord);
                        break;
                    }

                    case RecordType.Ptr when answerType == Convert.ToInt32(RecordType.Ptr):
                    {
                        PtrRecord ptrRecord = new PtrRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(ptrRecord);
                        break;
                    }

                    default:
                    {
                        statusCode = Convert.ToInt32(ReturnCode.ServerFailure);
                        break;
                    }
                    }
                }
            }

            return(recordList, (ReturnCode)statusCode);
        }
Ejemplo n.º 5
0
        public static string GeoIpLocal(string ipStr, bool onlyCountryCode = false)
        {
            try
            {
                string locStr = new WebClient().DownloadString(IsIp(ipStr)
                    ? $"{UrlSettings.GeoIpApi}{ipStr}": $"{UrlSettings.GeoIpApi}{Dns.GetHostAddresses(ipStr)[0]}");
                JsonValue json = Json.Parse(locStr);

                string countryCode;
                string organization;

                if (locStr.Contains("\"country_code\""))
                {
                    countryCode = json.AsObjectGetString("country_code");
                }
                else if (locStr.Contains("\"countryCode\""))
                {
                    countryCode = json.AsObjectGetString("countryCode");
                }
                else
                {
                    countryCode = "";
                }

                if (locStr.Contains("\"organization\""))
                {
                    organization = json.AsObjectGetString("organization");
                }
                else if (locStr.Contains("\"as\""))
                {
                    organization = json.AsObjectGetString("as");
                }
                else if (locStr.Contains("\"org\""))
                {
                    organization = json.AsObjectGetString("org");
                }
                else
                {
                    organization = "";
                }

                if (!organization.ToUpper().Contains("AS") && locStr.Contains("\"asn\""))
                {
                    try
                    {
                        organization = $"AS{json.AsObjectGetInt("asn")} {organization}";
                    }
                    catch
                    {
                        organization = $"AS{json.AsObjectGetString("asn")} {organization}";
                    }
                }

                if (onlyCountryCode)
                {
                    return(countryCode);
                }
                return(countryCode + " " + organization);
            }
            catch (Exception e)
            {
                MyTools.BackgroundLog(@"| DownloadString failed : " + e);
                return(null);
            }
        }
Ejemplo n.º 6
0
        private static (List <dynamic> list, int statusCode) ResolveOverHttps(string clientIpAddress, string domainName,
                                                                              bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A)
        {
            string         dnsStr;
            List <dynamic> recordList = new List <dynamic>();

            using (WebClient webClient = new WebClient())
            {
                webClient.Headers["User-Agent"] = "AuroraDNSC/0.1";

                if (proxyEnable)
                {
                    webClient.Proxy = wProxy;
                }

                dnsStr = webClient.DownloadString(
                    ADnsSetting.HttpsDnsUrl +
                    @"?ct=application/dns-json&" +
                    $"name={domainName}&type={type.ToString().ToUpper()}&edns_client_subnet={clientIpAddress}");
            }

            JsonValue dnsJsonValue = Json.Parse(dnsStr);

            int statusCode = dnsJsonValue.AsObjectGetInt("Status");

            if (statusCode != 0)
            {
                return(new List <dynamic>(), statusCode);
            }

            if (dnsStr.Contains("\"Answer\""))
            {
                var dnsAnswerJsonList = dnsJsonValue.AsObjectGetArray("Answer");

                foreach (var itemJsonValue in dnsAnswerJsonList)
                {
                    string answerAddr       = itemJsonValue.AsObjectGetString("data");
                    string answerDomainName = itemJsonValue.AsObjectGetString("name");
                    int    answerType       = itemJsonValue.AsObjectGetInt("type");
                    int    ttl = itemJsonValue.AsObjectGetInt("TTL");

                    if (type == RecordType.A)
                    {
                        if (Convert.ToInt32(RecordType.A) == answerType)
                        {
                            ARecord aRecord = new ARecord(
                                DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr));

                            recordList.Add(aRecord);
                        }
                        else if (Convert.ToInt32(RecordType.CName) == answerType)
                        {
                            CNameRecord cRecord = new CNameRecord(
                                DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));

                            recordList.Add(cRecord);

                            //recordList.AddRange(ResolveOverHttps(clientIpAddress,answerAddr));
                            //return recordList;
                        }
                    }
                    else if (type == RecordType.Aaaa && ADnsSetting.IPv6Enable)
                    {
                        if (Convert.ToInt32(RecordType.Aaaa) == answerType)
                        {
                            AaaaRecord aaaaRecord = new AaaaRecord(
                                DomainName.Parse(answerDomainName), ttl, IPAddress.Parse(answerAddr));
                            recordList.Add(aaaaRecord);
                        }
                        else if (Convert.ToInt32(RecordType.CName) == answerType)
                        {
                            CNameRecord cRecord = new CNameRecord(
                                DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                            recordList.Add(cRecord);
                        }
                    }
                    else if (type == RecordType.CName && answerType == Convert.ToInt32(RecordType.CName))
                    {
                        CNameRecord cRecord = new CNameRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(cRecord);
                    }
                    else if (type == RecordType.Ns && answerType == Convert.ToInt32(RecordType.Ns))
                    {
                        NsRecord nsRecord = new NsRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(nsRecord);
                    }
                    else if (type == RecordType.Mx && answerType == Convert.ToInt32(RecordType.Mx))
                    {
                        MxRecord mxRecord = new MxRecord(
                            DomainName.Parse(answerDomainName), ttl,
                            ushort.Parse(answerAddr.Split(' ')[0]),
                            DomainName.Parse(answerAddr.Split(' ')[1]));
                        recordList.Add(mxRecord);
                    }
                    else if (type == RecordType.Txt && answerType == Convert.ToInt32(RecordType.Txt))
                    {
                        TxtRecord txtRecord = new TxtRecord(DomainName.Parse(answerDomainName), ttl, answerAddr);
                        recordList.Add(txtRecord);
                    }
                    else if (type == RecordType.Ptr && answerType == Convert.ToInt32(RecordType.Ptr))
                    {
                        PtrRecord ptrRecord = new PtrRecord(
                            DomainName.Parse(answerDomainName), ttl, DomainName.Parse(answerAddr));
                        recordList.Add(ptrRecord);
                    }
                }
            }

            return(recordList, statusCode);
        }