2017-04-27 09:03:55 +02:00
|
|
|
#! /usr/bin/env bash
|
2015-07-06 01:39:54 +02:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
action="$1"
|
|
|
|
|
2019-09-22 15:38:22 +02:00
|
|
|
LEGACY_KEYS_DIR="/opt/dnscrypt-wrapper/etc/keys"
|
|
|
|
KEYS_DIR="/opt/encrypted-dns/etc/keys"
|
|
|
|
CONF_DIR="/opt/encrypted-dns/etc"
|
|
|
|
CONFIG_FILE="${CONF_DIR}/encrypted-dns.toml"
|
2019-09-22 17:16:03 +02:00
|
|
|
CONFIG_FILE_TEMPLATE="${CONF_DIR}/encrypted-dns.toml.in"
|
2015-07-06 01:39:54 +02:00
|
|
|
|
2018-01-22 20:23:54 +01:00
|
|
|
# -N provider-name -E external-ip-address:port
|
2015-07-06 01:39:54 +02:00
|
|
|
|
|
|
|
init() {
|
2019-05-12 13:07:02 +02:00
|
|
|
if [ "$(is_initialized)" = yes ]; then
|
2015-07-06 01:39:54 +02:00
|
|
|
start
|
|
|
|
exit $?
|
|
|
|
fi
|
2018-01-22 20:23:54 +01:00
|
|
|
while getopts "h?N:E:" opt; do
|
2015-07-06 01:39:54 +02:00
|
|
|
case "$opt" in
|
2019-09-22 17:16:03 +02:00
|
|
|
h | \?) usage ;;
|
|
|
|
N) provider_name=$(echo "$OPTARG" | sed -e 's/^[ \t]*//' | tr A-Z a-z) ;;
|
|
|
|
E) ext_address=$(echo "$OPTARG" | sed -e 's/^[ \t]*//' | tr A-Z a-z) ;;
|
2015-07-06 01:39:54 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
[ -z "$provider_name" ] && usage
|
|
|
|
case "$provider_name" in
|
2019-09-22 17:16:03 +02:00
|
|
|
.*) usage ;;
|
|
|
|
2.dnscrypt-cert.*) ;;
|
|
|
|
*) provider_name="2.dnscrypt-cert.${provider_name}" ;;
|
2015-07-06 01:39:54 +02:00
|
|
|
esac
|
2018-01-22 18:21:50 +01:00
|
|
|
|
2018-01-22 20:23:54 +01:00
|
|
|
[ -z "$ext_address" ] && usage
|
|
|
|
case "$ext_address" in
|
2019-09-22 17:16:03 +02:00
|
|
|
.*) usage ;;
|
|
|
|
0.*)
|
|
|
|
echo "Do not use 0.0.0.0, use an actual external IP address" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
2018-01-22 18:21:50 +01:00
|
|
|
esac
|
2018-01-22 20:23:54 +01:00
|
|
|
|
2018-01-22 18:21:50 +01:00
|
|
|
echo "Provider name: [$provider_name]"
|
2019-09-22 15:38:22 +02:00
|
|
|
|
2019-09-22 17:16:03 +02:00
|
|
|
echo "$provider_name" >"${KEYS_DIR}/provider_name"
|
2015-07-06 01:39:54 +02:00
|
|
|
chmod 644 "${KEYS_DIR}/provider_name"
|
2019-09-22 15:38:22 +02:00
|
|
|
|
2019-09-22 17:16:03 +02:00
|
|
|
sed \
|
|
|
|
-e "s/@PROVIDER_NAME@/${provider_name}/" \
|
|
|
|
-e "s/@EXTERNAL_IPV4@/${ext_address}/" \
|
|
|
|
"$CONFIG_FILE_TEMPLATE" >"$CONFIG_FILE"
|
|
|
|
|
|
|
|
/opt/encrypted-dns/sbin/encrypted-dns \
|
2019-09-22 15:38:22 +02:00
|
|
|
--config "$CONFIG_FILE" --dry-run |
|
|
|
|
tee "${KEYS_DIR}/provider-info.txt"
|
|
|
|
|
2015-07-06 01:39:54 +02:00
|
|
|
echo
|
|
|
|
echo -----------------------------------------------------------------------
|
|
|
|
echo
|
|
|
|
echo "Congratulations! The container has been properly initialized."
|
|
|
|
echo "Take a look up above at the way dnscrypt-proxy has to be configured in order"
|
|
|
|
echo "to connect to your resolver. Then, start the container with the default command."
|
|
|
|
}
|
|
|
|
|
|
|
|
provider_info() {
|
|
|
|
ensure_initialized
|
|
|
|
echo
|
2019-09-22 15:38:22 +02:00
|
|
|
cat "${KEYS_DIR}/provider-info.txt"
|
2015-07-06 01:39:54 +02:00
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
is_initialized() {
|
2019-09-22 15:38:22 +02:00
|
|
|
if [ ! -f "${KEYS_DIR}/encrypted-dns.state" ] && [ ! -f "${KEYS_DIR}/provider-info.txt" ] && [ ! -f "${KEYS_DIR}/provider_name" ]; then
|
2015-07-06 01:39:54 +02:00
|
|
|
echo no
|
|
|
|
else
|
|
|
|
echo yes
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure_initialized() {
|
2019-05-12 13:07:02 +02:00
|
|
|
if [ "$(is_initialized)" = no ]; then
|
2018-01-22 20:23:54 +01:00
|
|
|
echo "Please provide an initial configuration (init -N <provider_name> -E <external IP>)" >&2
|
2015-07-06 01:39:54 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
|
|
|
ensure_initialized
|
2019-09-22 17:31:17 +02:00
|
|
|
exec /etc/runit/2
|
|
|
|
}
|
|
|
|
|
|
|
|
shell() {
|
|
|
|
exec /bin/bash
|
2015-07-06 01:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
usage() {
|
2019-09-22 17:16:03 +02:00
|
|
|
cat <<EOT
|
2015-07-06 01:39:54 +02:00
|
|
|
Commands
|
|
|
|
========
|
|
|
|
|
2018-01-22 20:23:54 +01:00
|
|
|
* init -N <provider_name> -E <external ip>:<port>
|
2018-01-22 18:21:50 +01:00
|
|
|
initialize the container for a server accessible at ip <external ip> on port
|
|
|
|
<port>, for a provider named <provider_name>. This is required only once.
|
2015-07-06 01:39:54 +02:00
|
|
|
|
|
|
|
* start (default command): start the resolver and the dnscrypt server proxy.
|
|
|
|
Ports 443/udp and 443/tcp have to be publicly exposed.
|
|
|
|
|
2018-01-23 00:31:12 +01:00
|
|
|
* provider-info: prints the provider name and provider public key.
|
2015-07-06 01:39:54 +02:00
|
|
|
|
2019-09-22 17:31:17 +02:00
|
|
|
* shell: run a shell
|
|
|
|
|
2015-07-06 01:39:54 +02:00
|
|
|
This container has a single volume that you might want to securely keep a
|
2019-09-22 15:38:22 +02:00
|
|
|
backup of: /opt/encrypted-dns/etc/keys
|
2015-07-06 01:39:54 +02:00
|
|
|
EOT
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$action" in
|
2019-09-22 17:16:03 +02:00
|
|
|
start) start ;;
|
|
|
|
init)
|
|
|
|
shift
|
|
|
|
init $*
|
|
|
|
;;
|
|
|
|
provider-info) provider_info ;;
|
2019-09-22 17:31:17 +02:00
|
|
|
shell) shell ;;
|
2019-09-22 17:16:03 +02:00
|
|
|
*) usage ;;
|
2015-07-06 01:39:54 +02:00
|
|
|
esac
|