126 lines
3.3 KiB
Bash
Executable file
126 lines
3.3 KiB
Bash
Executable file
#!/bin/bash
|
|
# shellcheck disable=SC2016,SC2329
|
|
|
|
COMMAND='
|
|
[ "$(su -c settings get global adb_wifi_enabled)" != 1 ] && su -c settings put --user current global adb_wifi_enabled 1
|
|
su -c getprop service.adb.tls.port
|
|
'
|
|
PHONE_SERIAL=4B141JEBF23561
|
|
WIFI_NAME='🙂'
|
|
PHONE_DNS_PREFIX=phone
|
|
declare -a PHONE_VPN_HOSTS=(
|
|
'10.160.1.3'
|
|
'10.160.3.3'
|
|
'phone.spb.wzray.com'
|
|
'phone.msk.wzray.com'
|
|
)
|
|
|
|
find_host_wifi() {
|
|
local default_link domain
|
|
|
|
default_link="$(
|
|
resolvectl status |
|
|
grep 'Default Route: yes' -B10 |
|
|
grep 'Link' |
|
|
tail -1 |
|
|
grep -Eo '\(.*\)' |
|
|
tr -d '()'
|
|
)"
|
|
|
|
domain="$(resolvectl domain "$default_link" | cut -d: -f2 | awk '{ print $1 }')"
|
|
grep -q 'wzray.com' <<<"$domain" && printf "%s.%s" "$PHONE_DNS_PREFIX" "$domain"
|
|
}
|
|
|
|
find_hosts_vpn() {
|
|
[ "$( ip link | grep -o -e 'tun[[:digit:]]' -e 'awg_[a-z]\+' | wc -l)" != 0 ] &&
|
|
echo "${PHONE_VPN_HOSTS[@]}"
|
|
}
|
|
|
|
find_host_tethering() {
|
|
! find_host_wifi >/dev/null 2>&1 &&
|
|
[ "$(iwctl station wlan0 show | grep 'Connected network' | awk '{ print $3 }')" = "$WIFI_NAME" ] &&
|
|
ip route show dev wlan0 | grep default | grep -Eo 'via [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | cut -d ' ' -f2
|
|
}
|
|
|
|
can_use_adb() {
|
|
adb devices | grep -q "$PHONE_SERIAL\s\+device"
|
|
}
|
|
|
|
host_port_adb() {
|
|
can_use_adb && adb -d shell '
|
|
host="$(for ifname in tun0 wlan1 wlan0 lo; do
|
|
ip a show dev $ifname 2>&1 | grep "inet " | awk '\''{ print $2 }'\'' | cut -d/ -f1
|
|
done | head -1)"
|
|
port="$('"$COMMAND"')"
|
|
printf "%s:%s" "$host" "$port"
|
|
'
|
|
}
|
|
|
|
can_use_ssh() {
|
|
nmap --system-dns -sT -p8022 "$1" | grep -q '8022/tcp\s\+open'
|
|
}
|
|
|
|
port_ssh() {
|
|
ssh -o HostName="$1" \
|
|
-o UserKnownHostsFile=/dev/null \
|
|
-o IdentityFile=/dev/null \
|
|
-o StrictHostKeyChecking=no \
|
|
-o ConnectTimeout=1 \
|
|
-o LogLevel=ERROR \
|
|
phone "$COMMAND" 2>/dev/null
|
|
}
|
|
|
|
host_port_ssh() {
|
|
for host in $1; do
|
|
can_use_ssh "$host" &&
|
|
port="$(port_ssh "$host")" &&
|
|
printf "%s:%s" "$host" "$port" &&
|
|
return
|
|
done
|
|
}
|
|
|
|
host_port_nmap() {
|
|
for host in $1; do
|
|
ping -c1 -W 0.35 "$host" >/dev/null 2>&1 &&
|
|
port="$(
|
|
nmap -Pn -n -oG - --system-dns -p10000-65535 -sT "$host" |
|
|
grep -o 'Ports: .*/' |
|
|
sed 's,^Ports: ,,' |
|
|
sed -s 's/, /\n/g' |
|
|
grep -o '[0-9]\+/open/tcp' |
|
|
cut -d/ -f1 |
|
|
head -1
|
|
)" &&
|
|
printf "%s:%s" "$host" "$port"
|
|
done
|
|
}
|
|
|
|
adb devices | grep -q ':' && {
|
|
adb shell settings put --user current global adb_wifi_enabled 0 >/dev/null 2>&1 &
|
|
disown %1
|
|
sleep 0.1
|
|
adb disconnect >/dev/null 2>&1
|
|
echo 'disconnected'
|
|
exit
|
|
}
|
|
|
|
if can_use_adb; then
|
|
host_port="$(host_port_adb)"
|
|
else
|
|
if [ -n "$1" ]; then
|
|
hosts="$1"
|
|
else
|
|
hosts="$(find_host_wifi) $(find_host_tethering) $(find_hosts_vpn)"
|
|
fi
|
|
host_port="$(host_port_ssh "$hosts" || host_port_nmap "$hosts")"
|
|
fi
|
|
|
|
if [ -n "$host_port" ]; then
|
|
if [ -z "$(cut -d: -f2 <<<"$host_port")" ]; then
|
|
echo "Unable to start adb server! Start it manually or start sshd!"
|
|
else
|
|
adb connect "$host_port"
|
|
fi
|
|
else
|
|
echo "Unable to find phone address. You need to specify it manually."
|
|
fi
|