From d3f2103e86421fcbe62112b292c7c325d2896f5a Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 18 Sep 2026 00:20:31 +0500 Subject: [PATCH] optimized config --- config/state.json | 10 +- config/users.yaml | 29 +--- install.sh | 349 +++++++++++++++++++++++++--------------------- 3 files changed, 192 insertions(+), 196 deletions(-) diff --git a/config/state.json b/config/state.json index 4f8ede2..a37d310 100644 --- a/config/state.json +++ b/config/state.json @@ -1,8 +1,4 @@ -{ - "usage": { - "testuser1": { - "2026-09-17": 636 - } - }, +{ + "usage": {}, "temporary_grants": [] -} \ No newline at end of file +} diff --git a/config/users.yaml b/config/users.yaml index acb38db..dd16570 100644 --- a/config/users.yaml +++ b/config/users.yaml @@ -1,28 +1 @@ -users: -- id: 1 - username: testuser1 - enabled: true - daily_allowance: - sunday: 0 - monday: 0 - tuesday: 0 - wednesday: 0 - thursday: 900 - friday: 0 - saturday: 0 - access_windows: - thursday: - - start: '16:00' - end: '21:00' -- id: 2 - username: testuser2 - enabled: false - daily_allowance: - sunday: 0 - monday: 0 - tuesday: 0 - wednesday: 0 - thursday: 0 - friday: 0 - saturday: 0 - access_windows: {} +users: [] diff --git a/install.sh b/install.sh index dc4cd6b..89049e4 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,6 @@ -#!/bin/bash +#!/usr/bin/env bash -set -euo pipefail +set -Eeuo pipefail # ============================================================ # Linux Parental Control Installer @@ -23,6 +23,10 @@ POLKIT_DEST="/etc/polkit-1/actions/$POLKIT_ACTION.policy" SOCKET_PATH="/run/parental-control.sock" +INSTALL_USER="" +INSTALL_GROUP="" +PACKAGE_MANAGER="" +POLKIT_AGENT="" # ============================================================ # Output helpers @@ -48,29 +52,32 @@ error() { exit 1 } +# ============================================================ +# Error handling +# ============================================================ + +on_error() { + echo + echo "Installation failed." + echo "Line: $1" + echo "Command: $2" + echo +} + +trap 'on_error "$LINENO" "$BASH_COMMAND"' ERR # ============================================================ # Root check # ============================================================ if [ "$EUID" -ne 0 ]; then - echo - echo "This installer requires administrator privileges." - echo - echo "Run:" - echo - echo " sudo ./install.sh" - echo - exit 1 -fi + error "Run this installer with sudo: + sudo ./install.sh" +fi # ============================================================ # Find original user -# -# When running through sudo, SUDO_USER is the person who -# launched the installer. We use this to keep the source tree -# owned by that user rather than root. # ============================================================ if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then @@ -85,6 +92,11 @@ fi INSTALL_GROUP="$(id -gn "$INSTALL_USER")" +INSTALL_USER_HOME="$(getent passwd "$INSTALL_USER" | cut -d: -f6)" + +if [ -z "$INSTALL_USER_HOME" ] || [ ! -d "$INSTALL_USER_HOME" ]; then + error "Could not determine the home directory of $INSTALL_USER." +fi # ============================================================ # Header @@ -107,7 +119,6 @@ echo "Installation user:" echo " $INSTALL_USER" echo - # ============================================================ # Check project files # ============================================================ @@ -143,9 +154,8 @@ done success "All required project files found." - # ============================================================ -# Check Linux +# Detect operating system # ============================================================ info "Detecting operating system" @@ -164,7 +174,6 @@ OS_VERSION="${VERSION_ID:-unknown}" echo " Distribution: $OS_NAME" echo " Version: $OS_VERSION" - # ============================================================ # Check systemd # ============================================================ @@ -172,15 +181,22 @@ echo " Version: $OS_VERSION" info "Checking systemd" if ! command -v systemctl >/dev/null 2>&1; then - error "systemd is required, but systemctl was not found." + error "systemd is required for this version of the application. + +This installer does not support non-systemd distributions." fi if [ ! -d /run/systemd/system ]; then - error "systemd is installed but does not appear to be running." + error "systemd is installed but is not currently running." fi -success "systemd detected." +if ! command -v loginctl >/dev/null 2>&1; then + error "loginctl was not found. +The application requires systemd-logind." +fi + +success "systemd and loginctl detected." # ============================================================ # Detect package manager @@ -188,8 +204,6 @@ success "systemd detected." info "Detecting package manager" -PACKAGE_MANAGER="" - if command -v pacman >/dev/null 2>&1; then PACKAGE_MANAGER="pacman" @@ -202,12 +216,6 @@ elif command -v dnf >/dev/null 2>&1; then elif command -v zypper >/dev/null 2>&1; then PACKAGE_MANAGER="zypper" -elif command -v apk >/dev/null 2>&1; then - PACKAGE_MANAGER="apk" - -elif command -v xbps-install >/dev/null 2>&1; then - PACKAGE_MANAGER="xbps" - else error "No supported package manager was detected. @@ -216,15 +224,15 @@ Supported package managers: apt dnf zypper - apk - xbps-install" + +You can install the required dependencies manually, +then run the installer again." fi success "Package manager: $PACKAGE_MANAGER" - # ============================================================ -# Install system packages +# Install system dependencies # ============================================================ info "Installing system dependencies" @@ -236,7 +244,9 @@ case "$PACKAGE_MANAGER" in python \ python-pip \ polkit \ - desktop-file-utils + polkit-gnome \ + desktop-file-utils \ + systemd ;; apt) @@ -250,47 +260,37 @@ case "$PACKAGE_MANAGER" in python3-venv \ polkitd \ policykit-1 \ - desktop-file-utils + policykit-1-gnome \ + desktop-file-utils \ + systemd ;; dnf) dnf install -y \ python3 \ python3-pip \ + python3-virtualenv \ polkit \ - desktop-file-utils + polkit-gnome \ + desktop-file-utils \ + systemd ;; zypper) zypper --non-interactive install \ python3 \ python3-pip \ + python3-virtualenv \ polkit \ - desktop-file-utils - ;; - - apk) - apk add \ - python3 \ - py3-pip \ - py3-virtualenv \ - polkit \ - desktop-file-utils - ;; - - xbps) - xbps-install -Sy \ - python3 \ - python3-pip \ - polkit \ - desktop-file-utils + polkit-gnome \ + desktop-file-utils \ + systemd ;; esac success "System dependencies installed." - # ============================================================ # Find Python # ============================================================ @@ -311,11 +311,13 @@ if [ -z "$SYSTEM_PYTHON" ]; then fi PYTHON_MAJOR="$( - "$SYSTEM_PYTHON" -c 'import sys; print(sys.version_info.major)' + "$SYSTEM_PYTHON" -c \ + 'import sys; print(sys.version_info.major)' )" PYTHON_MINOR="$( - "$SYSTEM_PYTHON" -c 'import sys; print(sys.version_info.minor)' + "$SYSTEM_PYTHON" -c \ + 'import sys; print(sys.version_info.minor)' )" PYTHON_VERSION="$( @@ -324,39 +326,35 @@ PYTHON_VERSION="$( )" if [ "$PYTHON_MAJOR" -ne 3 ]; then - error "Python 3 is required. Found Python $PYTHON_VERSION." + error "Python 3 is required. + +Found Python $PYTHON_VERSION." +fi + +if [ "$PYTHON_MINOR" -lt 9 ]; then + error "Python $PYTHON_VERSION is too old. + +Python 3.9 or newer is required." fi echo " Python: $SYSTEM_PYTHON" echo " Version: $PYTHON_VERSION" - -# ============================================================ -# Check Python version for PySide6 -# ============================================================ - -if [ "$PYTHON_MINOR" -lt 9 ]; then - error "Python $PYTHON_VERSION is too old for this application. - -Python 3.9 or newer is required." -fi - success "Python version is supported." - # ============================================================ -# Create/recreate virtual environment +# Create virtual environment # ============================================================ info "Preparing Python virtual environment" if [ -x "$PYTHON" ]; then - success "Existing virtual environment is valid." + success "Existing virtual environment found." else if [ -d "$VENV_DIR" ]; then - echo " Existing virtual environment is broken." - echo " Removing it..." + warning "Existing virtual environment is incomplete." + warning "Removing it." rm -rf "$VENV_DIR" fi @@ -366,13 +364,12 @@ else if ! "$SYSTEM_PYTHON" -m venv "$VENV_DIR"; then error "Could not create the Python virtual environment. -Your distribution may require an additional Python venv package." +Install your distribution's Python venv package." fi success "Virtual environment created." fi - # ============================================================ # Install Python dependencies # ============================================================ @@ -389,9 +386,8 @@ info "Installing Python dependencies" success "Python dependencies installed." - # ============================================================ -# Validate Python application +# Validate Python source # ============================================================ info "Checking Python source files" @@ -403,35 +399,8 @@ info "Checking Python source files" success "Python source validation passed." - # ============================================================ -# Remove Python cache files created by installer -# ============================================================ - -find "$INSTALL_DIR/app" \ - "$INSTALL_DIR/desktop" \ - -type d \ - -name "__pycache__" \ - -prune \ - -exec rm -rf {} + \ - 2>/dev/null || true - - -# ============================================================ -# Restore source ownership -# ============================================================ - -info "Fixing source ownership" - -chown -R \ - "$INSTALL_USER:$INSTALL_GROUP" \ - "$INSTALL_DIR" - -success "Source tree owned by $INSTALL_USER." - - -# ============================================================ -# Verify configuration +# Validate configuration # ============================================================ info "Checking application configuration" @@ -444,8 +413,7 @@ if [ ! -f "$INSTALL_DIR/config/state.json" ]; then error "state.json was not found." fi -success "YAML/JSON configuration files found." - +success "Configuration files found." # ============================================================ # Install systemd service @@ -460,9 +428,6 @@ sed \ "$INSTALL_DIR/parental-control.service" \ > "$SERVICE_TEMP" -# Ensure the service does not write Python bytecode into the -# Git repository. - if ! grep -q '^Environment=PYTHONDONTWRITEBYTECODE=' "$SERVICE_TEMP"; then sed -i \ '/^\[Service\]/a Environment=PYTHONDONTWRITEBYTECODE=1' \ @@ -480,34 +445,102 @@ rm -f "$SERVICE_TEMP" success "Systemd service installed." - # ============================================================ # Install polkit policy # ============================================================ -info "Checking polkit policy" +info "Installing polkit policy" -if [ -f "$POLKIT_SOURCE" ]; then - - mkdir -p /etc/polkit-1/actions - - install \ - -o root \ - -g root \ - -m 644 \ - "$POLKIT_SOURCE" \ - "$POLKIT_DEST" - - success "Polkit policy installed." - -else - - warning "Polkit policy file is not present." - warning "Expected: $POLKIT_SOURCE" - warning "Skipping polkit policy installation." +if [ ! -f "$POLKIT_SOURCE" ]; then + error "Polkit policy file was not found: +$POLKIT_SOURCE" fi +mkdir -p /etc/polkit-1/actions + +install \ + -o root \ + -g root \ + -m 644 \ + "$POLKIT_SOURCE" \ + "$POLKIT_DEST" + +success "Polkit policy installed." + +# ============================================================ +# Detect polkit authentication agent +# ============================================================ + +info "Detecting polkit authentication agent" + +POLKIT_AGENT="" + +POLKIT_AGENT_CANDIDATES=( + "/usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1" + "/usr/libexec/polkit-gnome-authentication-agent-1" + "/usr/lib/polkit-1-gnome/polkit-gnome-authentication-agent-1" +) + +for candidate in "${POLKIT_AGENT_CANDIDATES[@]}"; do + if [ -x "$candidate" ]; then + POLKIT_AGENT="$candidate" + break + fi +done + +if [ -n "$POLKIT_AGENT" ]; then + success "Authentication agent found:" + success "$POLKIT_AGENT" +else + warning "A polkit authentication agent was not found." + warning "GUI administrative actions may require manual configuration." +fi + +# ============================================================ +# Configure Sway polkit authentication agent +# ============================================================ + +configure_sway_polkit() { + local user_home="$1" + local sway_config="$user_home/.config/sway/config" + local marker="parental-control-polkit-agent" + + if [ -z "$POLKIT_AGENT" ]; then + return 0 + fi + + if [ ! -f "$sway_config" ]; then + warning "Sway configuration not found:" + warning "$sway_config" + return 0 + fi + + if grep -Fq "$marker" "$sway_config"; then + success "Sway polkit authentication agent is already configured." + return 0 + fi + + cp -a \ + "$sway_config" \ + "$sway_config.parental-control-backup" + + cat >> "$sway_config" </dev/null 2>&1; then - update-desktop-database \ /usr/share/applications \ >/dev/null 2>&1 || true - fi +# ============================================================ +# Restore source ownership +# ============================================================ + +info "Fixing source ownership" + +chown -R \ + "$INSTALL_USER:$INSTALL_GROUP" \ + "$INSTALL_DIR" + +success "Source tree owned by $INSTALL_USER." # ============================================================ # Reload systemd @@ -573,8 +613,7 @@ info "Reloading systemd" systemctl daemon-reload -success "systemd reloaded." - +success "Systemd reloaded." # ============================================================ # Remove stale IPC socket @@ -594,7 +633,6 @@ if [ -e "$SOCKET_PATH" ]; then fi - # ============================================================ # Enable service # ============================================================ @@ -605,7 +643,6 @@ systemctl enable "$SERVICE_NAME" success "Service enabled." - # ============================================================ # Restart service # ============================================================ @@ -614,11 +651,12 @@ info "Starting parental control service" systemctl restart "$SERVICE_NAME" - # ============================================================ # Wait for service # ============================================================ +info "Waiting for service startup" + SERVICE_READY=0 for _ in $(seq 1 15); do @@ -632,7 +670,6 @@ for _ in $(seq 1 15); do done - if [ "$SERVICE_READY" -ne 1 ]; then echo @@ -659,7 +696,6 @@ fi success "Service is running." - # ============================================================ # Verify IPC socket # ============================================================ @@ -667,17 +703,12 @@ success "Service is running." info "Checking IPC socket" if [ ! -S "$SOCKET_PATH" ]; then - warning "IPC socket was not created:" warning "$SOCKET_PATH" - else - success "IPC socket is active." - fi - # ============================================================ # Final verification # ============================================================ @@ -686,7 +717,6 @@ info "Running final checks" CHECK_FAILED=0 - if systemctl is-active --quiet "$SERVICE_NAME"; then echo " Service: OK" else @@ -694,7 +724,6 @@ else CHECK_FAILED=1 fi - if [ -x "$PYTHON" ]; then echo " Python: OK" else @@ -702,7 +731,6 @@ else CHECK_FAILED=1 fi - if [ -f "$DESKTOP_FILE" ]; then echo " Desktop: OK" else @@ -710,21 +738,19 @@ else CHECK_FAILED=1 fi - if [ -S "$SOCKET_PATH" ]; then echo " IPC: OK" else echo " IPC: WARNING" fi - if [ -f "$POLKIT_DEST" ]; then echo " Polkit: OK" else - echo " Polkit: NOT INSTALLED" + echo " Polkit: FAILED" + CHECK_FAILED=1 fi - if [ -f "$INSTALL_DIR/config/users.yaml" ]; then echo " Users: OK" else @@ -732,7 +758,6 @@ else CHECK_FAILED=1 fi - if [ -f "$INSTALL_DIR/config/state.json" ]; then echo " State: OK" else @@ -740,12 +765,10 @@ else CHECK_FAILED=1 fi - if [ "$CHECK_FAILED" -ne 0 ]; then error "One or more required installation checks failed." fi - # ============================================================ # Finished # ============================================================ @@ -762,9 +785,6 @@ echo "Launch:" echo " Application menu" echo " Rofi" echo -echo "Rofi:" -echo " Super + D" -echo echo "Service:" echo " systemctl status $SERVICE_NAME" echo @@ -781,5 +801,12 @@ echo echo "Installation:" echo " $INSTALL_DIR" echo +echo "Polkit agent:" +if [ -n "$POLKIT_AGENT" ]; then + echo " $POLKIT_AGENT" +else + echo " Not detected" +fi +echo echo "==========================================" echo