Introduce a bunch of scripts to start the proxy, cope with captive portals,

and replace this infamous and totally broken dns-updater horror.
This commit is contained in:
Frank Denis
2012-07-29 22:12:33 -07:00
parent d126bd5784
commit f9a7da871a
8 changed files with 237 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
#! /bin/sh
try_resolution() {
exec alarmer 5 dig +tries=2 +time=3 +short resolver1.opendns.com \
| egrep '^208[.]67[.]' > /dev/null 2>&1
}
try_http_query() {
exec alarmer 5 curl -m 5 http://www.opendns.com 2>/dev/null | \
fgrep -ic OpenDNS > /dev/null 2>&1
}
try_everything() {
try_resolution &
resolution_pid=$!
try_http_query &
http_query_pid=$!
wait $resolution_pid
resolution_ret=$?
if [ $resolution_ret != 0 ]; then
return 1
fi
wait $http_query_pid
http_query_ret=$?
[ $resolution_ret = 0 -a $http_query_ret = 0 ]
}
try_everything_with_retries() {
try_everything || try_everything
}
try_everything_with_retries
+10
View File
@@ -0,0 +1,10 @@
#! /bin/sh
. common.inc
try_local_resolution() {
exec dig +tries=2 +time=3 +short resolver1.opendns.com @$INTERFACE_PROXY \
| egrep '^208[.]67[.]' > /dev/null 2>&1
}
try_local_resolution
+42
View File
@@ -0,0 +1,42 @@
#! /bin/ksh
# /Library/Preferences/SystemConfiguration
NETWORK_STATE_FILE="${STATES_DIR}/network-state"
DHCP_DNS_FILE="${STATES_DIR}/dhcp-dns"
AIRPORT_TOOL="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport"
. common.inc
mkdir -p "$STATES_DIR" || exit 1
ifs=$(ifconfig -a | \
awk '/^[^ :]*:/ { sub(/:.*$/,empty); iface=$0 } /status: active/ { print iface }')
ifs=$(echo $ifs)
typeset -A found
ips=""
for i in $ifs; do
ips_i=$(ipconfig getpacket $i | fgrep 'domain_name_server' | \
sed -e 's/^.*{//' -e 's/,/ /g' -e 's/}//' )
for ip_i in $ips_i; do
if [ ! ${found["$ip_i"]} ]; then
if [ "$ips" ]; then
ips="$ips "
fi
ips="$ips$ip_i"
found["$ip_i"]=1
fi
done
done
echo "$ips" > "$DHCP_DNS_FILE"
ssid=$("$AIRPORT_TOOL" -I 2>&1 | fgrep '[^B]SSID: ')
bssid=$("$AIRPORT_TOOL" -I 2>&1 | fgrep 'BSSID: ')
if [ -f "$NETWORK_STATE_FILE" ]; then
if echo "$ifs $ips $ssid $bssid" | \
cmp -- "$NETWORK_STATE_FILE" - >/dev/null; then
exit 1
fi
fi
echo "$ifs $ips $ssid $bssid" > "$NETWORK_STATE_FILE"
+17
View File
@@ -0,0 +1,17 @@
DNSCRYPT_BASE_DIR='/var/run/dnscrypt'
STATES_DIR="${DNSCRYPT_BASE_DIR}/states"
INTERFACE_UNBOUND="127.0.0.53"
INTERFACE_UNBOUND_MASK="24"
INTERFACE_PROXY="127.0.0.54"
INTERFACE_PROXY_MASK="24"
INTERFACE_PROBES="127.0.0.55"
INTERFACE_PROBES_MASK="24"
init_interfaces() {
sudo ifconfig lo0 alias ${INTERFACE_UNBOUND}/${INTERFACE_UNBOUND_MASK}
sudo ifconfig lo0 alias ${INTERFACE_PROXY}/${INTERFACE_PROXY_MASK}
sudo ifconfig lo0 alias ${INTERFACE_PROBES}/${INTERFACE_PROBES_MASK}
}
+33
View File
@@ -0,0 +1,33 @@
#! /bin/ksh
PAUSE_MAX=10
PAUSE_INCREMENT=0.1
. common.inc
./check-network-change.sh || exit 0
./check-hijacking.sh && exit 0
./set-dns-to-dhcp.sh
pause=0
while :; do
if [ $pause -lt $PAUSE_MAX ]; then
pause=$((pause + $PAUSE_INCREMENT))
fi
sleep $pause
./check-hijacking.sh || continue
./start-dnscrypt-proxy.sh || continue
./check-local-dns.sh || continue
./set-dns.sh "$INTERFACE_PROXY"
if [ $? != 0 ]; then
./set-dns-to-dhcp.sh
continue
fi
./check-hijacking.sh
if [ $? != 0 ]; then
./set-dns-to-dhcp.sh
continue
fi
break
done
echo done
+6
View File
@@ -0,0 +1,6 @@
#! /bin/sh
exec networksetup -listallnetworkservices 2>/dev/null | \
fgrep -v '*' | while read x ; do
networksetup -setdnsservers "$x" empty
done
Executable
+8
View File
@@ -0,0 +1,8 @@
#! /bin/sh
servers="$*"
exec networksetup -listallnetworkservices 2>/dev/null | \
fgrep -v '*' | while read x ; do
networksetup -setdnsservers "$x" $servers
done
+89
View File
@@ -0,0 +1,89 @@
#! /bin/sh
. common.inc
init_interfaces
mkdir -p -- "$DNSCRYPT_BASE_DIR" || exit 1
PROBES_BASE_DIR='/var/run/dnscrypt/probes'
rm -fr $PROBES_BASE_DIR || exit 1
mkdir -p -- "$PROBES_BASE_DIR" || exit 1
RES_DIR="${PROBES_BASE_DIR}/results" || exit 1
mkdir -p -- "$RES_DIR" || exit 1
PID_DIR="${PROBES_BASE_DIR}/pids" || exit 1
mkdir -p -- "$PID_DIR" || exit 1
DNSCRYPT_PROXY_BASE_DIR="${DNSCRYPT_BASE_DIR}/dnscrypt-proxy"
mkdir -p -- "$DNSCRYPT_PROXY_BASE_DIR" || exit 1
PROXY_PID_FILE="${DNSCRYPT_PROXY_BASE_DIR}/dnscrypt-proxy.pid"
try_resolver() {
local priority="$1"
local pid_file="${PID_DIR}/${priority}.pid"
shift
local args="$*"
exec alarmer 3 dnscrypt-proxy --pid="$pid_file" 2>&1 \
--local-address="${INTERFACE_PROBES}:${priority}" $args | \
while read line; do
case "$line" in
*Proxying\ from\ *)
answers=$(exec dig +time=1 +short +tries=2 -p $priority \
@"$INTERFACE_PROBES" dailymotion.com 2> /dev/null | \
fgrep -v 67.215.65. | wc -l)
[ -r "$pid_file" ] && kill $(cat -- "$pid_file")
if [ $answers -gt 0 ]; then
echo "$args" > "${RES_DIR}/${priority}"
fi
;;
*) ;;
esac
done
}
[ -r "$PROXY_PID_FILE" ] && kill $(cat "$PROXY_PID_FILE") && sleep 1
ping6 -c 1 2620:0:ccc::2 > /dev/null 2>&1
ipv6_supported="no"
[ $? = 0 ] && ipv6_supported="yes"
wait_pids=""
if [ ipv6_supported = "yes" ]; then
try_resolver 2000 "--resolver-address=[2620:0:ccc::2]:443" &
wait_pids="$wait_pids $!"
try_resolver 2001 "--resolver-address=[2620:0:ccc::2]:53" &
wait_pids="$wait_pids $!"
try_resolver 2002 "--resolver-address=[2620:0:ccc::2]:443 --tcp-only" &
wait_pids="$wait_pids $!"
try_resolver 2003 "--resolver-address=[2620:0:ccc::2]:53 --tcp-only" &
wait_pids="$wait_pids $!"
fi
try_resolver 2004 "--resolver-address=208.67.220.220:443" &
wait_pids="$wait_pids $!"
try_resolver 2005 "--resolver-address=208.67.220.220:53" &
wait_pids="$wait_pids $!"
try_resolver 2006 "--resolver-address=208.67.220.220:443 --tcp-only" &
wait_pids="$wait_pids $!"
try_resolver 2007 "--resolver-address=208.67.220.220:53 --tcp-only" &
wait_pids="$wait_pids $!"
for pid in $wait_pids; do
wait $pid
best_file=$(ls "$RES_DIR" | head -n 1)
[ x"$best_file" != "x" ] && break
done
[ x"$best_file" = "x" ] && exit 1
best_args=$(cat "${RES_DIR}/${best_file}")
dnscrypt-proxy $best_args --local-address="${INTERFACE_PROXY}" \
--pidfile="$PROXY_PID_FILE" --daemonize || exit 1
i=0
while [ $i -lt 30 ]; do
./check-local-dns.sh && break
sleep 0.1
i=$((i + 1))
done