Skip to main content

Share local adb devices with remote users via TCP proxy

Project description

ShareADB

Python Version License Platform

Share local Android Debug Bridge (ADB) devices with remote users via TCP proxy

GitHub Repository | Issues | Documentation


功能需求

共享本地adb设备给其他用户使用。

需要考虑的问题

  1. 多设备管理: 支持同时连接和管理多个adb设备。每个设备可以分配不同的端口号进行adb连接。
  2. 跨平台支持, 需要支持windows, linux等操作系统
  3. 一旦开启后,会记录已经开启的连接,如果设备断开后,可以自动进行重连(周期比如间隔5s检测设备是否已经连接上了, 用于维护连接的稳定性). 通知链接的断开和重新连接状态.
  4. 可以针对单个已经连接的设备进行操作.

基础功能的实现步骤

检测adb,如果没有安装adb命令,那么则需要提示用户安装或者输入adb路径

当单个设备时

adb shell setprop persist.adb.tcp.port 5555
adb shell stop adbd
adb shell start adbd
adb forward tcp:6000 tcp:5555

多个设备希望端口号可以慢慢自增

最终还需要在本地启动一个代理,允许其他设备连接当前设备的IP来访问adb功能.

类似:

socat -s tcp4-listen:7000,reuseaddr,fork tcp-connect:127.0.0.1:6000

不过不建议直接使用socat, 而是使用python利用socket来实现一个属于自己的代理,这样可以实现跨平台.

Requirements

Python Version

  • Python 3.8 or higher
  • Supports Python 3.8, 3.9, 3.10, 3.11, 3.12

System Requirements

  • ADB (Android Debug Bridge) installed and accessible in PATH
  • Linux, macOS, or Windows operating system
  • Network connectivity for remote connections

Dependencies

No external dependencies required! This package uses only Python standard library modules.

Installation

From PyPI (Recommended)

pip install shareadbpy

From Source

git clone https://github.com/hemin0721/shareadb.git
cd shareadb
pip install -e .

Verify Installation

shareadbpy --help

Features

  • Multi-Device Support: Manage multiple ADB devices simultaneously
  • Auto-Reconnect: Automatic device detection and reconnection (configurable interval)
  • Cross-Platform: Works on Linux, macOS, and Windows
  • Pure Python: No external dependencies, uses only standard library
  • TCP Proxy: Built-in TCP proxy for remote access
  • Port Auto-Assignment: Automatic port allocation for multiple devices
  • Local IP Detection: Automatically detects and displays local IP addresses
  • Connection Monitoring: Real-time device status and connection information
  • Selective Sharing: Share specific devices using --include parameter

项目结构

pyproject.toml        # 项目元数据与命令行入口
src/shareadb/         # Python 源码目录
  __init__.py
  adb_client.py       # 封装 adb 命令的异步客户端
  cli.py              # 命令行入口,负责参数解析与事件循环
  device_manager.py   # 设备会话管理、端口分配与自动重连逻辑
  tcp_proxy.py        # 基于 asyncio 的 TCP 代理实现

使用说明

  1. 确保本机已安装 adb 并可以在命令行中访问,或在执行时使用 --adb-path 指定 adb 的完整路径。
  2. 在仓库根目录执行:
python -m shareadb.cli --poll-interval 5 --status-interval 30

常用参数:

  • --listen-host: 代理监听的地址,默认 0.0.0.0
  • --forward-base-port: adb forward 起始端口,默认 6000
  • --proxy-base-port: TCP 代理起始端口,默认 7000
  • --include: 仅管理指定序列号的设备,例如 --include SERIAL1 SERIAL2
  1. 程序会每隔 poll-interval 秒检测设备状态:
    • 新设备上线时自动配置 adb tcp、建立端口转发并启动本地代理;
    • 设备断开时释放 forward 与代理,下次检测到设备重新连接时继续复用历史端口;
    • status-interval 控制定期输出设备状态日志,设置为 0 可关闭。
    • 设备连接成功后,会自动显示本机的所有可访问 IP 地址和对应的连接指令。

远端用户只需连接到 listen-host:proxy_port 即可访问对应设备的 adb。每台设备都会分配一对唯一的 forward 与代理端口,并在设备重连后保持一致。

连接信息示例

当设备成功连接后,程序会自动输出类似以下信息:

============================================================
ADB devices are now ready for remote connection!
Remote users can connect using:

Device: abc123def456
  Model: Pixel 6
  Proxy port: 7000
  Connection commands:
    adb connect 10.0.xx.xx:7000
    adb connect 192.168.1.100:7000

Note: Use the appropriate IP address that is accessible from the remote machine
============================================================

无需手动查找本机 IP,程序会自动检测所有可用的网络接口 IP 地址,方便远端用户选择合适的地址进行连接。

打包和发布

构建分发包

# 安装构建工具
pip install build twine

# 构建分发包
python -m build

构建完成后,会在 dist/ 目录下生成 .tar.gz.whl 文件。

上传到 PyPI

首先确保你已经注册了 PyPI 账号并配置了账号信息:

# 上传到测试 PyPI(用于测试)
python -m twine upload --repository testpypi dist/*

# 上传到正式 PyPI
python -m twine upload dist/*

从 PyPI 安装

# 从正式 PyPI 安装
pip install shareadbpy

# 从测试 PyPI 安装
pip install --index-url https://test.pypi.org/simple/ shareadbpy

安装后可以直接使用命令:

shareadbpy --poll-interval 5 --status-interval 30

已测试场景

  • Ubuntu 下的单设备移除和恢复
  • Ubuntu 下的双设备移除和恢复
  • 使用 --include 参数在双设备环境下只共享指定设备
  • 基础功能在 Linux 系统下运行正常

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Support

If you encounter any issues or have questions:

Acknowledgments

  • Built with pure Python using asyncio
  • No external dependencies
  • Cross-platform support (Linux, macOS, Windows)

Made with ❤️ by hemin0721

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shareadb-0.1.1.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shareadb-0.1.1-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file shareadb-0.1.1.tar.gz.

File metadata

  • Download URL: shareadb-0.1.1.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for shareadb-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c9afc409deb661c4014196d1a7edc3d35c62a3bca3be07dd935eb2da54e16222
MD5 89f47456f65cce29eb8a9ce1f77b06d4
BLAKE2b-256 94246317424bc5dc9b95a7ac47582652d3773c13a668c150a671a291e9578883

See more details on using hashes here.

File details

Details for the file shareadb-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: shareadb-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for shareadb-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7dde71e77ef704c1c50af275a48e0e1552f2430fc54059597463405aea7d3de
MD5 f1c64ab8f8f823d96e3438f33859e6d1
BLAKE2b-256 56de17dfba37f0d52586cbe62168709f9ab6f5cae7b60da47fe850e9d583c060

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page