private void SendResponse(Response response)
 {
     try
     {
         foreach (var client in _server.ListClients())
         {
             string json = "";
             if (Settings.Default.AddJsonRootObject)
             {
                 var root = new
                 {
                     Response = response
                 };
                 json = root.ToJson();
             }
             else
             {
                 json = response.ToJson();
             }
             _server.SendAsync(client, json);
             ResponseSent?.Invoke(this, json);
         }
     }
     catch (Exception ex)
     {
         Error?.Invoke(this, ex);
     }
 }
        public Task <IResponse> Resolve(IRequest request)
        {
            IResponse response = Response.FromRequest(request);

            response.AuthorativeServer = true;

            foreach (Question question in response.Questions)
            {
                if (question.Type == RecordType.A)
                {
                    string requestDomain = question.Name.ToString().ToLower();
                    string emailDomain   = GetEmailDomain(requestDomain);

                    RequestReceived?.Invoke(this, new RequestEventArgs(response.Id, requestDomain, emailDomain));

                    if (emailDomain != null)
                    {
                        var(isValid, ttl) = GetEmailDomainState(emailDomain);

                        if (isValid)
                        {
                            var soaRecord = new StartOfAuthorityResourceRecord(_dnsServerDomain, _dnsServerDomain, _responsiblePersonDomain, _serial,
                                                                               TimeSpan.FromDays(2), TimeSpan.FromDays(1), TimeSpan.FromDays(30), ttl, TimeSpan.FromDays(14));

                            response.AuthorityRecords.Add(soaRecord);
                            response.ResponseCode = ResponseCode.NameError;

                            ResponseSent?.Invoke(this, new ResponseEventArgs(response.Id, requestDomain, emailDomain, ResponseResult.Valid, ttl));
                        }
                        else
                        {
                            var record = new IPAddressResourceRecord(question.Name, s_loopbackIpAddress, ttl);
                            response.AnswerRecords.Add(record);

                            ResponseSent?.Invoke(this, new ResponseEventArgs(response.Id, requestDomain, emailDomain, ResponseResult.Invalid, ttl));
                        }
                    }
                    else
                    {
                        // Invalid domain name request, cache for 1000 days
                        var ttl       = TimeSpan.FromDays(1000);
                        var soaRecord = new StartOfAuthorityResourceRecord(question.Name, _dnsServerDomain, _responsiblePersonDomain, _serial,
                                                                           TimeSpan.FromDays(2), TimeSpan.FromDays(1), TimeSpan.FromDays(30), ttl, ttl);

                        response.AuthorityRecords.Add(soaRecord);
                        response.ResponseCode = ResponseCode.FormatError;

                        ResponseSent?.Invoke(this, new ResponseEventArgs(response.Id, requestDomain, emailDomain, ResponseResult.Error, ttl));
                    }
                }
            }

            return(Task.FromResult(response));
        }
 private void NotifyResponseSent(int statusCode, string?contentMediaType)
 {
     ResponseSent?.Invoke(this, new ResponseSentHandlerArgs(statusCode, contentMediaType));
 }
Exemple #4
0
 internal void HandleResponseSent(object sender, ResponseEventArgs args)
 {
     WrappedEventHandler(() => ResponseSent?.Invoke(sender, args), "ResponseSent", sender);
 }