#!/bin/bash # # Copyright (C) 2017 Genymobile # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -e # fail on error ADB=adb JAVA=java syntax() { echo "Syntax: $0 (install|uninstall|rt|start|stop|relay) ..." >&2 echo echo " $0 install [serial]" echo " Install the client on the Android device and exit." echo " If several devices are connected via adb, then serial must be" echo " specified." echo echo " $0 uninstall [serial]" echo " Uninstall the client from the Android device and exit." echo " If several devices are connected via adb, then serial must be" echo " specified." echo echo " $0 rt [serial] [-d DNS[,DNS2,...]]" echo " Enable reverse tethering for exactly one device:" echo " - install the client if necessary;" echo " - start the client;" echo " - start the relay server;" echo " - on Ctrl+C, stop both the relay server and the client." echo echo " $0 start [serial] [-d DNS[,DNS2,...]]" echo " Start a client on the Android device and exit." echo " If several devices are connected via adb, then serial must be" echo " specified." echo " If -d is given, then make the Android device use the specified" echo " DNS server(s). Otherwise, use 8.8.8.8 (Google public DNS)." echo " If the client is already started, then do nothing, and ignore" echo " DNS servers parameter." echo " To use the host 'localhost' as DNS, use 10.0.2.2." echo echo " $0 stop [serial]" echo " Stop the client on the Android device and exit." echo " If several devices are connected via adb, then serial must be" echo " specified." echo echo " $0 relay" echo " Start the relay server in the current terminal." exit 1 } adbs() { local serial="$1" cmd="'$ADB'" [[ "$serial" ]] && cmd+=" -s '$serial'" echo "$cmd" } evalv() { echo "$@" eval "$@" } locate_file() { for file in "$@" do if [[ -r "$file" ]] then echo "$file" return fi done # not found, use the name having the highest priority echo "$1" } get_relay_path() { locate_file relay.jar \ relay/build/libs/relay.jar } get_apk_path() { locate_file gnirehtet.apk \ app/build/outputs/apk/gnirehtet-release.apk \ app/build/outputs/apk/gnirehtet-debug.apk } apk_install() { echo 'Installing gnirehtet...' local serial="$1" evalv $(adbs "$serial") install -r "$(get_apk_path)" } apk_require_installed() { if [[ ! "$(eval $(adbs "$serial") shell pm list packages com.genymobile.gnirehtet)" ]] then apk_install # wait a bit after the app is installed so that intent actions are correctly registered sleep 0.5 fi } apk_uninstall() { echo 'Uninstalling gnirehtet...' local serial="$1" evalv $(adbs "$serial") uninstall com.genymobile.gnirehtet } start() { echo 'Starting gnirehtet...' local dns= eval set -- $(getopt d: "$@") while true; do case "$1" in -d) dns="$2"; shift 2;; --) shift; break;; *) break;; esac done local serial="$1" evalv $(adbs "$serial") reverse tcp:31416 tcp:31416 [[ $? = 0 ]] || return local dparam= [[ "$dns" ]] && dparam=" --esa dnsServers $dns" evalv $(adbs "$serial") shell am startservice -a com.genymobile.gnirehtet.START$dparam } stop() { echo 'Stopping gnirehtet...' local serial="$1" evalv $(adbs "$serial") shell am startservice -a com.genymobile.gnirehtet.STOP } relay() { evalv "'$JAVA'" -jar "$(get_relay_path)" } case "$1" in install) shift apk_install "$@";; uninstall) shift apk_uninstall "$@";; start) shift start "$@";; stop) shift stop "$@";; relay) shift relay "$@";; rt) shift apk_require_installed trap stop INT start "$@" relay;; *) syntax;; esac