Пример #1
0
        public async Task <PingbackResponse> ReceivePingAsync(string requestBody, string ip, Action <PingbackRecord> pingSuccessAction)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(requestBody))
                {
                    _logger.LogError("Pingback requestBody is null");
                    return(PingbackResponse.GenericError);
                }

                var valid = ValidatePingRequest(requestBody);
                if (!valid)
                {
                    return(PingbackResponse.InvalidPingRequest);
                }

                _logger.LogInformation($"Processing Pingback from: {_sourceUrl} ({ip}) to {_targetUrl}");

                var pingRequest = await _pingSourceInspector.ExamineSourceAsync(_sourceUrl, _targetUrl);

                if (null == pingRequest)
                {
                    return(PingbackResponse.InvalidPingRequest);
                }
                if (!pingRequest.SourceHasLink)
                {
                    _logger.LogError("Pingback error: The source URI does not contain a link to the target URI.");
                    return(PingbackResponse.Error17SourceNotContainTargetUri);
                }
                if (pingRequest.ContainsHtml)
                {
                    _logger.LogWarning("Spam detected on current Pingback...");
                    return(PingbackResponse.SpamDetectedFakeNotFound);
                }

                var(Id, Title) = await _pingbackRepository.GetPostIdTitle(pingRequest.TargetUrl, _dbConnection);

                if (Id == Guid.Empty)
                {
                    _logger.LogError($"Can not get post id and title for url '{pingRequest.TargetUrl}'");
                    return(PingbackResponse.Error32TargetUriNotExist);
                }
                _logger.LogInformation($"Post '{Id}:{Title}' is found for ping.");

                var pinged = await _pingbackRepository.HasAlreadyBeenPinged(Id, pingRequest.SourceUrl, ip, _dbConnection);

                if (pinged)
                {
                    return(PingbackResponse.Error48PingbackAlreadyRegistered);
                }

                _logger.LogInformation("Adding received pingback...");

                var uri = new Uri(_sourceUrl);
                var obj = new PingbackRecord
                {
                    Id              = Guid.NewGuid(),
                    PingTimeUtc     = DateTime.UtcNow,
                    Domain          = uri.Host,
                    SourceUrl       = _sourceUrl,
                    SourceTitle     = pingRequest.Title,
                    TargetPostId    = Id,
                    TargetPostTitle = Title,
                    SourceIp        = ip
                };

                await _pingbackRepository.SavePingbackRecordAsync(obj, _dbConnection);

                pingSuccessAction?.Invoke(obj);

                return(PingbackResponse.Success);
            }
            catch (Exception e)
            {
                _logger.LogError(e, nameof(ReceivePingAsync));
                return(PingbackResponse.GenericError);
            }
        }
Пример #2
0
        public async Task <PingbackResponse> ReceivePingAsync(string requestBody, string ip, Action <PingbackEntity> pingSuccessAction)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(requestBody))
                {
                    _logger.LogError("Pingback requestBody is null");
                    return(PingbackResponse.GenericError);
                }

                var valid = ValidateRequest(requestBody);
                if (!valid)
                {
                    return(PingbackResponse.InvalidPingRequest);
                }

                _logger.LogInformation($"Processing Pingback from: {_sourceUrl} ({ip}) to {_targetUrl}");

                var pingRequest = await _pingSourceInspector.ExamineSourceAsync(_sourceUrl, _targetUrl);

                if (null == pingRequest)
                {
                    return(PingbackResponse.InvalidPingRequest);
                }
                if (!pingRequest.SourceHasLink)
                {
                    _logger.LogError("Pingback error: The source URI does not contain a link to the target URI.");
                    return(PingbackResponse.Error17SourceNotContainTargetUri);
                }
                if (pingRequest.ContainsHtml)
                {
                    _logger.LogWarning("Spam detected on current Pingback...");
                    return(PingbackResponse.SpamDetectedFakeNotFound);
                }

                var(slug, pubDate) = GetSlugInfoFromUrl(pingRequest.TargetUrl);
                var spec = new PostSpec(pubDate, slug);
                var(id, title) = await _postRepo.SelectFirstOrDefaultAsync(spec, p => new Tuple <Guid, string>(p.Id, p.Title));

                if (id == Guid.Empty)
                {
                    _logger.LogError($"Can not get post id and title for url '{pingRequest.TargetUrl}'");
                    return(PingbackResponse.Error32TargetUriNotExist);
                }

                _logger.LogInformation($"Post '{id}:{title}' is found for ping.");

                var pinged = _pingbackRepo.Any(p =>
                                               p.TargetPostId == id &&
                                               p.SourceUrl == pingRequest.SourceUrl &&
                                               p.SourceIp.Trim() == ip);

                if (pinged)
                {
                    return(PingbackResponse.Error48PingbackAlreadyRegistered);
                }

                _logger.LogInformation("Adding received pingback...");

                var uri = new Uri(_sourceUrl);
                var obj = new PingbackEntity
                {
                    Id              = Guid.NewGuid(),
                    PingTimeUtc     = DateTime.UtcNow,
                    Domain          = uri.Host,
                    SourceUrl       = _sourceUrl,
                    SourceTitle     = pingRequest.Title,
                    TargetPostId    = id,
                    TargetPostTitle = title,
                    SourceIp        = ip
                };

                await _pingbackRepo.AddAsync(obj);

                pingSuccessAction?.Invoke(obj);

                return(PingbackResponse.Success);
            }
            catch (Exception e)
            {
                _logger.LogError(e, nameof(ReceivePingAsync));
                return(PingbackResponse.GenericError);
            }
        }