From 3954954f860e9f330d70557c16443bdf73f28bb2 Mon Sep 17 00:00:00 2001 From: almostm4 Date: Sun, 9 Nov 2025 14:45:01 +0100 Subject: [PATCH] Fix tor configuration append / graph visualization --- install.sh | 44 ++++++++++++++++++++++++++++++++++++++------ web/api/stats.php | 17 +++++++++++++---- web/setup.php | 2 +- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/install.sh b/install.sh index 6c46b17..6ea7377 100644 --- a/install.sh +++ b/install.sh @@ -32,6 +32,25 @@ TIMER="/etc/systemd/system/torpanel-collector.timer" export DEBIAN_FRONTEND=noninteractive +ensure_torrc_d_include() { + local main="$TOR_ETC/torrc" + install -d -m 755 "$TOR_TORRC_D" + + if [[ ! -f "$main" ]]; then + echo "# Created by TorPanel installer" > "$main" + fi + + if grep -Eq '^[[:space:]]*%include[[:space:]]+/etc/tor/torrc\.d/\*\.conf' "$main"; then + return 0 + fi + + if sed -n 's/^[[:space:]]*#\s*%include[[:space:]]\+\/etc\/tor\/torrc\.d\/\*\.conf/%include \/etc\/tor\/torrc.d\/\*\.conf/p' "$main" | grep -q .; then + sed -i 's/^[[:space:]]*#\s*%include[[:space:]]\+\/etc\/tor\/torrc\.d\/\*\.conf/%include \/etc\/tor\/torrc.d\/\*\.conf/' "$main" + else + printf '\n%%include /etc/tor/torrc.d/*.conf\n' >> "$main" + fi +} + echo -e "${C_BOLD}Installing TorPanel...${C_RESET}" info "Updating apt and installing packages" @@ -49,7 +68,7 @@ ln -sf "$PHP_SOCK" /run/php/php-fpm.sock || true info "Preparing directories" install -d "$PANEL_PUBLIC" "$STATE_DIR" "$LOG_DIR" "$ETC_APP" -touch "$STATE_DIR/stats.json" +echo -n '{"data":[]}' > "$STATE_DIR/stats.json" rsync -a --delete "$SCRIPT_DIR/web/" "$PANEL_PUBLIC/" chown -R www-data:www-data "$PANEL_ROOT" "$STATE_DIR" "$LOG_DIR" chmod 750 "$PANEL_ROOT" "$STATE_DIR" "$LOG_DIR" @@ -105,6 +124,10 @@ CookieAuthFileGroupReadable 1 TORRC ok "torrc written" +info "Ensuring main torrc includes torrc.d/*.conf" +ensure_torrc_d_include +ok "torrc.d include active" + info "Setting permissions for Tor managed config" chown root:www-data "$TOR_TORRC_D"; chmod 775 "$TOR_TORRC_D" chown root:www-data "$TOR_PANEL_CONF"; chmod 664 "$TOR_PANEL_CONF" @@ -129,6 +152,7 @@ cat > "$SVC" <<'UNIT' Description=TorPanel minute collector After=tor.service Wants=tor.service +ConditionPathExists=/run/tor/control.authcookie [Service] Type=oneshot @@ -146,17 +170,22 @@ ProtectKernelModules=yes ProtectKernelTunables=yes PrivateTmp=yes PrivateDevices=yes -PrivateUsers=yes +PrivateUsers=no LockPersonality=yes MemoryDenyWriteExecute=yes RestrictRealtime=yes RestrictSUIDSGID=yes UMask=0077 -ReadWriteDirectories=/var/lib/torpanel +ReadWritePaths=/var/lib/torpanel ReadOnlyPaths=/run/tor /etc/tor RestrictAddressFamilies=AF_UNIX SystemCallFilter=@system-service CapabilityBoundingSet= +StandardOutput=journal +StandardError=journal +Environment=PYTHONUNBUFFERED=1 +Restart=on-failure +RestartSec=15s [Install] WantedBy=timers.target @@ -165,11 +194,13 @@ UNIT cat > "$TIMER" <<'TIMER' [Unit] Description=Run TorPanel collector every minute + [Timer] -OnBootSec=30sec -OnUnitActiveSec=60sec -AccuracySec=15sec +OnCalendar=*-*-* *:*:00 +AccuracySec=15s Persistent=true +Unit=torpanel-collector.service + [Install] WantedBy=timers.target TIMER @@ -181,6 +212,7 @@ systemctl enable --now tor systemctl enable "$PHP_FPM_SVC" nginx >/dev/null systemctl restart "$PHP_FPM_SVC" systemctl restart nginx +systemctl start torpanel-collector.service systemctl enable --now torpanel-collector.timer ok "Services running" diff --git a/web/api/stats.php b/web/api/stats.php index bbb3eb4..d404794 100644 --- a/web/api/stats.php +++ b/web/api/stats.php @@ -1,7 +1,16 @@ []]); exit; } +header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); +header('Pragma: no-cache'); + +$path = '/var/lib/torpanel/stats.json'; +if (!is_readable($path)) { + echo json_encode(['data' => []]); + exit; +} + $raw = file_get_contents($path); -echo $raw ?: json_encode(["data"=>[]]); +echo $raw !== false && $raw !== '' ? $raw : json_encode(['data' => []]); \ No newline at end of file diff --git a/web/setup.php b/web/setup.php index 8de5f66..181657b 100644 --- a/web/setup.php +++ b/web/setup.php @@ -125,7 +125,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { -
+