Control 보안 정책 설정 확장
This commit is contained in:
+6
-146
@@ -72,144 +72,6 @@ function human_remaining_seconds(int $seconds): string
|
||||
return sprintf('%dm', max(1, $minutes));
|
||||
}
|
||||
|
||||
function setting_definitions(): array
|
||||
{
|
||||
return [
|
||||
'fan.auto_min_temp' => ['group' => 'fan', 'label' => '팬 시작 온도', 'type' => 'number', 'default' => 50, 'min' => 30, 'max' => 90, 'step' => 0.5, 'unit' => 'C'],
|
||||
'fan.auto_max_temp' => ['group' => 'fan', 'label' => '팬 최대 온도', 'type' => 'number', 'default' => 80, 'min' => 45, 'max' => 100, 'step' => 0.5, 'unit' => 'C'],
|
||||
'fan.full_temp' => ['group' => 'fan', 'label' => '즉시 최대 PWM 온도', 'type' => 'number', 'default' => 80, 'min' => 45, 'max' => 100, 'step' => 0.5, 'unit' => 'C'],
|
||||
'fan.ramp_up_step' => ['group' => 'fan', 'label' => '자동 상승 PWM step', 'type' => 'number', 'default' => 2, 'min' => 1, 'max' => 80, 'step' => 1, 'unit' => 'PWM'],
|
||||
'fan.ramp_down_step' => ['group' => 'fan', 'label' => '자동 하강 PWM step', 'type' => 'number', 'default' => 2, 'min' => 1, 'max' => 80, 'step' => 1, 'unit' => 'PWM'],
|
||||
'event.recent_window_seconds' => ['group' => 'event', 'label' => '저전압/스로틀링 최근 판정창', 'type' => 'number', 'default' => 600, 'min' => 60, 'max' => 7200, 'step' => 30, 'unit' => '초'],
|
||||
'event.recovery_seconds' => ['group' => 'event', 'label' => '복구 중 유지 시간', 'type' => 'number', 'default' => 300, 'min' => 30, 'max' => 3600, 'step' => 30, 'unit' => '초'],
|
||||
'event.repeated_count' => ['group' => 'event', 'label' => '반복 발생 기준 횟수', 'type' => 'number', 'default' => 2, 'min' => 1, 'max' => 20, 'step' => 1, 'unit' => '회'],
|
||||
'notice.baseline_samples' => ['group' => 'notice', 'label' => '팬 이상 기준 표본 수', 'type' => 'number', 'default' => 180, 'min' => 30, 'max' => 600, 'step' => 10, 'unit' => '개'],
|
||||
'notice.baseline_skip_recent' => ['group' => 'notice', 'label' => '기준 계산 제외 최신 표본', 'type' => 'number', 'default' => 3, 'min' => 0, 'max' => 30, 'step' => 1, 'unit' => '개'],
|
||||
'notice.rpm_delta_threshold' => ['group' => 'notice', 'label' => '팬 RPM 이상 감지 차이', 'type' => 'number', 'default' => 1000, 'min' => 100, 'max' => 5000, 'step' => 50, 'unit' => 'RPM'],
|
||||
'notice.temp_delta_threshold' => ['group' => 'notice', 'label' => '온도 이상 감지 차이', 'type' => 'number', 'default' => 3.0, 'min' => 0.5, 'max' => 15, 'step' => 0.1, 'unit' => 'C'],
|
||||
'notice.recover_rpm_delta' => ['group' => 'notice', 'label' => '팬 RPM 복구 차이', 'type' => 'number', 'default' => 500, 'min' => 50, 'max' => 3000, 'step' => 50, 'unit' => 'RPM'],
|
||||
'notice.recover_temp_delta' => ['group' => 'notice', 'label' => '온도 복구 차이', 'type' => 'number', 'default' => 1.5, 'min' => 0.2, 'max' => 10, 'step' => 0.1, 'unit' => 'C'],
|
||||
'battery.trend_hours' => ['group' => 'battery', 'label' => '단기 배터리 학습 범위', 'type' => 'number', 'default' => 24, 'min' => 1, 'max' => 48, 'step' => 1, 'unit' => '시간'],
|
||||
'battery.profile_days' => ['group' => 'battery', 'label' => '장기 배터리 학습 범위', 'type' => 'number', 'default' => 45, 'min' => 3, 'max' => 90, 'step' => 1, 'unit' => '일'],
|
||||
'battery.load_adjust_recent' => ['group' => 'battery', 'label' => '최근 추세 부하 보정 강도', 'type' => 'number', 'default' => 0.45, 'min' => 0, 'max' => 1, 'step' => 0.01, 'unit' => 'x'],
|
||||
'battery.load_adjust_regression' => ['group' => 'battery', 'label' => '강건 회귀 부하 보정 강도', 'type' => 'number', 'default' => 0.38, 'min' => 0, 'max' => 1, 'step' => 0.01, 'unit' => 'x'],
|
||||
'battery.load_adjust_voltage' => ['group' => 'battery', 'label' => '전압 후보 부하 보정 강도', 'type' => 'number', 'default' => 0.28, 'min' => 0, 'max' => 1, 'step' => 0.01, 'unit' => 'x'],
|
||||
'battery.voltage_floor_high' => ['group' => 'battery', 'label' => '전압 하한 높음', 'type' => 'number', 'default' => 3.28, 'min' => 2.8, 'max' => 3.7, 'step' => 0.01, 'unit' => 'V'],
|
||||
'battery.voltage_floor_mid' => ['group' => 'battery', 'label' => '전압 하한 중간', 'type' => 'number', 'default' => 3.18, 'min' => 2.8, 'max' => 3.7, 'step' => 0.01, 'unit' => 'V'],
|
||||
'battery.voltage_floor_low' => ['group' => 'battery', 'label' => '전압 하한 낮음', 'type' => 'number', 'default' => 3.05, 'min' => 2.8, 'max' => 3.7, 'step' => 0.01, 'unit' => 'V'],
|
||||
'battery.candidate_min_ratio' => ['group' => 'battery', 'label' => '후보 최소 속도 비율', 'type' => 'number', 'default' => 0.38, 'min' => 0.05, 'max' => 1, 'step' => 0.01, 'unit' => 'x'],
|
||||
'battery.candidate_max_ratio' => ['group' => 'battery', 'label' => '후보 최대 속도 비율', 'type' => 'number', 'default' => 2.65, 'min' => 1, 'max' => 8, 'step' => 0.05, 'unit' => 'x'],
|
||||
'battery.capacity_model_weight' => ['group' => 'battery', 'label' => '용량 모델 가중치', 'type' => 'number', 'default' => 0.18, 'min' => 0, 'max' => 1, 'step' => 0.01, 'unit' => 'x'],
|
||||
];
|
||||
}
|
||||
|
||||
function setting_rows(bool $reload = false): array
|
||||
{
|
||||
static $cache = null;
|
||||
if (!$reload && $cache !== null) {
|
||||
return $cache;
|
||||
}
|
||||
|
||||
try {
|
||||
$rows = db()->query("SELECT setting_key, setting_value FROM app_settings")->fetchAll();
|
||||
} catch (Throwable) {
|
||||
$cache = [];
|
||||
return $cache;
|
||||
}
|
||||
|
||||
$cache = [];
|
||||
foreach ($rows as $row) {
|
||||
$cache[(string)$row['setting_key']] = (string)$row['setting_value'];
|
||||
}
|
||||
|
||||
return $cache;
|
||||
}
|
||||
|
||||
function setting_value(string $key): int|float|string|bool|null
|
||||
{
|
||||
$defs = setting_definitions();
|
||||
if (!isset($defs[$key])) {
|
||||
return null;
|
||||
}
|
||||
$def = $defs[$key];
|
||||
$raw = setting_rows()[$key] ?? $def['default'];
|
||||
|
||||
if (($def['type'] ?? '') === 'number') {
|
||||
$value = is_numeric($raw) ? (float)$raw : (float)$def['default'];
|
||||
$value = clamp_float($value, (float)$def['min'], (float)$def['max']);
|
||||
return fmod((float)$def['step'], 1.0) === 0.0 ? (int)round($value) : $value;
|
||||
}
|
||||
|
||||
return $raw;
|
||||
}
|
||||
|
||||
function settings_payload(): array
|
||||
{
|
||||
$values = setting_rows();
|
||||
$groups = [
|
||||
'fan' => '팬 자동 제어',
|
||||
'battery' => '배터리 예측',
|
||||
'event' => '저전압/스로틀링',
|
||||
'notice' => '팬 이상 감지',
|
||||
];
|
||||
|
||||
$items = [];
|
||||
foreach (setting_definitions() as $key => $def) {
|
||||
$items[] = array_merge($def, [
|
||||
'key' => $key,
|
||||
'value' => setting_value($key),
|
||||
'default' => $def['default'],
|
||||
'custom' => array_key_exists($key, $values),
|
||||
]);
|
||||
}
|
||||
|
||||
return [
|
||||
'groups' => $groups,
|
||||
'items' => $items,
|
||||
];
|
||||
}
|
||||
|
||||
function save_settings(array $input): void
|
||||
{
|
||||
$defs = setting_definitions();
|
||||
$stmt = db()->prepare("
|
||||
INSERT INTO app_settings (setting_key, setting_value)
|
||||
VALUES (:setting_key, :setting_value)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
setting_value = VALUES(setting_value),
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
");
|
||||
|
||||
foreach ($input as $key => $value) {
|
||||
if (!isset($defs[$key])) {
|
||||
continue;
|
||||
}
|
||||
$def = $defs[$key];
|
||||
if (($def['type'] ?? '') === 'number') {
|
||||
if (!is_numeric($value)) {
|
||||
continue;
|
||||
}
|
||||
$value = clamp_float((float)$value, (float)$def['min'], (float)$def['max']);
|
||||
}
|
||||
$stmt->execute([
|
||||
':setting_key' => $key,
|
||||
':setting_value' => (string)$value,
|
||||
]);
|
||||
}
|
||||
setting_rows(true);
|
||||
}
|
||||
|
||||
function reset_settings(): void
|
||||
{
|
||||
$keys = array_keys(setting_definitions());
|
||||
if ($keys === []) {
|
||||
return;
|
||||
}
|
||||
$placeholders = implode(',', array_fill(0, count($keys), '?'));
|
||||
db()->prepare("DELETE FROM app_settings WHERE setting_key IN ($placeholders)")->execute($keys);
|
||||
setting_rows(true);
|
||||
}
|
||||
|
||||
function dmesg_log(): array
|
||||
{
|
||||
$path = '/tmp/dmesg.log';
|
||||
@@ -876,11 +738,6 @@ function numeric_trimmed_average(array $values, float $trimRatio = 0.1): ?float
|
||||
return array_sum($numbers) / count($numbers);
|
||||
}
|
||||
|
||||
function clamp_float(float $value, float $min, float $max): float
|
||||
{
|
||||
return max($min, min($max, $value));
|
||||
}
|
||||
|
||||
function weighted_linear_regression(array $points): ?array
|
||||
{
|
||||
$weightSum = 0.0;
|
||||
@@ -3524,8 +3381,9 @@ function control_api_dispatch(): void
|
||||
if ($action === 'reboot') {
|
||||
$phrase = trim((string)($_POST['phrase'] ?? ''));
|
||||
$password = (string)($_POST['password'] ?? '');
|
||||
$expectedPhrase = (string)setting_value('security.reboot_phrase');
|
||||
|
||||
if ($phrase !== '재부팅') {
|
||||
if ($phrase !== $expectedPhrase) {
|
||||
json_out([
|
||||
'ok' => false,
|
||||
'error' => 'bad_reboot_phrase',
|
||||
@@ -3549,7 +3407,7 @@ function control_api_dispatch(): void
|
||||
true
|
||||
);
|
||||
|
||||
$result = sh(['/usr/sbin/reboot'], true, 5);
|
||||
$result = sh(['/usr/sbin/reboot'], true, (int)setting_value('security.reboot_timeout_seconds'));
|
||||
if ($result['code'] !== 0) {
|
||||
json_out([
|
||||
'ok' => false,
|
||||
@@ -3578,6 +3436,8 @@ function control_api_dispatch(): void
|
||||
if (
|
||||
!in_array($unit, $allowedUnits, true)
|
||||
|| !in_array($verb, ['restart', 'reload'], true)
|
||||
|| ($verb === 'restart' && !(bool)setting_value('security.allow_wifi_restart'))
|
||||
|| ($verb === 'reload' && !(bool)setting_value('security.allow_wifi_reload'))
|
||||
) {
|
||||
json_out([
|
||||
'ok' => false,
|
||||
@@ -3585,7 +3445,7 @@ function control_api_dispatch(): void
|
||||
], 400);
|
||||
}
|
||||
|
||||
$result = sh(['/usr/bin/systemctl', $verb, $unit], true, 25);
|
||||
$result = sh(['/usr/bin/systemctl', $verb, $unit], true, (int)setting_value('security.wifi_command_timeout_seconds'));
|
||||
$out = $result['out'];
|
||||
|
||||
add_fan_action(
|
||||
|
||||
Reference in New Issue
Block a user