tailscale.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """Tailscale presence detection for virtual printers.
  2. Reports whether tailscaled is reachable and surfaces the host's Tailscale IPs
  3. and FQDN so the UI can show users which IP to paste into the slicer when
  4. they want to reach a VP over Tailscale.
  5. Historical note: this module previously provisioned Let's Encrypt certs via
  6. `tailscale cert` so the slicer would not need a manual CA import. That path
  7. was removed because BambuStudio's printer-MQTT trust path validates only
  8. against its bundled BBL CA (not the system trust store), so LE-signed certs
  9. are rejected regardless of hostname/IP. The self-signed CA flow (with one-
  10. time `bbl_ca.crt` import into the slicer) is the only viable trust mechanism;
  11. Tailscale's role is now strictly network reach.
  12. """
  13. import asyncio
  14. import json
  15. import logging
  16. import os
  17. import shutil
  18. from dataclasses import dataclass, field
  19. from pathlib import Path
  20. logger = logging.getLogger(__name__)
  21. # Minimal environment for tailscale subprocess — passes OS/shell variables that
  22. # tailscale needs to locate its socket and config, but strips application secrets
  23. # (JWT keys, DB URLs, SMTP passwords, etc.) that the subprocess has no need for.
  24. _SUBPROCESS_ENV: dict[str, str] = {
  25. k: v
  26. for k, v in os.environ.items()
  27. if k
  28. in {
  29. "PATH",
  30. "HOME",
  31. "USER",
  32. "USERNAME",
  33. "LOGNAME",
  34. # Windows equivalents
  35. "USERPROFILE",
  36. "APPDATA",
  37. "LOCALAPPDATA",
  38. "PROGRAMFILES",
  39. "PROGRAMFILES(X86)",
  40. "SYSTEMROOT",
  41. "WINDIR",
  42. "COMPUTERNAME",
  43. "TEMP",
  44. "TMP",
  45. # Linux XDG dirs used by tailscale for socket/config
  46. "XDG_RUNTIME_DIR",
  47. "XDG_CONFIG_HOME",
  48. }
  49. }
  50. @dataclass
  51. class TailscaleStatus:
  52. """Runtime Tailscale availability and identity."""
  53. available: bool
  54. hostname: str # "myhost"
  55. tailnet_name: str # "tailnetname.ts.net"
  56. fqdn: str # "myhost.tailnetname.ts.net"
  57. tailscale_ips: list[str] = field(default_factory=list)
  58. error: str | None = None
  59. class TailscaleService:
  60. """Wraps `tailscale status` for presence detection.
  61. All methods are safe to call when Tailscale is absent — they return
  62. sensible defaults and never raise exceptions.
  63. """
  64. _docker_hint_logged: bool = False
  65. @classmethod
  66. def _log_docker_socket_hint(cls) -> None:
  67. """Log a one-time hint when running in Docker without the Tailscale socket mounted."""
  68. if cls._docker_hint_logged:
  69. return
  70. if Path("/.dockerenv").exists() and not Path("/var/run/tailscale/tailscaled.sock").exists():
  71. logger.info(
  72. "Running in Docker but /var/run/tailscale/tailscaled.sock is not mounted. "
  73. "Add `- /var/run/tailscale/tailscaled.sock:/var/run/tailscale/tailscaled.sock` "
  74. "to docker-compose.yml (under volumes:) and run Tailscale on the host to "
  75. "expose virtual printers over your tailnet."
  76. )
  77. cls._docker_hint_logged = True
  78. async def _run_tailscale(self, *args: str, timeout: float = 30.0) -> tuple[int | None, bytes, bytes]:
  79. """Run a tailscale subcommand and return (returncode, stdout, stderr).
  80. Resolves the binary to an absolute path to guard against PATH hijacking.
  81. """
  82. binary = shutil.which("tailscale")
  83. if not binary:
  84. raise OSError("tailscale binary not found")
  85. process = await asyncio.create_subprocess_exec(
  86. binary,
  87. *args,
  88. stdout=asyncio.subprocess.PIPE,
  89. stderr=asyncio.subprocess.PIPE,
  90. env=_SUBPROCESS_ENV,
  91. )
  92. try:
  93. stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)
  94. except asyncio.TimeoutError:
  95. process.kill()
  96. await process.wait()
  97. raise
  98. return process.returncode, stdout, stderr
  99. async def get_status(self) -> TailscaleStatus:
  100. """Query Tailscale status and return machine identity.
  101. Returns TailscaleStatus(available=False) if the binary is missing,
  102. the daemon is not running, or any other error occurs.
  103. """
  104. if not shutil.which("tailscale"):
  105. self._log_docker_socket_hint()
  106. return TailscaleStatus(
  107. available=False,
  108. hostname="",
  109. tailnet_name="",
  110. fqdn="",
  111. error="tailscale binary not found",
  112. )
  113. try:
  114. returncode, stdout, stderr = await self._run_tailscale("status", "--json", timeout=5.0)
  115. except OSError as e:
  116. return TailscaleStatus(
  117. available=False,
  118. hostname="",
  119. tailnet_name="",
  120. fqdn="",
  121. error=str(e),
  122. )
  123. if returncode is None or returncode != 0:
  124. self._log_docker_socket_hint()
  125. return TailscaleStatus(
  126. available=False,
  127. hostname="",
  128. tailnet_name="",
  129. fqdn="",
  130. error=stderr.decode(errors="replace").strip(),
  131. )
  132. try:
  133. data = json.loads(stdout)
  134. except json.JSONDecodeError as e:
  135. return TailscaleStatus(
  136. available=False,
  137. hostname="",
  138. tailnet_name="",
  139. fqdn="",
  140. error=f"JSON parse error: {e}",
  141. )
  142. self_info = data.get("Self", {})
  143. # DNSName includes trailing dot: "myhost.tailnetname.ts.net."
  144. fqdn = self_info.get("DNSName", "").rstrip(".")
  145. if not fqdn:
  146. return TailscaleStatus(
  147. available=False,
  148. hostname="",
  149. tailnet_name="",
  150. fqdn="",
  151. error="Tailscale not connected (no DNSName)",
  152. )
  153. parts = fqdn.split(".", 1)
  154. hostname = parts[0]
  155. tailnet_name = parts[1] if len(parts) > 1 else ""
  156. tailscale_ips = self_info.get("TailscaleIPs", [])
  157. logger.debug("Tailscale available: fqdn=%s, ips=%s", fqdn, tailscale_ips)
  158. return TailscaleStatus(
  159. available=True,
  160. hostname=hostname,
  161. tailnet_name=tailnet_name,
  162. fqdn=fqdn,
  163. tailscale_ips=tailscale_ips,
  164. )
  165. # Module-level singleton — import this in other modules
  166. tailscale_service = TailscaleService()