From: Gerard Ryan Date: Tue, 19 Nov 2019 12:39:51 +0000 (+1000) Subject: cache-domains: Changed to hotplug script X-Git-Url: http://git.cdn.openwrt.org/?a=commitdiff_plain;h=124deec9ae2a92637490c616d0f91e3e369687bc;p=feed%2Fpackages.git cache-domains: Changed to hotplug script Since we have to restart dnsmasq to reload the config anyway, this package doesn't need to run before anything. We do however need to wait for the network so I've changed this service to be a hotplug script and utility script. Signed-off-by: Gerard Ryan --- diff --git a/utils/cache-domains/Makefile b/utils/cache-domains/Makefile index 8c6ace3e3c..cd14a3783c 100644 --- a/utils/cache-domains/Makefile +++ b/utils/cache-domains/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=cache-domains -PKG_VERSION:=1.0.0 +PKG_VERSION:=2.0.0 PKG_RELEASE:=1 PKG_MAINTAINER:=Gerard Ryan @@ -17,13 +17,16 @@ define Package/cache-domains/default endef define Package/cache-domains/description/default -Service to dynamically configure the local DNS (dnsmasq) to redirect game content servers to a LAN cache. +hotplug script to dynamically configure the local DNS (dnsmasq) to redirect game content servers to a LAN cache. Definitive list dynamically obtained from https://github.com/uklans/cache-domains. endef define Package/cache-domains/install/default - $(INSTALL_DIR) $(1)/etc/init.d - $(INSTALL_BIN) ./files/cache-domains.init $(1)/etc/init.d/cache-domains + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) ./files/cache-domains $(1)/usr/bin/ + + $(INSTALL_DIR) $(1)/etc/hotplug.d/iface/ + $(INSTALL_BIN) ./files/30-cache-domains $(1)/etc/hotplug.d/iface/ endef Build/Compile=# Nothing to compile, just install the scripts diff --git a/utils/cache-domains/README.md b/utils/cache-domains/README.md index 5ef40ddf52..3031db1a82 100644 --- a/utils/cache-domains/README.md +++ b/utils/cache-domains/README.md @@ -1,6 +1,6 @@ # cache-domains -Service to dynamically configure the local DNS (dnsmasq) to redirect game content servers to a LAN cache. Definitive list dynamically obtained from https://github.com/uklans/cache-domains. +hotplug script to dynamically configure the local DNS (dnsmasq) to redirect game content servers to a LAN cache. Definitive list dynamically obtained from https://github.com/uklans/cache-domains. ## Configuration Configuration file follows the same [syntax as the upsteam file](https://github.com/uklans/cache-domains/blob/master/scripts/config.example.json). The key for each `cache_domain` member matches the name of one of the `.txt` files in the [upstream root directory](https://github.com/uklans/cache-domains/blob/master/), except for the `default` key which matches the all the unreferenced `.txt` files. The value of each `cache_domain` member maps to one of the keys of the `ips` members, Thus mapping a cached domain to a list of IP addresses/LAN cache server. @@ -23,8 +23,8 @@ Configuration file follows the same [syntax as the upsteam file](https://github. } ``` -## Startup/Shutdown -On start the local DNS (dnsmasq) will be configured to redirect the configured cache domains and on stop the redirection will be removed. +## Configure/Cleanup +`/usr/bin/cache-domains configure` will configure the local DNS (dnsmasq) to redirect the configured cache domains. `/usr/bin/cache-domains cleanup` will cleanup redirection. The hotplug script calls `/usr/bin/cache-domains configure` when the WAN interface is brought up. ## Testing With the above configuration set and the service running `nslookup swcdn.apple.com` would return `10.10.3.12` diff --git a/utils/cache-domains/files/30-cache-domains b/utils/cache-domains/files/30-cache-domains new file mode 100644 index 0000000000..adc9cb0dff --- /dev/null +++ b/utils/cache-domains/files/30-cache-domains @@ -0,0 +1,8 @@ +#!/bin/sh + +source /lib/functions/network.sh +network_find_wan WAN_IFACE + +if [ "${ACTION}" == "ifup" ] && [ "${INTERFACE}" == "${WAN_IFACE}" ] && [ ! -d /var/cache-domains ]; then + /usr/bin/cache-domains configure +fi diff --git a/utils/cache-domains/files/cache-domains b/utils/cache-domains/files/cache-domains new file mode 100644 index 0000000000..cdf388a1ef --- /dev/null +++ b/utils/cache-domains/files/cache-domains @@ -0,0 +1,52 @@ +#!/bin/sh + +CACHE_DOMAINS_DIR="/var/cache-domains" +CACHE_DOMAINS_SRC="https://api.github.com/repos/uklans/cache-domains/tarball/master" +CONFIG_FILE="/etc/cache-domains.json" + +configure() { + mkdir -p ${CACHE_DOMAINS_DIR} + rm -fr ${CACHE_DOMAINS_DIR}/* + + if ! wget -qO - ${CACHE_DOMAINS_SRC} | tar -xzC ${CACHE_DOMAINS_DIR}; then + echo "ERROR: Could not retrieve ${CACHE_DOMAINS_SRC}" + return 1 + fi + + INITIAL_DIR="$(pwd)" + cd ${CACHE_DOMAINS_DIR}/*/scripts/ + + if [ ! -f ${CONFIG_FILE} ]; then + cp config.example.json ${CONFIG_FILE} + echo "Using example config file ${CONFIG_FILE}" + fi + + cp ${CONFIG_FILE} config.json + ./create-dnsmasq.sh + cp ./output/dnsmasq/* /var/dnsmasq.d/ + + cd ${INITIAL_DIR} + + /etc/init.d/dnsmasq restart +} + +cleanup() { + # leave dnsmasq in a clean state + for FILE in ${CACHE_DOMAINS_DIR}/*/scripts/output/dnsmasq/*; do + rm -f /tmp/dnsmasq.d/$(basename ${FILE}) + done + + /etc/init.d/dnsmasq restart +} + +case ${1} in + config*) + configure + ;; + clean*) + cleanup + ;; + *) + echo "${0} " + ;; +esac diff --git a/utils/cache-domains/files/cache-domains.init b/utils/cache-domains/files/cache-domains.init deleted file mode 100644 index ca7ac03be2..0000000000 --- a/utils/cache-domains/files/cache-domains.init +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/sh /etc/rc.common - -START=24 -SERVICE_NAME=cache-domains -CACHE_DOMAINS_DIR="/var/${SERVICE_NAME}" -CACHE_DOMAINS_SRC="https://api.github.com/repos/uklans/cache-domains/tarball/master" -CONFIG_FILE="/etc/${SERVICE_NAME}.json" - -start() { - mkdir -p ${CACHE_DOMAINS_DIR} - rm -fr ${CACHE_DOMAINS_DIR}/* - - if ! wget -qO - ${CACHE_DOMAINS_SRC} | tar -xzC ${CACHE_DOMAINS_DIR}; then - echo "ERROR: Could not retrieve ${CACHE_DOMAINS_SRC}" - return 1 - fi - - INITIAL_DIR="$(pwd)" - cd ${CACHE_DOMAINS_DIR}/*/scripts/ - - if [ ! -f ${CONFIG_FILE} ]; then - cp config.example.json ${CONFIG_FILE} - echo "Using example config file ${CONFIG_FILE}" - fi - - cp ${CONFIG_FILE} config.json - ./create-dnsmasq.sh - cp ./output/dnsmasq/* /tmp/dnsmasq.d/ - - cd ${INITIAL_DIR} - - /etc/init.d/dnsmasq restart -} - -stop() { - # leave dnsmasq in a clean state - for FILE in ${CACHE_DOMAINS_DIR}/*/scripts/output/dnsmasq/*; do - rm -f /tmp/dnsmasq.d/$(basename ${FILE}) - done - - /etc/init.d/dnsmasq restart -}