Skip to content

Commit f77e460

Browse files
committed
Introduce new env VAR for setting PIHOLE_DNS_x values.
If not set, keep existing values in setupVars if no existing values in setupVars - fall back to defaults Signed-off-by: Adam Warner <[email protected]>
1 parent b0a14b7 commit f77e460

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ There are other environment variables if you want to customize various things in
102102
| `ADMIN_EMAIL: <email address>`<br/> *Optional Default: ''* | Set an administrative contact address for the Block Page
103103
| `TZ: <Timezone>`<br/> **Recommended** *Default: UTC* | Set your [timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) to make sure logs rotate at local midnight instead of at UTC midnight.
104104
| `WEBPASSWORD: <Admin password>`<br/> **Recommended** *Default: random* | http://pi.hole/admin password. Run `docker logs pihole \| grep random` to find your random pass.
105-
| `DNS1: <IP>`<br/> *Optional* *Default: 8.8.8.8* | Primary upstream DNS provider, default is google DNS
106-
| `DNS2: <IP>`<br/> *Optional* *Default: 8.8.4.4* | Secondary upstream DNS provider, default is google DNS, `no` if only one DNS should used
105+
| `PIHOLE_DNS_: <IPs delimited by ;>`<br/> *Optional* *Default: 8.8.8.8;8.8.4.4* | Upstream DNS server(s) for Pi-hole to forward queries to, seperated by a semicolon <br/> (supports non-standard ports with `#[port number]`) e.g `127.0.0.1#5053;8.8.8.8;8.8.4.4`
107106
| `DNSSEC: <"true"\|"false">`<br/> *Optional* *Default: "false"* | Enable DNSSEC support
108107
| `DNS_BOGUS_PRIV: <"true"\|"false">`<br/> *Optional* *Default: "true"* | Enable forwarding of reverse lookups for private ranges
109108
| `DNS_FQDN_REQUIRED: <"true"\|"false">`<br/> *Optional* *Default: true* | Never forward non-FQDNs
@@ -131,8 +130,10 @@ While these may still work, they are likely to be removed in a future version. W
131130
| ----------------------- | ----------- | ----------- |
132131
| `CONDITIONAL_FORWARDING: <"true"\|"false">`<br/> *Optional* *Default: "false"* | Enable DNS conditional forwarding for device name resolution | `REV_SERVER`|
133132
| `CONDITIONAL_FORWARDING_IP: <Router's IP>`<br/> *Optional* | If conditional forwarding is enabled, set the IP of the local network router | `REV_SERVER_TARGET` |
134-
| `CONDITIONAL_FORWARDING_DOMAIN: <Network Domain>`<br/> *Optional* | If conditional forwarding is enabled, set the domain of the local network router | `REV_SERVER_DOMAIN` |
133+
| `CONDITIONAL_FORWARDING_DOMAIN: <Network Domain>`<br/> *Optional* | If conditional forwarding is enabled, set the domain of the local network router | `REV_SERVER_DOMAIN` |
135134
| `CONDITIONAL_FORWARDING_REVERSE: <Reverse DNS>`<br/> *Optional* | If conditional forwarding is enabled, set the reverse DNS of the local network router (e.g. `0.168.192.in-addr.arpa`) | `REV_SERVER_CIDR` |
135+
| `DNS1: <IP>`<br/> *Optional* *Default: 8.8.8.8* | Primary upstream DNS provider, default is google DNS | `PIHOLE_DNS_` |
136+
| `DNS2: <IP>`<br/> *Optional* *Default: 8.8.4.4* | Secondary upstream DNS provider, default is google DNS, `no` if only one DNS should used | `PIHOLE_DNS_` |
136137

137138
To use these env vars in docker run format style them like: `-e DNS1=1.1.1.1`
138139

bash_functions.sh

+9-37
Original file line numberDiff line numberDiff line change
@@ -68,39 +68,12 @@ validate_env() {
6868

6969
setup_dnsmasq_dns() {
7070
. /opt/pihole/webpage.sh
71-
local DNS1="${1:-8.8.8.8}"
72-
local DNS2="${2:-8.8.4.4}"
73-
local dnsType='default'
74-
if [ "$DNS1" != '8.8.8.8' ] || [ "$DNS2" != '8.8.4.4' ] ; then
75-
dnsType='custom'
76-
fi;
77-
78-
# TODO With the addition of this to /start.sh this needs a refactor
79-
if [ ! -f /.piholeFirstBoot ] ; then
80-
local setupDNS1="$(grep 'PIHOLE_DNS_1' ${setupVars})"
81-
local setupDNS2="$(grep 'PIHOLE_DNS_2' ${setupVars})"
82-
setupDNS1="${setupDNS1/PIHOLE_DNS_1=/}"
83-
setupDNS2="${setupDNS2/PIHOLE_DNS_2=/}"
84-
if [[ -n "$DNS1" && -n "$setupDNS1" ]] || \
85-
[[ -n "$DNS2" && -n "$setupDNS2" ]] ; then
86-
echo "Docker DNS variables not used"
87-
fi
88-
echo "Existing DNS servers used (${setupDNS1:-unset} & ${setupDNS2:-unset})"
89-
return
90-
fi
71+
local DNS1="8.8.8.8"
72+
local DNS2="8.8.4.4"
9173

92-
echo "Using $dnsType DNS servers: $DNS1 & $DNS2"
93-
if [[ -n "$DNS1" && -z "$setupDNS1" ]] ; then
94-
change_setting "PIHOLE_DNS_1" "${DNS1}"
95-
fi
96-
if [[ -n "$DNS2" && -z "$setupDNS2" ]] ; then
97-
if [[ "$DNS2" == "no" ]] ; then
98-
delete_setting "PIHOLE_DNS_2"
99-
unset PIHOLE_DNS_2
100-
else
101-
change_setting "PIHOLE_DNS_2" "${DNS2}"
102-
fi
103-
fi
74+
echo "Configuring default DNS servers: $DNS1 & $DNS2"
75+
change_setting "PIHOLE_DNS_1" "${DNS1}"
76+
change_setting "PIHOLE_DNS_2" "${DNS2}"
10477
}
10578

10679
setup_dnsmasq_interface() {
@@ -129,13 +102,12 @@ setup_dnsmasq_config_if_missing() {
129102
}
130103

131104
setup_dnsmasq() {
132-
local dns1="$1"
133-
local dns2="$2"
134-
local interface="$3"
135-
local dnsmasq_listening_behaviour="$4"
105+
local USE_DEFAULT_DNS_SERVERS="$1"
106+
local interface="$2"
107+
local dnsmasq_listening_behaviour="$3"
136108
# Coordinates
137109
setup_dnsmasq_config_if_missing
138-
setup_dnsmasq_dns "$dns1" "$dns2"
110+
[ $USE_DEFAULT_DNS_SERVERS -eq 1 ] && setup_dnsmasq_dns
139111
setup_dnsmasq_interface "$interface"
140112
setup_dnsmasq_listening_behaviour "$dnsmasq_listening_behaviour"
141113
setup_dnsmasq_user "${DNSMASQ_USER}"

start.sh

+30-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export TEMPERATUREUNIT
2929
export ADMIN_EMAIL
3030
export WEBUIBOXEDLAYOUT
3131
export QUERY_LOGGING
32+
export PIHOLE_DNS_
3233

3334
export adlistFile='/etc/pihole/adlists.list'
3435

@@ -81,12 +82,40 @@ if [ -z "$REV_SERVER" ];then
8182
[ -n "${CONDITIONAL_FORWARDING_REVERSE}" ] && change_setting "CONDITIONAL_FORWARDING_REVERSE" "$CONDITIONAL_FORWARDING_REVERSE"
8283
fi
8384

85+
if [ -z "${PIHOLE_DNS_}"]; then
86+
# For backward compatibility, if DNS1 and/or DNS2 are set, but PIHOLE_DNS_ is not, convert them to
87+
# a semi-colon delimited string and store in PIHOLE_DNS_
88+
# They are not used anywhere if PIHOLE_DNS_ is set already
89+
[ -n "${DNS1}" ] && PIHOLE_DNS_="$DNS1"
90+
[[ -n "${DNS2}" && "${DNS2}" != "no" ]] && PIHOLE_DNS_="$PIHOLE_DNS_;$DNS2"
91+
fi
92+
93+
# Parse the PIHOLE_DNS variable, if it exists, and apply upstream servers to Pi-hole config
94+
USE_DEFAULT_DNS_SERVERS=1
95+
if [ -n "${PIHOLE_DNS_}" ]; then
96+
# Split into an array (delimited by ;)
97+
PIHOLE_DNS_ARR=(${PIHOLE_DNS_//;/ })
98+
count=1
99+
for i in "${PIHOLE_DNS_ARR[@]}"; do
100+
change_setting "PIHOLE_DNS_$count" "$i"
101+
((count=count+1))
102+
done
103+
# DNS Servers have been set up in this side of the script, so don't set the defaults of 8.8.8.8 and 8.8.4.4
104+
USE_DEFAULT_DNS_SERVERS=0
105+
else
106+
# Environment variable has not been set, but there may be existing values in an existing setupVars.conf
107+
# if this is the case, we do not want to overwrite these with the defaults of 8.8.8.8 and 8.8.4.4
108+
# Pi-hole can run with only one upstream configured, so we will just check for one.
109+
setupVarsDNS="$(grep 'PIHOLE_DNS_1' /etc/pihole/setupVars.conf || true)"
110+
[ -n "${setupVarsDNS}" ] && USE_DEFAULT_DNS_SERVERS=0 # A configured upstream DNS has been detected in an existing setupvars.conf.
111+
fi
112+
84113
setup_web_port "$WEB_PORT"
85114
setup_web_password "$WEBPASSWORD"
86115
setup_temp_unit "$TEMPERATUREUNIT"
87116
setup_ui_layout "$WEBUIBOXEDLAYOUT"
88117
setup_admin_email "$ADMIN_EMAIL"
89-
setup_dnsmasq "$DNS1" "$DNS2" "$INTERFACE" "$DNSMASQ_LISTENING_BEHAVIOUR"
118+
setup_dnsmasq "$USE_DEFAULT_DNS_SERVERS" "$INTERFACE" "$DNSMASQ_LISTENING_BEHAVIOUR"
90119
setup_php_env
91120
setup_dnsmasq_hostnames "$ServerIP" "$ServerIPv6" "$HOSTNAME"
92121
setup_ipv4_ipv6

0 commit comments

Comments
 (0)