Exemplo n.º 1
0
        public Task HandleDnsConnection(InConnectionDns dns)
        {
            var req  = dns.DnsRequest;
            var name = TryGetName(req.Name);

            if (name == null)
            {
                dns.RedirectTo(if_notmatch);
                return(NaiveUtils.CompletedTask);
            }
            if (name.Length == 0 || name == list_name)
            {
                return(dns.SetResult(new DnsResponse(this, new IPAddress((long)listIp))));
            }
            var clis = FindClientsByName(name);

            if (clis.Count > 0)
            {
                return(dns.SetResult(new DnsResponse(this, clis.Select(x => x.Ip).ToArray())));
            }
            else
            {
                if (if_notfound == null)
                {
                    return(dns.SetResult(DnsResponse.Empty(this)));
                }
                dns.RedirectTo(if_notfound);
                return(NaiveUtils.CompletedTask);
            }
        }
Exemplo n.º 2
0
        public async Task <DnsResponse> ResolveName(IAdapter creator, AdapterRef handler, DnsRequest request)
        {
            var cxn = InConnectionDns.Create(creator, request);

            await HandleInConnection(cxn, handler);

            var result = cxn.ConnectResult;

            if (result?.Ok == false)
            {
                if (result.FailedReason != null)
                {
                    throw new Exception(result.FailedReason);
                }
                throw new Exception("name resolving failed.");
            }
            return(result as DnsResponse ?? DnsResponse.Empty(this));
        }
Exemplo n.º 3
0
        private async Task ResolveName(InConnectionDns cxn)
        {
            var name   = cxn.Dest.Host;
            var req    = dnsClient.Create();
            var domain = new Domain(name);

            req.Questions.Add(new Question(domain, cxn.RequestType != DnsRequestType.AAAA ? RecordType.A : RecordType.AAAA));
            req.OperationCode    = OperationCode.Query;
            req.RecursionDesired = true;
            IResponse r;

            try {
                r = await req.Resolve();
            } catch (ResponseException e) {
                var emptyResp = DnsResponse.Empty(this);
                emptyResp.Result       = ConnectResultEnum.Failed;
                emptyResp.FailedReason = e.Message;
                await cxn.SetResult(emptyResp);

                return;
            }
            if (r.ResponseCode != ResponseCode.NoError)
            {
                Logger.warning("resolving " + name + ": server returns " + r.ResponseCode);
            }
            int count = 0;

            foreach (var item in r.AnswerRecords)
            {
                if (item.Type == RecordType.A || item.Type == RecordType.AAAA)
                {
                    count++;
                }
            }
            if (count == 0)
            {
                if (r.AnswerRecords.Count == 0)
                {
                    Logger.warning("resolving " + name + ": no answer records");
                }
                else
                {
                    Logger.warning("resolving " + name + ": answer records without A/AAAA records");
                }
            }
            var arr = new IPAddress[count];
            int?ttl = null;
            int cur = 0;

            foreach (var item in r.AnswerRecords)
            {
                if (item.Type == RecordType.A || item.Type == RecordType.AAAA)
                {
                    arr[cur++] = ((IPAddressResourceRecord)item).IPAddress;
                    int newTtl = (int)item.TimeToLive.TotalSeconds;
                    ttl = ttl.HasValue ? Math.Min(newTtl, ttl.Value) : newTtl;
                }
            }
            var resp = new DnsResponse(this, arr)
            {
                TTL = ttl
            };
            await cxn.SetResult(resp);
        }