2020-05-10 18:50:03 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2020-08-11 00:17:10 +09:00
|
|
|
use App\ContentProvider;
|
2020-05-10 18:50:03 +09:00
|
|
|
use App\Metadata;
|
2020-07-23 13:08:20 +09:00
|
|
|
use App\MetadataResolver\DeniedHostException;
|
2020-08-11 00:17:10 +09:00
|
|
|
use App\MetadataResolver\DisallowedByProviderException;
|
2020-05-10 18:50:03 +09:00
|
|
|
use App\MetadataResolver\MetadataResolver;
|
2020-08-10 13:32:47 +09:00
|
|
|
use App\MetadataResolver\ResolverCircuitBreakException;
|
|
|
|
use App\MetadataResolver\UncaughtResolverException;
|
2020-05-10 18:50:03 +09:00
|
|
|
use App\Tag;
|
|
|
|
use App\Utilities\Formatter;
|
2020-08-11 00:17:10 +09:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Carbon\CarbonInterface;
|
|
|
|
use GuzzleHttp\Client;
|
2020-08-01 17:26:38 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-08-11 00:17:10 +09:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-05-10 18:50:03 +09:00
|
|
|
|
|
|
|
class MetadataResolveService
|
|
|
|
{
|
2020-08-10 13:32:47 +09:00
|
|
|
/** @var int メタデータの解決を中断するエラー回数。この回数以上エラーしていたら処理は行わない。 */
|
|
|
|
const CIRCUIT_BREAK_COUNT = 5;
|
|
|
|
|
2020-05-10 18:50:03 +09:00
|
|
|
/** @var MetadataResolver */
|
|
|
|
private $resolver;
|
|
|
|
/** @var Formatter */
|
|
|
|
private $formatter;
|
|
|
|
|
|
|
|
public function __construct(MetadataResolver $resolver, Formatter $formatter)
|
|
|
|
{
|
|
|
|
$this->resolver = $resolver;
|
|
|
|
$this->formatter = $formatter;
|
|
|
|
}
|
|
|
|
|
2020-08-10 13:32:47 +09:00
|
|
|
/**
|
|
|
|
* メタデータをキャッシュまたはリモートに問い合わせて取得します。
|
|
|
|
* @param string $url メタデータを取得したいURL
|
|
|
|
* @return Metadata 取得できたメタデータ
|
|
|
|
* @throws DeniedHostException アクセス先がブラックリスト入りしているため取得できなかった場合にスロー
|
|
|
|
* @throws UncaughtResolverException Resolver内で例外が発生して取得できなかった場合にスロー
|
|
|
|
*/
|
2020-05-10 18:50:03 +09:00
|
|
|
public function execute(string $url): Metadata
|
|
|
|
{
|
|
|
|
// URLの正規化
|
|
|
|
$url = $this->formatter->normalizeUrl($url);
|
|
|
|
|
2020-07-21 23:39:48 +09:00
|
|
|
// 自分自身は解決しない
|
|
|
|
if (parse_url($url, PHP_URL_HOST) === parse_url(config('app.url'), PHP_URL_HOST)) {
|
2020-07-23 13:08:20 +09:00
|
|
|
throw new DeniedHostException($url);
|
2020-07-21 23:39:48 +09:00
|
|
|
}
|
|
|
|
|
2020-08-11 00:17:10 +09:00
|
|
|
$metadata = Metadata::find($url);
|
|
|
|
|
|
|
|
// 無かったら取得
|
|
|
|
// TODO: ある程度古かったら再取得とかありだと思う
|
|
|
|
if ($metadata == null || $metadata->needRefresh()) {
|
|
|
|
$hostWithPort = $this->getHostWithPortFromUrl($url);
|
|
|
|
$metadata = $this->hostLock($hostWithPort, function (?CarbonInterface $lastAccess) use ($url) {
|
|
|
|
// HostLockの解放待ちをしている間に、他のプロセスで取得完了しているかもしれない
|
|
|
|
$metadata = Metadata::find($url);
|
|
|
|
if ($metadata !== null && !$metadata->needRefresh()) {
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->checkProviderPolicy($url, $lastAccess);
|
|
|
|
|
|
|
|
return $this->resolve($url, $metadata);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URLからホスト部とポート部を抽出
|
|
|
|
* @param string $url
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getHostWithPortFromUrl(string $url): string
|
|
|
|
{
|
|
|
|
$parts = parse_url($url);
|
|
|
|
$host = $parts['host'];
|
|
|
|
if (isset($parts['port'])) {
|
|
|
|
$host .= ':' . $parts['port'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* アクセス先ホスト単位の排他ロックを取って処理を実行
|
|
|
|
* @param string $host
|
|
|
|
* @param callable $fn
|
|
|
|
* @return mixed return of $fn
|
|
|
|
* @throws \RuntimeException いろいろな死に方をする
|
|
|
|
*/
|
|
|
|
private function hostLock(string $host, callable $fn)
|
|
|
|
{
|
|
|
|
$lockDir = storage_path('content_providers_lock');
|
|
|
|
if (!file_exists($lockDir)) {
|
|
|
|
if (!mkdir($lockDir)) {
|
|
|
|
throw new \RuntimeException("Lock failed! Can't create lock directory.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$lockFile = $lockDir . DIRECTORY_SEPARATOR . $host;
|
|
|
|
$fp = fopen($lockFile, 'c+b');
|
|
|
|
if ($fp === false) {
|
|
|
|
throw new \RuntimeException("Lock failed! Can't open lock file.");
|
|
|
|
}
|
|
|
|
|
2020-08-10 13:32:47 +09:00
|
|
|
try {
|
2020-08-11 00:17:10 +09:00
|
|
|
if (!flock($fp, LOCK_EX)) {
|
|
|
|
throw new \RuntimeException("Lock failed! Can't lock file.");
|
|
|
|
}
|
2020-08-10 13:32:47 +09:00
|
|
|
|
2020-08-11 00:17:10 +09:00
|
|
|
try {
|
|
|
|
$accessInfoText = stream_get_contents($fp);
|
|
|
|
if ($accessInfoText !== false) {
|
|
|
|
$accessInfo = json_decode($accessInfoText, true);
|
2020-08-10 13:32:47 +09:00
|
|
|
}
|
|
|
|
|
2020-08-11 00:17:10 +09:00
|
|
|
$result = $fn(isset($accessInfo['time']) ? new Carbon($accessInfo['time']) : null);
|
|
|
|
|
|
|
|
$accessInfo = [
|
|
|
|
'time' => now()->toIso8601String()
|
|
|
|
];
|
|
|
|
fseek($fp, 0);
|
|
|
|
if (fwrite($fp, json_encode($accessInfo)) === false) {
|
|
|
|
throw new \RuntimeException("I/O Error! Can't write to lock file.");
|
2020-08-10 13:32:47 +09:00
|
|
|
}
|
|
|
|
|
2020-08-11 00:17:10 +09:00
|
|
|
return $result;
|
|
|
|
} finally {
|
|
|
|
if (!flock($fp, LOCK_UN)) {
|
|
|
|
throw new \RuntimeException("Unlock failed! Can't unlock file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (!fclose($fp)) {
|
|
|
|
throw new \RuntimeException("Unlock failed! Can't close lock file.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 指定したメタデータURLのホストが持つrobots.txtをダウンロードします。
|
|
|
|
* @param string $url メタデータのURL
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function fetchRobotsTxt(string $url): ?string
|
|
|
|
{
|
|
|
|
$parts = parse_url($url);
|
|
|
|
$robotsUrl = http_build_url([
|
|
|
|
'scheme' => $parts['scheme'],
|
|
|
|
'host' => $parts['host'],
|
|
|
|
'port' => $parts['port'] ?? null,
|
|
|
|
'path' => '/robots.txt'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$client = app(Client::class);
|
|
|
|
try {
|
|
|
|
$res = $client->get($robotsUrl);
|
|
|
|
|
|
|
|
return (string) $res->getBody();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error("robots.txtの取得に失敗: {$e}");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentProviderポリシー情報との照合を行い、アクセス可能かチェックします。アクセスできない場合は例外をスローします。
|
|
|
|
* @param string $url メタデータを取得したいURL
|
|
|
|
* @param CarbonInterface|null $lastAccess アクセス先ホストへの最終アクセス日時 (記録がある場合)
|
|
|
|
* @throws DeniedHostException アクセス先がTissue内のブラックリストに入っている場合にスロー
|
|
|
|
* @throws DisallowedByProviderException アクセス先のrobots.txtによって拒否されている場合にスロー
|
|
|
|
*/
|
|
|
|
private function checkProviderPolicy(string $url, ?CarbonInterface $lastAccess): void
|
|
|
|
{
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$hostWithPort = $this->getHostWithPortFromUrl($url);
|
|
|
|
$contentProvider = ContentProvider::sharedLock()->find($hostWithPort);
|
|
|
|
if ($contentProvider === null) {
|
|
|
|
$contentProvider = ContentProvider::create([
|
|
|
|
'host' => $hostWithPort,
|
|
|
|
'robots' => $this->fetchRobotsTxt($url),
|
|
|
|
'robots_cached_at' => now(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($contentProvider->is_blocked) {
|
|
|
|
throw new DeniedHostException($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 連続アクセス制限
|
|
|
|
if ($lastAccess !== null) {
|
|
|
|
$elapsedSeconds = $lastAccess->diffInSeconds(now(), false);
|
|
|
|
if ($elapsedSeconds < $contentProvider->access_interval_sec) {
|
|
|
|
if ($elapsedSeconds < 0) {
|
|
|
|
$wait = abs($elapsedSeconds) + $contentProvider->access_interval_sec;
|
|
|
|
} else {
|
|
|
|
$wait = $contentProvider->access_interval_sec - $elapsedSeconds;
|
|
|
|
}
|
|
|
|
sleep($wait);
|
2020-05-10 18:50:03 +09:00
|
|
|
}
|
2020-08-11 00:17:10 +09:00
|
|
|
}
|
2020-08-10 13:32:47 +09:00
|
|
|
|
2020-08-11 00:17:10 +09:00
|
|
|
// Fetch robots.txt
|
|
|
|
if ($contentProvider->robots_cached_at->diffInDays(now()) >= 7) {
|
|
|
|
$contentProvider->update([
|
|
|
|
'robots' => $this->fetchRobotsTxt($url),
|
|
|
|
'robots_cached_at' => now(),
|
2020-08-10 13:32:47 +09:00
|
|
|
]);
|
2020-08-11 00:17:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check robots.txt
|
|
|
|
$robotsParser = new \RobotsTxtParser($contentProvider->robots);
|
|
|
|
$robotsParser->setUserAgent('TissueBot');
|
|
|
|
$robotsDelay = $robotsParser->getDelay();
|
|
|
|
if ($robotsDelay !== 0 && $robotsDelay >= $contentProvider->access_interval_sec) {
|
|
|
|
$contentProvider->access_interval_sec = (int) $robotsDelay;
|
|
|
|
$contentProvider->save();
|
|
|
|
}
|
|
|
|
if ($robotsParser->isDisallowed(parse_url($url, PHP_URL_PATH))) {
|
|
|
|
throw new DisallowedByProviderException($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::commit();
|
|
|
|
} catch (DeniedHostException | DisallowedByProviderException $e) {
|
|
|
|
// ContentProviderのデータ更新は行うため
|
|
|
|
DB::commit();
|
|
|
|
throw $e;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* メタデータをリモートサーバに問い合わせて取得します。
|
|
|
|
* @param string $url メタデータを取得したいURL
|
|
|
|
* @param Metadata|null $metadata キャッシュ済のメタデータ (存在する場合)
|
|
|
|
* @return Metadata 取得できたメタデータ
|
|
|
|
* @throws UncaughtResolverException Resolver内で例外が発生して取得できなかった場合にスロー
|
|
|
|
* @throws ResolverCircuitBreakException 規定回数以上の解決失敗により、メタデータの取得が不能となっている場合にスロー
|
|
|
|
*/
|
|
|
|
private function resolve(string $url, ?Metadata $metadata): Metadata
|
|
|
|
{
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
if ($metadata === null) {
|
|
|
|
$metadata = new Metadata(['url' => $url]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($metadata->error_count >= self::CIRCUIT_BREAK_COUNT) {
|
|
|
|
throw new ResolverCircuitBreakException($metadata->error_count, $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$resolved = $this->resolver->resolve($url);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$metadata->storeException(now(), $e);
|
2020-08-10 13:32:47 +09:00
|
|
|
$metadata->save();
|
2020-08-11 00:17:10 +09:00
|
|
|
throw new UncaughtResolverException(implode(': ', [
|
|
|
|
$metadata->error_count . '回目のメタデータ取得失敗', get_class($e), $e->getMessage()
|
|
|
|
]), 0, $e);
|
|
|
|
}
|
2020-08-10 13:32:47 +09:00
|
|
|
|
2020-08-11 00:17:10 +09:00
|
|
|
$metadata->fill([
|
|
|
|
'title' => $resolved->title,
|
|
|
|
'description' => $resolved->description,
|
|
|
|
'image' => $resolved->image,
|
|
|
|
'expires_at' => $resolved->expires_at
|
|
|
|
]);
|
|
|
|
$metadata->clearError();
|
|
|
|
$metadata->save();
|
|
|
|
|
|
|
|
$tagIds = [];
|
|
|
|
foreach ($resolved->normalizedTags() as $tagName) {
|
|
|
|
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
|
|
|
$tagIds[] = $tag->id;
|
2020-05-10 18:50:03 +09:00
|
|
|
}
|
2020-08-11 00:17:10 +09:00
|
|
|
$metadata->tags()->sync($tagIds);
|
2020-05-10 18:50:03 +09:00
|
|
|
|
2020-08-10 13:32:47 +09:00
|
|
|
DB::commit();
|
|
|
|
|
2020-08-01 17:26:38 +09:00
|
|
|
return $metadata;
|
2020-08-10 13:32:47 +09:00
|
|
|
} catch (UncaughtResolverException $e) {
|
|
|
|
// Metadataにエラー情報を記録するため
|
|
|
|
DB::commit();
|
|
|
|
throw $e;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
|
|
|
}
|
2020-05-10 18:50:03 +09:00
|
|
|
}
|
|
|
|
}
|