forked from BackSlasher/dns-override
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns-override.sh
executable file
·59 lines (48 loc) · 1.26 KB
/
dns-override.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
# Usage:
# ./dns-override.sh --servers 1.2.3.4,5.6.7.8
# ./dns-soverride.sh
set -euo pipefail # Strict bash
readonly SEPARATOR=','
SERVERS=''
show_help() {
cat >&2 <<EOF
$0: Run a file with modified DNS server list
Usage:
$0 -s SERVERLIST [-s SERVERLIST] BINARY
$0 -h (shows help)
SERVERLIST:
List of servers, separated by $SEPARATOR
Argument can be repeated
BINARY:
Executable to run
EOF
exit 1
}
# Parse args
while getopts 's:h' opt; do
case $opt in
s) SERVERS="${SERVERS}${SEPARATOR}${OPTARG}" ;;
h|?|:) show_help ;;
esac
done
shift $(( $OPTIND -1 ))
# Remove starting seperator
SERVERS="$(echo "$SERVERS" | sed -e "s/^$SEPARATOR//")"
if [[ ( ! "$SERVERS" ) || ( ! "$@" ) ]]; then
echo "Missing servers or binary" >&2
show_help
fi
# generate new resolv.conf
RESOLV_CONF="$(grep -Pv '^\s*nameserver ' /etc/resolv.conf)"
RESOLV_CONF="${RESOLV_CONF}$(echo ; echo "$SERVERS" | tr "$SEPARATOR" "\n" | sed 's/^/nameserver /')"
export RESOLV_CONF
# Collect dns-override.so
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PRELOAD_FILE="$DIR/dns-override.so"
if [ ! -r "$PRELOAD_FILE" ]; then
echo "Cannot read $PRELOAD_FILE: Did you \`make\` it?"
exit 2
fi
export LD_PRELOAD=${PRELOAD_FILE}:${LD_PRELOAD-}
exec "$@"