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 }); } }
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); } }
public PingSuccessEventArgs(string domain, PingRequest pingRequest) { Domain = domain; PingRequest = pingRequest; }