2017-04-27 07:03:55 +00:00
|
|
|
#! /usr/bin/env bash
|
2015-07-05 23:39:54 +00:00
|
|
|
|
|
|
|
KEYS_DIR="/opt/dnscrypt-wrapper/etc/keys"
|
|
|
|
STKEYS_DIR="${KEYS_DIR}/short-term"
|
|
|
|
|
|
|
|
prune() {
|
2017-04-27 07:03:55 +00:00
|
|
|
/usr/bin/find "$STKEYS_DIR" -type f -cmin +1440 -exec rm -f {} \;
|
2015-07-05 23:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rotation_needed() {
|
2015-07-12 13:43:57 +00:00
|
|
|
if [ ! -f "${STKEYS_DIR}/dnscrypt.cert" ]; then
|
2015-07-05 23:39:54 +00:00
|
|
|
echo true
|
|
|
|
else
|
2017-04-27 07:03:55 +00:00
|
|
|
if [ $(/usr/bin/find "$STKEYS_DIR" -type f -cmin -720 -print -quit | wc -l | sed 's/[^0-9]//g') -le 0 ]; then
|
2015-07-12 13:43:57 +00:00
|
|
|
echo true
|
|
|
|
else
|
|
|
|
echo false
|
|
|
|
fi
|
2015-07-05 23:39:54 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
new_key() {
|
|
|
|
ts=$(date '+%s')
|
|
|
|
/opt/dnscrypt-wrapper/sbin/dnscrypt-wrapper --gen-crypt-keypair \
|
|
|
|
--crypt-secretkey-file="${STKEYS_DIR}/${ts}.key" &&
|
|
|
|
/opt/dnscrypt-wrapper/sbin/dnscrypt-wrapper --gen-cert-file \
|
|
|
|
--provider-publickey-file="${KEYS_DIR}/public.key" \
|
|
|
|
--provider-secretkey-file="${KEYS_DIR}/secret.key" \
|
|
|
|
--crypt-secretkey-file="${STKEYS_DIR}/${ts}.key" \
|
2015-07-12 13:45:44 +00:00
|
|
|
--provider-cert-file="${STKEYS_DIR}/${ts}.cert" \
|
2015-07-06 00:10:51 +00:00
|
|
|
--cert-file-expire-days=1 && \
|
2017-06-09 12:10:30 +00:00
|
|
|
mv -f "${STKEYS_DIR}/${ts}.cert" "${STKEYS_DIR}/dnscrypt.cert" && \
|
|
|
|
/opt/dnscrypt-wrapper/sbin/dnscrypt-wrapper --gen-cert-file \
|
|
|
|
--xchacha20 \
|
|
|
|
--provider-publickey-file="${KEYS_DIR}/public.key" \
|
|
|
|
--provider-secretkey-file="${KEYS_DIR}/secret.key" \
|
|
|
|
--crypt-secretkey-file="${STKEYS_DIR}/${ts}.key" \
|
|
|
|
--provider-cert-file="${STKEYS_DIR}/${ts}-xchacha20.cert" \
|
|
|
|
--cert-file-expire-days=1 && \
|
|
|
|
mv -f "${STKEYS_DIR}/${ts}-xchacha20.cert" "${STKEYS_DIR}/dnscrypt-xchacha20.cert"
|
2015-07-05 23:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stkeys_files() {
|
|
|
|
res=""
|
|
|
|
for file in $(ls "$STKEYS_DIR"/[0-9]*.key); do
|
|
|
|
res="${res}${file},"
|
|
|
|
done
|
|
|
|
echo "$res"
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ ! -f "$KEYS_DIR/provider_name" ]; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
provider_name=$(cat "$KEYS_DIR/provider_name")
|
|
|
|
|
|
|
|
mkdir -p "$STKEYS_DIR"
|
|
|
|
prune
|
|
|
|
[ $(rotation_needed) = true ] && new_key
|
|
|
|
|
|
|
|
exec /opt/dnscrypt-wrapper/sbin/dnscrypt-wrapper \
|
|
|
|
--user=_dnscrypt-wrapper \
|
|
|
|
--listen-address=0.0.0.0:443 \
|
2015-11-28 12:52:35 +00:00
|
|
|
--resolver-address=127.0.0.1:553 \
|
2015-07-05 23:39:54 +00:00
|
|
|
--provider-name="$provider_name" \
|
2017-06-16 09:34:53 +00:00
|
|
|
--provider-cert-file="${STKEYS_DIR}/dnscrypt.cert,${STKEYS_DIR}/dnscrypt-xchacha20.cert" \
|
2015-07-05 23:39:54 +00:00
|
|
|
--crypt-secretkey-file=$(stkeys_files)
|