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));
        }
        public void Respond(HttpWebServerResponseData response)
        {
            try {
                if (Stream != null)
                {
                    byte[] bData = Encoding.UTF8.GetBytes(response.ToString());

                    Stream.Write(bData, 0, bData.Length);

                    if (ResponseSent != null)
                    {
                        FrostbiteConnection.RaiseEvent(ResponseSent.GetInvocationList(), this, response);
                    }

                    Shutdown();
                }
            }
            catch (Exception e) {
            }
        }
Exemple #4
0
        private async Task QueueListener_RequestReceived(object sender, RequestReceivedEventArgs e)
        {
            var iMessages = Counters.IncrementMessages();

            try
            {
                Counters.IncrementReceivingTime(e.Request.Header.TotalTime);
                if (_serverQueues?.AdditionalSendQueues?.Any() == true)
                {
                    e.ResponseQueues.AddRange(_serverQueues.AdditionalSendQueues);
                }
                e.Response.Header.Response.Label = Config.ResponseOptions.ServerSenderOptions.Label;
                Core.Log.InfoDetail("Request message received with CorrelationId = {0} . Current messages processing = {1}", e.Request.CorrelationId, iMessages);
                if (RequestReceived != null)
                {
                    await RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }
                e.Response.Header.Response.Label = string.IsNullOrEmpty(e.Response.Header.Response.Label) ? e.Response.Body?.ToString() ?? typeof(ResponseMessage).FullName : e.Response.Header.Response.Label;
                if (MQueueServerEvents.RequestReceived != null)
                {
                    await MQueueServerEvents.RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }

                if (e.SendResponse && e.Response?.Body != ResponseMessage.NoResponse)
                {
                    e.Response.Header.Response.ApplicationSentDate = Core.Now;

                    ResponseSentEventArgs rsea = null;
                    if (BeforeSendResponse != null || MQueueServerEvents.BeforeSendResponse != null ||
                        ResponseSent != null || MQueueServerEvents.ResponseSent != null)
                    {
                        rsea = new ResponseSentEventArgs(Name, e.Response);
                        if (BeforeSendResponse != null)
                        {
                            await BeforeSendResponse.InvokeAsync(this, rsea).ConfigureAwait(false);
                        }
                        if (MQueueServerEvents.BeforeSendResponse != null)
                        {
                            await MQueueServerEvents.BeforeSendResponse.InvokeAsync(this, rsea).ConfigureAwait(false);
                        }
                    }

                    var sentBytes = await OnSendAsync(e.Response, e).ConfigureAwait(false);

                    if (sentBytes > -1)
                    {
                        if (rsea != null)
                        {
                            rsea.MessageLength = sentBytes;
                            if (ResponseSent != null)
                            {
                                await ResponseSent.InvokeAsync(this, rsea).ConfigureAwait(false);
                            }
                            if (MQueueServerEvents.ResponseSent != null)
                            {
                                await MQueueServerEvents.ResponseSent.InvokeAsync(this, rsea).ConfigureAwait(false);
                            }
                        }
                    }
                    else
                    {
                        Core.Log.Warning("The message couldn't be sent.");
                    }
                }
                Counters.DecrementMessages();
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception)
            {
                Counters.IncrementTotalExceptions();
                Counters.DecrementMessages();
                throw;
            }
        }
 private void NotifyResponseSent(int statusCode, string?contentMediaType)
 {
     ResponseSent?.Invoke(this, new ResponseSentHandlerArgs(statusCode, contentMediaType));
 }
Exemple #6
0
 internal void HandleResponseSent(object sender, ResponseEventArgs args)
 {
     WrappedEventHandler(() => ResponseSent?.Invoke(sender, args), "ResponseSent", sender);
 }
Exemple #7
0
        private async Task QueueListener_RequestReceived(object sender, RawRequestReceivedEventArgs e)
        {
            var iMessages = Counters.IncrementMessages();

            try
            {
                if (_serverQueues?.AdditionalSendQueues?.Any() == true)
                {
                    e.ResponseQueues.AddRange(_serverQueues.AdditionalSendQueues);
                }
                Counters.IncrementTotalReceivingBytes(e.MessageLength);
                Core.Log.InfoDetail("Request message received with CorrelationId = {0} . Current messages processing = {1}", e.CorrelationId, iMessages);
                if (RequestReceived != null)
                {
                    await RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }
                if (MQueueRawServerEvents.RequestReceived != null)
                {
                    await MQueueRawServerEvents.RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }

                if (e.SendResponse && e.Response != null)
                {
                    var response = e.Response;

                    RawResponseSentEventArgs rsea = null;
                    if (BeforeSendResponse != null || MQueueRawServerEvents.BeforeSendResponse != null ||
                        ResponseSent != null || MQueueRawServerEvents.ResponseSent != null)
                    {
                        rsea = new RawResponseSentEventArgs(Name, response, e.CorrelationId);
                        if (BeforeSendResponse != null)
                        {
                            await BeforeSendResponse.InvokeAsync(this, rsea).ConfigureAwait(false);
                        }
                        if (MQueueRawServerEvents.BeforeSendResponse != null)
                        {
                            await MQueueRawServerEvents.BeforeSendResponse.InvokeAsync(this, rsea).ConfigureAwait(false);
                        }
                    }


                    response = rsea?.Message ?? e.Response;
                    var sentBytes = await OnSendAsync(response, e).ConfigureAwait(false);

                    if (sentBytes > -1)
                    {
                        if (rsea != null)
                        {
                            rsea.MessageLength = sentBytes;
                            if (ResponseSent != null)
                            {
                                await ResponseSent.InvokeAsync(this, rsea).ConfigureAwait(false);
                            }
                            if (MQueueRawServerEvents.ResponseSent != null)
                            {
                                await MQueueRawServerEvents.ResponseSent.InvokeAsync(this, rsea).ConfigureAwait(false);
                            }
                        }
                    }
                    else
                    {
                        Core.Log.Warning("The message couldn't be sent.");
                    }
                }
                Counters.DecrementMessages();
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception)
            {
                Counters.IncrementTotalExceptions();
                Counters.DecrementMessages();
                throw;
            }
        }
Exemple #8
0
        private async Task QueueListener_RequestReceived(object sender, RequestReceivedEventArgs e)
        {
            var iMessages = Counters.IncrementMessages();

            try
            {
                Counters.IncrementReceivingTime(e.Request.Header.TotalTime);
                Counters.IncrementTotalReceivingBytes(e.MessageLength);
                if (_serverQueues?.AdditionalSendQueues?.Any() == true)
                {
                    e.ResponseQueues.AddRange(_serverQueues.AdditionalSendQueues);
                }

                #region Client Queues Routes Rebindings
                if (_serverQueues?.ClientQueuesRoutesRebindings != null)
                {
                    Core.Log.LibDebug("Processing ClientQueues Route Rebinding");
                    foreach (var queue in e.ResponseQueues)
                    {
                        if (queue.IsSkippingRoute())
                        {
                            continue;
                        }
                        if (_serverQueues.ClientQueuesRoutesRebindings.TryGet(queue.Route, out var qRebinding))
                        {
                            Core.Log.LibVerbose("Rebinding Route: '{0}' to '{1}'", queue.Route, qRebinding.Value);
                            queue.Route = qRebinding.Value;
                        }
                        else
                        {
                            Core.Log.LibVerbose("Route '{0}' doesn't have a Rebinding.", queue.Route);
                        }
                    }
                }
                #endregion

                e.Response.Header.Response.Label = Config.ResponseOptions.ServerSenderOptions.Label;
                Core.Log.LibDebug("Request message received with CorrelationId = {0} . Current messages processing = {1}", e.Request.CorrelationId, iMessages);
                if (RequestReceived != null)
                {
                    await RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }
                e.Response.Header.Response.Label = string.IsNullOrEmpty(e.Response.Header.Response.Label) ? ResponseMessageFullName : e.Response.Header.Response.Label;
                if (MQueueServerEvents.RequestReceived != null)
                {
                    await MQueueServerEvents.RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }

                if (e.SendResponse && e.Response != null && e.Response.Body != ResponseMessage.NoResponseSerialized)
                {
                    e.Response.Header.Response.ApplicationSentDate = Core.Now;

                    ResponseSentEventArgs rsea = null;
                    if (BeforeSendResponse != null || MQueueServerEvents.BeforeSendResponse != null ||
                        ResponseSent != null || MQueueServerEvents.ResponseSent != null)
                    {
                        rsea = new ResponseSentEventArgs(Name, e.Response);
                        Task t1 = null, t2 = null;
                        if (BeforeSendResponse != null)
                        {
                            t1 = BeforeSendResponse.InvokeAsync(this, rsea);
                        }
                        if (MQueueServerEvents.BeforeSendResponse != null)
                        {
                            t2 = MQueueServerEvents.BeforeSendResponse.InvokeAsync(this, rsea);
                        }
                        if (t1 != null)
                        {
                            await t1.ConfigureAwait(false);
                        }
                        if (t2 != null)
                        {
                            await t2.ConfigureAwait(false);
                        }
                    }

                    var sentBytes = await OnSendAsync(e.Response, e).ConfigureAwait(false);

                    if (sentBytes > -1)
                    {
                        if (rsea != null)
                        {
                            rsea.MessageLength = sentBytes;
                            Task t1 = null, t2 = null;
                            if (ResponseSent != null)
                            {
                                t1 = ResponseSent.InvokeAsync(this, rsea);
                            }
                            if (MQueueServerEvents.ResponseSent != null)
                            {
                                t2 = MQueueServerEvents.ResponseSent.InvokeAsync(this, rsea);
                            }
                            if (t1 != null)
                            {
                                await t1.ConfigureAwait(false);
                            }
                            if (t2 != null)
                            {
                                await t2.ConfigureAwait(false);
                            }
                        }
                    }
                    else
                    {
                        Core.Log.Warning("The message couldn't be sent.");
                    }
                }
                Counters.DecrementMessages();
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception)
            {
                Counters.IncrementTotalExceptions();
                Counters.DecrementMessages();
                throw;
            }
        }
Exemple #9
0
        private async Task QueueListener_RequestReceived(object sender, RawRequestReceivedEventArgs e)
        {
            var iMessages = Counters.IncrementMessages();

            try
            {
                if (_serverQueues?.AdditionalSendQueues?.Any() == true)
                {
                    e.ResponseQueues.AddRange(_serverQueues.AdditionalSendQueues);
                }

                #region Client Queues Routes Rebindings
                if (_serverQueues.ClientQueuesRoutesRebindings != null)
                {
                    Core.Log.LibDebug("Processing ClientQueues Route Rebinding");
                    foreach (var queue in e.ResponseQueues)
                    {
                        if (queue.IsSkippingRoute())
                        {
                            continue;
                        }
                        if (_serverQueues.ClientQueuesRoutesRebindings.TryGet(queue.Route, out var qRebinding))
                        {
                            Core.Log.LibVerbose("Rebinding Route: '{0}' to '{1}'", queue.Route, qRebinding.Value);
                            queue.Route = qRebinding.Value;
                        }
                        else
                        {
                            Core.Log.LibVerbose("Route '{0}' doesn't have a Rebinding.", queue.Route);
                        }
                    }
                }
                #endregion

                Counters.IncrementTotalReceivingBytes(e.MessageLength);
                Core.Log.LibDebug("Request message received with CorrelationId = {0} . Current messages processing = {1}", e.CorrelationId, iMessages);
                if (RequestReceived != null)
                {
                    await RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }
                if (MQueueRawServerEvents.RequestReceived != null)
                {
                    await MQueueRawServerEvents.RequestReceived.InvokeAsync(sender, e).ConfigureAwait(false);
                }

                if (e.SendResponse && e.Response != MultiArray <byte> .Empty)
                {
                    var response = e.Response;

                    RawResponseSentEventArgs rsea = null;
                    if (BeforeSendResponse != null || MQueueRawServerEvents.BeforeSendResponse != null ||
                        ResponseSent != null || MQueueRawServerEvents.ResponseSent != null)
                    {
                        rsea = new RawResponseSentEventArgs(Name, response, e.CorrelationId);
                        if (BeforeSendResponse != null)
                        {
                            await BeforeSendResponse.InvokeAsync(this, rsea).ConfigureAwait(false);
                        }
                        if (MQueueRawServerEvents.BeforeSendResponse != null)
                        {
                            await MQueueRawServerEvents.BeforeSendResponse.InvokeAsync(this, rsea).ConfigureAwait(false);
                        }
                    }


                    response = rsea?.Message ?? e.Response;
                    var sentBytes = await OnSendAsync(response, e).ConfigureAwait(false);

                    if (sentBytes > -1)
                    {
                        if (rsea != null)
                        {
                            rsea.MessageLength = sentBytes;
                            if (ResponseSent != null)
                            {
                                await ResponseSent.InvokeAsync(this, rsea).ConfigureAwait(false);
                            }
                            if (MQueueRawServerEvents.ResponseSent != null)
                            {
                                await MQueueRawServerEvents.ResponseSent.InvokeAsync(this, rsea).ConfigureAwait(false);
                            }
                        }
                    }
                    else
                    {
                        Core.Log.Warning("The message couldn't be sent.");
                    }
                }
                Counters.DecrementMessages();
                Counters.IncrementTotalMessagesProccesed();
            }
            catch (Exception)
            {
                Counters.IncrementTotalExceptions();
                Counters.DecrementMessages();
                throw;
            }
        }