23 lines
1.6 KiB
PHP
23 lines
1.6 KiB
PHP
<?php
|
|
require getcwd() . '/vendor/autoload.php';
|
|
$app = require getcwd() . '/bootstrap/app.php';
|
|
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
|
|
$opts = getopt('', ['backup:', 'expected-current-sha256:']);
|
|
$dir = $opts['backup'] ?? throw new RuntimeException('Missing backup');
|
|
umask(0077);
|
|
$old = App\Models\SubscribeTemplate::where('name', 'singbox')->firstOrFail();
|
|
$default = json_decode(file_get_contents('resources/rules/default.sing-box.json'), true, flags: JSON_THROW_ON_ERROR);
|
|
$replacement = file_get_contents(__DIR__ . '/sing-box-template.json');
|
|
$new = json_decode($replacement, true, flags: JSON_THROW_ON_ERROR);
|
|
$current = json_decode($old->content, true, flags: JSON_THROW_ON_ERROR);
|
|
if ($current === $new) { echo "Template already updated\n"; exit; }
|
|
$expected = $opts['expected-current-sha256'] ?? '';
|
|
$knownRevision = preg_match('/^[a-f0-9]{64}$/', $expected) && hash_equals($expected, hash('sha256', $old->content));
|
|
if ($current !== $default && !$knownRevision) throw new RuntimeException('Custom or unexpected template detected; inspect instead of overwriting');
|
|
if (!is_dir($dir)) throw new RuntimeException('Backup directory missing');
|
|
$path = "$dir/singbox-template-before-" . hash('sha256', $old->content) . '.json';
|
|
if (is_file($path)) throw new RuntimeException('Existing backup; inspect before retry');
|
|
file_put_contents($path, json_encode($old->getAttributes(), JSON_THROW_ON_ERROR));
|
|
App\Models\SubscribeTemplate::setContent('singbox', $replacement);
|
|
echo "DPL default subscription template updated; custom templates and other airports untouched\n";
|