|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +cmd="${1:-run}" |
| 5 | + |
| 6 | +export HOME="${HOME:-/home/bridge}" |
| 7 | + |
| 8 | +# Persist everything under /data by default (works well with k8s PVCs). |
| 9 | +export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-/data/config}" |
| 10 | +export XDG_DATA_HOME="${XDG_DATA_HOME:-/data/data}" |
| 11 | +export XDG_CACHE_HOME="${XDG_CACHE_HOME:-/data/cache}" |
| 12 | + |
| 13 | +mkdir -p "$XDG_CONFIG_HOME" "$XDG_DATA_HOME" "$XDG_CACHE_HOME" |
| 14 | + |
| 15 | +uid="$(id -u)" |
| 16 | +export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/tmp/runtime-$uid}" |
| 17 | +mkdir -p "$XDG_RUNTIME_DIR" |
| 18 | +chmod 700 "$XDG_RUNTIME_DIR" |
| 19 | + |
| 20 | +DBUS_SOCKET="${DBUS_SOCKET:-$XDG_RUNTIME_DIR/dbus-session.sock}" |
| 21 | +export DBUS_SESSION_BUS_ADDRESS="unix:path=$DBUS_SOCKET" |
| 22 | + |
| 23 | +start_dbus_and_keyring() { |
| 24 | + # Start a private session bus and gnome-keyring (Secret Service) so the bridge |
| 25 | + # can store/retrieve credentials. |
| 26 | + rm -f "$DBUS_SOCKET" |
| 27 | + dbus-daemon --session --address="unix:path=$DBUS_SOCKET" --nofork --nopidfile & |
| 28 | + |
| 29 | + # gnome-keyring-daemon prints shell exports. Keep output quiet. |
| 30 | + # shellcheck disable=SC2046 |
| 31 | + eval "$(gnome-keyring-daemon --start --components=secrets 2>/dev/null)" |
| 32 | + |
| 33 | + # Best-effort unlock. If the keyring is configured with an empty password this |
| 34 | + # will succeed; otherwise Bridge may prompt/require manual unlock. |
| 35 | + printf '%s' "${KEYRING_PASSWORD:-}" | gnome-keyring-daemon --unlock >/dev/null 2>&1 || true |
| 36 | +} |
| 37 | + |
| 38 | +start_socat_forwarders() { |
| 39 | + container_smtp="${CONTAINER_SMTP_PORT:-1025}" |
| 40 | + container_imap="${CONTAINER_IMAP_PORT:-1143}" |
| 41 | + bridge_smtp="${PROTON_BRIDGE_SMTP_PORT:-1026}" |
| 42 | + bridge_imap="${PROTON_BRIDGE_IMAP_PORT:-1144}" |
| 43 | + socat_opts="${SOCAT_OPTS:-}" |
| 44 | + |
| 45 | + socat "TCP-LISTEN:${container_smtp},fork,reuseaddr${socat_opts:+,${socat_opts}}" \ |
| 46 | + "TCP:127.0.0.1:${bridge_smtp}" & |
| 47 | + socat "TCP-LISTEN:${container_imap},fork,reuseaddr${socat_opts:+,${socat_opts}}" \ |
| 48 | + "TCP:127.0.0.1:${bridge_imap}" & |
| 49 | +} |
| 50 | + |
| 51 | +bridge_cli_args() { |
| 52 | + # Proton Bridge has used both "--cli" and "-c" for interactive CLI mode across versions. |
| 53 | + if bridge --help 2>&1 | grep -q -- "--cli"; then |
| 54 | + printf '%s\n' "--cli" |
| 55 | + return 0 |
| 56 | + fi |
| 57 | + printf '%s\n' "-c" |
| 58 | +} |
| 59 | + |
| 60 | +start_dbus_and_keyring |
| 61 | + |
| 62 | +case "$cmd" in |
| 63 | + init) |
| 64 | + shift || true |
| 65 | + exec bridge "$(bridge_cli_args)" "$@" |
| 66 | + ;; |
| 67 | + run) |
| 68 | + shift || true |
| 69 | + start_socat_forwarders |
| 70 | + add_noninteractive=1 |
| 71 | + for a in "$@"; do |
| 72 | + case "$a" in |
| 73 | + --noninteractive|-n|--cli|-c) |
| 74 | + add_noninteractive=0 |
| 75 | + ;; |
| 76 | + esac |
| 77 | + done |
| 78 | + if [ "$add_noninteractive" -eq 1 ]; then |
| 79 | + set -- --noninteractive "$@" |
| 80 | + fi |
| 81 | + exec bridge "$@" |
| 82 | + ;; |
| 83 | + *) |
| 84 | + # Allow running arbitrary commands (debug, etc). |
| 85 | + exec "$@" |
| 86 | + ;; |
| 87 | +esac |
0 commit comments