|
| 1 | +# Bridge Development & Testing Guide |
| 2 | + |
| 3 | +This guide explains how to develop and test secure bridge daemons for LocalGPT using the `localgpt-bridge` architecture. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The bridge architecture consists of two parts: |
| 8 | +1. **Server (`localgpt daemon`)**: Holds the master key, verifies identities, and dispenses credentials. |
| 9 | +2. **Client (Bridge Binary)**: Connects to the server, proves its identity, and receives its specific secrets (e.g., API tokens). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +Ensure you have built the project: |
| 14 | +```bash |
| 15 | +cd localgpt |
| 16 | +cargo build |
| 17 | +``` |
| 18 | + |
| 19 | +## Testing Workflow |
| 20 | + |
| 21 | +You can verify the entire secure credential flow using the included `test-bridge` binary. |
| 22 | + |
| 23 | +### 1. Register a Credential |
| 24 | +First, securely store a secret for your bridge. This uses the main CLI to encrypt the secret with the device master key. |
| 25 | + |
| 26 | +```bash |
| 27 | +# Register a dummy secret for the bridge ID "test-bridge" |
| 28 | +# This creates ~/.local/share/localgpt/bridges/test-bridge.enc |
| 29 | +cargo run -- bridge register --id test-bridge --secret "super-secret-token-123" |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Start the Daemon |
| 33 | +The daemon hosts the secure IPC socket. Run it in the foreground to monitor logs and verify the socket path. |
| 34 | + |
| 35 | +```bash |
| 36 | +# Start daemon in foreground |
| 37 | +cargo run -- daemon start --foreground |
| 38 | +``` |
| 39 | + |
| 40 | +**Note:** Look for the log line indicating the socket path: |
| 41 | +`INFO BridgeManager listening on .../bridge.sock` |
| 42 | + |
| 43 | +On **macOS**, this path depends on your `TMPDIR` and typically looks like: |
| 44 | +`/var/folders/xx/xxxx/T/localgpt-501/bridge.sock` |
| 45 | + |
| 46 | +### 3. Run the Bridge Client |
| 47 | +In a **new terminal**, run the test client. It will connect to the daemon, authenticate, and request the secret for "test-bridge". |
| 48 | + |
| 49 | +**macOS / Linux:** |
| 50 | +```bash |
| 51 | +# Replace <SOCKET_PATH> with the full path from the daemon logs |
| 52 | +# Example: /var/folders/.../T/localgpt-501/bridge.sock |
| 53 | +cargo run -p localgpt-bridge --bin test_client -- --socket "<SOCKET_PATH>" --bridge-id test-bridge |
| 54 | +``` |
| 55 | + |
| 56 | +**Windows:** |
| 57 | +```bash |
| 58 | +# On Windows, the socket is a named pipe |
| 59 | +cargo run -p localgpt-bridge --bin test_client -- --socket "localgpt-bridge" --bridge-id test-bridge |
| 60 | +``` |
| 61 | + |
| 62 | +### Expected Output |
| 63 | + |
| 64 | +**Client Terminal:** |
| 65 | +```text |
| 66 | +INFO Connecting to bridge socket at: ... |
| 67 | +INFO Requesting credentials for: test-bridge |
| 68 | +INFO Successfully retrieved credentials! |
| 69 | +INFO Secret length: 22 bytes |
| 70 | +INFO Secret content (utf8): super-secret-token-123 |
| 71 | +``` |
| 72 | + |
| 73 | +**Daemon Terminal:** |
| 74 | +```text |
| 75 | +INFO Accepted connection from: PeerIdentity { uid: Some(501), ... } |
| 76 | +``` |
| 77 | + |
| 78 | +## Developing a New Bridge |
| 79 | + |
| 80 | +To create a new bridge (e.g., `localgpt-bridge-telegram`): |
| 81 | + |
| 82 | +1. **New Binary**: Create a new crate or binary target in `crates/bridge/src/bin/`. |
| 83 | +2. **Dependencies**: Depend on `localgpt-bridge` and `tarpc`. |
| 84 | +3. **Connect**: Use `localgpt_bridge::connect(socket_path)` to establish the secure channel. |
| 85 | +4. **Authenticate**: Call `client.get_credentials(context, "my-bridge-id")`. |
| 86 | +5. **Run**: Initialize your service (e.g., Telegram bot) using the retrieved secret. |
| 87 | + |
| 88 | +### Example Code |
| 89 | + |
| 90 | +```rust |
| 91 | +use localgpt_bridge::connect; |
| 92 | +use tarpc::context; |
| 93 | + |
| 94 | +#[tokio::main] |
| 95 | +async fn main() -> anyhow::Result<()> { |
| 96 | + // 1. Connect |
| 97 | + let socket = "/path/to/socket"; |
| 98 | + let client = connect(socket).await?; |
| 99 | + |
| 100 | + // 2. Fetch Secret |
| 101 | + let secret_bytes = client.get_credentials(context::current(), "telegram".to_string()).await??; |
| 102 | + let token = String::from_utf8(secret_bytes)?; |
| 103 | + |
| 104 | + // 3. Start Bot |
| 105 | + start_telegram_bot(&token).await?; |
| 106 | + |
| 107 | + Ok(()) |
| 108 | +} |
| 109 | +``` |
0 commit comments