Files
fikakassa/setup-fikakassa-kioskmode.sh
Joakim 2c7653bc4c N/A
2026-11-01 16:54:54 +01:00

408 lines
12 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
# Fikakassan kiosk setup
# Debian 13 / X11 / Openbox / Chromium / NetworkManager
#
# Run as root:
# sudo ./setup-kiosk.sh
#
# The script:
# - installs the kiosk stack and Python runtime
# - uses the existing Debian user as the kiosk user
# - installs the Fikakassan app from the directory containing this script
# - starts FastAPI automatically at boot
# - configures Wi-Fi through NetworkManager
# - autologins the kiosk user
# - starts Chromium in true kiosk mode
# - disables suspend/blanking and common escape routes
# - blocks ordinary Internet traffic over the Wi-Fi hotspot while allowing
# full Internet access over wired Ethernet when a cable is connected
# - keeps Wi-Fi and Ethernet connected simultaneously
#
# Before running, put this script in the root of the Fikakassan project.
KIOSK_USER="${KIOSK_USER:-joakim}"
APP_DIR="${APP_DIR:-/opt/fikakassa}"
KIOSK_URL="${KIOSK_URL:-http://127.0.0.1:8000/}"
WIFI_CONNECTION="${WIFI_CONNECTION:-Fikakassan-WiFi}"
WIFI_SSID="${WIFI_SSID:-}"
WIFI_PASSWORD="${WIFI_PASSWORD:-}"
# Set to 0 if you explicitly want ordinary Internet access from the kiosk.
LOCK_DOWN_INTERNET="${LOCK_DOWN_INTERNET:-1}"
log() { printf '\n\033[1;32m==> %s\033[0m\n' "$*"; }
warn() { printf '\n\033[1;33mWARNING: %s\033[0m\n' "$*" >&2; }
die() { printf '\n\033[1;31mERROR: %s\033[0m\n' "$*" >&2; exit 1; }
[[ $EUID -eq 0 ]] || die "Kör skriptet som root eller med sudo."
command -v apt-get >/dev/null || die "Det här skriptet är avsett för Debian/derivat med apt."
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
[[ -f "$SCRIPT_DIR/app/main.py" ]] || die "app/main.py saknas. Kör skriptet från Fikakassans projektkatalog."
[[ -f "$SCRIPT_DIR/requirements.txt" ]] || die "requirements.txt saknas."
if [[ -z "$WIFI_SSID" ]]; then
read -r -p "Wi-Fi SSID för Samsung A50-hotspot: " WIFI_SSID
fi
if [[ -z "$WIFI_PASSWORD" ]]; then
read -r -s -p "Wi-Fi-lösenord: " WIFI_PASSWORD
echo
fi
log "Installerar systempaket"
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y \
network-manager \
openssh-server \
python3 \
python3-venv \
python3-pip \
xorg \
xinit \
openbox \
chromium \
unclutter \
x11-xserver-utils \
fonts-dejavu \
fonts-liberation \
nftables \
rsync
systemctl enable NetworkManager.service
systemctl enable ssh.service
systemctl enable nftables.service
log "Kontrollerar kiosk-användare"
if ! id "$KIOSK_USER" >/dev/null 2>&1; then
die "Användaren '$KIOSK_USER' finns inte. Skapa användaren först eller ange KIOSK_USER=..."
fi
# Use the existing Debian user for the kiosk session. Do not alter its
# existing group memberships or sudo permissions.
usermod -s /bin/bash "$KIOSK_USER"
log "Installerar Fikakassan i $APP_DIR"
install -d -o root -g root "$APP_DIR"
rsync -a --delete \
--exclude='.venv/' \
--exclude='__pycache__/' \
--exclude='*.pyc' \
"$SCRIPT_DIR/" "$APP_DIR/"
# The kiosk user needs to read the application, but not modify it.
chown -R root:root "$APP_DIR"
find "$APP_DIR" -type d -exec chmod 755 {} +
find "$APP_DIR" -type f -exec chmod 644 {} +
log "Skapar Python virtualenv"
rm -rf "$APP_DIR/.venv"
python3 -m venv "$APP_DIR/.venv"
# Do not execute the pip wrapper directly. Some Debian filesystems/policies
# can leave the wrapper without execute permission. Running pip through the
# virtualenv Python avoids that failure.
"$APP_DIR/.venv/bin/python" -m pip install \
--disable-pip-version-check \
--no-cache-dir \
-r "$APP_DIR/requirements.txt"
log "Skapar systemd-tjänst för Fikakassan"
cat > /etc/systemd/system/fikakassa.service <<EOF
[Unit]
Description=Fikakassan FastAPI
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=$APP_DIR
Environment=PYTHONUNBUFFERED=1
ExecStart=$APP_DIR/.venv/bin/uvicorn app.main:app --host 0.0.0.0 --port 8000
Restart=always
RestartSec=2
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
ReadWritePaths=$APP_DIR/data
[Install]
WantedBy=multi-user.target
EOF
install -d -m 755 "$APP_DIR/data"
systemctl daemon-reload
systemctl enable --now fikakassa.service
log "Konfigurerar Wi-Fi"
systemctl enable --now NetworkManager.service
# Remove an old profile with the same name, then create a clean one.
nmcli connection delete "$WIFI_CONNECTION" >/dev/null 2>&1 || true
nmcli radio wifi on
nmcli device wifi connect "$WIFI_SSID" \
password "$WIFI_PASSWORD" \
name "$WIFI_CONNECTION"
nmcli connection modify "$WIFI_CONNECTION" \
connection.autoconnect yes \
connection.autoconnect-retries 0 \
connection.mdns 0 \
ipv4.never-default yes \
ipv4.route-metric 600 \
ipv6.method disabled \
ipv6.never-default yes
# Give the hotspot connection high priority if several Wi-Fi networks are known.
nmcli connection modify "$WIFI_CONNECTION" connection.autoconnect-priority 100
nmcli connection up "$WIFI_CONNECTION" || true
# Wired Ethernet is the maintenance/Internet path. Do not create a special
# Ethernet profile: NetworkManager's normal DHCP profile should be allowed to
# obtain a default route whenever a cable is connected. The Wi-Fi profile has
# never-default enabled above, so Ethernet wins the default route naturally.
# Existing Ethernet profiles are left untouched.
log "Konfigurerar lokal brandvägg"
if [[ "$LOCK_DOWN_INTERNET" == "1" ]]; then
# The kiosk needs:
# - local LAN traffic to the A50/kiosk
# - DNS for NTP hostname resolution
# - UDP/123 for NTP
# - DHCP
# - established traffic
#
# It must NOT be able to browse the Internet through the phone.
# A wired Ethernet connection (interface names en*) is intentionally
# exempted from the outbound lockdown, so plugging in a cable gives
# normal Internet access for updates and development while Wi-Fi remains
# available for the A50/local kiosk network.
#
# All RFC1918 traffic is permitted so the Android bridge can talk to the
# kiosk even if Samsung changes the hotspot subnet.
cat > /etc/nftables.conf <<'EOF'
#!/usr/sbin/nft -f
flush ruleset
table inet kiosk_filter {
chain input {
type filter hook input priority filter; policy drop;
iifname "lo" accept
ct state established,related accept
# ICMP is useful for diagnostics and does not provide Internet browsing.
ip protocol icmp accept
# DHCP client traffic.
udp sport 67 udp dport 68 accept
# SSH and Fikakassan are available from the local network only.
ip saddr 10.0.0.0/8 tcp dport 22 accept
ip saddr 172.16.0.0/12 tcp dport 22 accept
ip saddr 192.168.0.0/16 tcp dport 22 accept
ip saddr 10.0.0.0/8 tcp dport 8000 accept
ip saddr 172.16.0.0/12 tcp dport 8000 accept
ip saddr 192.168.0.0/16 tcp dport 8000 accept
}
chain forward {
type filter hook forward priority filter; policy drop;
}
chain output {
type filter hook output priority filter; policy drop;
oifname "lo" accept
ct state established,related accept
# DHCP client.
udp sport 68 udp dport 67 accept
# A wired Ethernet connection is the maintenance/Internet path.
# Allow normal outbound traffic there; Wi-Fi remains locked to local
# networks plus DNS/NTP.
oifname "en*" accept
# DNS and NTP are explicitly allowed.
udp dport 53 accept
tcp dport 53 accept
udp dport 123 accept
# Permit local/private networks only.
ip daddr 10.0.0.0/8 accept
ip daddr 172.16.0.0/12 accept
ip daddr 192.168.0.0/16 accept
# ICMP for local diagnostics.
ip daddr 10.0.0.0/8 ip protocol icmp accept
ip daddr 172.16.0.0/12 ip protocol icmp accept
ip daddr 192.168.0.0/16 ip protocol icmp accept
}
}
EOF
nft -f /etc/nftables.conf
else
warn "Internet-lockdown är avstängt (LOCK_DOWN_INTERNET=0)."
fi
log "Stänger av automatisk sleep/blanking"
mkdir -p /etc/systemd/logind.conf.d
cat > /etc/systemd/logind.conf.d/90-fikakassa.conf <<'EOF'
[Login]
HandlePowerKey=ignore
HandleSuspendKey=ignore
HandleHibernateKey=ignore
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
IdleAction=ignore
EOF
systemctl restart systemd-logind.service || true
log "Konfigurerar X11/Openbox"
KIOSK_HOME="/home/$KIOSK_USER"
install -d -o "$KIOSK_USER" -g "$KIOSK_USER" \
"$KIOSK_HOME/.config/openbox" \
"$KIOSK_HOME/.config"
cat > "$KIOSK_HOME/.xinitrc" <<'EOF'
#!/bin/sh
exec openbox-session
EOF
chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.xinitrc"
chmod 755 "$KIOSK_HOME/.xinitrc"
cat > "$KIOSK_HOME/.config/openbox/autostart" <<EOF
#!/bin/sh
# Disable screen blanking and DPMS.
xset s off
xset -dpms
xset s noblank
# Hide mouse cursor after a short delay.
unclutter --timeout 2 --jitter 5 --ignore-scrolling &
# Start the kiosk browser.
exec chromium \\
--kiosk \\
--start-fullscreen \\
--no-first-run \\
--no-default-browser-check \\
--disable-session-crashed-bubble \\
--disable-infobars \\
--disable-translate \\
--disable-features=TranslateUI \\
--password-store=basic \\
--disable-pinch \\
--overscroll-history-navigation=0 \\
--disable-background-networking \\
--disable-component-update \\
--disable-domain-reliability \\
--disable-sync \\
--metrics-recording-only \\
--noerrdialogs \\
"$KIOSK_URL"
EOF
chown -R "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.config"
chmod 755 "$KIOSK_HOME/.config/openbox/autostart"
log "Konfigurerar automatisk inloggning på tty1"
mkdir -p /etc/systemd/system/getty@tty1.service.d
cat > /etc/systemd/system/getty@tty1.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin $KIOSK_USER --noclear %I \$TERM
Type=idle
EOF
# Start X only on tty1 for the kiosk account.
cat > "$KIOSK_HOME/.bash_profile" <<'EOF'
# Start graphical kiosk session automatically.
if [ -z "$DISPLAY" ] && [ "$(tty 2>/dev/null)" = "/dev/tty1" ]; then
exec startx
fi
EOF
chown "$KIOSK_USER:$KIOSK_USER" "$KIOSK_HOME/.bash_profile"
chmod 644 "$KIOSK_HOME/.bash_profile"
systemctl daemon-reload
systemctl enable getty@tty1.service
systemctl restart getty@tty1.service || true
log "Sätter systemtid/NTP"
# systemd-timesyncd is enough; no Internet browsing is required.
apt-get install -y systemd-timesyncd
systemctl enable --now systemd-timesyncd.service || true
log "Aktiverar automatisk omstart av Chromium om X-sessionen kraschar"
# Xorg/startx returns to tty1 if Chromium/Openbox dies. systemd getty will
# respawn the session. This keeps the kiosk self-healing after crashes.
systemctl restart getty@tty1.service || true
cat <<EOF
========================================
FIKAKASSAN KIOSK ÄR KONFIGURERAD
========================================
Kiosk-användare: $KIOSK_USER
Applikation: $APP_DIR
Webbapp: $KIOSK_URL
Wi-Fi: $WIFI_SSID
Wi-Fi-profil: $WIFI_CONNECTION
FastAPI:
systemctl status fikakassa
journalctl -u fikakassa -f
SSH:
systemctl status ssh
Nätverk:
nmcli device status
nmcli connection show
Kiosk:
tty1 autologin -> Xorg -> Openbox -> Chromium
Internet-lockdown:
$([[ "$LOCK_DOWN_INTERNET" == "1" ]] && echo "AKTIV: endast lokalt nät + DNS/NTP" || echo "AVSTÄNGD")
Starta om maskinen nu:
reboot
OBS:
"Power on efter strömavbrott" är en BIOS/UEFI-inställning och kan inte
garanteras av Linux. På X1 Yoga behöver "Power on with AC attach" /
motsvarande BIOS-inställning aktiveras om maskinen ska starta automatiskt
efter ett strömavbrott.
EOF