Android配置whistle手机代理,为了避免频繁自己手动去WiFi代理输入私有IP地址,特地建了一个Python脚本来帮助一键配置好代理,解除代理。
原始配置流程手续:
- 需要打开http://127.0.0.1:8899/ 查看whistle的IP和端口号
- 需要打开手机WiFi代理,输入上述的IP和端口号
- 如果清除代理,需要再次打开WiFi代理,取消代理
现在的配置流程手续:
- 直接输入下述命令即可设置代理
python proxy.py set
- 直接输入下述命令即可清除代理
python proxy.py clear
proxy.py文件代码如下,主要核心也就是用Python模拟发送adb命令去控制代理。省掉了自己打开WiFi输入IP地址的过程。
python hljs">import subprocess
import re
import sysdef get_whistle_ip_port():try:# 获取 whistle 的状态输出result = subprocess.run(['w2', 'status'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)output = result.stdout.decode('utf-8').strip()# 打印原始输出以便调试print("Whistle output:", output)# 使用正则表达式从输出中提取非127.0.0.1的IP地址和端口号ip_match = re.search(r'http://(?!127\.0\.0\.1)([\d.]+):(\d+)/', output)if ip_match:ip = ip_match.group(1)port = ip_match.group(2)return ip, portelse:print("Could not parse external IP and port from whistle output.")except Exception as e:print(f"Failed to get whistle config: {e}")return None, Nonedef set_android_wifi_proxy(ip, port):try:# 通过ADB连接设备并设置WiFi代理subprocess.run(['adb', 'shell', 'settings', 'put', 'global', 'http_proxy', f"{ip}:{port}"])print(f"Proxy set to {ip}:{port} successfully.")except Exception as e:print(f"Failed to set proxy on Android device: {e}")def clear_android_wifi_proxy():try:# 通过ADB连接设备并清除WiFi代理设置subprocess.run(['adb', 'shell', 'settings', 'put', 'global', 'http_proxy', ':0'])print("Proxy cleared successfully.")except Exception as e:print(f"Failed to clear proxy on Android device: {e}")def main(action):if action == 'set':ip, port = get_whistle_ip_port()if ip and port:set_android_wifi_proxy(ip, port)else:print("Could not retrieve whistle IP and port.")elif action == 'clear':clear_android_wifi_proxy()else:print("Invalid action. Use 'set' to set proxy or 'clear' to clear proxy.")if __name__ == "__main__":if len(sys.argv) != 2:print("Usage: python script.py [set|clear]")else:main(sys.argv[1])
python proxy.py set
运行之后,如下所示:
python proxy.py clear