dpl/scripts/node-onboarding/apply-template.php

21 lines
1.3 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:']);
$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; }
if ($current !== $default) throw new RuntimeException('Custom template detected; inspect instead of overwriting');
if (!is_dir($dir)) throw new RuntimeException('Backup directory missing');
$path = "$dir/singbox-template-before.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";