labtool-cf 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.

Potentially problematic release.


This version of labtool-cf might be problematic. Click here for more details.

@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Amirhossein Ziaee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,233 @@
1
+ Metadata-Version: 2.4
2
+ Name: labtool-cf
3
+ Version: 0.1.0
4
+ Summary: Manage a Cloudflare Tunnel for machines behind NAT
5
+ Author-email: Your Name <you@example.com>
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/afourteene/labtool
8
+ Keywords: cloudflare,tunnel,cloudflared,cli
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Environment :: Console
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.10
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: click>=8.1
16
+ Requires-Dist: pyyaml>=6.0
17
+ Provides-Extra: dev
18
+ Requires-Dist: pytest>=8.0; extra == "dev"
19
+ Requires-Dist: build; extra == "dev"
20
+ Requires-Dist: twine; extra == "dev"
21
+ Dynamic: license-file
22
+
23
+ # labtool
24
+
25
+ A small CLI that sets up and manages a Cloudflare Tunnel (`cloudflared`) for machines behind NAT, so they can be reached over the internet without opening any ports.
26
+
27
+ Everything is described in one file, `lab.yaml`. The tool turns it into a tunnel, DNS records and a `cloudflared` `config.yml`.
28
+
29
+ ## Requirements
30
+
31
+ - Python 3.10+
32
+ - A domain managed by Cloudflare (status: Active)
33
+ - A Cloudflare account (used once for `labtool login`)
34
+ - `cloudflared` (the tool can install it for you: `labtool install`)
35
+
36
+ ## Using your domain with Cloudflare
37
+
38
+ `labtool` creates DNS records such as `node1.example.com`, so your domain must be managed by Cloudflare (its nameservers must point to Cloudflare). You only do this once per domain.
39
+
40
+ ### Option A: you already own a domain at another registrar
41
+
42
+ 1. Create a free account at [dash.cloudflare.com](https://dash.cloudflare.com).
43
+ 2. Go to **Domains** and click **Add domain**. Enter your domain (for example `example.com`) and choose the **Free** plan.
44
+ 3. Cloudflare scans your existing DNS records. Review them and continue. Keep any records you still need (website, mail).
45
+ 4. Cloudflare shows you two nameservers, for example `xxx.ns.cloudflare.com` and `yyy.ns.cloudflare.com`.
46
+ 5. Log in to your registrar (where you bought the domain), open the domain's nameserver settings, and replace the current nameservers with the two from Cloudflare.
47
+ 6. Wait until the domain shows **Active** in the Cloudflare dashboard. This usually takes minutes, but can take up to 24 hours.
48
+
49
+ Check from the terminal:
50
+
51
+ ```bash
52
+ dig +short NS example.com
53
+ ```
54
+
55
+ The output should list the two `*.ns.cloudflare.com` servers.
56
+
57
+ ### Option B: buy the domain from Cloudflare
58
+
59
+ In the dashboard choose **Domains**, then **Buy domain**. A domain bought there is managed by Cloudflare right away; there is nothing to move.
60
+
61
+ ### Authorize labtool for that domain
62
+
63
+ Run:
64
+
65
+ ```bash
66
+ labtool login
67
+ ```
68
+
69
+ Open the printed URL, log in, and **select your domain** on the authorization page. Cloudflare saves a `cert.pem` that lets `labtool` create tunnels and DNS records for that domain only. If you own several domains, pick the one set as `domain:` in `lab.yaml`.
70
+
71
+ ### What `labtool init` does to your DNS
72
+
73
+ For every node in `lab.yaml` it creates a proxied `CNAME` record that points to the tunnel:
74
+
75
+ | Record | Type | Points to |
76
+ |---|---|---|
77
+ | `node1.example.com` | CNAME (proxied) | `<TUNNEL_ID>.cfargotunnel.com` |
78
+
79
+ Notes:
80
+
81
+ - No server IP is stored in DNS, and no port has to be opened on your machines.
82
+ - If a record with the same name already exists, `init` overwrites it. Do not reuse a hostname that serves something else.
83
+ - To use a subdomain only for this tool, set `domain: lab.example.com` in `lab.yaml`; nodes then become `node1.lab.example.com`.
84
+ - After `labtool delete`, the CNAME records stay in DNS. Remove them in the dashboard under **DNS > Records**.
85
+ - The free plan is enough. No payment method is needed for DNS and tunnels.
86
+
87
+ ### Troubleshooting the domain
88
+
89
+ | Problem | Fix |
90
+ |---|---|
91
+ | Domain stays **Pending** | Nameservers at the registrar are not changed yet, or have not propagated. Re-check step 5 and wait. |
92
+ | `labtool init` cannot create DNS records | The domain is not Active, or you selected a different domain during `labtool login`. Delete `~/.cloudflared/cert.pem` and run `labtool login` again. |
93
+ | `node1.example.com` does not resolve | The record is missing. Run `labtool init` again and check the dashboard under **DNS > Records**. |
94
+
95
+ ## Installation
96
+
97
+ From the project root (the folder containing `pyproject.toml`):
98
+
99
+ ```bash
100
+ python3 -m venv venv
101
+ source venv/bin/activate
102
+ pip install -e .
103
+ labtool -h
104
+ ```
105
+
106
+ For development (tests):
107
+
108
+ ```bash
109
+ pip install pytest
110
+ pytest -v
111
+ ```
112
+
113
+ You can also run it as a module: `python -m labtool -h`.
114
+
115
+ ## Configuration: `lab.yaml`
116
+
117
+ ```yaml
118
+ domain: example.com # a domain active on Cloudflare
119
+ tunnel_name: my-tunnel # name of the tunnel
120
+ nodes:
121
+ - name: node1 # becomes node1.example.com
122
+ ip: 192.168.56.11 # address the tunnel machine can reach
123
+ - name: node2
124
+ ip: 192.168.56.12
125
+ ```
126
+
127
+ Each node is exposed as `ssh://<ip>:22` under `<name>.<domain>`. Run commands from the folder that contains `lab.yaml`, or pass `-f path/to/lab.yaml`.
128
+
129
+ ## Quick start
130
+
131
+ ```bash
132
+ labtool install # 1. install cloudflared for this OS
133
+ labtool login # 2. authorize with Cloudflare (creates cert.pem)
134
+ labtool init # 3. create tunnel + DNS records + config.yml
135
+ labtool up # 4. run the tunnel in the background
136
+ labtool status # 5. check it
137
+ labtool down # 6. stop it
138
+ ```
139
+
140
+ ## How it works
141
+
142
+ 1. **You describe the lab** in `lab.yaml`: a domain, a tunnel name and a list of nodes (name + IP).
143
+ 2. **`labtool init`** talks to Cloudflare through the `cloudflared` binary. It creates the tunnel (once), adds a DNS record `<node>.<domain>` for every node, and writes a `config.yml` whose `ingress` rules map each hostname to `ssh://<ip>:22`. A last catch-all rule (`http_status:404`) is added automatically, because `cloudflared` requires it.
144
+ 3. **`labtool up`** starts `cloudflared tunnel run` in the background and remembers its PID. The tunnel connects *outward* to Cloudflare, so no inbound port is opened.
145
+ 4. **A client** connects to `node1.example.com`. The request reaches Cloudflare, travels through the tunnel to your machine, and is forwarded to the node's IP.
146
+ 5. **`labtool down`** stops the process; the hostnames stop working until you run `up` again.
147
+
148
+ ```
149
+ client ──ssh──► Cloudflare ──► tunnel (cloudflared on your machine) ──► node IP:22
150
+ ```
151
+
152
+ ## Commands
153
+
154
+ Every command supports `-h` / `--help`. `labtool -v` prints the version.
155
+
156
+ | Command | What it does |
157
+ |---|---|
158
+ | `install` | Installs `cloudflared` (Homebrew on macOS, `.deb` or binary on Linux, `winget` on Windows). Skips if already installed. |
159
+ | `login` | Runs `cloudflared tunnel login`. Prints a URL; open it in any browser and click Authorize. Skips if `cert.pem` already exists. |
160
+ | `init` | Logs in if needed, creates the tunnel if missing, routes DNS for every node, and writes `~/.cloudflared/config.yml`. This is the only command that creates the tunnel. |
161
+ | `up` | Starts `cloudflared tunnel run` in the background. Does nothing if already running. |
162
+ | `down` | Stops the running tunnel. |
163
+ | `status` | Shows whether the tunnel is running (and its PID). |
164
+ | `delete` | Stops and deletes the tunnel from Cloudflare. Asks for confirmation; `-y` skips the prompt. Irreversible. |
165
+
166
+ Common option: `-f, --file PATH` (default `lab.yaml`).
167
+
168
+ All commands are safe to run more than once (idempotent).
169
+
170
+ ## Files used
171
+
172
+ | Path | Purpose |
173
+ |---|---|
174
+ | `~/.cloudflared/cert.pem` | Result of `login`. Admin access to your domain; treat it like a password. |
175
+ | `~/.cloudflared/<TUNNEL_ID>.json` | Tunnel credentials, needed on any machine that runs the tunnel. |
176
+ | `~/.cloudflared/config.yml` | Generated by `init`. |
177
+ | `~/.labtool/tunnel.pid` | PID of the background tunnel. |
178
+ | `~/.labtool/tunnel.log` | `cloudflared` output (check here if `up` fails). |
179
+
180
+ ## Connecting to a node
181
+
182
+ Install `cloudflared` on the client machine, then:
183
+
184
+ ```bash
185
+ ssh -i ~/.ssh/id_rsa \
186
+ -o ProxyCommand="cloudflared access ssh --hostname %h" \
187
+ user@node1.example.com
188
+ ```
189
+
190
+ ## Running the tunnel on another machine
191
+
192
+ - `cert.pem` is only needed for management commands (`init`, `delete`). Running a tunnel (`up`) needs only `<TUNNEL_ID>.json` and `config.yml`.
193
+ - The credentials file exists only on the machine where the tunnel was created. Either copy it to the other machine, or set a different `tunnel_name` in that machine's `lab.yaml` and run `labtool init` there to create a separate tunnel.
194
+ - Do not run the same tunnel on two machines at once unless you want traffic split between them. Stop one with `labtool down` (or `Ctrl+C`).
195
+ - `init` overwrites existing DNS records, so they will point to the tunnel you just initialised.
196
+
197
+ ## Troubleshooting
198
+
199
+ | Message | Cause and fix |
200
+ |---|---|
201
+ | `Cannot determine default origin certificate path` | No `cert.pem`. Run `labtool login`. |
202
+ | `Tunnel credentials file ... doesn't exist` | Credentials are missing on this machine. Copy the `.json`, or create a tunnel here. |
203
+ | `cloudflared exited immediately` | Check `~/.labtool/tunnel.log` (bad config or missing credentials). |
204
+ | `You have an existing certificate ... which login would overwrite` | You are already logged in. Nothing to do. |
205
+ | `lab.yaml is empty or invalid` | The file is empty or has the wrong format. |
206
+ | `No module named labtool.cli` | `cli.py` must be in `src/labtool/`; then run `pip install -e .` again. |
207
+
208
+ `Ctrl+C` cancels any command cleanly (exit code 130).
209
+
210
+ ## Project layout
211
+
212
+ ```
213
+ labtool/
214
+ ├── pyproject.toml
215
+ ├── lab.yaml
216
+ ├── src/labtool/
217
+ │ ├── cli.py # click commands only (wiring)
218
+ │ ├── ui.py # colored output
219
+ │ ├── services.py # use cases: init, up, down, delete, ...
220
+ │ ├── domain.py # models and pure rules, no I/O
221
+ │ ├── config_builder.py # pure function: Lab -> tunnel config
222
+ │ ├── ports.py # interfaces
223
+ │ └── adapters/ # cloudflared, process runner, system, lab file
224
+ └── tests/ # run with pytest, using fakes (no Cloudflare needed)
225
+ ```
226
+
227
+ Dependencies point inward: `cli -> services -> domain`, and adapters implement the interfaces in `ports`. Services never import `click` or `subprocess`, which is why they can be tested with fakes.
228
+
229
+ ## Security notes
230
+
231
+ - Never commit `cert.pem`, `<TUNNEL_ID>.json`, SSH private keys, or tokens. Add them to `.gitignore` (`venv/`, `.labtool/`, `*.json`, `ssh-keys/`).
232
+ - Use key-only SSH on the nodes (`PasswordAuthentication no`).
233
+ - A tunnel hostname is public while the tunnel runs. Stop it with `labtool down` when you are not using it.
@@ -0,0 +1,211 @@
1
+ # labtool
2
+
3
+ A small CLI that sets up and manages a Cloudflare Tunnel (`cloudflared`) for machines behind NAT, so they can be reached over the internet without opening any ports.
4
+
5
+ Everything is described in one file, `lab.yaml`. The tool turns it into a tunnel, DNS records and a `cloudflared` `config.yml`.
6
+
7
+ ## Requirements
8
+
9
+ - Python 3.10+
10
+ - A domain managed by Cloudflare (status: Active)
11
+ - A Cloudflare account (used once for `labtool login`)
12
+ - `cloudflared` (the tool can install it for you: `labtool install`)
13
+
14
+ ## Using your domain with Cloudflare
15
+
16
+ `labtool` creates DNS records such as `node1.example.com`, so your domain must be managed by Cloudflare (its nameservers must point to Cloudflare). You only do this once per domain.
17
+
18
+ ### Option A: you already own a domain at another registrar
19
+
20
+ 1. Create a free account at [dash.cloudflare.com](https://dash.cloudflare.com).
21
+ 2. Go to **Domains** and click **Add domain**. Enter your domain (for example `example.com`) and choose the **Free** plan.
22
+ 3. Cloudflare scans your existing DNS records. Review them and continue. Keep any records you still need (website, mail).
23
+ 4. Cloudflare shows you two nameservers, for example `xxx.ns.cloudflare.com` and `yyy.ns.cloudflare.com`.
24
+ 5. Log in to your registrar (where you bought the domain), open the domain's nameserver settings, and replace the current nameservers with the two from Cloudflare.
25
+ 6. Wait until the domain shows **Active** in the Cloudflare dashboard. This usually takes minutes, but can take up to 24 hours.
26
+
27
+ Check from the terminal:
28
+
29
+ ```bash
30
+ dig +short NS example.com
31
+ ```
32
+
33
+ The output should list the two `*.ns.cloudflare.com` servers.
34
+
35
+ ### Option B: buy the domain from Cloudflare
36
+
37
+ In the dashboard choose **Domains**, then **Buy domain**. A domain bought there is managed by Cloudflare right away; there is nothing to move.
38
+
39
+ ### Authorize labtool for that domain
40
+
41
+ Run:
42
+
43
+ ```bash
44
+ labtool login
45
+ ```
46
+
47
+ Open the printed URL, log in, and **select your domain** on the authorization page. Cloudflare saves a `cert.pem` that lets `labtool` create tunnels and DNS records for that domain only. If you own several domains, pick the one set as `domain:` in `lab.yaml`.
48
+
49
+ ### What `labtool init` does to your DNS
50
+
51
+ For every node in `lab.yaml` it creates a proxied `CNAME` record that points to the tunnel:
52
+
53
+ | Record | Type | Points to |
54
+ |---|---|---|
55
+ | `node1.example.com` | CNAME (proxied) | `<TUNNEL_ID>.cfargotunnel.com` |
56
+
57
+ Notes:
58
+
59
+ - No server IP is stored in DNS, and no port has to be opened on your machines.
60
+ - If a record with the same name already exists, `init` overwrites it. Do not reuse a hostname that serves something else.
61
+ - To use a subdomain only for this tool, set `domain: lab.example.com` in `lab.yaml`; nodes then become `node1.lab.example.com`.
62
+ - After `labtool delete`, the CNAME records stay in DNS. Remove them in the dashboard under **DNS > Records**.
63
+ - The free plan is enough. No payment method is needed for DNS and tunnels.
64
+
65
+ ### Troubleshooting the domain
66
+
67
+ | Problem | Fix |
68
+ |---|---|
69
+ | Domain stays **Pending** | Nameservers at the registrar are not changed yet, or have not propagated. Re-check step 5 and wait. |
70
+ | `labtool init` cannot create DNS records | The domain is not Active, or you selected a different domain during `labtool login`. Delete `~/.cloudflared/cert.pem` and run `labtool login` again. |
71
+ | `node1.example.com` does not resolve | The record is missing. Run `labtool init` again and check the dashboard under **DNS > Records**. |
72
+
73
+ ## Installation
74
+
75
+ From the project root (the folder containing `pyproject.toml`):
76
+
77
+ ```bash
78
+ python3 -m venv venv
79
+ source venv/bin/activate
80
+ pip install -e .
81
+ labtool -h
82
+ ```
83
+
84
+ For development (tests):
85
+
86
+ ```bash
87
+ pip install pytest
88
+ pytest -v
89
+ ```
90
+
91
+ You can also run it as a module: `python -m labtool -h`.
92
+
93
+ ## Configuration: `lab.yaml`
94
+
95
+ ```yaml
96
+ domain: example.com # a domain active on Cloudflare
97
+ tunnel_name: my-tunnel # name of the tunnel
98
+ nodes:
99
+ - name: node1 # becomes node1.example.com
100
+ ip: 192.168.56.11 # address the tunnel machine can reach
101
+ - name: node2
102
+ ip: 192.168.56.12
103
+ ```
104
+
105
+ Each node is exposed as `ssh://<ip>:22` under `<name>.<domain>`. Run commands from the folder that contains `lab.yaml`, or pass `-f path/to/lab.yaml`.
106
+
107
+ ## Quick start
108
+
109
+ ```bash
110
+ labtool install # 1. install cloudflared for this OS
111
+ labtool login # 2. authorize with Cloudflare (creates cert.pem)
112
+ labtool init # 3. create tunnel + DNS records + config.yml
113
+ labtool up # 4. run the tunnel in the background
114
+ labtool status # 5. check it
115
+ labtool down # 6. stop it
116
+ ```
117
+
118
+ ## How it works
119
+
120
+ 1. **You describe the lab** in `lab.yaml`: a domain, a tunnel name and a list of nodes (name + IP).
121
+ 2. **`labtool init`** talks to Cloudflare through the `cloudflared` binary. It creates the tunnel (once), adds a DNS record `<node>.<domain>` for every node, and writes a `config.yml` whose `ingress` rules map each hostname to `ssh://<ip>:22`. A last catch-all rule (`http_status:404`) is added automatically, because `cloudflared` requires it.
122
+ 3. **`labtool up`** starts `cloudflared tunnel run` in the background and remembers its PID. The tunnel connects *outward* to Cloudflare, so no inbound port is opened.
123
+ 4. **A client** connects to `node1.example.com`. The request reaches Cloudflare, travels through the tunnel to your machine, and is forwarded to the node's IP.
124
+ 5. **`labtool down`** stops the process; the hostnames stop working until you run `up` again.
125
+
126
+ ```
127
+ client ──ssh──► Cloudflare ──► tunnel (cloudflared on your machine) ──► node IP:22
128
+ ```
129
+
130
+ ## Commands
131
+
132
+ Every command supports `-h` / `--help`. `labtool -v` prints the version.
133
+
134
+ | Command | What it does |
135
+ |---|---|
136
+ | `install` | Installs `cloudflared` (Homebrew on macOS, `.deb` or binary on Linux, `winget` on Windows). Skips if already installed. |
137
+ | `login` | Runs `cloudflared tunnel login`. Prints a URL; open it in any browser and click Authorize. Skips if `cert.pem` already exists. |
138
+ | `init` | Logs in if needed, creates the tunnel if missing, routes DNS for every node, and writes `~/.cloudflared/config.yml`. This is the only command that creates the tunnel. |
139
+ | `up` | Starts `cloudflared tunnel run` in the background. Does nothing if already running. |
140
+ | `down` | Stops the running tunnel. |
141
+ | `status` | Shows whether the tunnel is running (and its PID). |
142
+ | `delete` | Stops and deletes the tunnel from Cloudflare. Asks for confirmation; `-y` skips the prompt. Irreversible. |
143
+
144
+ Common option: `-f, --file PATH` (default `lab.yaml`).
145
+
146
+ All commands are safe to run more than once (idempotent).
147
+
148
+ ## Files used
149
+
150
+ | Path | Purpose |
151
+ |---|---|
152
+ | `~/.cloudflared/cert.pem` | Result of `login`. Admin access to your domain; treat it like a password. |
153
+ | `~/.cloudflared/<TUNNEL_ID>.json` | Tunnel credentials, needed on any machine that runs the tunnel. |
154
+ | `~/.cloudflared/config.yml` | Generated by `init`. |
155
+ | `~/.labtool/tunnel.pid` | PID of the background tunnel. |
156
+ | `~/.labtool/tunnel.log` | `cloudflared` output (check here if `up` fails). |
157
+
158
+ ## Connecting to a node
159
+
160
+ Install `cloudflared` on the client machine, then:
161
+
162
+ ```bash
163
+ ssh -i ~/.ssh/id_rsa \
164
+ -o ProxyCommand="cloudflared access ssh --hostname %h" \
165
+ user@node1.example.com
166
+ ```
167
+
168
+ ## Running the tunnel on another machine
169
+
170
+ - `cert.pem` is only needed for management commands (`init`, `delete`). Running a tunnel (`up`) needs only `<TUNNEL_ID>.json` and `config.yml`.
171
+ - The credentials file exists only on the machine where the tunnel was created. Either copy it to the other machine, or set a different `tunnel_name` in that machine's `lab.yaml` and run `labtool init` there to create a separate tunnel.
172
+ - Do not run the same tunnel on two machines at once unless you want traffic split between them. Stop one with `labtool down` (or `Ctrl+C`).
173
+ - `init` overwrites existing DNS records, so they will point to the tunnel you just initialised.
174
+
175
+ ## Troubleshooting
176
+
177
+ | Message | Cause and fix |
178
+ |---|---|
179
+ | `Cannot determine default origin certificate path` | No `cert.pem`. Run `labtool login`. |
180
+ | `Tunnel credentials file ... doesn't exist` | Credentials are missing on this machine. Copy the `.json`, or create a tunnel here. |
181
+ | `cloudflared exited immediately` | Check `~/.labtool/tunnel.log` (bad config or missing credentials). |
182
+ | `You have an existing certificate ... which login would overwrite` | You are already logged in. Nothing to do. |
183
+ | `lab.yaml is empty or invalid` | The file is empty or has the wrong format. |
184
+ | `No module named labtool.cli` | `cli.py` must be in `src/labtool/`; then run `pip install -e .` again. |
185
+
186
+ `Ctrl+C` cancels any command cleanly (exit code 130).
187
+
188
+ ## Project layout
189
+
190
+ ```
191
+ labtool/
192
+ ├── pyproject.toml
193
+ ├── lab.yaml
194
+ ├── src/labtool/
195
+ │ ├── cli.py # click commands only (wiring)
196
+ │ ├── ui.py # colored output
197
+ │ ├── services.py # use cases: init, up, down, delete, ...
198
+ │ ├── domain.py # models and pure rules, no I/O
199
+ │ ├── config_builder.py # pure function: Lab -> tunnel config
200
+ │ ├── ports.py # interfaces
201
+ │ └── adapters/ # cloudflared, process runner, system, lab file
202
+ └── tests/ # run with pytest, using fakes (no Cloudflare needed)
203
+ ```
204
+
205
+ Dependencies point inward: `cli -> services -> domain`, and adapters implement the interfaces in `ports`. Services never import `click` or `subprocess`, which is why they can be tested with fakes.
206
+
207
+ ## Security notes
208
+
209
+ - Never commit `cert.pem`, `<TUNNEL_ID>.json`, SSH private keys, or tokens. Add them to `.gitignore` (`venv/`, `.labtool/`, `*.json`, `ssh-keys/`).
210
+ - Use key-only SSH on the nodes (`PasswordAuthentication no`).
211
+ - A tunnel hostname is public while the tunnel runs. Stop it with `labtool down` when you are not using it.
@@ -0,0 +1,32 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "labtool-cf"
7
+ version = "0.1.0"
8
+ description = "Manage a Cloudflare Tunnel for machines behind NAT"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ license-files = ["LICENSE"]
12
+ requires-python = ">=3.10"
13
+ authors = [{ name = "Your Name", email = "you@example.com" }]
14
+ keywords = ["cloudflare", "tunnel", "cloudflared", "cli"]
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "Environment :: Console",
18
+ "Operating System :: OS Independent",
19
+ ]
20
+ dependencies = ["click>=8.1", "pyyaml>=6.0"]
21
+
22
+ [project.optional-dependencies]
23
+ dev = ["pytest>=8.0", "build", "twine"]
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/afourteene/labtool"
27
+
28
+ [project.scripts]
29
+ labtool = "labtool.cli:main"
30
+
31
+ [tool.setuptools.packages.find]
32
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ main()
File without changes
@@ -0,0 +1,67 @@
1
+ import json
2
+ import subprocess
3
+ from pathlib import Path
4
+
5
+ import yaml
6
+
7
+ from ..domain import LabError, Tunnel
8
+
9
+ CF_DIR = Path.home() / ".cloudflared"
10
+
11
+
12
+ def _run(cmd):
13
+ """Run a command; raise LabError with stderr on failure."""
14
+ r = subprocess.run(cmd, capture_output=True, text=True)
15
+ if r.returncode != 0:
16
+ raise LabError(f"{' '.join(cmd)}\n{r.stderr.strip()}")
17
+ return r.stdout
18
+
19
+
20
+ class CloudflaredGateway:
21
+ """Talks to Cloudflare through the cloudflared binary."""
22
+
23
+ def find(self, name):
24
+ """Return the Tunnel with this name, or None."""
25
+ out = _run(["cloudflared", "tunnel", "list", "--output", "json"])
26
+ for t in json.loads(out) or []:
27
+ if t["name"] == name:
28
+ return Tunnel(id=t["id"], name=t["name"])
29
+ return None
30
+
31
+ def create(self, name):
32
+ _run(["cloudflared", "tunnel", "create", name])
33
+ return self.find(name)
34
+
35
+ def route_dns(self, tunnel_name, hostname):
36
+ """Create or overwrite the CNAME record for hostname."""
37
+ _run(["cloudflared", "tunnel", "route", "dns",
38
+ "--overwrite-dns", tunnel_name, hostname])
39
+
40
+ def delete(self, name):
41
+ """Delete the tunnel; -f also cleans up stale connections."""
42
+ _run(["cloudflared", "tunnel", "delete", "-f", name])
43
+
44
+ def credentials_path(self, tunnel):
45
+ return str(CF_DIR / f"{tunnel.id}.json")
46
+
47
+ def is_logged_in(self):
48
+ """cert.pem exists only after `cloudflared tunnel login`."""
49
+ return (CF_DIR / "cert.pem").exists()
50
+
51
+ def login(self, log=print):
52
+ """Run the interactive login; the URL prints straight to the terminal."""
53
+ log("Open the displayed URL in your browser and click Authorize...")
54
+ r = subprocess.run(["cloudflared", "tunnel", "login"])
55
+ if r.returncode != 0:
56
+ raise LabError("Login failed.")
57
+
58
+
59
+ class YamlConfigStore:
60
+ """Writes the cloudflared config.yml file."""
61
+
62
+ def __init__(self, path=CF_DIR / "config.yml"):
63
+ self.path = path
64
+
65
+ def save(self, config):
66
+ # sort_keys=False: ingress order matters to cloudflared
67
+ self.path.write_text(yaml.safe_dump(config, sort_keys=False))
@@ -0,0 +1,80 @@
1
+ import yaml
2
+ import os
3
+ from pathlib import Path
4
+ import click
5
+ from ..domain import Lab, LabError, Node
6
+
7
+
8
+ APP_NAME = "labtool"
9
+
10
+ TEMPLATE = """\
11
+ # labtool configuration
12
+ domain: example.com # a domain that is Active on Cloudflare
13
+ tunnel_name: my-tunnel # name of the tunnel
14
+ nodes:
15
+ - name: node1 # becomes node1.example.com
16
+ ip: 192.168.56.11 # address the tunnel machine can reach
17
+ """
18
+
19
+
20
+ def user_config_path():
21
+ """Per-user config location (differs per OS)."""
22
+ return Path(click.get_app_dir(APP_NAME)) / "lab.yaml"
23
+
24
+
25
+ def resolve_lab_path(explicit=None):
26
+ """Find lab.yaml: -f flag, env var, current dir, then user config dir."""
27
+ if explicit:
28
+ return Path(explicit)
29
+
30
+ env = os.environ.get("LABTOOL_FILE")
31
+ if env:
32
+ return Path(env)
33
+
34
+ for candidate in (Path("lab.yaml"), user_config_path()):
35
+ if candidate.exists():
36
+ return candidate
37
+
38
+ raise LabError(
39
+ "lab.yaml not found. Create one with: labtool config init\n"
40
+ f"(or pass -f PATH, or set LABTOOL_FILE). Searched: ./lab.yaml, {user_config_path()}"
41
+ )
42
+
43
+
44
+ def write_template(path, force=False):
45
+ """Write the template; refuse to overwrite unless force=True."""
46
+ path = Path(path)
47
+ if path.exists() and not force:
48
+ raise LabError(f"{path} already exists (use --force to overwrite).")
49
+ path.parent.mkdir(parents=True, exist_ok=True)
50
+ path.write_text(TEMPLATE)
51
+ return path
52
+
53
+
54
+ def load_lab(path):
55
+ """Read and validate the lab file; raise LabError with a clear message."""
56
+ path = Path(path)
57
+ try:
58
+ raw = yaml.safe_load(path.read_text())
59
+ except FileNotFoundError:
60
+ raise LabError(f"File {path} not found")
61
+ except yaml.YAMLError as e:
62
+ raise LabError(f"{path} is not valid YAML: {e}")
63
+
64
+ if not isinstance(raw, dict):
65
+ raise LabError(f"{path} is empty or has the wrong format")
66
+
67
+ for key in ("domain", "tunnel_name", "nodes"):
68
+ if key not in raw:
69
+ raise LabError(f"{path}: missing required key '{key}'")
70
+
71
+ if not isinstance(raw["nodes"], list) or not raw["nodes"]:
72
+ raise LabError(f"{path}: 'nodes' must be a non-empty list")
73
+
74
+ try:
75
+ nodes = tuple(Node(**n) for n in raw["nodes"])
76
+ return Lab(domain=raw["domain"], tunnel_name=raw["tunnel_name"], nodes=nodes)
77
+ except (TypeError, ValueError) as e:
78
+ # TypeError: unknown/missing node field, ValueError: invalid IP, duplicate names
79
+ raise LabError(f"{path}: invalid node definition: {e}")
80
+