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:
@@ -46,7 +46,31 @@ class PostContentRenderer
|
||||
$html = app(\App\Blog\Support\PluginManager::class)->applyFilters('post.rendered', $html, $post);
|
||||
|
||||
// 兜底:任何情况下不把裸 [paid] 标签输出到页面(如会员插件被停用时)
|
||||
return preg_replace('/\[(\/?paid)\]/i', '', $html);
|
||||
$html = preg_replace('/\[(\/?paid)\]/i', '', $html);
|
||||
|
||||
// 外链中转:站外链接改写为 /go/{hash}(点击统计/拦截)
|
||||
return $this->rewriteExternalLinks($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把正文里的站外 <a href> 改写为 /go 中转地址(站内/相对/锚点/mailto 不变)。
|
||||
*/
|
||||
private function rewriteExternalLinks(string $html): string
|
||||
{
|
||||
return preg_replace_callback(
|
||||
'/<a\s+([^>]*?href=["\'])([^"\']+)(["\'][^>]*)>(.*?)<\/a>/is',
|
||||
function (array $m): string {
|
||||
$url = html_entity_decode(trim($m[2]));
|
||||
$go = go_url($url);
|
||||
|
||||
if ($go === $url) {
|
||||
return $m[0];
|
||||
}
|
||||
|
||||
return '<a '.$m[1].e($go).$m[3].'>'.$m[4].'</a>';
|
||||
},
|
||||
$html
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Blog\Services;
|
||||
|
||||
use App\Models\Redirect;
|
||||
use Illuminate\Database\UniqueConstraintViolationException;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* 外链中转:把站外链接改写为 /go/{hash},用于点击统计、二次拦截与未来广告位。
|
||||
* 站内链接、相对路径、mailto/tel/锚点不中转。
|
||||
*/
|
||||
class UrlRedirector
|
||||
{
|
||||
public function goUrl(string $url): string
|
||||
{
|
||||
$url = trim((string) $url);
|
||||
|
||||
if (! $this->isExternal($url)) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
return route('go.show', $this->hashFor($url));
|
||||
}
|
||||
|
||||
public function isExternal(string $url): bool
|
||||
{
|
||||
if (! preg_match('#^https?://#i', $url)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$host = parse_url($url, PHP_URL_HOST);
|
||||
|
||||
if (! $host) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$internalHost = parse_url((string) config('app.url'), PHP_URL_HOST);
|
||||
|
||||
return strcasecmp($host, (string) $internalHost) !== 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取 URL 对应的短 hash(确定性:同一 URL 复用同一条记录,并发安全)。
|
||||
*/
|
||||
public function hashFor(string $url): string
|
||||
{
|
||||
return Cache::remember('go.hash.'.md5($url), now()->addDay(), function () use ($url) {
|
||||
$hash = substr(hash('sha256', $url), 0, 10);
|
||||
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
try {
|
||||
Redirect::create(['hash' => $hash, 'target_url' => $url]);
|
||||
|
||||
return $hash;
|
||||
} catch (UniqueConstraintViolationException) {
|
||||
$existing = Redirect::where('target_url', $url)->first();
|
||||
|
||||
if ($existing) {
|
||||
return $existing->hash;
|
||||
}
|
||||
|
||||
$hash = substr(hash('sha256', $url.$i), 0, 10);
|
||||
}
|
||||
}
|
||||
|
||||
return $hash;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user