Skip to content

Instantly share code, notes, and snippets.

@pgaskin
Last active August 11, 2024 13:27
Show Gist options
  • Save pgaskin/b51ba3b0cfe79b5aafbfce0fe9186e10 to your computer and use it in GitHub Desktop.
Save pgaskin/b51ba3b0cfe79b5aafbfce0fe9186e10 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
process() {
local adb_addr pair_code pair_addr vars="$(
set -euo pipefail
magick "$1" -white-threshold 30% -black-threshold 20% PNG:- |
tesseract --psm 3 stdin stdout |
sed -E 's/^\w\s//' |
tr '\n' '#' |
grep -oEe '#(IP address & Port#[0-9.]+:[0-9]+|Wi-Fi pairing code#[0-9]+)#' |
tee /dev/stderr |
cut -d# -f3 |
xargs printf 'adb_addr=%q\npair_code=%q\npair_addr=%q\n'
)" || {
echo failed to process screenshot >&2
return 1
}
eval "$vars"
if [[ -n "$adb_addr" ]] && [[ -n "$pair_code" ]] && [[ -z "$pair_addr" ]]
then
pair_addr="$pair_code"
pair_code="$adb_addr"
adb_addr=
fi
printf "adb_addr=%q\npair_code=%q\npair_addr=%q\n" "$adb_addr" "$pair_code" "$pair_addr" >&2
if [[ -n "$adb_addr" ]]
then
if [[ "$adb_addr" != *":"* ]]
then
echo invalid adb addr >&2
return 1
fi
fi
if [[ -n "$pair_code" ]] || [[ -n "$pair_addr" ]]
then
if [[ "$pair_addr" != *":"* ]]
then
echo invalid pair addr >&2
return 1
fi
if [[ -z "$pair_code" ]]
then
echo invalid pair code >&2
return 1
fi
fi
printf "adb_addr=%q\npair_code=%q\npair_addr=%q\n" "$adb_addr" "$pair_code" "$pair_addr"
}
screenshot() {
local scr
while true
do
sleep .1
for spec in "2 seconds ago" "1 second ago" "now" "1 second" "2 seconds"
do
for scr in "/sdcard/Pictures/Screenshots/Screenshot_$(date --date="$spec" +"%Y%m%d-%I%M%S")_Settings.png"
do
if [[ -f "$scr" ]]
then
echo "$scr"
return 0
fi
done
done
done
}
while true
do
echo waiting for screenshot
scr="$(set -e; screenshot)"
echo "processing $scr"
eval "$(set -e; process "$scr")"
rm -v "$scr"
if [[ -n "$pair_code" ]]
then
echo pairing
adb disconnect
adb pair "$pair_addr" "$pair_code"
continue
fi
echo connecting
adb disconnect
adb connect "$adb_addr"
echo checking if rooted
whoami="$(set -e; adb shell whoami)"
if [[ -z "$whoami" ]]
then
echo failed to get user >&2
exit 1
fi
if [[ "$whoami" != "root" ]]
then
echo rooting
timeout 1 adb root || true
adb disconnect
continue
fi
break
done
echo starting shizuku
adb shell sh /storage/emulated/0/Android/data/moe.shizuku.privileged.api/start.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment