示例#1
0
        static void Main(string[] args)
        {
            using (var dnsServer = new DnsServer(IPAddress.Any, 10, 10))
            {
                DNSEncoder.Init();
                var client     = new QuicClient();
                var connection = client.Connect("127.0.0.1", 11000);
                var stream     = connection.CreateStream(QuickNet.Utilities.StreamType.ClientBidirectional);
                async Task OnDnsServerOnQueryReceived(object sender, QueryReceivedEventArgs e)
                {
                    if (!(e.Query is DnsMessage query))
                    {
                        return;
                    }
                    await Task.Run(() =>
                    {
                        stream.Send(DNSEncoder.Encode(query));
                        e.Response = DnsMessage.Parse(stream.Receive());
                    });
                }

                dnsServer.QueryReceived += OnDnsServerOnQueryReceived;
                dnsServer.Start();
                Console.WriteLine(@"-------ARASHI Q DNS------");
                Console.WriteLine(@"-------DOTNET ALPHA------");

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(@"ArashiDNS Server Running");

                Console.WriteLine(@"Press any key to stop dns server");
                Console.WriteLine("------------------------");
                Console.ReadLine();
                Console.WriteLine("------------------------");
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            DNSEncoder.Init();
            var server = new WebSocketServer("ws://0.0.0.0:3030");

            server.ListenerSocket.NoDelay = true;
            server.Start(ws =>
            {
                ws.OnOpen   = () => Console.WriteLine(ws.ConnectionInfo.ClientIpAddress + ":Open!");
                ws.OnClose  = () => Console.WriteLine(ws.ConnectionInfo.ClientIpAddress + ":Close!");
                ws.OnBinary = msg =>
                {
                    using (var bgWorker = new BackgroundWorker())
                    {
                        bgWorker.DoWork += (sender, eventArgs) =>
                        {
                            var dnsQMsg = DnsMessage.Parse(msg);
                            var dnsRMsg =
                                new DnsClient(IPAddress.Parse("8.8.8.8"), 1000).SendMessage(dnsQMsg);
                            ws.Send(DNSEncoder.Encode(dnsRMsg));

                            Console.ForegroundColor = ConsoleColor.Cyan;
                            dnsQMsg.Questions.ForEach(o => Console.WriteLine("Qu:" + o));
                            Console.ForegroundColor = ConsoleColor.Green;
                            dnsRMsg.AnswerRecords.ForEach(o => Console.WriteLine("An:" + o));
                            Console.ForegroundColor = ConsoleColor.DarkGreen;
                            dnsRMsg.AuthorityRecords.ForEach(o => Console.WriteLine("Au:" + o));
                            Console.ForegroundColor = ConsoleColor.Green;
                        };
                        bgWorker.RunWorkerAsync();
                    }
                };
            });

            Console.ReadLine();
        }
示例#3
0
        public static (List <DnsRecordBase> list, ReturnCode statusCode) ResolveOverHttpsByDnsMsg(string clientIpAddress, string domainName, string dohUrl,
                                                                                                  bool proxyEnable = false, IWebProxy wProxy = null, RecordType type = RecordType.A)
        {
            DnsMessage dnsMsg;
            DnsMessage dnsQMsg = new DnsMessage();

            dnsQMsg.Questions.Add(new DnsQuestion(DomainName.Parse(domainName), type, RecordClass.INet));
            dnsQMsg.IsEDnsEnabled = true;
            dnsQMsg.IsQuery       = true;
            dnsQMsg.EDnsOptions.Options.Add(new ClientSubnetOption(24, IPAddress.Parse(clientIpAddress)));
            var dnsBase64String = Convert.ToBase64String(DNSEncoder.Encode(dnsQMsg)).TrimEnd('=')
                                  .Replace('+', '-').Replace('/', '_');

            try
            {
                var dnsDataBytes = MyCurl.GetData(
                    $"{dohUrl}?ct=application/dns-message&dns={dnsBase64String}",
                    DnsSettings.Http2Enable, proxyEnable, wProxy, DnsSettings.AllowAutoRedirect);
                dnsMsg = DnsMessage.Parse(dnsDataBytes);

                if (DnsSettings.Ipv4Disable || DnsSettings.Ipv6Disable)
                {
                    foreach (var item in dnsMsg.AnswerRecords.ToArray())
                    {
                        if (item.RecordType == RecordType.A && DnsSettings.Ipv4Disable)
                        {
                            dnsMsg.AnswerRecords.Remove(item);
                        }
                        if (item.RecordType == RecordType.Aaaa && DnsSettings.Ipv6Disable)
                        {
                            dnsMsg.AnswerRecords.Remove(item);
                        }
                    }
                }
            }
            catch (WebException e)
            {
                HttpWebResponse response = (HttpWebResponse)e.Response;
                try
                {
                    BackgroundLog(
                        $@"| - Catch WebException : {Convert.ToInt32(response.StatusCode)} {response.StatusCode} | {e.Status} |  {domainName} | {response.ResponseUri} | {dnsBase64String}");
                    if (response.StatusCode == HttpStatusCode.BadRequest)
                    {
                        DnsSettings.DnsMsgEnable = false;
                    }
                }
                catch (Exception exception)
                {
                    BackgroundLog(
                        $@"| - Catch WebException : {exception.Message} | {e.Status} | {domainName} | {dohUrl} | {dnsBase64String}");
                }

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