You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

83 lines
1.7 KiB

#!/bin/sh
. /lib/config/uci.sh
set -e
CACHE_DOMAINS_SRC="https://api.github.com/repos/uklans/cache-domains/tarball/master"
CACHE_DOMAINS_DIR="/var/cache-domains"
CACHE_DOMAINS_CONF_FILE="${CACHE_DOMAINS_DIR}/lancache.conf"
CONFIG_FILE="/etc/cache-domains.json"
dnsmasq_conf() {
local I=0
local DNSMASQ_CONF_DIR
local DNSMASQ_CONF_FILE
while uci_get "dhcp" "@dnsmasq[${I}]" > /dev/null; do
DNSMASQ_CONF_DIR="$(uci_get "dhcp" "@dnsmasq[${I}]" "confdir" "/tmp/dnsmasq.d" || :)"
DNSMASQ_CONF_FILE="${DNSMASQ_CONF_DIR}/$(basename "${CACHE_DOMAINS_CONF_FILE}")"
case ${1} in
add)
ln -sf "${CACHE_DOMAINS_CONF_FILE}" "${DNSMASQ_CONF_FILE}"
;;
remove)
rm -f "${DNSMASQ_CONF_FILE}"
;;
*)
echo "ERROR: Invalid action '${1}' for dnsmasq_conf()"
return 1
;;
esac
I=$((${I} + 1))
done
/etc/init.d/dnsmasq "restart"
}
configure() {
local INITIAL_DIR
local SOURCE_DIR
mkdir -p "${CACHE_DOMAINS_DIR}"
rm -fr "${CACHE_DOMAINS_DIR:?}/"*
if ! wget -qO - "${CACHE_DOMAINS_SRC}" | tar -xzC "${CACHE_DOMAINS_DIR}"; then
rm -fr "${CACHE_DOMAINS_DIR}"
echo "ERROR: Could not retrieve ${CACHE_DOMAINS_SRC}"
exit 1
fi
SOURCE_DIR="${CACHE_DOMAINS_DIR}/$(ls "${CACHE_DOMAINS_DIR}")"
if [ ! -f "${CONFIG_FILE}" ]; then
cp "${SOURCE_DIR}/scripts/config.example.json" "${CONFIG_FILE}"
echo "Using example config file ${CONFIG_FILE}"
fi
INITIAL_DIR="$(pwd)"
cd "${SOURCE_DIR}/scripts/"
cp "${CONFIG_FILE}" "config.json"
./create-dnsmasq.sh > /dev/null
cat "./output/dnsmasq/"*".conf" > "${CACHE_DOMAINS_CONF_FILE}"
cd "${INITIAL_DIR}"
rm -fr "${SOURCE_DIR}"
dnsmasq_conf add
}
cleanup() {
dnsmasq_conf remove
}
case ${1} in
config*)
configure
;;
clean*)
cleanup
;;
*)
echo "${0} <configure|cleanup>"
;;
esac