ftp_profiles.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Per-printer-model FTP tuning knobs.
  2. Mirrors the shape of :mod:`backend.app.services.camera_profiles` — a
  3. small registry of per-model overrides so quirky firmwares can be
  4. tuned without sprinkling ``if model == "X":`` branches through
  5. ``bambu_ftp.py``. Adding a new model's quirk is a config edit (an
  6. entry in ``_PROFILES`` plus the alias for its internal SSDP code if
  7. needed), not another hard-coded branch.
  8. The default profile matches the historical pre-fix behaviour, so
  9. every model that doesn't have an entry here keeps its existing FTP
  10. behaviour byte-for-byte.
  11. Currently only the TLS-version cap lives here (P2S firmware
  12. 01.02.00.00 needs it — see ``cap_tls_v1_2`` below). The A1
  13. data-channel-plaintext quirk still lives in :class:`BambuFTPClient`
  14. via ``A1_MODELS`` / ``skip_session_reuse``; folding that into a
  15. profile field is a future cleanup, not load-bearing for this fix.
  16. """
  17. from __future__ import annotations
  18. from dataclasses import dataclass
  19. @dataclass(frozen=True)
  20. class FTPProfile:
  21. """Tuning knobs for one printer model's FTP path.
  22. All defaults reflect the historical behaviour. Models with quirky
  23. firmware override individual fields rather than re-defining the
  24. whole profile.
  25. """
  26. # Pin the SSL context's ``maximum_version`` to TLS 1.2.
  27. #
  28. # Python 3.13's default ``ssl.create_default_context()`` negotiates
  29. # TLS 1.3 when both peers support it. The Bambuddy Docker image is
  30. # ``python:3.13-slim-trixie``, so every Docker user gets 1.3 by
  31. # default. Some Bambu printer firmwares (P2S 01.02.00.00 confirmed
  32. # by @iitazz, #1401) implement session reuse on the FTPS data
  33. # channel against an old vsFTPd build that doesn't tolerate TLS
  34. # 1.3's asynchronous session-ticket model: the data channel gets
  35. # torn down mid-stream and the upload aborts with 426 "Failure
  36. # reading network stream" — visible as a clean truncation at a
  37. # chunk boundary (one reporter saw exactly 7 × 64 KB landed on
  38. # the printer). Capping to TLS 1.2 makes session resumption
  39. # synchronous and the upload completes normally.
  40. #
  41. # **Defaults to False** — only applied to printer models where a
  42. # reporter has confirmed the symptom. Existing P1S / X1C / H2D
  43. # installs that work fine today stay on the negotiated TLS 1.3.
  44. # This is deliberately conservative; flipping a printer to the
  45. # capped path is a config edit when a new model surfaces the
  46. # same bug.
  47. cap_tls_v1_2: bool = False
  48. # ---------------------------------------------------------------------------
  49. # Profile registry
  50. # ---------------------------------------------------------------------------
  51. # Default profile = historical behaviour. Used for every model that
  52. # doesn't have an entry in ``_PROFILES``.
  53. DEFAULT_PROFILE = FTPProfile()
  54. # Per-model overrides. Keys are uppercase display names (e.g. "P2S")
  55. # AFTER alias normalisation, so internal SSDP codes ("N7") resolve via
  56. # ``_MODEL_ALIASES`` below.
  57. _PROFILES: dict[str, FTPProfile] = {
  58. # P2S firmware 01.02.00.00 trips the vsFTPd + TLS 1.3 session-reuse
  59. # bug on the FTPS data channel (#1401, reporter @iitazz). Cap to
  60. # TLS 1.2 so session resumption is synchronous and the upload
  61. # completes.
  62. "P2S": FTPProfile(
  63. cap_tls_v1_2=True,
  64. ),
  65. }
  66. # SSDP internal codes that should resolve to a display-name profile.
  67. # Mirrors the same map in :mod:`camera_profiles`.
  68. _MODEL_ALIASES: dict[str, str] = {
  69. "N7": "P2S", # P2S internal SSDP code
  70. }
  71. def get_ftp_profile(model: str | None) -> FTPProfile:
  72. """Return the :class:`FTPProfile` for *model*, or the default.
  73. ``model`` can be either a display name (e.g. ``"P2S"``) or an
  74. internal SSDP code (e.g. ``"N7"``). Unknown / missing models fall
  75. back to :data:`DEFAULT_PROFILE` so the FTP path is never blocked
  76. on a missing entry.
  77. """
  78. if not model:
  79. return DEFAULT_PROFILE
  80. key = model.upper().strip()
  81. key = _MODEL_ALIASES.get(key, key)
  82. return _PROFILES.get(key, DEFAULT_PROFILE)