#!/usr/bin/env sh # usage: adb-ssh-socks5 [-d] # stupid mobile data tethering over USB # uses adb to forward ssh ports from USB-connected android smartphone running # sshd so as to share it's mobile data connection via socks5 proxy without # being throttled as hotspot/tethering usage by mobile carriers # typical setup (very fiddly and not very deterministic!): # 1. ensure sshd is already running on port 8022 on Termux # 2. ensure you have enabled pubkey authentication or # at least a plaintext password (via passwd) on Termux before starting # 3. enable 'Developer Options > USB debugging' on android device # 4. accept adb server's pubkey or re-accept it after 7+ days of inactivity # 5. ensure File Transfer/MTP mode is enabled when connected over USB # 6. just keep re-running this script if any step happens in the wrong order 🤪 # expected ports and addresses # force IPv4 SSHD_PORT='8022' PROXY_ADDR='127.0.0.1' PROXY_PORT='1080' cd $HOME # undo changes made by by this stupid hack if [ "$1" = '-d' ]; then patch -p1 -R -r - < "$0.patch" adb forward --remove-all adb kill-server pkill autossh printf '%s\n' 'Changes undone.' exit 0 fi # makes this script somewhat re-entrant patch -p1 --dry-run --forward < "$0.patch" > /dev/null 2>&1 || { printf '%s\n' 'Already in use, resetting environment...' "$0" -d } # forward ports from phone and open SSH session adb start-server printf '%s\r' 'Waiting for ADB connection over USB...' while ! [ $(adb devices -l | wc -l) -gt 2 ]; do sleep 1; done { printf '%s %s\r' 'Forwarding port' "$(adb forward tcp:$SSHD_PORT tcp:$SSHD_PORT)" } printf '%s\r' "Ensure sshd is running on port $SSHD_PORT" autossh -D $PROXY_PORT -p $SSHD_PORT -fnN $PROXY_ADDR # TODO: make this more re-entrant without using flags # don't feel like understanding iptables and tunnel devices # just patch every config file to use socks5 proxy sleep 1 && patch -p1 -r - < "$0.patch"