feat: 外链中转 /go——点击统计 + 拦截 + 隐藏真实链接

- redirects 表 + UrlRedirector:go_url() 站外链接改写为 /go/{hash}(确定性 hash、同 URL 复用、并发安全、缓存)
- /go/{hash} 302 跳转 + 点击计数 + 最后点击时间;停用状态 410(二次拦截);后台「外链中转」资源可查看点击量/启停/删除
- 接入点:正文外链(渲染器统一改写)、评论正文自动链接(comment_body)、评论作者网址、友链(侧边栏+友链页);站内/相对/mailto/tel/锚点不中转
- 测试 6 个(改写/计数/拦截/复用/渲染/评论正文);README 补充
This commit is contained in:
ak
2026-08-13 01:28:10 +08:00
parent 5f74dd76e4
commit 8f1ff6e3f9
17 changed files with 421 additions and 8 deletions
+28
View File
@@ -42,3 +42,31 @@ if (! function_exists('blog_setting')) {
return \App\Models\Setting::get($key, $default);
}
}
if (! function_exists('go_url')) {
/**
* 外链中转:站外链接改写为 /go/{hash}(点击统计/拦截),站内链接原样返回。
*/
function go_url(string $url): string
{
return app(\App\Blog\Services\UrlRedirector::class)->goUrl($url);
}
}
if (! function_exists('comment_body')) {
/**
* 评论正文:转义 + 自动链接 URL(走 /go 中转,nofollow)。
*/
function comment_body(string $text): string
{
$text = e($text);
$text = preg_replace_callback(
'#(https?://[^\s<>"\'()]+)#i',
fn (array $m) => '<a href="'.e(go_url($m[1])).'" target="_blank" rel="nofollow noopener">'.$m[1].'</a>',
$text
);
return nl2br($text);
}
}