localghost 4.2.0__tar.gz → 4.3.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.
Files changed (79) hide show
  1. {localghost-4.2.0 → localghost-4.3.0}/PKG-INFO +5 -4
  2. {localghost-4.2.0 → localghost-4.3.0}/README.md +4 -3
  3. {localghost-4.2.0 → localghost-4.3.0}/pyproject.toml +1 -1
  4. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/cli.py +43 -20
  5. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/feedback.py +21 -6
  6. localghost-4.3.0/src/localghost/html_server.js +29 -0
  7. localghost-4.3.0/src/localghost/html_server.py +207 -0
  8. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/runner.py +25 -1
  9. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale.py +35 -14
  10. {localghost-4.2.0 → localghost-4.3.0}/LICENSE +0 -0
  11. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/.dockerignore +0 -0
  12. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/Dockerfile +0 -0
  13. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/__init__.py +0 -0
  14. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/compose.py +0 -0
  15. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/config.py +0 -0
  16. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/detached.py +0 -0
  17. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/forwarder.py +0 -0
  18. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/generator.py +0 -0
  19. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/init.py +0 -0
  20. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/paths.py +0 -0
  21. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/picker.py +0 -0
  22. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/ports.py +0 -0
  23. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/proxy_compose.yaml +0 -0
  24. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/proxy_compose_https.yaml +0 -0
  25. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/proxy_compose_tailscale.yaml +0 -0
  26. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/proxy_compose_tailscale_https.yaml +0 -0
  27. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/registry.py +0 -0
  28. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/roots.py +0 -0
  29. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/routes.py +0 -0
  30. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/sessions.py +0 -0
  31. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/statusbar.py +0 -0
  32. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/Dockerfile +0 -0
  33. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/go.mod +0 -0
  34. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/go.sum +0 -0
  35. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/identity.go +0 -0
  36. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/identity_test.go +0 -0
  37. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/main.go +0 -0
  38. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/main_test.go +0 -0
  39. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/tailscale_gateway/proxy_test.go +0 -0
  40. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/.traefik.yml +0 -0
  41. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/ca.go +0 -0
  42. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/ca_test.go +0 -0
  43. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/cmd/bootstrap/main.go +0 -0
  44. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/docker.go +0 -0
  45. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/docker_test.go +0 -0
  46. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/go.mod +0 -0
  47. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/go.sum +0 -0
  48. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/provider.go +0 -0
  49. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/provider_test.go +0 -0
  50. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/storage.go +0 -0
  51. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/LICENSE +0 -0
  52. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/config.go +0 -0
  53. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/http_config.go +0 -0
  54. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/marshaler.go +0 -0
  55. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/middlewares.go +0 -0
  56. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/plugins.go +0 -0
  57. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/tcp_config.go +0 -0
  58. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/tcp_middlewares.go +0 -0
  59. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/tls/certificate.go +0 -0
  60. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/tls/tls.go +0 -0
  61. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/types/domains.go +0 -0
  62. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/types/tls.go +0 -0
  63. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/github.com/traefik/genconf/dynamic/udp_config.go +0 -0
  64. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-ca/vendor/modules.txt +0 -0
  65. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-client/.traefik.yml +0 -0
  66. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-client/client.go +0 -0
  67. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-client/client_test.go +0 -0
  68. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-client/go.mod +0 -0
  69. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-client/identity.go +0 -0
  70. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-client/identity_test.go +0 -0
  71. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-fallback/.traefik.yml +0 -0
  72. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-fallback/fallback.go +0 -0
  73. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-fallback/fallback_test.go +0 -0
  74. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-fallback/go.mod +0 -0
  75. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-fallback/logo.go +0 -0
  76. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-tailnet-ca/.traefik.yml +0 -0
  77. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-tailnet-ca/go.mod +0 -0
  78. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/traefik_plugin/src/github.com/SmileyChris/traefik-localghost-tailnet-ca/provider.go +0 -0
  79. {localghost-4.2.0 → localghost-4.3.0}/src/localghost/trust.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: localghost
3
- Version: 4.2.0
3
+ Version: 4.3.0
4
4
  Summary: Localghost serves local applications at friendly .localhost URLs
5
5
  Keywords: localghost,localhost,docker,compose,traefik,development,proxy
6
6
  Author: SmileyChris
@@ -73,9 +73,10 @@ port 80 available. From an application directory, run:
73
73
  uvx localghost run
74
74
  ```
75
75
 
76
- Localghost detects Django, Vite, Astro, CakePHP, Laravel, plain PHP, and Docker
77
- Compose projects, starts the shared hub when needed, and keeps the local URL
78
- visible while the application runs. Press Ctrl+C to stop the application.
76
+ Localghost detects Django, Vite, Astro, CakePHP, Laravel, plain PHP, static
77
+ HTML, and Docker Compose projects, starts the shared hub when needed, and
78
+ keeps the local URL visible while the application runs. Press Ctrl+C to stop
79
+ the application.
79
80
 
80
81
  An unconfigured Compose project asks you to save its routing setup first:
81
82
 
@@ -44,9 +44,10 @@ port 80 available. From an application directory, run:
44
44
  uvx localghost run
45
45
  ```
46
46
 
47
- Localghost detects Django, Vite, Astro, CakePHP, Laravel, plain PHP, and Docker
48
- Compose projects, starts the shared hub when needed, and keeps the local URL
49
- visible while the application runs. Press Ctrl+C to stop the application.
47
+ Localghost detects Django, Vite, Astro, CakePHP, Laravel, plain PHP, static
48
+ HTML, and Docker Compose projects, starts the shared hub when needed, and
49
+ keeps the local URL visible while the application runs. Press Ctrl+C to stop
50
+ the application.
50
51
 
51
52
  An unconfigured Compose project asks you to save its routing setup first:
52
53
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "localghost"
3
- version = "4.2.0"
3
+ version = "4.3.0"
4
4
  description = "Localghost serves local applications at friendly .localhost URLs"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -31,6 +31,8 @@ from .config import (
31
31
  write_run_config,
32
32
  )
33
33
  from .feedback import (
34
+ LIME,
35
+ MINT,
34
36
  action,
35
37
  choices,
36
38
  compose_dry_run,
@@ -41,6 +43,7 @@ from .feedback import (
41
43
  routes,
42
44
  run_plan,
43
45
  sign_off,
46
+ styled,
44
47
  success,
45
48
  title,
46
49
  warning,
@@ -90,11 +93,14 @@ from .tailscale import (
90
93
  API as TailscaleAPI,
91
94
  )
92
95
  from .tailscale import (
96
+ DNS_CONSOLE_URL,
97
+ MACHINES_CONSOLE_URL,
93
98
  OAUTH_CONSOLE_URL,
94
99
  POLICY_CONSOLE_URL,
95
100
  TailnetHub,
96
101
  TailscaleError,
97
102
  TailscaleState,
103
+ tag_in_use,
98
104
  tailnet_hubs,
99
105
  )
100
106
  from .tailscale import (
@@ -1252,7 +1258,7 @@ def _refuse_active_mapping(
1252
1258
  f"split DNS for .{suffix} already points at "
1253
1259
  f"{', '.join(active)} — another machine may be hosting this "
1254
1260
  "suffix. Choose a different --suffix, or pass --takeover to "
1255
- "replace the mapping."
1261
+ f"replace the mapping. Split DNS is listed at {DNS_CONSOLE_URL}."
1256
1262
  )
1257
1263
 
1258
1264
 
@@ -1310,31 +1316,41 @@ def _resolve_tailscale_credential(
1310
1316
  info("Using the OAuth credential stored in the system keyring.")
1311
1317
  return stored.client_id, stored.client_secret
1312
1318
  if tag is not None:
1313
- details(
1314
- [
1319
+ client = "Add OAuth credential"
1320
+ if tag_in_use(tag):
1321
+ # A device already carries the tag, so only the client is left.
1322
+ success(f"The {tag} device tag is already allowed on the tailnet.")
1323
+ steps = [(client, OAUTH_CONSOLE_URL)]
1324
+ indent = ""
1325
+ else:
1326
+ steps = [
1315
1327
  ("1. Allow the device tag", POLICY_CONSOLE_URL),
1316
1328
  (
1317
1329
  " Add to the policy file",
1318
1330
  f'"tagOwners": {{"{tag}": ["autogroup:admin"]}}',
1319
1331
  ),
1320
- ("2. Create an OAuth client", OAUTH_CONSOLE_URL),
1321
- (
1322
- " Scopes",
1323
- f"auth_keys (with {tag} allowed) and dns:write",
1324
- ),
1325
- ],
1326
- title=(
1327
- "Tailscale admin-console setup (already done? enter that "
1328
- "client's id and secret)"
1329
- ),
1332
+ (f"2. {client}", OAUTH_CONSOLE_URL),
1333
+ ]
1334
+ indent = " "
1335
+ details(steps)
1336
+ styled(
1337
+ [
1338
+ (f"{indent}with scopes ", "default"),
1339
+ ("DNS: Write", MINT),
1340
+ (", ", "default"),
1341
+ ("Auth Keys: Write", MINT),
1342
+ (" (", "default"),
1343
+ (tag, MINT),
1344
+ (")", "default"),
1345
+ ]
1330
1346
  )
1331
1347
  else:
1332
1348
  details(
1333
- [("OAuth client with dns:write", OAUTH_CONSOLE_URL)],
1349
+ [("OAuth credential with DNS: Write", OAUTH_CONSOLE_URL)],
1334
1350
  title="No OAuth credential is stored in the system keyring",
1335
1351
  )
1336
1352
  if not client_id:
1337
- client_id = click.prompt("Client id")
1353
+ client_id = click.prompt("Client ID")
1338
1354
  if not client_secret:
1339
1355
  client_secret = click.prompt("Client secret", hide_input=True)
1340
1356
  return client_id, client_secret
@@ -1399,14 +1415,18 @@ def tailscale_unshare(
1399
1415
  "`localghost trust remove` on each to revoke."
1400
1416
  )
1401
1417
  info(
1402
- f"Remove the offline tagged localghost-{state.node_label} device in the "
1403
- "Tailscale admin console."
1418
+ f"Remove the offline tagged localghost-{state.node_label} device at "
1419
+ f"{MACHINES_CONSOLE_URL}."
1404
1420
  )
1405
1421
 
1406
1422
 
1407
1423
  def _forget_tailscale_credential() -> None:
1408
1424
  if delete_tailscale_credential():
1409
1425
  info("The OAuth credential was removed from the system keyring.")
1426
+ info(
1427
+ "The OAuth credential still exists in Tailscale; revoke it on the Trust "
1428
+ f"credentials page ({OAUTH_CONSOLE_URL}) if no other machine uses it."
1429
+ )
1410
1430
  else:
1411
1431
  info("No OAuth credential was stored in the system keyring.")
1412
1432
 
@@ -1515,14 +1535,17 @@ def _trust_tailnet_hub(target: str | None) -> None:
1515
1535
  def _share_trust(suffix: str, certificate: PublicCertificate) -> None:
1516
1536
  """Show another tailnet machine how to trust this hub's root."""
1517
1537
  action(
1518
- f"Trust https://*.{suffix} on other machines",
1538
+ f"To trust https://*.{suffix} on other machines",
1519
1539
  f"uvx localghost tailscale trust {suffix}",
1520
1540
  )
1521
1541
  # The fingerprint is for clients that cannot ask Tailscale for the hub;
1522
1542
  # the plain command above is the way forward for everyone else.
1523
1543
  info(
1524
- "Without the tailscale CLI, add --fingerprint "
1525
- f"{certificate.fingerprint} to pin the root."
1544
+ [
1545
+ ("Without the tailscale CLI, add ", "default"),
1546
+ (f"--fingerprint {certificate.fingerprint}", LIME),
1547
+ (" to pin the root.", "default"),
1548
+ ]
1526
1549
  )
1527
1550
 
1528
1551
 
@@ -106,7 +106,7 @@ def details(
106
106
  console.print(Text(title, style="bold"))
107
107
  table = Table.grid(padding=(0, 1))
108
108
  table.add_column(style="bold")
109
- table.add_column(style=MINT)
109
+ table.add_column(style=MINT, overflow="fold")
110
110
  for label, value in entries:
111
111
  table.add_row(label, value)
112
112
  console.print(table)
@@ -117,6 +117,16 @@ def details(
117
117
  console.print("\n".join(lines), soft_wrap=True)
118
118
 
119
119
 
120
+ def styled(parts: Iterable[tuple[str, str]], *, err: bool = False) -> None:
121
+ """Show one line of (text, style) parts; plain text off a terminal."""
122
+ entries = list(parts)
123
+ console = _console(err)
124
+ if _rich_terminal(err):
125
+ console.print(Text.assemble(*entries))
126
+ return
127
+ console.print("".join(text for text, _ in entries), soft_wrap=True)
128
+
129
+
120
130
  def choices(title: str, items: Iterable[tuple[str, str, bool]]) -> None:
121
131
  """Show interactive choices with the same terminal hierarchy as other output."""
122
132
  entries = list(items)
@@ -141,7 +151,7 @@ def choices(title: str, items: Iterable[tuple[str, str, bool]]) -> None:
141
151
  console.print("\n".join(lines), soft_wrap=True)
142
152
 
143
153
 
144
- def info(message: str, *, err: bool = False) -> None:
154
+ def info(message: str | list[tuple[str, str]], *, err: bool = False) -> None:
145
155
  _message(message, MINT, err=err)
146
156
 
147
157
 
@@ -171,7 +181,7 @@ def _labeled_block(rows: list[tuple[str, str]], *, heading: str, err: bool) -> N
171
181
  console = _console(err)
172
182
  console.print(Text.assemble(("▌ ", MINT), (heading, "bold")))
173
183
  table = Table.grid(padding=(0, 1))
174
- table.add_column(style=MINT)
184
+ table.add_column(style=MINT, overflow="fold")
175
185
  table.add_column(style="bold")
176
186
  table.add_column()
177
187
  for label, value in rows:
@@ -269,12 +279,17 @@ def interrupt_break() -> None:
269
279
  _console(False).print()
270
280
 
271
281
 
272
- def _message(message: str, color: str, *, err: bool, symbol: str = "•") -> None:
282
+ def _message(
283
+ message: str | list[tuple[str, str]], color: str, *, err: bool, symbol: str = "•"
284
+ ) -> None:
285
+ """Show a message; one given as (text, style) parts can highlight a command."""
286
+ parts = [message] if isinstance(message, str) else message
273
287
  console = _console(err)
274
288
  if _rich_terminal(err):
275
- console.print(Text.assemble((f"{symbol} ", color), message))
289
+ console.print(Text.assemble((f"{symbol} ", color), *parts))
276
290
  else:
277
- console.print(message, soft_wrap=True)
291
+ plain = "".join(part if isinstance(part, str) else part[0] for part in parts)
292
+ console.print(plain, soft_wrap=True)
278
293
 
279
294
 
280
295
  def _console(err: bool) -> Console:
@@ -0,0 +1,29 @@
1
+ // Served by html_server.py as /__localghost/reload.js. Each event lists the
2
+ // changed files' URL paths, one per line. Changes this page never loaded are
3
+ // ignored; changed stylesheets it links are swapped in place, keeping scroll,
4
+ // form and script state; anything else reloads the page.
5
+ performance.setResourceTimingBufferSize(1000);
6
+ new EventSource("/__localghost/reload").onmessage = (event) => {
7
+ const page = location.pathname;
8
+ const used = new Set(
9
+ page.endsWith("/") ? [page + "index.html", page + "index.htm"] : [page],
10
+ );
11
+ for (const entry of performance.getEntriesByType("resource")) {
12
+ const url = new URL(entry.name);
13
+ if (url.origin === location.origin) used.add(url.pathname);
14
+ }
15
+ const changed = event.data.split("\n").filter((path) => used.has(path));
16
+ if (!changed.length) return;
17
+ const links = [...document.querySelectorAll("link[rel=stylesheet]")].filter(
18
+ (link) => changed.includes(new URL(link.href).pathname),
19
+ );
20
+ const linked = new Set(links.map((link) => new URL(link.href).pathname));
21
+ if (!changed.every((path) => linked.has(path))) return location.reload();
22
+ for (const old of links) {
23
+ // The new sheet loads before the old one goes, so nothing flashes unstyled.
24
+ const link = old.cloneNode();
25
+ link.href = new URL(old.href).pathname + "?" + Date.now();
26
+ link.onload = () => old.remove();
27
+ old.after(link);
28
+ }
29
+ };
@@ -0,0 +1,207 @@
1
+ """Serve the current directory, updating open pages when a file changes.
2
+
3
+ Run as `python -I -m localghost.html_server PORT BIND`. HTML pages get a
4
+ script (`/__localghost/reload.js`, from html_server.js) that holds an event
5
+ stream open; while one is open the server checks file modification times and
6
+ sends the paths of any that change, and the page swaps changed stylesheets or
7
+ reloads. With no page open, nothing is checked. Dotfiles and dot-directories
8
+ are never served.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import contextlib
14
+ import os
15
+ import re
16
+ import sys
17
+ import threading
18
+ import time
19
+ import urllib.parse
20
+ from functools import partial
21
+ from http import HTTPStatus
22
+ from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
23
+ from pathlib import Path
24
+
25
+ RELOAD_PATH = "/__localghost/reload"
26
+ # Served from a path rather than inline, so a `script-src 'self'` policy
27
+ # still lets it run.
28
+ RELOAD_SCRIPT_PATH = "/__localghost/reload.js"
29
+ RELOAD_SCRIPT = Path(__file__).with_name("html_server.js").read_bytes()
30
+ SCRIPT = f'<script src="{RELOAD_SCRIPT_PATH}"></script>'.encode()
31
+ _BODY_END = re.compile(rb"</body\s*>", re.IGNORECASE)
32
+ _INTERVAL = 0.5
33
+ # A comment line this often notices a closed tab, so its check stops.
34
+ _HEARTBEAT = 15.0
35
+ _SKIPPED_DIRECTORY = "node_modules"
36
+ # The stdlib server's own index names; its `index_pages` is 3.12+.
37
+ _INDEX_PAGES = ("index.html", "index.htm")
38
+ # Every open page shares one scan per interval rather than walking alone.
39
+ _scan_lock = threading.Lock()
40
+ _last_scan: tuple[Path, float, dict[str, int]] | None = None
41
+
42
+
43
+ def snapshot(root: Path) -> dict[str, int]:
44
+ """Modification times of every file under root.
45
+
46
+ Dot-directories and node_modules are skipped: neither is served content
47
+ anyone edits by hand, and node_modules can hold a hundred thousand files.
48
+ """
49
+ times = {}
50
+ for directory, subdirectories, files in os.walk(root):
51
+ subdirectories[:] = [
52
+ name
53
+ for name in subdirectories
54
+ if not name.startswith(".") and name != _SKIPPED_DIRECTORY
55
+ ]
56
+ for name in files:
57
+ path = os.path.join(directory, name)
58
+ # A file removed between the listing and the stat is just gone.
59
+ with contextlib.suppress(OSError):
60
+ times[path] = os.stat(path).st_mtime_ns
61
+ return times
62
+
63
+
64
+ def shared_snapshot(root: Path) -> dict[str, int]:
65
+ """A snapshot at most one interval old, so open pages share the walk.
66
+
67
+ A reused snapshot can only predate a change, which errs toward one extra
68
+ reload rather than a missed one.
69
+ """
70
+ global _last_scan
71
+ with _scan_lock:
72
+ now = time.monotonic()
73
+ stale = _last_scan is None or now - _last_scan[1] >= _INTERVAL
74
+ if stale or _last_scan[0] != root:
75
+ _last_scan = (root, now, snapshot(root))
76
+ return _last_scan[2]
77
+
78
+
79
+ def changed_paths(
80
+ root: Path, before: dict[str, int], after: dict[str, int]
81
+ ) -> list[str]:
82
+ """URL paths of every file added, removed or modified between snapshots."""
83
+ changed = {
84
+ path
85
+ for path in before.keys() | after.keys()
86
+ if before.get(path) != after.get(path)
87
+ }
88
+ return sorted(
89
+ urllib.parse.quote("/" + Path(path).relative_to(root).as_posix())
90
+ for path in changed
91
+ )
92
+
93
+
94
+ def inject(page: bytes) -> bytes:
95
+ """Put the reload script before the last </body>, or at the end without one."""
96
+ matches = list(_BODY_END.finditer(page))
97
+ if not matches:
98
+ return page + SCRIPT
99
+ at = matches[-1].start()
100
+ return page[:at] + SCRIPT + page[at:]
101
+
102
+
103
+ class Handler(SimpleHTTPRequestHandler):
104
+ def do_GET(self) -> None:
105
+ route = self.path.split("?", 1)[0]
106
+ if route == RELOAD_PATH:
107
+ self._stream_reloads()
108
+ return
109
+ if route == RELOAD_SCRIPT_PATH:
110
+ self._send(RELOAD_SCRIPT, "text/javascript")
111
+ return
112
+ self._serve()
113
+
114
+ def do_HEAD(self) -> None:
115
+ self._serve()
116
+
117
+ def _serve(self) -> None:
118
+ """GET and HEAD alike, so both report the injected page's length."""
119
+ if self._hidden():
120
+ self.send_error(HTTPStatus.NOT_FOUND, "File not found")
121
+ return
122
+ page = self._html_file()
123
+ if page is None:
124
+ if self.command == "HEAD":
125
+ super().do_HEAD()
126
+ else:
127
+ super().do_GET()
128
+ return
129
+ try:
130
+ body = inject(page.read_bytes())
131
+ except OSError:
132
+ self.send_error(HTTPStatus.NOT_FOUND, "File not found")
133
+ return
134
+ # No charset: the page's own <meta charset> or BOM decides, as it
135
+ # would from the stdlib server. The tag is ASCII, so it fits any.
136
+ self._send(body, self.guess_type(str(page)))
137
+
138
+ def _send(self, body: bytes, content_type: str) -> None:
139
+ self.send_response(HTTPStatus.OK)
140
+ self.send_header("Content-Type", content_type)
141
+ self.send_header("Content-Length", str(len(body)))
142
+ self.send_header("Cache-Control", "no-cache")
143
+ self.end_headers()
144
+ if self.command != "HEAD":
145
+ self.wfile.write(body)
146
+
147
+ def _hidden(self) -> bool:
148
+ """Does the path name a dotfile or anything inside a dot-directory?"""
149
+ path = urllib.parse.unquote(self.path.split("?", 1)[0].split("#", 1)[0])
150
+ return any(part.startswith(".") for part in path.split("/"))
151
+
152
+ def _html_file(self) -> Path | None:
153
+ """The HTML file this request serves, leaving redirects and listings alone."""
154
+ path = Path(self.translate_path(self.path))
155
+ if path.is_dir():
156
+ if not self.path.split("?", 1)[0].endswith("/"):
157
+ return None
158
+ path = next(
159
+ (path / name for name in _INDEX_PAGES if (path / name).is_file()),
160
+ path / "index.html",
161
+ )
162
+ if path.suffix.lower() in (".html", ".htm") and path.is_file():
163
+ return path
164
+ return None
165
+
166
+ def _stream_reloads(self) -> None:
167
+ # Before the headers, so a save the page races to see is still a change.
168
+ root = Path(self.directory)
169
+ before = shared_snapshot(root)
170
+ self.send_response(HTTPStatus.OK)
171
+ self.send_header("Content-Type", "text/event-stream")
172
+ self.send_header("Cache-Control", "no-cache")
173
+ self.end_headers()
174
+ quiet_since = time.monotonic()
175
+ # The stream stays open: a page that swaps a stylesheet rather than
176
+ # reloading would otherwise miss edits made while it reconnected.
177
+ try:
178
+ while True:
179
+ time.sleep(_INTERVAL)
180
+ after = shared_snapshot(root)
181
+ if after != before:
182
+ paths = changed_paths(root, before, after)
183
+ event = "".join(f"data: {path}\n" for path in paths) + "\n"
184
+ before = after
185
+ elif time.monotonic() - quiet_since > _HEARTBEAT:
186
+ event = ": still here\n\n"
187
+ else:
188
+ continue
189
+ self.wfile.write(event.encode())
190
+ self.wfile.flush()
191
+ quiet_since = time.monotonic()
192
+ except OSError: # The tab closed, or the connection otherwise went.
193
+ return
194
+
195
+
196
+ def main() -> None:
197
+ port, bind = int(sys.argv[1]), sys.argv[2]
198
+ handler = partial(Handler, directory=os.getcwd())
199
+ with (
200
+ ThreadingHTTPServer((bind, port), handler) as server,
201
+ contextlib.suppress(KeyboardInterrupt),
202
+ ):
203
+ server.serve_forever()
204
+
205
+
206
+ if __name__ == "__main__":
207
+ main()
@@ -68,6 +68,7 @@ SUPPORTED_TYPES = (
68
68
  "cakephp",
69
69
  "laravel",
70
70
  "php",
71
+ "html",
71
72
  )
72
73
  RUN_TYPES = tuple(item for item in SUPPORTED_TYPES if item != "dockerfile")
73
74
  # Types the non-Compose save path can persist. Compose is handled separately
@@ -93,6 +94,7 @@ DEFAULT_PORTS = {
93
94
  "cakephp": 8765,
94
95
  "laravel": 8000,
95
96
  "php": 8080,
97
+ "html": 8080,
96
98
  }
97
99
 
98
100
  COMPOSE_FILENAMES = (
@@ -299,6 +301,8 @@ def build_plan(
299
301
  default_port, selected_command, working_directory = php_command(
300
302
  project_root, port
301
303
  )
304
+ elif selected_type == "html":
305
+ default_port, selected_command = html_command(port)
302
306
  else: # Click validates the public option; retain this for direct callers.
303
307
  raise click.ClickException(type_choices("--type"))
304
308
  wanted_port = port or default_port
@@ -383,7 +387,9 @@ def discover_type(
383
387
 
384
388
  Ambiguity is computed after filtering to `allowed`, so a type another
385
389
  command handles never blocks this one. Generic `php` defers to anything
386
- stronger found further up; see `_php_project_strength`.
390
+ stronger found further up; see `_php_project_strength`. `html` defers
391
+ to any other type at the same directory, and to any but a weak `php`
392
+ further up: the nearer of two fallbacks wins.
387
393
  """
388
394
  if requested is not None and requested not in SUPPORTED_TYPES:
389
395
  raise click.ClickException(type_choices("--type"))
@@ -400,6 +406,10 @@ def discover_type(
400
406
  fallback: tuple[str, Path] | None = None
401
407
  for candidate in _search_path(start):
402
408
  detected = [item for item in _types_at(candidate) if item in allowed]
409
+ if requested is None and "html" in detected:
410
+ detected.remove("html")
411
+ if not detected and fallback is None:
412
+ fallback = ("html", candidate)
403
413
  if requested is not None:
404
414
  if requested in detected:
405
415
  if requested == "php" and _php_project_strength(candidate) != "strong":
@@ -531,6 +541,8 @@ def _types_at(cwd: Path) -> list[str]:
531
541
  php = _php_type(cwd, _composer_manifest(cwd))
532
542
  if php is not None:
533
543
  detected.append(php)
544
+ if (cwd / "index.html").is_file():
545
+ detected.append("html")
534
546
  return detected
535
547
 
536
548
 
@@ -642,6 +654,18 @@ def php_command(
642
654
  )
643
655
 
644
656
 
657
+ def html_command(requested_port: int | None) -> tuple[int, tuple[str, ...]]:
658
+ """Serve the directory, reloading open pages on change; see `html_server`.
659
+
660
+ -I keeps a stray module in the served directory from being imported.
661
+ """
662
+ bind = "127.0.0.1" if forwarder.available() else "0.0.0.0"
663
+ return (
664
+ requested_port or DEFAULT_PORTS["html"],
665
+ (sys.executable, "-I", "-m", "localghost.html_server", "{port}", bind),
666
+ )
667
+
668
+
645
669
  def astro_command(cwd: Path, requested_port: int | None) -> tuple[int, tuple[str, ...]]:
646
670
  manifest = _astro_manifest(cwd)
647
671
  if manifest is None:
@@ -25,8 +25,10 @@ from .paths import state_directory
25
25
 
26
26
  API_ROOT = "https://api.tailscale.com/api/v2"
27
27
  TOKEN_URL = f"{API_ROOT}/oauth/token"
28
- OAUTH_CONSOLE_URL = "https://login.tailscale.com/admin/settings/oauth"
29
- POLICY_CONSOLE_URL = "https://login.tailscale.com/admin/acls/file"
28
+ OAUTH_CONSOLE_URL = "https://console.tailscale.com/admin/settings/trust-credentials"
29
+ POLICY_CONSOLE_URL = "https://console.tailscale.com/admin/acls/file"
30
+ MACHINES_CONSOLE_URL = "https://console.tailscale.com/admin/machines"
31
+ DNS_CONSOLE_URL = "https://console.tailscale.com/admin/dns"
30
32
  KEYRING_SERVICE = "localghost-tailscale"
31
33
  _SUFFIX = re.compile(r"^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$")
32
34
 
@@ -328,8 +330,9 @@ class API:
328
330
  ) from exc
329
331
  if exc.code == 403:
330
332
  raise TailscaleError(
331
- f"the OAuth client may not create auth keys: it needs the "
332
- f"auth_keys scope with {tag} allowed ({OAUTH_CONSOLE_URL})"
333
+ f"the OAuth credential may not create auth keys: it needs the "
334
+ f"Auth Keys: Write scope with {tag} selected (Trust "
335
+ f"credentials page, {OAUTH_CONSOLE_URL})"
333
336
  ) from exc
334
337
  raise
335
338
  try:
@@ -341,8 +344,8 @@ class API:
341
344
  def _dns_scope_guidance(exc: APIError) -> TailscaleError:
342
345
  if exc.code == 403:
343
346
  return TailscaleError(
344
- "the OAuth client lacks the dns:write scope; recreate it at "
345
- f"{OAUTH_CONSOLE_URL}"
347
+ "the OAuth credential lacks the DNS: Write scope; recreate it on the "
348
+ f"Trust credentials page ({OAUTH_CONSOLE_URL})"
346
349
  )
347
350
  return exc
348
351
 
@@ -435,13 +438,8 @@ class TailnetHub:
435
438
  ) # fmt: skip
436
439
 
437
440
 
438
- def tailnet_hubs(tag: str = "tag:localghost") -> list[TailnetHub] | None:
439
- """The online localghost gateways in this client's network map.
440
-
441
- The names and addresses come from the coordination server through the
442
- local Tailscale client, not from DNS, so split DNS cannot redirect them.
443
- None means there is no tailscale CLI to ask.
444
- """
441
+ def _network_map_peers() -> dict | None:
442
+ """The peers in this client's network map; None without a tailscale CLI."""
445
443
  try:
446
444
  result = subprocess.run(
447
445
  ["tailscale", "status", "--json"],
@@ -454,9 +452,32 @@ def tailnet_hubs(tag: str = "tag:localghost") -> list[TailnetHub] | None:
454
452
  if result.returncode != 0:
455
453
  return None
456
454
  try:
457
- peers = json.loads(result.stdout).get("Peer") or {}
455
+ return json.loads(result.stdout).get("Peer") or {}
458
456
  except (json.JSONDecodeError, AttributeError):
459
457
  return None
458
+
459
+
460
+ def tag_in_use(tag: str) -> bool:
461
+ """Whether a device in this client's network map, online or not, has tag.
462
+
463
+ A device can only carry a tag its owner allowed, so this proves the
464
+ policy file once allowed it. False proves nothing: the policy may hide
465
+ tagged devices from this client.
466
+ """
467
+ peers = _network_map_peers() or {}
468
+ return any(tag in (peer.get("Tags") or ()) for peer in peers.values())
469
+
470
+
471
+ def tailnet_hubs(tag: str = "tag:localghost") -> list[TailnetHub] | None:
472
+ """The online localghost gateways in this client's network map.
473
+
474
+ The names and addresses come from the coordination server through the
475
+ local Tailscale client, not from DNS, so split DNS cannot redirect them.
476
+ None means there is no tailscale CLI to ask.
477
+ """
478
+ peers = _network_map_peers()
479
+ if peers is None:
480
+ return None
460
481
  hubs = [
461
482
  TailnetHub(
462
483
  str(peer["DNSName"]).lower().removesuffix("."),
File without changes