|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +sleep 5 |
| 4 | + |
| 5 | +## backup original resolv.conf |
| 6 | +if [ ! -f "/opt/docker/.resolv.conf" ]; then |
| 7 | + ## backup original file |
| 8 | + cp /etc/resolv.conf /opt/docker/.resolv.conf |
| 9 | + |
| 10 | + ## Copy resolv.conf for dnsmasq (default resolver) |
| 11 | + cp /etc/resolv.conf /var/run/dnsmasq/resolv.conf |
| 12 | +fi |
| 13 | + |
| 14 | +## Restore original resolvconf |
| 15 | +function restore_resolvconf() { |
| 16 | + ## restore original resolv.conf |
| 17 | + cp /opt/docker/.resolv.conf /etc/resolv.conf |
| 18 | +} |
| 19 | + |
| 20 | +## Start and configure dnsmasq |
| 21 | +function dnsmasq_start() { |
| 22 | + echo "[dnsmasq] Found Webserver IP: $1" |
| 23 | + |
| 24 | + restore_resolvconf |
| 25 | + |
| 26 | + ## clear dns file |
| 27 | + echo > /etc/dnsmasq.d/development |
| 28 | + |
| 29 | + ## add IP for each domain (wildcard!) |
| 30 | + for DOMAIN in $DNS_DOMAIN; do |
| 31 | + echo "address=/${DOMAIN}/${1}" >> /etc/dnsmasq.d/development |
| 32 | + done |
| 33 | + |
| 34 | + ## set forward servers |
| 35 | + cat /opt/docker/.resolv.conf | grep nameserver | sed 's/nameserver /server=/' > /etc/dnsmasq.d/forward |
| 36 | + |
| 37 | + ## (re)start dnsmasq as DNS server |
| 38 | + service dnsmasq restart |
| 39 | + |
| 40 | + ## set dnsmasq to main nameserver |
| 41 | + echo "nameserver 127.0.0.1" > /etc/resolv.conf |
| 42 | + |
| 43 | + ## wait for 10 hours |
| 44 | + sleep 10h |
| 45 | +} |
| 46 | + |
| 47 | +## Fetch IP from services |
| 48 | +if [ -f "/data/dns/web.ip" ]; then |
| 49 | + ## Found WEB |
| 50 | + dnsmasq_start "$(cat /data/dns/web.ip)" |
| 51 | +elif [ -f "/data/dns/httpd.ip" ]; then |
| 52 | + ## Found HTTPD (fallback) |
| 53 | + dnsmasq_start "$(cat /data/dns/httpd.ip)" |
| 54 | +elif [ -f "/data/dns/nginx.ip" ]; then |
| 55 | + ## Found NGINX (fallback) |
| 56 | + dnsmasq_start "$(cat /data/dns/nginx.ip)" |
| 57 | +else |
| 58 | + ## Found nothing, restore original resolvconf |
| 59 | + restore_resolvconf |
| 60 | + sleep 15 |
| 61 | +fi |
| 62 | + |
| 63 | +exit 0 |
0 commit comments