#!/bin/sh # mikmar, from https://m.codefloe.page/mikmar/release: the app, fetched and opened. Nothing to install. # # curl -sL https://m.codefloe.page/mikmar | sh # keeps the app in ~/.cache/mikmar: the next start fetches nothing (until it changes) # curl -sL https://m.codefloe.page/mikmar | sh -s -- --private # keeps nothing: the app runs from memory, writes nothing, and is gone when it closes # # The app's bundle comes from where this script did (https://m.codefloe.page/mikmar/release), and is refused unless it's the one # named below (its SHA-256). The app talks to this server's own keys (pin, wire: below) and no # other. All of it is one function, run on the last line: a download cut short runs nothing. mikmar() { set -eu site='https://m.codefloe.page/mikmar/release' server='' pin='' wire='' wire_at='' version='ff14812811a3176c2faf7ee8dcc7e4ca3828618ec3915e37a60bc09e306883e1' patches='' private='' find='ask' # (names it knows, each with where its server really is: its own onion service, so the name's # door, which anyone with the name could stand at, is never asked) known='melani=dvqzy4mrywvklyos6ptomqkykzaqf3y4hyz4vuz5njmgide5rlpfliad.onion' # (options, kept for the app; anything else, the name of the server to find) given= for a in "$@"; do shift case "$a" in --private) private=1; set -- "$@" "$a" ;; -*) set -- "$@" "$a" ;; *) given=$a ;; esac done say() { printf 'mikmar: %s ' "$*" >&2; } # (fetched with curl, else wget: whichever the machine has; BusyBox's wget does, though it # doesn't check who it's talking to: what it fetches is refused unless it's the SHA-256 named # here. GNU wget's IPv4 first: where IPv6 is broken, it tries that half a minute) if command -v curl >/dev/null 2>&1; then get() { curl -fsS -o "$2" "$1"; } elif wget --version 2>/dev/null | grep -q 'GNU Wget'; then get() { wget -q --prefer-family=IPv4 -O "$2" "$1"; } elif command -v wget >/dev/null 2>&1; then get() { wget -q -O "$2" "$1"; } else say "it needs curl or wget, to fetch the app"; exit 1 fi if [ "$find" = ask ]; then # (where its server is: a name said, MIKMAR_NAME, MIKMAR_CODE; else asked for, not shown as # it's typed) find=${given:-${MIKMAR_NAME:-${MIKMAR_CODE:-melani}}} if [ -z "$find" ]; then printf 'mikmar: the name of its server (or its code): ' >&2 stty -echo < /dev/tty 2>/dev/null || true read -r find < /dev/tty || true stty echo < /dev/tty 2>/dev/null || true printf '\n' >&2 fi [ -n "$find" ] || { say "it opens at a server it's told of: curl … | sh -s NAME"; exit 1; } fi # (where its server is, handed over in its environment, not its command line: any program can # list command lines; once the app is up, its environment is the system's to read alone) if [ -n "$find" ]; then # (a code: mk1 and 55 letters; anything else, a name) case "$find" in mk1???????????????????????????????????????????????????????) export MIKMAR_CODE="$find" ;; *) export MIKMAR_NAME="$find" word=$(printf %s "$find" | tr 'A-Z' 'a-z') for k in $known; do [ "${k%%=*}" = "$word" ] && export MIKMAR_ONION="${k#*=}"; done ;; esac else export MIKMAR_SERVER="$server" MIKMAR_PIN="$pin" MIKMAR_WIRE_KEY="$wire" MIKMAR_WIRE_AT="$wire_at" fi [ -n "${WAYLAND_DISPLAY:-}" ] || { say "the app needs a Wayland session"; exit 1; } if [ -n "$private" ]; then # a folder in memory (tmpfs), only yours, gone when the app closes mem=${XDG_RUNTIME_DIR:-/dev/shm} [ "$(stat -f -c %T "$mem" 2>/dev/null)" = tmpfs ] || mem=/dev/shm [ "$(stat -f -c %T "$mem" 2>/dev/null)" = tmpfs ] || { say "there's no folder in memory to run the app from"; exit 1; } dir=$(mktemp -d "$mem/mikmar-XXXXXXXX") trap 'rm -rf "$dir"' EXIT trap 'exit 129' HUP INT TERM else cache=${XDG_CACHE_HOME:-$HOME/.cache}/mikmar dir=$cache/$version fi if [ ! -x "$dir/mikmar" ]; then # (asked only when fetching: an app kept here was fetched for this machine; a fork less to # open it) [ "$(uname -sm)" = "Linux x86_64" ] || { say "the app is for Linux on x86_64"; exit 1; } if [ -z "$private" ]; then mkdir -p "$cache" && chmod 700 "$cache" rm -rf "$cache"/.fetching-* into=$(mktemp -d "$cache/.fetching-XXXXXXXX") else into=$dir fi got= # an app kept from an earlier version, and a patch from it: only what changed fetched (what # it makes checked as the whole would be: it's the version, or it's not used) if [ -z "$private" ] && command -v zstd >/dev/null 2>&1; then for old in $patches; do if [ -f "$cache/$old/app.tar" ] && get "$site/app.patch.$old" "$into/patch" 2>/dev/null && zstd -dqf --long=27 --patch-from="$cache/$old/app.tar" "$into/patch" -o "$into/app.tar" 2>/dev/null && [ "$(sha256sum < "$into/app.tar" | cut -c1-64)" = "$version" ]; then got=1 break fi rm -f "$into/patch" "$into/app.tar" done rm -f "$into/patch" fi if [ -z "$got" ]; then if command -v zstd >/dev/null 2>&1; then packed=zst sum=dc5bed60ce1e7a87ac8739be7c0f6b2b2792cb886b46c3e6e0dc3623b9c09461 unpack='zstd -dcq' elif true; then packed=gz sum=f69c566bb45279162e6dd19335223dcf14a7129a347d8b61fb033f98bf0027e2 unpack='gzip -dc' else say "this machine can't unpack the app (it needs zstd or gzip)"; exit 1 fi get "$site/app.tar.$packed" "$into/app.tar.$packed" || { say "couldn't fetch the app from $site"; exit 1; } [ "$(sha256sum < "$into/app.tar.$packed" | cut -c1-64)" = "$sum" ] || { say "the app fetched isn't the one this server announced"; exit 1; } $unpack "$into/app.tar.$packed" > "$into/app.tar" && rm "$into/app.tar.$packed" fi tar -xf "$into/app.tar" -C "$into" # (kept, unpacked, to patch the next version from; private: nothing kept) [ -z "$private" ] || rm "$into/app.tar" [ -x "$into/mikmar" ] || { say "the app didn't unpack"; exit 1; } if [ "$into" != "$dir" ]; then rm -rf "$dir" && mv "$into" "$dir" # (apps from before: let go of) for o in "$cache"/*; do [ "$o" = "$dir" ] || rm -rf "$o"; done fi fi # its own loader and libraries first; then the system's, for its GPU driver libs=$dir/lib for d in /run/opengl-driver/lib /usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib /lib/x86_64-linux-gnu /lib64 /lib; do [ -d "$d" ] && libs=$libs:$d done if [ -n "$private" ]; then "$dir/lib/ld-linux-x86-64.so.2" --library-path "$libs" "$dir/mikmar" --private "$@" else exec "$dir/lib/ld-linux-x86-64.so.2" --library-path "$libs" "$dir/mikmar" "$@" fi } mikmar "$@"