Created
November 28, 2024 07:04
-
-
Save zyc945/44fb209b374d225867a45fd5f580492e to your computer and use it in GitHub Desktop.
通过 adb 为 android 设备设置 http 代理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| param=$1 | |
| # 如果没有参数或者是 -h/--help,则显示帮助信息 | |
| if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then | |
| echo "用法: $0 [代理地址|选项]" | |
| echo "" | |
| echo "选项:" | |
| echo " -a 自动获取本机局域网IP并设置代理为 <局域网IP>:8080" | |
| echo " -d 移除当前设备的代理设置" | |
| echo " -h,--help 显示帮助信息" | |
| echo "" | |
| echo "示例:" | |
| echo " $0 192.168.1.100:8080 设置代理为 192.168.1.100:8080" | |
| echo " $0 -a 自动获取本机局域网IP并设置代理" | |
| echo " $0 -d 移除代理设置" | |
| exit 0 | |
| fi | |
| # 获取手机名称 | |
| adb_phone_name=$(adb shell getprop ro.product.model) | |
| if [ -z "$adb_phone_name" ]; then | |
| echo "无法获取手机名称" | |
| adb_phone_name="当前连接手机" | |
| fi | |
| if [ "$param" = "-a" ]; then | |
| # 获取局域网IP | |
| if [ "$(uname)" = "Darwin" ]; then | |
| # macOS | |
| lanIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}' | head -n 1) | |
| else | |
| # Linux | |
| lanIp=$(ip addr | grep 'state UP' -A2 | grep inet | awk '{print $2}' | cut -f1 -d'/' | head -n 1) | |
| fi | |
| if [ -z "$lanIp" ]; then | |
| echo "无法获取局域网IP" | |
| exit 1 | |
| fi | |
| proxy="$lanIp:8080" | |
| echo "自动获取到局域网IP: $lanIp" | |
| fi | |
| if [ "$param" = "-d" ]; then | |
| echo "正在移除[$adb_phone_name]代理..." | |
| adb shell settings put global http_proxy :0 | |
| echo "[$adb_phone_name]代理已移除" | |
| exit 0 | |
| fi | |
| if [ -z "$proxy" ]; then | |
| echo "请提供代理地址(如: 127.0.0.1:8080)或使用 -d 移除代理" | |
| exit 1 | |
| fi | |
| echo "正在设置[$adb_phone_name]代理为: $proxy" | |
| adb shell settings put global http_proxy $proxy | |
| echo "[$adb_phone_name]代理设置完成" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment