1#!/bin/bash
2
3nethandle=0
4
5readonly TEST_HOST="connectivitycheck.gstatic.com"
6readonly TEST_PATH="/generate_204"
7readonly PREFIX=">>>"
8
9function getUrls() {
10    if [ ! -z $(echo "$1" | sed -e 's/[^:]//g') ]; then
11        echo "http://[$1]$TEST_PATH"
12        echo "http://[$1]:80$TEST_PATH"
13    else
14        echo "http://$1$TEST_PATH"
15        echo "http://$1:80$TEST_PATH"
16    fi
17}
18
19function toHex() {
20    readonly local hexValue=$(bc -q 2>/dev/null << EOT
21obase=16
22$1
23EOT
24)
25    if [ ! -z "$hexValue" ]; then
26        echo "0x$hexValue"
27    fi
28}
29
30
31if [ ! -z "$1" ]; then
32    nethandle="$1"
33fi
34echo "$PREFIX Using nethandle $nethandle ($(toHex $nethandle))"
35echo ""
36
37readonly IPADDRESSES=$(
38    adb shell /system/bin/dnschk --nethandle $nethandle $TEST_HOST |
39    sed -e 's/#.*//' -e '/^$/d')
40
41
42for host in $TEST_HOST $IPADDRESSES; do
43    urls=$(getUrls $host)
44    for url in $urls; do
45        echo "$PREFIX Checking $url" >&2
46        adb shell /system/bin/httpurl --nethandle $nethandle "$url"
47    done
48done
49