예제 #1
0
        public async Task <PingRequest> ExamineSourceAsync(string sourceUrl, string targetUrl, int timeoutSeconds = 30)
        {
            try
            {
                var regexHtml = new Regex(
                    @"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>",
                    RegexOptions.Singleline | RegexOptions.Compiled);

                var regexTitle = new Regex(
                    @"(?<=<title.*>)([\s\S]*)(?=</title>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(timeoutSeconds) };
                var html = await httpClient.GetStringAsync(sourceUrl);

                var title         = regexTitle.Match(html).Value.Trim();
                var containsHtml  = regexHtml.IsMatch(title);
                var sourceHasLink = html.ToUpperInvariant().Contains(targetUrl.ToUpperInvariant());

                var pingRequest = new PingRequest
                {
                    Title         = title,
                    ContainsHtml  = containsHtml,
                    SourceHasLink = sourceHasLink,
                    TargetUrl     = targetUrl,
                    SourceUrl     = sourceUrl
                };

                return(pingRequest);
            }
            catch (WebException ex)
            {
                _logger.LogError(ex, nameof(ExamineSourceAsync));
                return(null);
            }
        }
예제 #2
0
        private async Task <PingRequest> ExamineSourceAsync()
        {
            try
            {
                var regexHtml = new Regex(
                    @"</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>",
                    RegexOptions.Singleline | RegexOptions.Compiled);

                var regexTitle = new Regex(
                    @"(?<=<title.*>)([\s\S]*)(?=</title>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                using (var httpClient = new HttpClient {
                    Timeout = TimeSpan.FromSeconds(RemoteTimeout)
                })
                {
                    var html = await httpClient.GetStringAsync(_sourceUrl);

                    var title = regexTitle.Match(html).Value.Trim();
                    Logger.LogInformation($"ExamineSourceAsync:title: {title}");

                    var containsHtml = regexHtml.IsMatch(title);
                    Logger.LogInformation($"ExamineSourceAsync:containsHtml: {containsHtml}");

                    var sourceHasLink = html.ToUpperInvariant().Contains(_targetUrl.ToUpperInvariant());
                    Logger.LogInformation($"ExamineSourceAsync:sourceHasLink: {sourceHasLink}");

                    var pingRequest = new PingRequest
                    {
                        SourceDocumentInfo = new SourceDocumentInfo
                        {
                            Title         = title,
                            ContainsHtml  = containsHtml,
                            SourceHasLink = sourceHasLink
                        },
                        TargetUrl       = _targetUrl,
                        SourceUrl       = _sourceUrl,
                        SourceIpAddress = _remoteIpAddress
                    };

                    return(pingRequest);
                }
            }
            catch (WebException ex)
            {
                Logger.LogError(ex, nameof(ExamineSourceAsync));
                return(new PingRequest
                {
                    SourceDocumentInfo = new SourceDocumentInfo
                    {
                        SourceHasLink = false
                    },
                    SourceUrl = _sourceUrl,
                    SourceIpAddress = _remoteIpAddress
                });
            }
        }
예제 #3
0
        public PingbackServiceResponse ProcessReceivedPingback(PingRequest req, Func <bool> ifTargetResourceExists, Func <bool> ifAlreadyBeenPinged)
        {
            try
            {
                if (null == req)
                {
                    return(PingbackServiceResponse.InvalidPingRequest);
                }

                var ti = ifTargetResourceExists();
                if (!ti)
                {
                    return(PingbackServiceResponse.Error32TargetUriNotExist);
                }

                var pd = ifAlreadyBeenPinged();
                if (pd)
                {
                    return(PingbackServiceResponse.Error48PingbackAlreadyRegistered);
                }

                if (req.SourceDocumentInfo.SourceHasLink && !req.SourceDocumentInfo.ContainsHtml)
                {
                    Logger.LogInformation("Adding received pingback...");
                    var domain = GetDomain(_sourceUrl);

                    OnPingSuccess?.Invoke(this, new PingSuccessEventArgs(domain, req));
                    return(PingbackServiceResponse.Success);
                }

                if (!req.SourceDocumentInfo.SourceHasLink)
                {
                    Logger.LogError("Pingback error: The source URI does not contain a link to the target URI, and so cannot be used as a source.");
                    return(PingbackServiceResponse.Error17SourceNotContainTargetUri);
                }
                Logger.LogWarning("Spam detected on current Pingback...");
                return(PingbackServiceResponse.SpamDetectedFakeNotFound);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, nameof(ProcessReceivedPingback));
                return(PingbackServiceResponse.GenericError);
            }
        }
예제 #4
0
 public PingSuccessEventArgs(string domain, PingRequest pingRequest)
 {
     Domain      = domain;
     PingRequest = pingRequest;
 }