Example #1
0
 protected override BWebServiceResponse OnRequestPP(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
 {
     if (UrlParameters.ContainsKey("secret") && UrlParameters["secret"] == InternalCallPrivateKey)
     {
         return(Process(_Context, _ErrorMessageAction));
     }
     return(BWebResponse.Forbidden("You are trying to access to a private service."));
 }
Example #2
0
        protected override BWebServiceResponse OnRequestPP(HttpListenerContext _Context, Action <string> _ErrorMessageAction = null)
        {
            _ErrorMessageAction?.Invoke($"InternalWebServiceBaseWebhook->OnRequest: Message received. HttpMethod: {_Context.Request.HttpMethod}");

            // Cloud Event Schema v1.0
            // https://github.com/cloudevents/spec/blob/v1.0/http-webhook.md#4-abuse-protection
            if (_Context.Request.HttpMethod == "OPTIONS")
            {
                var WebhookRequestCallbak = _Context.Request.Headers.Get("webhook-request-callback");
                var WebhookRequestOrigin  = _Context.Request.Headers.Get("webhook-request-origin");

                BTaskWrapper.Run(() =>
                {
                    Thread.CurrentThread.IsBackground = true;

                    Thread.Sleep(3000);
                    using var Handler = new HttpClientHandler
                          {
                              SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls,
                              ServerCertificateCustomValidationCallback = (a, b, c, d) => true
                          };
                    using var Client = new HttpClient(Handler);
                    Client.DefaultRequestHeaders.TryAddWithoutValidation("WebHook-Allowed-Origin", WebhookRequestOrigin);
                    Client.DefaultRequestHeaders.TryAddWithoutValidation("WebHook-Allowed-Rate", "*");
                    using (var RequestTask = Client.GetAsync(WebhookRequestCallbak))
                    {
                        RequestTask.Wait();
                        using var Response        = RequestTask.Result;
                        using var ResponseContent = Response.Content;

                        using var ReadResponseTask = ResponseContent.ReadAsStringAsync();
                        ReadResponseTask.Wait();

                        var ResponseString        = ReadResponseTask.Result;
                        var ResponseStatusCode    = (int)Response.StatusCode;
                        var ResponseSuccessString = Response.IsSuccessStatusCode ? "Request is successful." : "Request has an error.";
                        _ErrorMessageAction?.Invoke($"InternalWebServiceBaseWebhook->ValidationRequest: {ResponseSuccessString} Origin: '{WebhookRequestOrigin}', Response: '{ResponseString}', Code: '{ResponseStatusCode}'");
                    }
                });

                return(BWebResponse.StatusOK("OK."));
            }

            if (UrlParameters.ContainsKey("secret") && UrlParameters["secret"] == InternalCallPrivateKey)
            {
                return(Process(_Context, _ErrorMessageAction));
            }

            return(BWebResponse.Forbidden("You are trying to access to a private service."));
        }