datap 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datap-0.1.0/PKG-INFO +207 -0
- datap-0.1.0/README.md +197 -0
- datap-0.1.0/pyproject.toml +23 -0
- datap-0.1.0/setup.cfg +4 -0
- datap-0.1.0/src/btsh/__init__.py +36 -0
- datap-0.1.0/src/btsh/__main__.py +5 -0
- datap-0.1.0/src/btsh/audio.py +143 -0
- datap-0.1.0/src/btsh/ble.py +147 -0
- datap-0.1.0/src/btsh/bt.py +90 -0
- datap-0.1.0/src/btsh/channel.py +112 -0
- datap-0.1.0/src/btsh/channels.py +119 -0
- datap-0.1.0/src/btsh/cli.py +528 -0
- datap-0.1.0/src/btsh/client.py +75 -0
- datap-0.1.0/src/btsh/codec.py +120 -0
- datap-0.1.0/src/btsh/crypto.py +233 -0
- datap-0.1.0/src/btsh/framing.py +96 -0
- datap-0.1.0/src/btsh/hub.py +266 -0
- datap-0.1.0/src/btsh/mcp.py +213 -0
- datap-0.1.0/src/btsh/modem.py +196 -0
- datap-0.1.0/src/btsh/node.py +190 -0
- datap-0.1.0/src/btsh/py.typed +0 -0
- datap-0.1.0/src/btsh/server.py +194 -0
- datap-0.1.0/src/btsh/session.py +187 -0
- datap-0.1.0/src/btsh/transport.py +325 -0
- datap-0.1.0/src/datap.egg-info/PKG-INFO +207 -0
- datap-0.1.0/src/datap.egg-info/SOURCES.txt +33 -0
- datap-0.1.0/src/datap.egg-info/dependency_links.txt +1 -0
- datap-0.1.0/src/datap.egg-info/entry_points.txt +2 -0
- datap-0.1.0/src/datap.egg-info/top_level.txt +1 -0
- datap-0.1.0/tests/test_channels.py +182 -0
- datap-0.1.0/tests/test_crypto.py +102 -0
- datap-0.1.0/tests/test_e2e.py +132 -0
- datap-0.1.0/tests/test_framing.py +33 -0
- datap-0.1.0/tests/test_mesh.py +158 -0
- datap-0.1.0/tests/test_modem.py +51 -0
datap-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: datap
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Shell over a Bluetooth speaker's audio codec: run commands without an SSH server
|
|
5
|
+
Author: Marc Partensky
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: bluetooth,a2dp,sbc,modem,shell,ssh
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# btsh
|
|
12
|
+
|
|
13
|
+
A shell that rides on a Bluetooth link, no SSH server anywhere.
|
|
14
|
+
|
|
15
|
+
`btsh` puts a framed, acknowledged, optionally authenticated protocol on top of
|
|
16
|
+
whatever Bluetooth channel you can actually use, and turns it into `exec` and
|
|
17
|
+
`shell` commands on the peer. Three links ship:
|
|
18
|
+
|
|
19
|
+
| Link | Carries | Works on | Server side |
|
|
20
|
+
|---|---|---|---|
|
|
21
|
+
| `portaudio` | 4-FSK modem over the speaker codec (SBC verified) | Linux, macOS, Windows | any of the three |
|
|
22
|
+
| `pw` | same modem, PipeWire nodes, codec read from `pw-dump` | Linux | Linux |
|
|
23
|
+
| `ble` | same frames, no modem, GATT write/notify | Linux, macOS, Windows | Android app (`android/`) |
|
|
24
|
+
| `fifo` | same modem, two named pipes | any | any (two local processes, tests) |
|
|
25
|
+
|
|
26
|
+
## The honest part about Android
|
|
27
|
+
|
|
28
|
+
The intended topology ("an Android app that pretends to be AirPods, plays music
|
|
29
|
+
and runs the shell") cannot use A2DP audio on an unrooted phone:
|
|
30
|
+
|
|
31
|
+
* `A2dpSinkService` is a system service and every entry point enforces
|
|
32
|
+
`BLUETOOTH_PRIVILEGED` (see AOSP `packages/modules/Bluetooth`,
|
|
33
|
+
`a2dpsink/A2dpSinkService.java`). A normal app cannot register as an A2DP
|
|
34
|
+
sink, so the phone cannot be the "speaker".
|
|
35
|
+
* Even the system sink never hands the decoded stream to an app: A2DP audio is
|
|
36
|
+
routed inside the Bluetooth stack to the audio HAL. There is no
|
|
37
|
+
`AudioRecord`/`AudioTrack` path for it.
|
|
38
|
+
* HFP is the same story: SCO audio belongs to the telephony stack.
|
|
39
|
+
|
|
40
|
+
So the Android app in `android/` is a **BLE GATT peripheral** that advertises as
|
|
41
|
+
`AirPods Pro` (name plus Apple manufacturer data in the scan response) and
|
|
42
|
+
serves the exact same frames. That is the only radio channel an app may serve
|
|
43
|
+
on Android, and `bleak` drives it from all three desktop OSes. The acoustic
|
|
44
|
+
modem stays available where the OS does expose the stream (desktops), and it is
|
|
45
|
+
what makes "the speaker's codec carries your shell" literal.
|
|
46
|
+
|
|
47
|
+
## Install
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
pip install -e . # core, stdlib only
|
|
51
|
+
pip install -e ".[all]" # + sounddevice (PortAudio) and bleak
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`ffmpeg` is only needed for `--codec sbc` and `btsh selftest`: it is what pushes
|
|
55
|
+
a burst through the same SBC transform a Bluetooth speaker applies.
|
|
56
|
+
|
|
57
|
+
## Quick start
|
|
58
|
+
|
|
59
|
+
In-process demo, commands really executed, every burst pushed through SBC:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
btsh demo
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Two processes on one host, no socket and no SSH, still through SBC:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
btsh serve --transport fifo --codec sbc --key ~/.btsh.key &
|
|
69
|
+
btsh exec --transport fifo --codec sbc --key ~/.btsh.key -- "uname -a | head -1"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Real speaker, PipeWire (Linux):
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
btsh doctor # devices, nodes, negotiated codec
|
|
76
|
+
btsh connect --device "Xiaomi OpenWear"
|
|
77
|
+
btsh serve --transport pw --codec sbc --target bluez_output.14:6C:27:05:D3:BE.1
|
|
78
|
+
btsh exec --transport pw --codec sbc --target bluez_output.14:6C:27:05:D3:BE.1 -- "id -un"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Real speaker, any OS (PortAudio): make the speaker the default output device,
|
|
82
|
+
and for duplex its microphone the default input device, then:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
btsh devices # pick indexes or names
|
|
86
|
+
btsh serve --transport portaudio --output-device 4 --input-device 4
|
|
87
|
+
btsh exec --transport portaudio --output-device 4 --input-device 4 -- "uname -sr"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Android server over BLE:
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
btsh scan --timeout 10 # the app advertises as "AirPods Pro"
|
|
94
|
+
btsh exec --transport ble --ble-name "AirPods" -- "id"
|
|
95
|
+
btsh shell --transport ble --ble-name "AirPods"
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## CLI
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
btsh doctor [--json] devices, BlueZ nodes + codec, PortAudio devices
|
|
102
|
+
btsh connect --device NAME connect a Bluetooth device (BlueZ)
|
|
103
|
+
btsh devices [--json] list audio devices (PortAudio)
|
|
104
|
+
btsh scan [--timeout N] scan BLE peripherals
|
|
105
|
+
btsh serve [link options] run the shell side
|
|
106
|
+
btsh exec [link options] -- CMD run one command
|
|
107
|
+
btsh shell [link options] interactive REPL
|
|
108
|
+
btsh demo [--codec sbc] in-process end to end test
|
|
109
|
+
btsh selftest [--frames N] frame recovery, clean and through SBC
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`serve` options: `--cwd`, `--timeout`, `--max-output`, `--allow REGEX`,
|
|
113
|
+
`--deny REGEX`, `--key FILE`. Link options: `--transport`,
|
|
114
|
+
`--codec none|sbc`, `--key`, `--retries`, `--ack-timeout`, `--verbose`, plus
|
|
115
|
+
`--target/--target-in` (pw), `--output-device/--input-device` (portaudio),
|
|
116
|
+
`--ble-address/--ble-name` (ble), `--fifo-dir` (fifo).
|
|
117
|
+
|
|
118
|
+
## Protocol
|
|
119
|
+
|
|
120
|
+
One frame, 12 bytes of overhead:
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
0 version 1
|
|
124
|
+
1 type 0 DATA, 1 ACK, 2 NAK, 4 HELLO, 5 HELLO_ACK, 6 BYE
|
|
125
|
+
2 seq mod 256
|
|
126
|
+
3 flags 1 START, 2 END
|
|
127
|
+
4 length payload length, big endian, 2 bytes (max 240)
|
|
128
|
+
6 payload [16 byte HMAC tag][JSON chunk]
|
|
129
|
+
.. crc32 over version..payload
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
* Acoustic link: each frame is modulated as 4-FSK (2 ms symbols, 1000/2000/
|
|
133
|
+
3000/4000 Hz, 2 bits per symbol), 24 ms of lead silence, 12 symbol preamble,
|
|
134
|
+
32 bit sync word. Measured: 8/8 frames recovered through ffmpeg SBC at 128,
|
|
135
|
+
200 and 328 kbit/s. ~108 B/s of payload.
|
|
136
|
+
* Stop-and-wait ARQ: a frame is resent until ACKed (`--retries`, `--ack-timeout`).
|
|
137
|
+
* Messages are JSON, chunked into 224 byte bodies, reassembled with START/END.
|
|
138
|
+
* Auth is a pre-shared key file (`--key`), HMAC-SHA256 over `seq || body`,
|
|
139
|
+
truncated to 16 bytes, plus a nonce handshake. It replaces sshd, not SSH's
|
|
140
|
+
asymmetric crypto: no forward secrecy, no host key pinning. Keep it on a link
|
|
141
|
+
you already trust, or treat it as "not open to the room".
|
|
142
|
+
|
|
143
|
+
## Limits, stated plainly
|
|
144
|
+
|
|
145
|
+
* Payload throughput is ~108 B/s on the acoustic link. It is a shell for small
|
|
146
|
+
commands, not for `tar | gzip` over the air.
|
|
147
|
+
* A2DP is one way: a full session needs HFP (the speaker's microphone) or two
|
|
148
|
+
paired devices. `pw` and `portaudio` record from the same node/device they
|
|
149
|
+
play into, so duplex means a headset, not a plain speaker.
|
|
150
|
+
* The Android server runs commands in the app sandbox via `/system/bin/sh`; it
|
|
151
|
+
tries `su -c` when the device is rooted. No root, no bash, no `/etc`.
|
|
152
|
+
* BLE needs a central that keeps the connection: `bleak` does, macOS needs the
|
|
153
|
+
app to be foregrounded once to grant Bluetooth permission.
|
|
154
|
+
* Nothing here is tested against a physical speaker in CI: the codec path is
|
|
155
|
+
verified through ffmpeg's SBC encoder/decoder, the Android app is source only
|
|
156
|
+
(no SDK on the machine that generated this repo).
|
|
157
|
+
|
|
158
|
+
## Tests
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
162
|
+
btsh selftest --frames 12
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Android app
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
android/app/src/main/java/com/marcpartensky/btsh/
|
|
169
|
+
├── Protocol.kt frame pack/unpack, same bytes as Python
|
|
170
|
+
├── ShellRunner.kt /system/bin/sh or su -c, output capped
|
|
171
|
+
├── BtshGattServer.kt GATT service, ACKs, chunking, HMAC, AirPods advertising
|
|
172
|
+
├── BtshService.kt foreground service, keeps advertising alive
|
|
173
|
+
└── MainActivity.kt key field, start/stop, log
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Build on a machine with the Android SDK (not on tower):
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
cd android && ./gradlew assembleDebug # needs gradle wrapper, see below
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
No gradle wrapper is committed: run `gradle wrapper` once, or open the folder in
|
|
183
|
+
Android Studio. Install with `adb install app/build/outputs/apk/debug/app-debug.apk`,
|
|
184
|
+
then start it, set the same key as the client, and connect from the desktop:
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
btsh exec --transport ble --ble-name "AirPods" --key ~/.btsh.key -- "getprop ro.product.model"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Nix
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
nix build .# # the package
|
|
194
|
+
nix run .# -- demo
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
`module.nix` installs the CLI and can run a `btsh serve` unit:
|
|
198
|
+
|
|
199
|
+
```nix
|
|
200
|
+
services.btsh = {
|
|
201
|
+
enable = true;
|
|
202
|
+
user = "marc";
|
|
203
|
+
keyFile = "/run/secrets/btsh.key";
|
|
204
|
+
cwd = "/home/marc";
|
|
205
|
+
allow = [ "^ls" "^cat" "^uname" ];
|
|
206
|
+
};
|
|
207
|
+
```
|
datap-0.1.0/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# btsh
|
|
2
|
+
|
|
3
|
+
A shell that rides on a Bluetooth link, no SSH server anywhere.
|
|
4
|
+
|
|
5
|
+
`btsh` puts a framed, acknowledged, optionally authenticated protocol on top of
|
|
6
|
+
whatever Bluetooth channel you can actually use, and turns it into `exec` and
|
|
7
|
+
`shell` commands on the peer. Three links ship:
|
|
8
|
+
|
|
9
|
+
| Link | Carries | Works on | Server side |
|
|
10
|
+
|---|---|---|---|
|
|
11
|
+
| `portaudio` | 4-FSK modem over the speaker codec (SBC verified) | Linux, macOS, Windows | any of the three |
|
|
12
|
+
| `pw` | same modem, PipeWire nodes, codec read from `pw-dump` | Linux | Linux |
|
|
13
|
+
| `ble` | same frames, no modem, GATT write/notify | Linux, macOS, Windows | Android app (`android/`) |
|
|
14
|
+
| `fifo` | same modem, two named pipes | any | any (two local processes, tests) |
|
|
15
|
+
|
|
16
|
+
## The honest part about Android
|
|
17
|
+
|
|
18
|
+
The intended topology ("an Android app that pretends to be AirPods, plays music
|
|
19
|
+
and runs the shell") cannot use A2DP audio on an unrooted phone:
|
|
20
|
+
|
|
21
|
+
* `A2dpSinkService` is a system service and every entry point enforces
|
|
22
|
+
`BLUETOOTH_PRIVILEGED` (see AOSP `packages/modules/Bluetooth`,
|
|
23
|
+
`a2dpsink/A2dpSinkService.java`). A normal app cannot register as an A2DP
|
|
24
|
+
sink, so the phone cannot be the "speaker".
|
|
25
|
+
* Even the system sink never hands the decoded stream to an app: A2DP audio is
|
|
26
|
+
routed inside the Bluetooth stack to the audio HAL. There is no
|
|
27
|
+
`AudioRecord`/`AudioTrack` path for it.
|
|
28
|
+
* HFP is the same story: SCO audio belongs to the telephony stack.
|
|
29
|
+
|
|
30
|
+
So the Android app in `android/` is a **BLE GATT peripheral** that advertises as
|
|
31
|
+
`AirPods Pro` (name plus Apple manufacturer data in the scan response) and
|
|
32
|
+
serves the exact same frames. That is the only radio channel an app may serve
|
|
33
|
+
on Android, and `bleak` drives it from all three desktop OSes. The acoustic
|
|
34
|
+
modem stays available where the OS does expose the stream (desktops), and it is
|
|
35
|
+
what makes "the speaker's codec carries your shell" literal.
|
|
36
|
+
|
|
37
|
+
## Install
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install -e . # core, stdlib only
|
|
41
|
+
pip install -e ".[all]" # + sounddevice (PortAudio) and bleak
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`ffmpeg` is only needed for `--codec sbc` and `btsh selftest`: it is what pushes
|
|
45
|
+
a burst through the same SBC transform a Bluetooth speaker applies.
|
|
46
|
+
|
|
47
|
+
## Quick start
|
|
48
|
+
|
|
49
|
+
In-process demo, commands really executed, every burst pushed through SBC:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
btsh demo
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Two processes on one host, no socket and no SSH, still through SBC:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
btsh serve --transport fifo --codec sbc --key ~/.btsh.key &
|
|
59
|
+
btsh exec --transport fifo --codec sbc --key ~/.btsh.key -- "uname -a | head -1"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Real speaker, PipeWire (Linux):
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
btsh doctor # devices, nodes, negotiated codec
|
|
66
|
+
btsh connect --device "Xiaomi OpenWear"
|
|
67
|
+
btsh serve --transport pw --codec sbc --target bluez_output.14:6C:27:05:D3:BE.1
|
|
68
|
+
btsh exec --transport pw --codec sbc --target bluez_output.14:6C:27:05:D3:BE.1 -- "id -un"
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Real speaker, any OS (PortAudio): make the speaker the default output device,
|
|
72
|
+
and for duplex its microphone the default input device, then:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
btsh devices # pick indexes or names
|
|
76
|
+
btsh serve --transport portaudio --output-device 4 --input-device 4
|
|
77
|
+
btsh exec --transport portaudio --output-device 4 --input-device 4 -- "uname -sr"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Android server over BLE:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
btsh scan --timeout 10 # the app advertises as "AirPods Pro"
|
|
84
|
+
btsh exec --transport ble --ble-name "AirPods" -- "id"
|
|
85
|
+
btsh shell --transport ble --ble-name "AirPods"
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## CLI
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
btsh doctor [--json] devices, BlueZ nodes + codec, PortAudio devices
|
|
92
|
+
btsh connect --device NAME connect a Bluetooth device (BlueZ)
|
|
93
|
+
btsh devices [--json] list audio devices (PortAudio)
|
|
94
|
+
btsh scan [--timeout N] scan BLE peripherals
|
|
95
|
+
btsh serve [link options] run the shell side
|
|
96
|
+
btsh exec [link options] -- CMD run one command
|
|
97
|
+
btsh shell [link options] interactive REPL
|
|
98
|
+
btsh demo [--codec sbc] in-process end to end test
|
|
99
|
+
btsh selftest [--frames N] frame recovery, clean and through SBC
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`serve` options: `--cwd`, `--timeout`, `--max-output`, `--allow REGEX`,
|
|
103
|
+
`--deny REGEX`, `--key FILE`. Link options: `--transport`,
|
|
104
|
+
`--codec none|sbc`, `--key`, `--retries`, `--ack-timeout`, `--verbose`, plus
|
|
105
|
+
`--target/--target-in` (pw), `--output-device/--input-device` (portaudio),
|
|
106
|
+
`--ble-address/--ble-name` (ble), `--fifo-dir` (fifo).
|
|
107
|
+
|
|
108
|
+
## Protocol
|
|
109
|
+
|
|
110
|
+
One frame, 12 bytes of overhead:
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
0 version 1
|
|
114
|
+
1 type 0 DATA, 1 ACK, 2 NAK, 4 HELLO, 5 HELLO_ACK, 6 BYE
|
|
115
|
+
2 seq mod 256
|
|
116
|
+
3 flags 1 START, 2 END
|
|
117
|
+
4 length payload length, big endian, 2 bytes (max 240)
|
|
118
|
+
6 payload [16 byte HMAC tag][JSON chunk]
|
|
119
|
+
.. crc32 over version..payload
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
* Acoustic link: each frame is modulated as 4-FSK (2 ms symbols, 1000/2000/
|
|
123
|
+
3000/4000 Hz, 2 bits per symbol), 24 ms of lead silence, 12 symbol preamble,
|
|
124
|
+
32 bit sync word. Measured: 8/8 frames recovered through ffmpeg SBC at 128,
|
|
125
|
+
200 and 328 kbit/s. ~108 B/s of payload.
|
|
126
|
+
* Stop-and-wait ARQ: a frame is resent until ACKed (`--retries`, `--ack-timeout`).
|
|
127
|
+
* Messages are JSON, chunked into 224 byte bodies, reassembled with START/END.
|
|
128
|
+
* Auth is a pre-shared key file (`--key`), HMAC-SHA256 over `seq || body`,
|
|
129
|
+
truncated to 16 bytes, plus a nonce handshake. It replaces sshd, not SSH's
|
|
130
|
+
asymmetric crypto: no forward secrecy, no host key pinning. Keep it on a link
|
|
131
|
+
you already trust, or treat it as "not open to the room".
|
|
132
|
+
|
|
133
|
+
## Limits, stated plainly
|
|
134
|
+
|
|
135
|
+
* Payload throughput is ~108 B/s on the acoustic link. It is a shell for small
|
|
136
|
+
commands, not for `tar | gzip` over the air.
|
|
137
|
+
* A2DP is one way: a full session needs HFP (the speaker's microphone) or two
|
|
138
|
+
paired devices. `pw` and `portaudio` record from the same node/device they
|
|
139
|
+
play into, so duplex means a headset, not a plain speaker.
|
|
140
|
+
* The Android server runs commands in the app sandbox via `/system/bin/sh`; it
|
|
141
|
+
tries `su -c` when the device is rooted. No root, no bash, no `/etc`.
|
|
142
|
+
* BLE needs a central that keeps the connection: `bleak` does, macOS needs the
|
|
143
|
+
app to be foregrounded once to grant Bluetooth permission.
|
|
144
|
+
* Nothing here is tested against a physical speaker in CI: the codec path is
|
|
145
|
+
verified through ffmpeg's SBC encoder/decoder, the Android app is source only
|
|
146
|
+
(no SDK on the machine that generated this repo).
|
|
147
|
+
|
|
148
|
+
## Tests
|
|
149
|
+
|
|
150
|
+
```bash
|
|
151
|
+
PYTHONPATH=src python -m unittest discover -s tests -v
|
|
152
|
+
btsh selftest --frames 12
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Android app
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
android/app/src/main/java/com/marcpartensky/btsh/
|
|
159
|
+
├── Protocol.kt frame pack/unpack, same bytes as Python
|
|
160
|
+
├── ShellRunner.kt /system/bin/sh or su -c, output capped
|
|
161
|
+
├── BtshGattServer.kt GATT service, ACKs, chunking, HMAC, AirPods advertising
|
|
162
|
+
├── BtshService.kt foreground service, keeps advertising alive
|
|
163
|
+
└── MainActivity.kt key field, start/stop, log
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Build on a machine with the Android SDK (not on tower):
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
cd android && ./gradlew assembleDebug # needs gradle wrapper, see below
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
No gradle wrapper is committed: run `gradle wrapper` once, or open the folder in
|
|
173
|
+
Android Studio. Install with `adb install app/build/outputs/apk/debug/app-debug.apk`,
|
|
174
|
+
then start it, set the same key as the client, and connect from the desktop:
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
btsh exec --transport ble --ble-name "AirPods" --key ~/.btsh.key -- "getprop ro.product.model"
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Nix
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
nix build .# # the package
|
|
184
|
+
nix run .# -- demo
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
`module.nix` installs the CLI and can run a `btsh serve` unit:
|
|
188
|
+
|
|
189
|
+
```nix
|
|
190
|
+
services.btsh = {
|
|
191
|
+
enable = true;
|
|
192
|
+
user = "marc";
|
|
193
|
+
keyFile = "/run/secrets/btsh.key";
|
|
194
|
+
cwd = "/home/marc";
|
|
195
|
+
allow = [ "^ls" "^cat" "^uname" ];
|
|
196
|
+
};
|
|
197
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "datap"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Shell over a Bluetooth speaker's audio codec: run commands without an SSH server"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Marc Partensky" }]
|
|
13
|
+
keywords = ["bluetooth", "a2dp", "sbc", "modem", "shell", "ssh"]
|
|
14
|
+
dependencies = []
|
|
15
|
+
|
|
16
|
+
[project.scripts]
|
|
17
|
+
btsh = "btsh.cli:main"
|
|
18
|
+
|
|
19
|
+
[tool.setuptools.packages.find]
|
|
20
|
+
where = ["src"]
|
|
21
|
+
|
|
22
|
+
[tool.setuptools.package-data]
|
|
23
|
+
btsh = ["py.typed"]
|
datap-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""btsh - an ssh-like shell that rides on a Bluetooth speaker's audio codec."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
|
|
7
|
+
from .channel import Channel, ChannelError # noqa: E402
|
|
8
|
+
from .client import Client # noqa: E402
|
|
9
|
+
from .server import ShellConfig, ShellServer, execute # noqa: E402
|
|
10
|
+
from .session import AuthError, Session # noqa: E402
|
|
11
|
+
from .transport import ( # noqa: E402
|
|
12
|
+
AcousticTransport,
|
|
13
|
+
CodecTransport,
|
|
14
|
+
FifoTransport,
|
|
15
|
+
LoopbackTransport,
|
|
16
|
+
Transport,
|
|
17
|
+
TransportError,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"__version__",
|
|
22
|
+
"AcousticTransport",
|
|
23
|
+
"AuthError",
|
|
24
|
+
"Channel",
|
|
25
|
+
"ChannelError",
|
|
26
|
+
"Client",
|
|
27
|
+
"CodecTransport",
|
|
28
|
+
"FifoTransport",
|
|
29
|
+
"LoopbackTransport",
|
|
30
|
+
"Session",
|
|
31
|
+
"ShellConfig",
|
|
32
|
+
"ShellServer",
|
|
33
|
+
"Transport",
|
|
34
|
+
"TransportError",
|
|
35
|
+
"execute",
|
|
36
|
+
]
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"""Cross-platform audio transport: PortAudio through ``sounddevice``.
|
|
2
|
+
|
|
3
|
+
One backend for Linux, macOS and Windows, unlike the PipeWire transport which
|
|
4
|
+
is Linux only. The Bluetooth device just has to be the system's output device
|
|
5
|
+
(and, for duplex, its input device): the OS does the A2DP/HFP plumbing, we
|
|
6
|
+
speak into it and listen from it.
|
|
7
|
+
|
|
8
|
+
Linux pactl list short sinks -> device index or name
|
|
9
|
+
macOS system output / input -> the AirPods show up as one device
|
|
10
|
+
Windows WASAPI/MME device names -> the AirPods output, not "Stereo Mix"
|
|
11
|
+
|
|
12
|
+
``sounddevice`` needs the PortAudio shared library: package ``portaudio`` on
|
|
13
|
+
Debian/Fedora, ``portaudio`` on Homebrew, nothing on Windows (bundled).
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import queue
|
|
19
|
+
import time
|
|
20
|
+
|
|
21
|
+
from .modem import SAMPLE_RATE
|
|
22
|
+
from .transport import Transport, TransportError
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _sd():
|
|
26
|
+
try:
|
|
27
|
+
import sounddevice # noqa: PLC0415 - optional dependency
|
|
28
|
+
except OSError as exc: # pragma: no cover - depends on the host
|
|
29
|
+
raise TransportError(f"PortAudio missing: {exc}") from exc
|
|
30
|
+
except ImportError as exc: # pragma: no cover
|
|
31
|
+
raise TransportError("install btsh[audio] (pip install sounddevice)") from exc
|
|
32
|
+
return sounddevice
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def list_devices() -> list[dict]:
|
|
36
|
+
"""Audio devices, with the default input/output flagged."""
|
|
37
|
+
sd = _sd()
|
|
38
|
+
try:
|
|
39
|
+
raw = sd.query_devices()
|
|
40
|
+
default_in, default_out = sd.default.device
|
|
41
|
+
except Exception as exc: # pragma: no cover - host dependent
|
|
42
|
+
raise TransportError(f"could not enumerate audio devices: {exc}") from exc
|
|
43
|
+
devices = []
|
|
44
|
+
for index, device in enumerate(raw):
|
|
45
|
+
devices.append(
|
|
46
|
+
{
|
|
47
|
+
"index": index,
|
|
48
|
+
"name": device["name"],
|
|
49
|
+
"inputs": int(device["max_input_channels"]),
|
|
50
|
+
"outputs": int(device["max_output_channels"]),
|
|
51
|
+
"default_input": index == default_in,
|
|
52
|
+
"default_output": index == default_out,
|
|
53
|
+
"host_api": sd.query_hostapis(device["hostapi"])["name"],
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
return devices
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class PortAudioTransport(Transport):
|
|
60
|
+
"""Duplex PCM over the system's default (or named) audio devices."""
|
|
61
|
+
|
|
62
|
+
name = "portaudio"
|
|
63
|
+
|
|
64
|
+
def __init__(
|
|
65
|
+
self,
|
|
66
|
+
output_device: str | int | None = None,
|
|
67
|
+
input_device: str | int | None = None,
|
|
68
|
+
rate: int = SAMPLE_RATE,
|
|
69
|
+
blocksize: int = 480,
|
|
70
|
+
) -> None:
|
|
71
|
+
self.sd = _sd()
|
|
72
|
+
self.rate = rate
|
|
73
|
+
self.output_device = output_device
|
|
74
|
+
self.input_device = input_device
|
|
75
|
+
self._queue: queue.Queue[bytes] = queue.Queue()
|
|
76
|
+
self._min_burst = int(rate * 0.25) * 2 # 250 ms of s16
|
|
77
|
+
try:
|
|
78
|
+
self._out = self.sd.RawOutputStream(
|
|
79
|
+
samplerate=rate,
|
|
80
|
+
blocksize=blocksize,
|
|
81
|
+
device=output_device,
|
|
82
|
+
channels=1,
|
|
83
|
+
dtype="int16",
|
|
84
|
+
)
|
|
85
|
+
self._in = self.sd.RawInputStream(
|
|
86
|
+
samplerate=rate,
|
|
87
|
+
blocksize=blocksize,
|
|
88
|
+
device=input_device,
|
|
89
|
+
channels=1,
|
|
90
|
+
dtype="int16",
|
|
91
|
+
callback=self._callback,
|
|
92
|
+
)
|
|
93
|
+
except Exception as exc: # pragma: no cover - host dependent
|
|
94
|
+
raise TransportError(f"could not open audio devices: {exc}") from exc
|
|
95
|
+
self._out.start()
|
|
96
|
+
self._in.start()
|
|
97
|
+
|
|
98
|
+
def _callback(self, indata, frames, time_info, status) -> None: # noqa: ANN001 - PortAudio signature
|
|
99
|
+
self._queue.put(bytes(indata))
|
|
100
|
+
|
|
101
|
+
def send_burst(self, pcm: bytes) -> None:
|
|
102
|
+
try:
|
|
103
|
+
self._out.write(pcm)
|
|
104
|
+
except Exception as exc: # pragma: no cover - device unplugged
|
|
105
|
+
raise TransportError(f"playback failed: {exc}") from exc
|
|
106
|
+
|
|
107
|
+
def recv_burst(self, timeout: float = 5.0) -> bytes | None:
|
|
108
|
+
deadline = time.monotonic() + timeout
|
|
109
|
+
buffer = bytearray()
|
|
110
|
+
while time.monotonic() < deadline:
|
|
111
|
+
try:
|
|
112
|
+
buffer.extend(self._queue.get(timeout=max(0.01, deadline - time.monotonic())))
|
|
113
|
+
except queue.Empty:
|
|
114
|
+
break
|
|
115
|
+
if len(buffer) >= self._min_burst:
|
|
116
|
+
while True:
|
|
117
|
+
try:
|
|
118
|
+
buffer.extend(self._queue.get_nowait())
|
|
119
|
+
except queue.Empty:
|
|
120
|
+
break
|
|
121
|
+
break
|
|
122
|
+
return bytes(buffer) or None
|
|
123
|
+
|
|
124
|
+
def describe(self) -> dict:
|
|
125
|
+
out = super().describe()
|
|
126
|
+
out.update(
|
|
127
|
+
{
|
|
128
|
+
"output_device": self.output_device,
|
|
129
|
+
"input_device": self.input_device,
|
|
130
|
+
"rate": self.rate,
|
|
131
|
+
}
|
|
132
|
+
)
|
|
133
|
+
return out
|
|
134
|
+
|
|
135
|
+
def close(self) -> None:
|
|
136
|
+
for stream in (getattr(self, "_in", None), getattr(self, "_out", None)):
|
|
137
|
+
if stream is None:
|
|
138
|
+
continue
|
|
139
|
+
try:
|
|
140
|
+
stream.stop()
|
|
141
|
+
stream.close()
|
|
142
|
+
except Exception: # pragma: no cover
|
|
143
|
+
pass
|