tails-i2p 3.09 KB
Newer Older
1 2 3
#!/bin/sh

set -e
kytv's avatar
kytv committed
4 5
set -u
ZSH_VERSION="${ZSH_VERSION:-}"
6 7 8 9 10 11 12 13 14 15 16 17

# Get LANG
. /etc/default/locale
export LANG

# Initialize gettext support
. gettext.sh
TEXTDOMAIN="tails"
export TEXTDOMAIN

# Import wait_until()
. /usr/local/lib/tails-shell-library/common.sh
18
# Import i2p_built_a_tunnel, i2p_reseed_failed, i2p_router_console_is_ready(),
19
# and set_best_i2p_router_console_lang().
20 21 22
. /usr/local/lib/tails-shell-library/i2p.sh

I2P_STARTUP_TIMEOUT=60
23 24 25 26 27 28 29 30

# When there are network problems (either local or remote), it can take up to 3
# minutes for all of the current reseed servers to be tried.
I2P_BOOTSTRAP_TIMEOUT=210

# After the router infos (RIs) are downloaded from the reseed servers
# it can take 3-5 minutes for a tunnel to be built, e.g.
# once we get to this point I2P should be ready to be used.
31
I2P_TUNNEL_BUILD_TIMEOUT=300
32 33 34 35 36

startup_failure() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P failed to start\"`" \
        "`gettext \"Something went wrong when I2P was starting. Check the logs in /var/log/i2p for more information.\"`"
37 38
    service i2p dump # generate a thread dump
    sleep 5 # Give thread dump ample time to complete
39
    systemctl stop i2p # clean up, just in case
40 41 42 43 44 45 46
    exit 1
}

wait_until_i2p_router_console_is_ready() {
    wait_until ${I2P_STARTUP_TIMEOUT} i2p_router_console_is_ready
}

kytv's avatar
kytv committed
47 48
wait_until_i2p_has_bootstrapped() {
    wait_until ${I2P_BOOTSTRAP_TIMEOUT} '[ "$(i2p_reseed_status)" = success ]'
49 50
}

51 52 53
notify_router_console_success() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P's router console is ready\"`" \
54
        "`gettext \"You can now access I2P's router console in the I2P Browser.\"`"
55 56 57 58 59
}

bootstrap_failure() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P is not ready\"`" \
60
        "`gettext \"Eepsite tunnel not built within six minutes. Check the router console in the I2P Browser or the logs in /var/log/i2p for more information. Reconnect to the network to try again.\"`"
61 62 63
    exit 1
}

64
wait_until_i2p_builds_a_tunnel() {
kytv's avatar
Reorder  
kytv committed
65
    wait_until ${I2P_TUNNEL_BUILD_TIMEOUT} i2p_built_a_tunnel
66 67
    # static sleep to work around upstream bug.
    sleep 240
68 69 70 71 72 73 74 75 76 77
}

notify_bootstrap_success() {
    /usr/local/sbin/tails-notify-user \
        "`gettext \"I2P is ready\"`" \
        "`gettext \"You can now access services on I2P.\"`"
}

case "${1}" in
    start|restart)
78 79
        # Stop I2P before setting the router console language in case
        # it pushes any updated options on quit.
80
        if systemctl --quiet is-active i2p; then
81
            systemctl stop i2p
82 83 84 85 86
        fi
        # Get LANG, since we may run this from an environment that
        # doesn't have it set.
        . /etc/default/locale
        set_best_i2p_router_console_lang
87
        systemctl start i2p
88 89
        wait_until_i2p_router_console_is_ready || startup_failure
        notify_router_console_success
kytv's avatar
kytv committed
90
        wait_until_i2p_has_bootstrapped || bootstrap_failure
91
        wait_until_i2p_builds_a_tunnel || bootstrap_failure
92 93 94
        notify_bootstrap_success
        ;;
    stop)
95
        exec systemctl stop i2p
96 97 98 99 100 101
        ;;
    *)
        echo "invalid argument '${1}'" >&2
        exit 1
        ;;
esac