Example #1
0
 private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
 {
     MyTools.BgwLog(e.ExceptionObject.ToString());
     if (e.IsTerminating)
     {
         MessageBox.Show(
             $"发生了可能致命的严重错误,请从以下错误信息汲取灵感。{Environment.NewLine}" +
             $"程序可能即将中止运行。{Environment.NewLine}" + e.ExceptionObject,
             "意外的错误。", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var bgWorker = new BackgroundWorker();

            bgWorker.DoWork += (o, args) =>
            {
                try
                {
                    ListStrings = new WebClient().DownloadString(TypeDNS ? UrlSettings.MDnsList : UrlSettings.MDohList)
                                  .Split('\n').ToList();
                }
                catch (Exception exception)
                {
                    MyTools.BgwLog(@"| DownloadString failed : " + exception);
                }

                if (string.IsNullOrWhiteSpace(ListStrings[ListStrings.Count - 1]))
                {
                    ListStrings.RemoveAt(ListStrings.Count - 1);
                }
            };
            bgWorker.RunWorkerCompleted += (o, args) =>
            {
                if (File.Exists($"{MainWindow.SetupBasePath}dns.list") && TypeDNS)
                {
                    foreach (var item in File.ReadAllLines($"{MainWindow.SetupBasePath}dns.list"))
                    {
                        ListStrings.Add(item);
                    }
                }
                else if (File.Exists($"{MainWindow.SetupBasePath}doh.list") && !TypeDNS)
                {
                    foreach (var item in File.ReadAllLines($"{MainWindow.SetupBasePath}doh.list"))
                    {
                        ListStrings.Add(item);
                    }
                }

                foreach (var item in ListStrings)
                {
                    SpeedListView.Items.Add(new SpeedList
                    {
                        Server = TypeDNS ? item.Split('*', ',')[0].Trim() : item.Split('*', ',')[0].Trim().Split('/', ':')[3],
                        Name   = item.Contains('*') || item.Contains(',') ? item.Split('*', ',')[1].Trim() : ""
                    });
                }

                StratButton.IsEnabled = true;
                ProgressBar.Maximum   = SpeedListView.Items.Count;
                Grid.Effect           = null;
            };
            bgWorker.RunWorkerAsync();
        }
Example #3
0
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MyTools.BgwLog(e.Exception.ToString());
            MessageBoxResult msgResult = MessageBox.Show(
                $"未经处理的异常错误,请从以下错误信息汲取灵感。{Environment.NewLine}" +
                $"点击取消中止程序运行。{Environment.NewLine}" + e.Exception,
                "意外的错误。", MessageBoxButton.OKCancel, MessageBoxImage.Error);

            if (MessageBoxResult.OK == msgResult)
            {
                e.Handled = true;
            }
            else
            {
                Shutdown(1);
            }
        }
Example #4
0
        private void RunAsAdmin_OnClick(object sender, RoutedEventArgs e)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = GetType().Assembly.Location,
                Verb     = "runas"
            };

            try
            {
                Process.Start(startInfo);
                Environment.Exit(Environment.ExitCode);
            }
            catch (Exception exception)
            {
                MyTools.BgwLog(exception.ToString());
            }
        }
Example #5
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List <string> dohListStrings = null, dnsListStrings = null;
            var           bgWorker = new BackgroundWorker();

            bgWorker.DoWork += (o, args) =>
            {
                try
                {
                    dohListStrings = new WebClient()
                                     .DownloadString("https://cdn.jsdelivr.net/gh/AuroraDNS/AuroraDNS.github.io/DoH.list")
                                     .Split('\n').ToList();
                    dnsListStrings = new WebClient()
                                     .DownloadString("https://cdn.jsdelivr.net/gh/AuroraDNS/AuroraDNS.github.io/DNS.list")
                                     .Split('\n').ToList();
                }
                catch (Exception exception)
                {
                    MyTools.BgwLog(@"| Download list failed : " + exception);
                }

                if (string.IsNullOrWhiteSpace(dohListStrings[dohListStrings.Count - 1]))
                {
                    dohListStrings.RemoveAt(dohListStrings.Count - 1);
                }
                if (string.IsNullOrWhiteSpace(dnsListStrings[dnsListStrings.Count - 1]))
                {
                    dnsListStrings.RemoveAt(dnsListStrings.Count - 1);
                }
            };
            bgWorker.RunWorkerCompleted += (o, args) =>
            {
                foreach (var item in dohListStrings)
                {
                    DoHUrlText.Items.Add(item);
                }
                foreach (var item in dnsListStrings)
                {
                    BackupDNS.Items.Add(item);
                }
            };
            bgWorker.RunWorkerAsync();
        }
Example #6
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var bgWorker = new BackgroundWorker();

            bgWorker.DoWork += (o, args) =>
            {
                try
                {
                    ListStrings = new WebClient().DownloadString(TypeDNS ? "https://cdn.jsdelivr.net/gh/AuroraDNS/AuroraDNS.github.io/DNS.list"
                        : "https://cdn.jsdelivr.net/gh/AuroraDNS/AuroraDNS.github.io/DoH.list").Split('\n').ToList();
                }
                catch (Exception exception)
                {
                    MyTools.BgwLog(@"| DownloadString failed : " + exception);
                }

                if (string.IsNullOrWhiteSpace(ListStrings[ListStrings.Count - 1]))
                {
                    ListStrings.RemoveAt(ListStrings.Count - 1);
                }
            };
            bgWorker.RunWorkerCompleted += (o, args) =>
            {
                foreach (var item in ListStrings)
                {
                    SpeedListView.Items.Add(!TypeDNS
                        ? new SpeedList {
                        Server = item.Split('/', ':')[3]
                    }
                        : new SpeedList {
                        Server = item
                    });
                }
                IsEnabled = true;
            };
            bgWorker.RunWorkerAsync();
        }
Example #7
0
        public static async Task ServerOnQueryReceived(object sender, QueryReceivedEventArgs e)
        {
            if (!(e.Query is DnsMessage query))
            {
                return;
            }

            IPAddress clientAddress = e.RemoteEndpoint.Address;

            if (DnsSettings.EDnsCustomize)
            {
                if (Equals(DnsSettings.EDnsIp, IPAddress.Parse("0.0.0.1")))
                {
                    clientAddress = IPAddress.Parse(MainWindow.IntIPAddr.ToString().Substring(0,
                                                                                              MainWindow.IntIPAddr.ToString().LastIndexOf(".", StringComparison.Ordinal)) + ".0");
                }
                else
                {
                    clientAddress = DnsSettings.EDnsIp;
                }
            }

            else if (Equals(clientAddress, IPAddress.Loopback) ||
                     IpTools.InSameLaNet(clientAddress, MainWindow.LocIPAddr))
            {
                clientAddress = MainWindow.IntIPAddr;
            }

            DnsMessage response = query.CreateResponseInstance();

            if (query.Questions.Count <= 0)
            {
                response.ReturnCode = ReturnCode.ServerFailure;
            }

            else
            {
                foreach (DnsQuestion dnsQuestion in query.Questions)
                {
                    response.ReturnCode = ReturnCode.NoError;

                    if (DnsSettings.DebugLog)
                    {
                        MyTools.BgwLog($@"| {DateTime.Now} {e.RemoteEndpoint.Address} : {dnsQuestion.Name} | {dnsQuestion.RecordType.ToString().ToUpper()}");
                    }

                    if (DnsSettings.BlackListEnable && BlackList.Contains(dnsQuestion.Name) && dnsQuestion.RecordType == RecordType.A)
                    {
                        //BlackList
                        ARecord blackRecord = new ARecord(dnsQuestion.Name, 10, IPAddress.Any);
                        response.AnswerRecords.Add(blackRecord);
                        if (DnsSettings.DebugLog)
                        {
                            MyTools.BgwLog(@"|- BlackList");
                        }
                    }

                    else if (DnsSettings.WhiteListEnable && WhiteList.ContainsKey(dnsQuestion.Name) && dnsQuestion.RecordType == RecordType.A)
                    {
                        //WhiteList
                        ARecord blackRecord = new ARecord(dnsQuestion.Name, 10, WhiteList[dnsQuestion.Name]);
                        response.AnswerRecords.Add(blackRecord);
                        if (DnsSettings.DebugLog)
                        {
                            MyTools.BgwLog(@"|- WhiteList");
                        }
                    }

                    else
                    {
                        //Resolve
                        try
                        {
                            var(resolvedDnsList, statusCode) = ResolveOverHttps(clientAddress.ToString(),
                                                                                dnsQuestion.Name.ToString(),
                                                                                DnsSettings.ProxyEnable, DnsSettings.WProxy, dnsQuestion.RecordType);
                            if (resolvedDnsList != null && resolvedDnsList != new List <dynamic>())
                            {
                                foreach (var item in resolvedDnsList)
                                {
                                    response.AnswerRecords.Add(item);
                                }
                            }
                            else
                            {
                                response.ReturnCode = (ReturnCode)statusCode;
                            }
                        }
                        catch (Exception ex)
                        {
                            response.ReturnCode = ReturnCode.ServerFailure;
                            MyTools.BgwLog(@"| " + ex);
                        }
                    }
                }
            }

            e.Response = response;
        }
Example #8
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     = null;
            List <dynamic> recordList = new List <dynamic>();

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

                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
                    {
                        MyTools.BgwLog(
                            $@"| Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode}");
                    }
                    catch (Exception exception)
                    {
                        MyTools.BgwLog($@"| Catch WebException : {exception.Message}");
                    }
                }
            }

            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");

                    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;
                    }
                    }
                }
            }

            return(recordList, statusCode);
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List <string> dohListStrings = null, dnsListStrings = null;
            var           bgWorker = new BackgroundWorker();

            bgWorker.DoWork += (o, args) =>
            {
                try
                {
                    dohListStrings = new WebClient()
                                     .DownloadString(UrlSettings.MDohList).Split('\n').ToList();
                    dnsListStrings = new WebClient()
                                     .DownloadString(UrlSettings.MDnsList).Split('\n').ToList();
                }
                catch (Exception exception)
                {
                    MyTools.BgwLog(@"| Download list failed : " + exception);
                }

                if (string.IsNullOrWhiteSpace(dohListStrings[dohListStrings.Count - 1]))
                {
                    dohListStrings.RemoveAt(dohListStrings.Count - 1);
                }
                if (string.IsNullOrWhiteSpace(dnsListStrings[dnsListStrings.Count - 1]))
                {
                    dnsListStrings.RemoveAt(dnsListStrings.Count - 1);
                }
            };
            bgWorker.RunWorkerCompleted += (o, args) =>
            {
                if (UrlSettings.MDohList.Contains(".list"))
                {
                    foreach (var item in dohListStrings)
                    {
                        DoHUrlText.Items.Add(item.Split('*')[0].Trim());
                    }
                }
                if (UrlSettings.MDnsList.Contains(".list"))
                {
                    foreach (var item in dnsListStrings)
                    {
                        BackupDNS.Items.Add(item.Split('*')[0].Trim());
                    }
                }

                if (File.Exists($"{MainWindow.SetupBasePath}doh.list"))
                {
                    foreach (var item in File.ReadAllLines($"{MainWindow.SetupBasePath}doh.list"))
                    {
                        DoHUrlText.Items.Add(item);
                    }
                }
                if (File.Exists($"{MainWindow.SetupBasePath}dns.list"))
                {
                    foreach (var item in File.ReadAllLines($"{MainWindow.SetupBasePath}dns.list"))
                    {
                        BackupDNS.Items.Add(item);
                    }
                }
            };
            bgWorker.RunWorkerAsync();
        }