dashPi: a self-hosted home dashboard, its sync server and phone app
The dashboard (Raspberry Pi 3.5" screen + web pages), the end-to-end encrypted sync mailbox (PHP + SQLite) and the Android companion app (Flutter), with docs, clean-install checks and screenshots made from demo data. Built mostly with an AI coding assistant; not yet reviewed line by line by a human (see README and SECURITY.md). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
fb3c01cdd7
333 changed files with 45151 additions and 0 deletions
72
server/bin/admin.php
Normal file
72
server/bin/admin.php
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
// Accounts and device tokens, on the server's command line:
|
||||
//
|
||||
// php bin/admin.php add-account <name>
|
||||
// php bin/admin.php add-device <account> <device name> prints the token once (only its hash is kept)
|
||||
// php bin/admin.php list
|
||||
// php bin/admin.php revoke <account> <device name>
|
||||
// php bin/admin.php stats
|
||||
declare(strict_types=1);
|
||||
|
||||
if (PHP_SAPI !== 'cli') {
|
||||
exit(1);
|
||||
}
|
||||
$config = require __DIR__ . '/../config.php';
|
||||
require __DIR__ . '/../src/db.php';
|
||||
$pdo = open_db($config['db']);
|
||||
$a = $argv;
|
||||
$now = gmdate('c');
|
||||
|
||||
function account_id(PDO $pdo, string $name): int {
|
||||
$st = $pdo->prepare('SELECT id FROM account WHERE name = ?');
|
||||
$st->execute([$name]);
|
||||
$id = $st->fetchColumn();
|
||||
if ($id === false) {
|
||||
fwrite(STDERR, "No account “{$name}”.\n");
|
||||
exit(1);
|
||||
}
|
||||
return (int)$id;
|
||||
}
|
||||
|
||||
switch ($a[1] ?? '') {
|
||||
case 'add-account':
|
||||
if (!isset($a[2]) || !preg_match('/^[a-z0-9_-]{2,40}$/', $a[2])) {
|
||||
exit("Usage: add-account <name> (a-z, 0-9, _ -)\n");
|
||||
}
|
||||
$pdo->prepare('INSERT INTO account (name, created) VALUES (?, ?)')->execute([$a[2], $now]);
|
||||
echo "Account “{$a[2]}” made.\n";
|
||||
break;
|
||||
case 'add-device':
|
||||
if (!isset($a[2], $a[3])) {
|
||||
exit("Usage: add-device <account> <device name>\n");
|
||||
}
|
||||
$acc = account_id($pdo, $a[2]);
|
||||
$token = rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
|
||||
$pdo->prepare('INSERT INTO device (account_id, name, token_hash, created) VALUES (?, ?, ?, ?)')
|
||||
->execute([$acc, $a[3], hash('sha256', $token), $now]);
|
||||
echo "Device “{$a[3]}” added to “{$a[2]}”. Its token (shown only now):\n\n {$token}\n\n";
|
||||
break;
|
||||
case 'list':
|
||||
foreach ($pdo->query('SELECT a.name AS account, d.name, d.created, d.last_seen, d.revoked
|
||||
FROM device d JOIN account a ON a.id = d.account_id ORDER BY a.name, d.id') as $r) {
|
||||
printf("%-12s %-20s made %s seen %s%s\n", $r['account'], $r['name'], substr($r['created'], 0, 10),
|
||||
$r['last_seen'] ? substr($r['last_seen'], 0, 16) : 'never', $r['revoked'] ? ' REVOKED' : '');
|
||||
}
|
||||
break;
|
||||
case 'revoke':
|
||||
if (!isset($a[2], $a[3])) {
|
||||
exit("Usage: revoke <account> <device name>\n");
|
||||
}
|
||||
$st = $pdo->prepare('UPDATE device SET revoked = 1 WHERE account_id = ? AND name = ?');
|
||||
$st->execute([account_id($pdo, $a[2]), $a[3]]);
|
||||
echo $st->rowCount() ? "Revoked.\n" : "No such device.\n";
|
||||
break;
|
||||
case 'stats':
|
||||
foreach ($pdo->query('SELECT a.name, COUNT(b.seq) AS n, COALESCE(SUM(b.size), 0) AS bytes, MAX(b.at) AS last
|
||||
FROM account a LEFT JOIN batch b ON b.account_id = a.id GROUP BY a.id') as $r) {
|
||||
printf("%-12s %6d batches %8.1f KB last %s\n", $r['name'], $r['n'], $r['bytes'] / 1024, $r['last'] ?? '—');
|
||||
}
|
||||
break;
|
||||
default:
|
||||
echo "php bin/admin.php add-account <name> | add-device <account> <device> | list | revoke <account> <device> | stats\n";
|
||||
}
|
||||
65
server/bin/setup-server.sh
Normal file
65
server/bin/setup-server.sh
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
#!/bin/bash
|
||||
# One-time setup of the sync server on a Debian/Ubuntu machine with Apache + PHP + certbot, run as
|
||||
# root from the folder the server was copied to (e.g. /srv/dash-sync — anywhere outside other sites):
|
||||
#
|
||||
# sudo bash /srv/dash-sync/bin/setup-server.sh <host name> <account> [<device> …]
|
||||
# sudo bash /srv/dash-sync/bin/setup-server.sh sync.example.com me
|
||||
#
|
||||
# It makes data/ (the web server's, 700), the Apache site (own file, nothing existing is edited),
|
||||
# the Let's Encrypt certificate, the account and its first device (default: dashboard) — whose
|
||||
# token it prints once, here in your terminal only. Phones and other dashboards get theirs from the
|
||||
# dashboard: Settings ▸ General ▸ Devices (a QR code). Safe to run again: existing pieces are left alone.
|
||||
set -euo pipefail
|
||||
[ $# -ge 2 ] || { sed -n '2,10p' "$0"; exit 2; }
|
||||
HOST=$1; ACCOUNT=$2; shift 2
|
||||
if [ $# -gt 0 ]; then DEVICES=("$@"); else DEVICES=(dashboard); fi
|
||||
DIR=$(cd "$(dirname "$0")/.." && pwd)
|
||||
SITE=/etc/apache2/sites-available/dash-sync-$HOST.conf
|
||||
LOGS='${APACHE_LOG_DIR}'
|
||||
|
||||
[ "$(id -u)" = 0 ] || { echo "run it with sudo"; exit 1; }
|
||||
case "$DIR" in /var/www/html|/var/www/html/*) echo "copy it outside another site's folder (e.g. /srv/dash-sync)"; exit 1 ;; esac
|
||||
cd "$DIR"
|
||||
|
||||
[ -f config.php ] || cp config.example.php config.php
|
||||
mkdir -p data
|
||||
chown -R www-data:www-data data
|
||||
chmod 700 data
|
||||
|
||||
if [ ! -f "$SITE" ]; then
|
||||
cat > "$SITE" <<EOF
|
||||
# ── $HOST — dash-sync mailbox (dashboard + phone app; PROTOCOL.md) ──────
|
||||
<VirtualHost *:80>
|
||||
ServerName $HOST
|
||||
DocumentRoot $DIR/public
|
||||
<Directory $DIR/public>
|
||||
AllowOverride All
|
||||
Require all granted
|
||||
</Directory>
|
||||
LimitRequestBody 9437184
|
||||
ErrorLog $LOGS/dash-sync.error.log
|
||||
CustomLog $LOGS/dash-sync.access.log combined
|
||||
</VirtualHost>
|
||||
EOF
|
||||
echo "site written: $SITE"
|
||||
fi
|
||||
a2enmod -q rewrite >/dev/null # public/.htaccess: every request → index.php
|
||||
a2ensite -q "dash-sync-$HOST" >/dev/null
|
||||
apache2ctl configtest
|
||||
systemctl reload apache2
|
||||
|
||||
if [ ! -d /etc/letsencrypt/live/$HOST ]; then
|
||||
certbot --apache -n --redirect -d $HOST
|
||||
fi
|
||||
|
||||
admin() { sudo -u www-data php "$DIR/bin/admin.php" "$@"; }
|
||||
admin add-account "$ACCOUNT" >/dev/null 2>&1 || true # already there on a second run
|
||||
for dev in "${DEVICES[@]}"; do
|
||||
if ! admin list 2>/dev/null | grep -qE "^$ACCOUNT +$dev "; then
|
||||
echo; echo "== token for $dev (shown once — keep it for its setup):"
|
||||
admin add-device "$ACCOUNT" "$dev"
|
||||
fi
|
||||
done
|
||||
|
||||
echo; echo "== check (should say 401 without a token):"
|
||||
curl -s -o /dev/null -w "%{http_code}\n" https://$HOST/v1/hello || echo "not reachable yet (DNS / certificate?)"
|
||||
Loading…
Add table
Add a link
Reference in a new issue