110 lines
2.7 KiB
Bash
Executable File
110 lines
2.7 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
unset _NGX_RESOLVER_STACK _NGX_RESOLVER_TIMEOUT
|
|
## here should be SANE defaults (!)
|
|
_NGX_RESOLVER_STACK=ipv4
|
|
_NGX_RESOLVER_TIMEOUT=10s
|
|
|
|
if [ -z "${NGX_RESOLVER_STACK:-}" ] ; then
|
|
NGX_RESOLVER_STACK=${_NGX_RESOLVER_STACK}
|
|
else
|
|
NGX_RESOLVER_STACK=$(printf '%s' "${NGX_RESOLVER_STACK}" | tr '[:upper:]' '[:lower:]')
|
|
case "${NGX_RESOLVER_STACK}" in
|
|
none | ipv4 | ipv6 | any ) ;;
|
|
## adjust
|
|
0 | no )
|
|
NGX_RESOLVER_STACK=none
|
|
;;
|
|
4 | ip4 | v4 )
|
|
NGX_RESOLVER_STACK=ipv4
|
|
;;
|
|
6 | ip6 | v6 )
|
|
NGX_RESOLVER_STACK=ipv6
|
|
;;
|
|
all | dual )
|
|
NGX_RESOLVER_STACK=any
|
|
;;
|
|
* )
|
|
log_always "NGX_RESOLVER_STACK: unrecognized value: ${NGX_RESOLVER_STACK}"
|
|
log_always "setting NGX_RESOLVER_STACK=${_NGX_RESOLVER_STACK}"
|
|
NGX_RESOLVER_STACK=${_NGX_RESOLVER_STACK}
|
|
;;
|
|
esac
|
|
fi
|
|
export NGX_RESOLVER_STACK
|
|
|
|
if [ "${NGX_RESOLVER_STACK}" = 'none' ] ; then
|
|
unset NGX_RESOLV_CONF NGX_RESOLVER_TIMEOUT NGX_RESOLVERS
|
|
else
|
|
if [ -z "${NGX_RESOLVER_TIMEOUT:-}" ] ; then
|
|
NGX_RESOLVER_TIMEOUT=${_NGX_RESOLVER_TIMEOUT}
|
|
else
|
|
case "${NGX_RESOLVER_TIMEOUT}" in
|
|
[1-9] | [1-9][0-9] )
|
|
## convert implicit "seconds" to explicit
|
|
NGX_RESOLVER_TIMEOUT="${NGX_RESOLVER_TIMEOUT}s"
|
|
;;
|
|
## adjust
|
|
[1-9][Ss] | [1-9][0-9][Ss] )
|
|
NGX_RESOLVER_TIMEOUT="${NGX_RESOLVER_TIMEOUT%?}s"
|
|
;;
|
|
[1-9][Mm][Ss] | [1-9][0-9][Mm][Ss] | [1-9][0-9][0-9][Mm][Ss] | [1-9][0-9][0-9][0-9][Mm][Ss] | [1-9][0-9][0-9][0-9][0-9][Mm][Ss] )
|
|
NGX_RESOLVER_TIMEOUT="${NGX_RESOLVER_TIMEOUT%??}ms"
|
|
;;
|
|
* )
|
|
log_always "NGX_RESOLVER_TIMEOUT: unrecognized value: ${NGX_RESOLVER_TIMEOUT}"
|
|
log_always "setting NGX_RESOLVER_TIMEOUT=${_NGX_RESOLVER_TIMEOUT}"
|
|
NGX_RESOLVER_TIMEOUT=${_NGX_RESOLVER_TIMEOUT}
|
|
;;
|
|
esac
|
|
fi
|
|
export NGX_RESOLVER_TIMEOUT
|
|
|
|
unset _resolv_conf
|
|
while [ -z "${NGX_RESOLVERS+x}" ] ; do
|
|
_resolv_conf="${NGX_RESOLV_CONF-/etc/resolv.conf}"
|
|
[ -n "${_resolv_conf}" ] || break
|
|
[ -f "${_resolv_conf}" ] || break
|
|
[ -s "${_resolv_conf}" ] || break
|
|
|
|
unset i
|
|
while read -r i ; do
|
|
[ -n "$i" ] || continue
|
|
|
|
case "$i" in
|
|
## NB: /etc/resolv.conf allows (!) IPv6 addresses in dotted form (RFC 2373) but this is discouraged
|
|
*:* )
|
|
## TODO: IPv6 address validation
|
|
i="[$i]"
|
|
|
|
case "${NGX_RESOLVER_STACK}" in
|
|
any | ipv6 )
|
|
NGX_RESOLVERS=$(append_list "${NGX_RESOLVERS}" "$i")
|
|
;;
|
|
esac
|
|
;;
|
|
* )
|
|
if ! is_ipv4_address "$i" ; then
|
|
log_always "invalid IPv4 address: $i"
|
|
continue
|
|
fi
|
|
|
|
case "${NGX_RESOLVER_STACK}" in
|
|
any | ipv4 )
|
|
NGX_RESOLVERS=$(append_list "${NGX_RESOLVERS}" "$i")
|
|
;;
|
|
esac
|
|
;;
|
|
esac
|
|
done <<-EOF
|
|
$(mawk '$1 == "nameserver" {print $2}' < "${_resolv_conf}")
|
|
EOF
|
|
unset i
|
|
done
|
|
unset _resolv_conf
|
|
|
|
[ -z "${NGX_RESOLVERS}" ] || export NGX_RESOLVERS
|
|
fi
|
|
|
|
unset _NGX_RESOLVER_STACK _NGX_RESOLVER_TIMEOUT
|