|
1 | | -# Connecting OpenCLI via CDP (Remote/Headless Servers) |
| 1 | +# Connecting OpenCLI via CDP |
2 | 2 |
|
3 | | -If you cannot use the opencli Browser Bridge extension (e.g., in a remote headless server environment without a UI), OpenCLI provides an alternative: connecting directly to Chrome via **CDP (Chrome DevTools Protocol)**. |
| 3 | +If you cannot or do not want to use the opencli Browser Bridge extension, OpenCLI can also connect directly to a Chrome/Chromium debugging endpoint via **CDP (Chrome DevTools Protocol)**. |
4 | 4 |
|
5 | | -Because CDP binds to `localhost` by default for security reasons, accessing it from a remote server requires an additional networking tunnel. |
| 5 | +OpenCLI now provides a dedicated `browser` command group for this workflow, so you do not have to manage everything manually through environment variables and raw Chrome commands. |
6 | 6 |
|
7 | | -This guide is broken down into three phases: |
8 | | -1. **Preparation**: Start Chrome with CDP enabled locally. |
9 | | -2. **Network Tunnels**: Expose that CDP port to your remote server using either **SSH Tunnels** or **Reverse Proxies**. |
10 | | -3. **Execution**: Run OpenCLI on your server. |
| 7 | +This guide covers two common modes: |
| 8 | + |
| 9 | +1. **Local CDP browser managed by OpenCLI** |
| 10 | +2. **Remote or headless CDP endpoint managed outside OpenCLI** |
11 | 11 |
|
12 | 12 | --- |
13 | 13 |
|
14 | | -## Phase 1: Preparation (Local Machine) |
| 14 | +## Local CDP Workflow with `opencli browser` |
15 | 15 |
|
16 | | -First, you need to start a Chrome browser on your local machine with remote debugging enabled. |
| 16 | +For most local workflows, start with the built-in browser commands: |
17 | 17 |
|
18 | | -**macOS:** |
19 | 18 | ```bash |
20 | | -/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \ |
21 | | - --remote-debugging-port=9222 \ |
22 | | - --user-data-dir="$HOME/chrome-debug-profile" \ |
23 | | - --remote-allow-origins="*" |
| 19 | +opencli browser launch --port 9222 |
| 20 | +opencli browser list |
| 21 | +opencli browser doctor --backend cdp --cdp-endpoint http://127.0.0.1:9222 --live |
| 22 | +opencli browser run --backend cdp --cdp-endpoint http://127.0.0.1:9222 -- zhihu search --keyword AI |
| 23 | +opencli browser stop --port 9222 |
24 | 24 | ``` |
25 | 25 |
|
26 | | -**Linux:** |
| 26 | +### Temporary vs Persistent Profiles |
| 27 | + |
| 28 | +By default, `opencli browser launch` creates a **temporary profile**: |
| 29 | + |
27 | 30 | ```bash |
28 | | -google-chrome \ |
29 | | - --remote-debugging-port=9222 \ |
30 | | - --user-data-dir="$HOME/chrome-debug-profile" \ |
31 | | - --remote-allow-origins="*" |
| 31 | +opencli browser launch --port 9222 |
32 | 32 | ``` |
33 | 33 |
|
34 | | -**Windows:** |
35 | | -```cmd |
36 | | -"C:\Program Files\Google\Chrome\Application\chrome.exe" ^ |
37 | | - --remote-debugging-port=9222 ^ |
38 | | - --user-data-dir="%USERPROFILE%\chrome-debug-profile" ^ |
39 | | - --remote-allow-origins="*" |
| 34 | +If you want to preserve login state or browser data, use a named persistent profile: |
| 35 | + |
| 36 | +```bash |
| 37 | +opencli browser launch --port 9222 --profile zhihu |
| 38 | +opencli browser profiles |
40 | 39 | ``` |
41 | 40 |
|
42 | | -> **Note**: The `--remote-allow-origins="*"` flag is often required for modern Chrome versions to accept cross-origin CDP WebSocket connections (e.g. from reverse proxies like ngrok). |
| 41 | +You can later reuse the same profile on a different port: |
43 | 42 |
|
44 | | -Once this browser instance opens, **log into the target websites you want to use** (e.g., bilibili.com, zhihu.com) so that the session contains the correct cookies. |
| 43 | +```bash |
| 44 | +opencli browser stop --port 9222 |
| 45 | +opencli browser launch --port 9339 --profile zhihu |
| 46 | +``` |
45 | 47 |
|
46 | | ---- |
| 48 | +### Managing Profiles |
47 | 49 |
|
48 | | -## Phase 2: Remote Access Methods |
| 50 | +List persistent and temporary profiles: |
49 | 51 |
|
50 | | -Once CDP is running locally on port `9222`, you must securely expose this port to your remote server. Choose one of the two methods below depending on your network conditions. |
| 52 | +```bash |
| 53 | +opencli browser profiles |
| 54 | +``` |
51 | 55 |
|
52 | | -### Method A: SSH Tunnel (Recommended) |
| 56 | +Remove a named persistent profile: |
53 | 57 |
|
54 | | -If your local machine has SSH access to the remote server, this is the most secure and straightforward method. |
| 58 | +```bash |
| 59 | +opencli browser profiles rm zhihu |
| 60 | +``` |
55 | 61 |
|
56 | | -Run this command on your **Local Machine** to forward the remote server's port `9222` back to your local port `9222`: |
| 62 | +Remove unused temporary profiles: |
57 | 63 |
|
58 | 64 | ```bash |
59 | | -ssh -R 9222:localhost:9222 your-server-user@your-server-ip |
| 65 | +opencli browser profiles prune --temporary |
60 | 66 | ``` |
61 | 67 |
|
62 | | -Leave this SSH session running in the background. |
| 68 | +### Passing Raw Chrome Flags |
| 69 | + |
| 70 | +If you need additional native Chrome/Chromium launch flags, repeat `--browser-arg`: |
| 71 | + |
| 72 | +```bash |
| 73 | +opencli browser launch \ |
| 74 | + --port 9222 \ |
| 75 | + --profile zhihu \ |
| 76 | + --browser-arg=--lang=en-US \ |
| 77 | + --browser-arg=--window-size=1440,900 |
| 78 | +``` |
| 79 | + |
| 80 | +This is useful for window sizing, language overrides, proxies, and other Chromium flags that OpenCLI does not expose as first-class options. |
| 81 | + |
| 82 | +--- |
63 | 83 |
|
64 | | -### Method B: Reverse Proxy (ngrok / frp / socat) |
| 84 | +## Remote or Headless CDP Endpoints |
65 | 85 |
|
66 | | -If you cannot establish a direct SSH connection (e.g., due to NAT or firewalls), you can use an intranet penetration tool like `ngrok`. |
| 86 | +If Chrome is already running elsewhere and exposing a CDP endpoint, you can connect OpenCLI directly without using `opencli browser launch`. |
67 | 87 |
|
68 | | -Run this command on your **Local Machine** to expose your local port `9222` to the public internet securely via ngrok: |
| 88 | +Typical examples: |
| 89 | + |
| 90 | +- a remote Linux server running headless Chrome |
| 91 | +- a manually started local Chrome instance |
| 92 | +- a tunneled CDP endpoint exposed through SSH or ngrok |
| 93 | + |
| 94 | +You can either pass the endpoint explicitly: |
69 | 95 |
|
70 | 96 | ```bash |
71 | | -ngrok http 9222 |
| 97 | +opencli browser doctor --backend cdp --cdp-endpoint http://127.0.0.1:9222 --live |
| 98 | +opencli browser run --backend cdp --cdp-endpoint http://127.0.0.1:9222 -- bilibili hot --limit 5 |
| 99 | +``` |
| 100 | + |
| 101 | +Or export it once: |
| 102 | + |
| 103 | +```bash |
| 104 | +export OPENCLI_CDP_ENDPOINT="http://127.0.0.1:9222" |
| 105 | +opencli doctor |
| 106 | +opencli bilibili hot --limit 5 |
72 | 107 | ``` |
73 | 108 |
|
74 | | -This will print a forwarding URL, such as `https://abcdef.ngrok.app`. **Copy this URL**. |
| 109 | +> Tip: If you provide a standard HTTP/HTTPS CDP endpoint, OpenCLI requests the `/json` target list and picks the most likely inspectable app/page target automatically. If multiple targets exist, narrow selection with `OPENCLI_CDP_TARGET`. |
75 | 110 |
|
76 | 111 | --- |
77 | 112 |
|
78 | | -## Phase 3: Execution (Remote Server) |
| 113 | +## Exposing a Local CDP Port to a Remote Server |
79 | 114 |
|
80 | | -Now switch to your **Remote Server** where OpenCLI is installed. |
| 115 | +Because CDP binds to `localhost` by default, a remote machine usually cannot access it directly. Use one of these patterns if OpenCLI runs on a different machine than the browser. |
81 | 116 |
|
82 | | -Depending on the network tunnel method you chose in Phase 2, set the `OPENCLI_CDP_ENDPOINT` environment variable and run your commands. |
| 117 | +### Method A: SSH Reverse Tunnel |
83 | 118 |
|
84 | | -### If you used Method A (SSH Tunnel): |
| 119 | +Run this on your **local machine**: |
| 120 | + |
| 121 | +```bash |
| 122 | +ssh -R 9222:localhost:9222 your-server-user@your-server-ip |
| 123 | +``` |
| 124 | + |
| 125 | +Then on the **remote server**: |
85 | 126 |
|
86 | 127 | ```bash |
87 | 128 | export OPENCLI_CDP_ENDPOINT="http://localhost:9222" |
88 | | -opencli doctor # Verify connection |
89 | | -opencli bilibili hot --limit 5 # Test a command |
| 129 | +opencli doctor |
| 130 | +opencli bilibili hot --limit 5 |
90 | 131 | ``` |
91 | 132 |
|
92 | | -### If you used Method B (Reverse Proxy like ngrok): |
| 133 | +### Method B: Reverse Proxy or Tunnel Tool |
| 134 | + |
| 135 | +For example with `ngrok` on your **local machine**: |
| 136 | + |
| 137 | +```bash |
| 138 | +ngrok http 9222 |
| 139 | +``` |
| 140 | + |
| 141 | +Then on the **remote server**: |
93 | 142 |
|
94 | 143 | ```bash |
95 | | -# Use the URL you copied from ngrok earlier |
96 | 144 | export OPENCLI_CDP_ENDPOINT="https://abcdef.ngrok.app" |
97 | | -opencli doctor # Verify connection |
98 | | -opencli bilibili hot --limit 5 # Test a command |
| 145 | +opencli doctor |
| 146 | +opencli bilibili hot --limit 5 |
| 147 | +``` |
| 148 | + |
| 149 | +> Note: Some Chrome versions may require `--remote-allow-origins="*"` when CDP is accessed through reverse proxies or other cross-origin WebSocket paths. |
| 150 | +
|
| 151 | +--- |
| 152 | + |
| 153 | +## Starting Chrome Manually |
| 154 | + |
| 155 | +If you still prefer to start Chrome yourself instead of using `opencli browser launch`, these commands work. |
| 156 | + |
| 157 | +**macOS** |
| 158 | + |
| 159 | +```bash |
| 160 | +/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \ |
| 161 | + --remote-debugging-port=9222 \ |
| 162 | + --user-data-dir="$HOME/chrome-debug-profile" \ |
| 163 | + --remote-allow-origins="*" |
99 | 164 | ``` |
100 | 165 |
|
101 | | -> *Tip: If you provide a standard HTTP/HTTPS CDP endpoint, OpenCLI requests the `/json` target list and picks the most likely inspectable app/page target automatically. If multiple app targets exist, you can further narrow selection with `OPENCLI_CDP_TARGET` (for example `antigravity` or `codex`).* |
| 166 | +**Linux** |
| 167 | + |
| 168 | +```bash |
| 169 | +google-chrome \ |
| 170 | + --remote-debugging-port=9222 \ |
| 171 | + --user-data-dir="$HOME/chrome-debug-profile" \ |
| 172 | + --remote-allow-origins="*" |
| 173 | +``` |
| 174 | + |
| 175 | +**Windows** |
| 176 | + |
| 177 | +```cmd |
| 178 | +"C:\Program Files\Google\Chrome\Application\chrome.exe" ^ |
| 179 | + --remote-debugging-port=9222 ^ |
| 180 | + --user-data-dir="%USERPROFILE%\chrome-debug-profile" ^ |
| 181 | + --remote-allow-origins="*" |
| 182 | +``` |
| 183 | + |
| 184 | +Once the browser is open, log into the target websites you want OpenCLI to reuse. |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +## Recommended Workflow |
| 189 | + |
| 190 | +Use this order of operations for most CDP tasks: |
| 191 | + |
| 192 | +1. Start or discover a CDP browser |
| 193 | +2. Verify connectivity with `opencli browser doctor` |
| 194 | +3. Run existing site commands through `opencli browser run` |
| 195 | +4. Stop the browser with `opencli browser stop` |
| 196 | +5. Manage profiles with `opencli browser profiles` |
102 | 197 |
|
103 | | -If you plan to use this setup frequently, you can persist the environment variable by adding the `export` line to your `~/.bashrc` or `~/.zshrc` on the server. |
| 198 | +That keeps the CDP path explicit, inspectable, and scriptable for both humans and AI agents. |
0 commit comments