作为客户端研发人员,请求抓包,改请求参数和回调结果的需求非常常见。charles的请求代理和map local功能也非常方便,强大。but,给Android手机设置代理却是一个非常痛苦的事情。
1. 打开wiki设置
2. 长按高级配置
3. 查看本级ip
4. 输入ip,和端口
5. 保存退出
6. 回到刚才终端的场景
如果你也为上述问题所烦恼,通过adb命令给Android手机设置代理,可以解决你的问题。脚本我已经写好了,拿走不谢。
proxy
#! /usr/bin/env bash
PORT=8888
function get_proxy() {
p="$1"
if [[ -n "$p" ]]
then
echo "$p"
else
_ip=$(ifconfig en0 | awk '/inet /{print $2}')
local_proxy="${_ip}:${PORT}"
echo "$local_proxy"
fi
}
# 设置代理
function adb_set_proxy() {
p=$1
m=$2
_devices=$(adb devices | awk 'NF == 2{print $1}')
for d in ${_devices}
do
adb -s ${d} shell settings put global http_proxy ${p}
echo "${m}: <device: ${d}> <proxy: ${p}> <status: $?>"
done
}
function print_help() {
command=$(basename $0)
echo "============= Usage Demo ==========="
echo "# 设置为本机代理"
echo "${command} set"
echo
echo "# 清除代理"
echo "${command} clean"
echo "${command} clear"
echo "===================================="
}
# 命令分发
if [ -z $1 ]
then
print_help
exit -1
fi
case "$1" in
set ) adb_set_proxy $(get_proxy "$2") "设置代理成功";;
clea[rn] ) adb_set_proxy ':0' "清理代理成功";;
* ) echo "unkonwn command: ${1}"; print_help; exit -1;;
esac