feat: Automate device proxy configuration

This change automates the setup of the device's global HTTP proxy to route traffic through the mitmproxy container.

Key changes:
- Detect if the target is an emulator or a physical device.
- For emulators, the proxy is set to the host loopback address (10.0.2.2).
- For physical devices, the script now attempts to automatically determine the host's local IP address (`en0`/`en1`).
- The device's global proxy settings are configured via `adb`.
- Instructions are updated to reflect the automated setup and include the command to clear the proxy settings.
This commit is contained in:
benjamin-luescher 2026-02-11 08:01:46 +01:00
parent 18f5562aca
commit 0c5c835263

View file

@ -19,6 +19,7 @@ ADB=""
PACKAGE_NAME="" PACKAGE_NAME=""
PULL_DIR="" PULL_DIR=""
DEBUGGABLE_DIR="" DEBUGGABLE_DIR=""
PROXY_HOST=""
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Proxy configuration # Proxy configuration
@ -433,6 +434,24 @@ start_proxy() {
sleep 1 sleep 1
waited=$((waited + 1)) waited=$((waited + 1))
done done
# Configure device to route traffic through proxy
local proxy_host=""
if [[ "$DEVICE_SERIAL" == emulator-* ]]; then
proxy_host="10.0.2.2"
else
# Physical device: find host's local IP
proxy_host=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || true)
if [[ -z "$proxy_host" ]]; then
print_warning "Could not determine host IP for physical device."
print_info "Manually set HTTP proxy on device to <your-host-ip>:$PROXY_PORT"
return
fi
fi
"$ADB" -s "$DEVICE_SERIAL" shell settings put global http_proxy "$proxy_host:$PROXY_PORT"
PROXY_HOST="$proxy_host"
print_step "Device proxy set to $proxy_host:$PROXY_PORT"
} }
main() { main() {
@ -465,6 +484,7 @@ main() {
echo -e " ${GREEN}Proxy:${NC} http://127.0.0.1:$PROXY_PORT" echo -e " ${GREEN}Proxy:${NC} http://127.0.0.1:$PROXY_PORT"
echo -e " ${GREEN}Web UI:${NC} http://localhost:$WEB_PORT" echo -e " ${GREEN}Web UI:${NC} http://localhost:$WEB_PORT"
echo -e " ${GREEN}Password:${NC} $PROXY_PASSWORD" echo -e " ${GREEN}Password:${NC} $PROXY_PASSWORD"
[[ -n "$PROXY_HOST" ]] && echo -e " ${GREEN}Device via:${NC} $PROXY_HOST:$PROXY_PORT"
echo "" echo ""
echo -e " ${YELLOW}Install the mitmproxy CA certificate on the device:${NC}" echo -e " ${YELLOW}Install the mitmproxy CA certificate on the device:${NC}"
echo " 1. Open Settings → search \"certificate\"" echo " 1. Open Settings → search \"certificate\""
@ -472,10 +492,9 @@ main() {
echo " 3. Tap \"Install anyway\"" echo " 3. Tap \"Install anyway\""
echo " 4. Select \"mitmproxy-ca-cert.cer\" from internal storage" echo " 4. Select \"mitmproxy-ca-cert.cer\" from internal storage"
echo "" echo ""
echo " Configure your device to use proxy 10.0.2.2:$PROXY_PORT" echo " Stop proxy and clear device settings:"
echo " (for emulators, 10.0.2.2 is the host machine)" echo " docker stop $CONTAINER_NAME"
echo "" echo " adb -s $DEVICE_SERIAL shell settings put global http_proxy :0"
echo " Stop proxy: docker stop $CONTAINER_NAME"
else else
echo "" echo ""
echo " To attach a debugger:" echo " To attach a debugger:"