diff --git a/dnsmasq/Dockerfile b/dnsmasq/Dockerfile new file mode 100644 index 0000000..35393e0 --- /dev/null +++ b/dnsmasq/Dockerfile @@ -0,0 +1,15 @@ +FROM %%BASE_IMAGE%% + +# Add env +ENV LANG C.UTF-8 + +# Setup base +RUN apk add --no-cache tzdata jq dnsmasq + +# Copy data +COPY run.sh / +COPY dnsmasq.conf /etc/ + +RUN chmod a+x /run.sh + +CMD [ "/run.sh" ] diff --git a/dnsmasq/config.json b/dnsmasq/config.json new file mode 100644 index 0000000..e3583b4 --- /dev/null +++ b/dnsmasq/config.json @@ -0,0 +1,33 @@ +{ + "name": "Dnsmasq server", + "version": "0.1", + "slug": "dnsmasq", + "description": "A simple dns server with benefits", + "url": "https://home-assistant.io/addons/dnsmasq/", + "startup": "before", + "boot": "auto", + "ports": { + "53/udp": 53 + }, + "options": { + "defaults": ["8.8.8.8", "8.8.4.4"], + "forwards": [], + "hosts": [], + }, + "schema": { + "defaults": ["str"], + "forwards": [ + { + "domain": "str", + "server": "str" + } + ], + "hosts": [ + { + "host": "str", + "ip": "str" + } + ] + }, + "image": "homeassistant/{arch}-addon-dnsmasq" +} diff --git a/dnsmasq/dnsmasq.conf b/dnsmasq/dnsmasq.conf new file mode 100644 index 0000000..60d06c7 --- /dev/null +++ b/dnsmasq/dnsmasq.conf @@ -0,0 +1,6 @@ +no-resolv +no-hosts +keep-in-foreground +log-queries +log-facility=- +no-poll diff --git a/dnsmasq/run.sh b/dnsmasq/run.sh new file mode 100644 index 0000000..3690c94 --- /dev/null +++ b/dnsmasq/run.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e + +CONFIG_PATH=/data/options.json + +DEFAULTS=$(jq --raw-output '.domain' $CONFIG_PATH) +FORWARDS=$(jq --raw-output '.forwards | length' $CONFIG_PATH) +HOSTS=$(jq --raw-output '.hosts | length' $CONFIG_PATH) + +# Add default forward servers +for line in $DEFAULTS; do + echo "server=$line" >> /etc/dnsmasq.conf +done + +# Create domain forwards +for (( i=0; i < "$FORWARDS"; i++ )); do + DOMAIN=$(jq --raw-output ".forwards[$i].domain" $CONFIG_PATH) + SERVER=$(jq --raw-output ".forwards[$i].server" $CONFIG_PATH) + + echo "server=/$DOMAIN/$SERVER" >> /etc/dnsmasq.conf +done + +# Create static hosts +for (( i=0; i < "$HOSTS"; i++ )); do + HOST=$(jq --raw-output ".forwards[$i].host" $CONFIG_PATH) + IP=$(jq --raw-output ".forwards[$i].ip" $CONFIG_PATH) + + echo "address=/$HOST/$IP" >> /etc/dnsmasq.conf +done + +# run dnsmasq +exec dnsmasq -C /etc/dnsmasq.conf < /dev/null