mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: add bot detection to skip recording views for known bots
This commit is contained in:
@@ -280,6 +280,8 @@ namespace SharpbinV3.Server.Controllers
|
|||||||
[Authorize(Policy = "NotBanned")]
|
[Authorize(Policy = "NotBanned")]
|
||||||
public async Task<IActionResult> RecordPasteView(string id)
|
public async Task<IActionResult> RecordPasteView(string id)
|
||||||
{
|
{
|
||||||
|
if (HttpContext.IsKnownBot())
|
||||||
|
return Ok(new { success = true, message = "View not recorded for known bot." });
|
||||||
var paste = await _pasteService.Get(id);
|
var paste = await _pasteService.Get(id);
|
||||||
if (paste == null)
|
if (paste == null)
|
||||||
return NotFound(new { success = false, message = "Paste not found." });
|
return NotFound(new { success = false, message = "Paste not found." });
|
||||||
|
|||||||
@@ -17,6 +17,25 @@ namespace SharpbinV3.Server.Extensions
|
|||||||
"X-ProxyUser-IP",
|
"X-ProxyUser-IP",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public static readonly string[] BotUserAgents =
|
||||||
|
[
|
||||||
|
"bot",
|
||||||
|
"crawl",
|
||||||
|
"spider",
|
||||||
|
"slurp",
|
||||||
|
"mediapartners-google",
|
||||||
|
"adsbot-google",
|
||||||
|
"googlebot",
|
||||||
|
"bingbot",
|
||||||
|
"yandexbot",
|
||||||
|
"duckduckbot",
|
||||||
|
"baiduspider",
|
||||||
|
"sogou",
|
||||||
|
"exabot",
|
||||||
|
"facebot",
|
||||||
|
"ia_archiver",
|
||||||
|
];
|
||||||
|
|
||||||
private const string ApiKeyContextKey = "ApiKey";
|
private const string ApiKeyContextKey = "ApiKey";
|
||||||
|
|
||||||
private static string? GetClaimValue(IEnumerable<Claim> claims, params string[] claimTypes)
|
private static string? GetClaimValue(IEnumerable<Claim> claims, params string[] claimTypes)
|
||||||
@@ -110,5 +129,11 @@ namespace SharpbinV3.Server.Extensions
|
|||||||
}
|
}
|
||||||
return context.Connection.RemoteIpAddress?.ToString() ?? "Unknown";
|
return context.Connection.RemoteIpAddress?.ToString() ?? "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsKnownBot(this HttpContext context)
|
||||||
|
{
|
||||||
|
var userAgent = context.Request.Headers.UserAgent.FirstOrDefault()?.ToLower() ?? "";
|
||||||
|
return BotUserAgents.Any(userAgent.Contains);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user