1
1
mirror of https://github.com/dnscrypt/dnscrypt-server-docker synced 2024-11-22 23:51:59 +01:00
dnscrypt-server-docker/entrypoint.sh

214 lines
7.1 KiB
Bash
Raw Normal View History

#! /usr/bin/env bash
2015-07-06 01:39:54 +02:00
set -e
action="$1"
LEGACY_KEYS_DIR="/opt/dnscrypt-wrapper/etc/keys"
2019-09-25 16:10:09 +02:00
LEGACY_LISTS_DIR="/opt/dnscrypt-wrapper/etc/lists"
2019-09-26 10:47:55 +02:00
LEGACY_STATE_DIR="${LEGACY_KEYS_DIR}/state"
KEYS_DIR="/opt/encrypted-dns/etc/keys"
2019-09-25 22:50:53 +02:00
STATE_DIR="${KEYS_DIR}/state"
2019-09-25 16:10:09 +02:00
LISTS_DIR="/opt/encrypted-dns/etc/lists"
CONF_DIR="/opt/encrypted-dns/etc"
CONFIG_FILE="${CONF_DIR}/encrypted-dns.toml"
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-09-25 22:50:53 +02:00
if [ "$(is_initialized)" = yes ]; then
2015-07-06 01:39:54 +02:00
start
exit $?
fi
2019-09-24 18:17:48 +02:00
2019-10-14 12:50:49 +02:00
anondns_enabled="false"
anondns_blacklisted_ips=""
while getopts "h?N:E:T:A" opt; do
2015-07-06 01:39:54 +02:00
case "$opt" in
2019-10-14 12:50:49 +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) ;;
T) tls_proxy_upstream_address=$(echo "$OPTARG" | sed -e 's/^[ \t]*//' | tr A-Z a-z) ;;
A) anondns_enabled="true" ;;
2015-07-06 01:39:54 +02:00
esac
done
[ -z "$provider_name" ] && usage
case "$provider_name" in
2019-10-14 12:50:49 +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-10-14 12:50:49 +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
2019-09-24 18:17:48 +02:00
tls_proxy_configuration=""
if [ -n "$tls_proxy_upstream_address" ]; then
tls_proxy_configuration="upstream_addr = \"${tls_proxy_upstream_address}\""
fi
2019-09-25 16:10:09 +02:00
domain_blacklist_file="${LISTS_DIR}/blacklist.txt"
domain_blacklist_configuration=""
if [ -s "$domain_blacklist_file" ]; then
domain_blacklist_configuration="domain_blacklist = \"${domain_blacklist_file}\""
fi
2018-01-22 18:21:50 +01:00
echo "Provider name: [$provider_name]"
2019-10-14 12:50:49 +02:00
echo "$provider_name" >"${KEYS_DIR}/provider_name"
2015-07-06 01:39:54 +02:00
chmod 644 "${KEYS_DIR}/provider_name"
sed \
2019-09-25 17:13:49 +02:00
-e "s#@PROVIDER_NAME@#${provider_name}#" \
-e "s#@EXTERNAL_IPV4@#${ext_address}#" \
-e "s#@TLS_PROXY_CONFIGURATION@#${tls_proxy_configuration}#" \
-e "s#@DOMAIN_BLACKLIST_CONFIGURATION@#${domain_blacklist_configuration}#" \
2019-10-14 12:50:49 +02:00
-e "s#@ANONDNS_ENABLEDN@#${anondns_enabled}#" \
-e "s#@ANONDNS_BLACKLISTED_IPS@#${anondns_blacklisted_ips}#" \
"$CONFIG_FILE_TEMPLATE" >"$CONFIG_FILE"
2019-09-25 22:50:53 +02:00
mkdir -p -m 700 "${STATE_DIR}"
chown _encrypted-dns:_encrypted-dns "${STATE_DIR}"
if [ -f "${KEYS_DIR}/secret.key" ]; then
echo "Importing the previous secret key [${KEYS_DIR}/secret.key]"
/opt/encrypted-dns/sbin/encrypted-dns \
--config "$CONFIG_FILE" \
--import-from-dnscrypt-wrapper "${KEYS_DIR}/secret.key" \
2019-10-14 12:50:49 +02:00
--dry-run >/dev/null || exit 1
mv -f "${KEYS_DIR}/secret.key" "${KEYS_DIR}/secret.key.migrated"
2019-09-25 22:50:53 +02:00
fi
/opt/encrypted-dns/sbin/encrypted-dns \
--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
cat "${KEYS_DIR}/provider-info.txt"
2015-07-06 01:39:54 +02:00
echo
}
2019-09-26 10:47:55 +02:00
legacy_compat() {
if [ -f "${KEYS_DIR}/provider-info.txt" ] && [ -f "${KEYS_DIR}/provider_name" ]; then
return 0
fi
if [ -f "${LEGACY_KEYS_DIR}/provider-info.txt" ] && [ -f "${LEGACY_KEYS_DIR}/provider_name" ]; then
echo "Using [${LEGACY_KEYS_DIR}] for keys" >&2
mkdir -p "${KEYS_DIR}"
2019-10-14 12:50:49 +02:00
mv -f "${KEYS_DIR}/provider-info.txt" "${KEYS_DIR}/provider-info.txt.migrated" 2>/dev/null || :
ln -s "${LEGACY_KEYS_DIR}/provider-info.txt" "${KEYS_DIR}/provider-info.txt" 2>/dev/null || :
mv -f "${KEYS_DIR}/provider_name" "${KEYS_DIR}/provider_name.migrated" 2>/dev/null || :
ln -s "${LEGACY_KEYS_DIR}/provider_name" "${KEYS_DIR}/provider_name" 2>/dev/null || :
mv -f "${KEYS_DIR}/secret.key" "${KEYS_DIR}/secret.key.migrated" 2>/dev/null || :
ln -s "${LEGACY_KEYS_DIR}/secret.key" "${KEYS_DIR}/secret.key" 2>/dev/null || :
2019-09-26 10:47:55 +02:00
mkdir -p -m 700 "${LEGACY_STATE_DIR}"
chown _encrypted-dns:_encrypted-dns "${LEGACY_STATE_DIR}"
2019-10-14 12:50:49 +02:00
mv -f "$STATE_DIR" "${STATE_DIR}.migrated" 2>/dev/null || :
ln -s "$LEGACY_STATE_DIR" "${STATE_DIR}" 2>/dev/null || :
2019-09-26 10:47:55 +02:00
fi
if [ -f "${LEGACY_LISTS_DIR}/blacklist.txt" ]; then
echo "Using [${LEGACY_LISTS_DIR}] for lists" >&2
mkdir -p "${LISTS_DIR}"
2019-10-14 12:50:49 +02:00
mv -f "${LISTS_DIR}/blacklist.txt" "${LISTS_DIR}/blacklist.txt.migrated" 2>/dev/null || :
ln -s "${LEGACY_LISTS_DIR}/blacklist.txt" "${LISTS_DIR}/blacklist.txt" 2>/dev/null || :
2019-09-26 10:47:55 +02:00
fi
}
2015-07-06 01:39:54 +02:00
is_initialized() {
2019-09-26 10:47:55 +02:00
if [ -f "$CONFIG_FILE" ] && [ -f "${STATE_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 yes
2019-09-26 10:47:55 +02:00
else
legacy_compat
if [ -f "$CONFIG_FILE" ] && [ -f "${STATE_DIR}/encrypted-dns.state" ] && [ -f "${KEYS_DIR}/provider-info.txt" ] && [ -f "${KEYS_DIR}/provider_name" ]; then
echo yes
else
echo no
fi
2015-07-06 01:39:54 +02:00
fi
}
ensure_initialized() {
if [ "$(is_initialized)" = no ]; then
2019-09-23 23:34:51 +02:00
if [ -d "$LEGACY_KEYS_DIR" ]; then
echo "Please provide an initial configuration (init -N <provider_name> -E <external IP>)" >&2
fi
2015-07-06 01:39:54 +02:00
exit 1
fi
}
start() {
ensure_initialized
if [ -f "${KEYS_DIR}/secret.key" ]; then
echo "Importing the previous secret key [${KEYS_DIR}/secret.key]"
/opt/encrypted-dns/sbin/encrypted-dns \
--config "$CONFIG_FILE" \
--import-from-dnscrypt-wrapper "${KEYS_DIR}/secret.key" \
2019-10-14 12:50:49 +02:00
--dry-run >/dev/null || exit 1
mv -f "${KEYS_DIR}/secret.key" "${KEYS_DIR}/secret.key.migrated"
fi
2019-09-23 19:40:44 +02:00
/opt/encrypted-dns/sbin/encrypted-dns \
--config "$CONFIG_FILE" --dry-run |
tee "${KEYS_DIR}/provider-info.txt"
2019-10-14 12:50:49 +02:00
exec /etc/runit/2 </dev/null >/dev/null 2>/dev/null
2019-09-22 17:31:17 +02:00
}
shell() {
exec /bin/bash
2015-07-06 01:39:54 +02:00
}
usage() {
2019-10-14 12:50:49 +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.
2019-10-14 12:50:49 +02:00
2019-09-24 18:17:48 +02:00
If TLS connections to the same port have to be redirected to a HTTPS server
(e.g. for DoH), add -T <https server ip>:<port>
2015-07-06 01:39:54 +02:00
2019-10-14 12:50:49 +02:00
To enable Anonymized DNS relaying, add -A.
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-24 17:41:49 +02:00
* shell: run a shell.
2019-09-22 17:31:17 +02:00
2015-07-06 01:39:54 +02:00
This container has a single volume that you might want to securely keep a
backup of: /opt/encrypted-dns/etc/keys
2015-07-06 01:39:54 +02:00
EOT
exit 1
}
case "$action" in
2019-10-14 12:50:49 +02:00
start) start ;;
init)
shift
init "$@"
;;
provider-info) provider_info ;;
shell) shell ;;
*) usage ;;
2015-07-06 01:39:54 +02:00
esac