ftp_server.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. """Implicit FTPS server for receiving 3MF uploads from slicers.
  2. Implements an implicit FTPS server (TLS from byte 0) that accepts file uploads
  3. from Bambu Studio and OrcaSlicer, matching the real Bambu printer behavior.
  4. Unlike explicit FTPS (AUTH TLS), implicit FTPS wraps the connection in TLS
  5. immediately upon connection, before any FTP commands are exchanged.
  6. """
  7. import asyncio
  8. import hmac
  9. import logging
  10. import os
  11. import random
  12. import ssl
  13. from collections.abc import Callable
  14. from pathlib import Path
  15. logger = logging.getLogger(__name__)
  16. # Default FTP port for Bambu printers (implicit FTPS).
  17. # Must be 990 (same as real printers) to avoid iptables REDIRECT,
  18. # which rewrites the destination IP to the incoming interface's primary
  19. # address — breaking multi-VP setups with different bind IPs.
  20. # Requires CAP_NET_BIND_SERVICE or root.
  21. FTP_PORT = 990
  22. # Hard cap on a single upload. 4 GiB covers the largest realistic
  23. # multi-plate .gcode.3mf and rejects runaway / malicious clients before
  24. # they can exhaust the disk or OOM the host. STOR still buffers the
  25. # whole file in memory before write_bytes — peak RSS ~2x file size during
  26. # the b''.join — so the cap also caps that peak. If real users hit it
  27. # with a legitimate file, raise here.
  28. MAX_UPLOAD_BYTES = 4 * 1024 * 1024 * 1024 # 4 GiB
  29. class FTPSession:
  30. """Handles a single FTP client session."""
  31. def __init__(
  32. self,
  33. reader: asyncio.StreamReader,
  34. writer: asyncio.StreamWriter,
  35. upload_dir: Path,
  36. access_code: str,
  37. ssl_context: ssl.SSLContext,
  38. on_file_received: Callable[[Path, str], None] | None,
  39. passive_port_range: tuple[int, int] = (50000, 50100),
  40. pasv_address: str = "",
  41. bind_address: str = "0.0.0.0", # nosec B104
  42. vp_name: str = "",
  43. ):
  44. self.reader = reader
  45. self.writer = writer
  46. self.upload_dir = upload_dir
  47. self.access_code = access_code
  48. self.ssl_context = ssl_context
  49. self.on_file_received = on_file_received
  50. self.passive_port_range = passive_port_range
  51. self.pasv_address = pasv_address
  52. self.bind_address = bind_address
  53. self.vp_name = vp_name
  54. self._log_prefix = f"[{vp_name}] " if vp_name else ""
  55. self.authenticated = False
  56. self.username: str | None = None
  57. self.current_dir = upload_dir
  58. self.transfer_type = "A" # ASCII by default
  59. self.data_server: asyncio.Server | None = None
  60. self.data_port: int | None = None
  61. # For data transfer coordination
  62. self._data_reader: asyncio.StreamReader | None = None
  63. self._data_writer: asyncio.StreamWriter | None = None
  64. self._data_connected = asyncio.Event()
  65. self._transfer_done = asyncio.Event()
  66. peername = writer.get_extra_info("peername")
  67. self.remote_ip = peername[0] if peername else "unknown"
  68. async def send(self, code: int, message: str) -> None:
  69. """Send an FTP response."""
  70. response = f"{code} {message}\r\n"
  71. logger.debug("%sFTP -> %s: %s", self._log_prefix, self.remote_ip, response.strip())
  72. self.writer.write(response.encode("utf-8"))
  73. await self.writer.drain()
  74. async def handle(self) -> None:
  75. """Handle the FTP session."""
  76. try:
  77. # Send welcome banner
  78. await self.send(220, "Bambuddy Virtual Printer FTP ready")
  79. while True:
  80. try:
  81. line = await asyncio.wait_for(
  82. self.reader.readline(),
  83. timeout=300, # 5 minute timeout
  84. )
  85. except TimeoutError:
  86. logger.debug("%sFTP session timeout from %s", self._log_prefix, self.remote_ip)
  87. break
  88. if not line:
  89. break
  90. try:
  91. command_line = line.decode("utf-8").strip()
  92. except UnicodeDecodeError:
  93. command_line = line.decode("latin-1").strip()
  94. if not command_line:
  95. continue
  96. # Never log passwords
  97. if command_line.upper().startswith("PASS"):
  98. logger.debug("%sFTP <- %s: PASS ********", self._log_prefix, self.remote_ip)
  99. else:
  100. logger.debug("%sFTP <- %s: %s", self._log_prefix, self.remote_ip, command_line)
  101. # Parse command and argument
  102. parts = command_line.split(" ", 1)
  103. cmd = parts[0].upper()
  104. arg = parts[1] if len(parts) > 1 else ""
  105. # Dispatch command
  106. handler = getattr(self, f"cmd_{cmd}", None)
  107. if handler:
  108. await handler(arg)
  109. else:
  110. logger.debug("%sFTP command not implemented: %s", self._log_prefix, cmd)
  111. await self.send(502, f"Command {cmd} not implemented")
  112. except asyncio.CancelledError:
  113. logger.info("%sFTP session cancelled from %s", self._log_prefix, self.remote_ip)
  114. except Exception as e:
  115. logger.error("%sFTP session error from %s: %s", self._log_prefix, self.remote_ip, e)
  116. finally:
  117. logger.info("%sFTP session ended from %s", self._log_prefix, self.remote_ip)
  118. await self._cleanup()
  119. async def _cleanup(self) -> None:
  120. """Clean up session resources."""
  121. # Release any waiting data connection callback
  122. self._transfer_done.set()
  123. if self.data_server:
  124. self.data_server.close()
  125. try:
  126. await self.data_server.wait_closed()
  127. except OSError:
  128. pass # Best-effort data server cleanup; may already be closed
  129. self.data_server = None
  130. try:
  131. self.writer.close()
  132. await self.writer.wait_closed()
  133. except OSError:
  134. pass # Best-effort control connection cleanup; client may have disconnected
  135. # FTP Commands
  136. async def cmd_USER(self, arg: str) -> None:
  137. """Handle USER command."""
  138. self.username = arg
  139. if arg.lower() == "bblp":
  140. await self.send(331, "Password required")
  141. else:
  142. await self.send(530, "Invalid user")
  143. async def cmd_PASS(self, arg: str) -> None:
  144. """Handle PASS command."""
  145. if self.username and self.username.lower() == "bblp":
  146. # ``hmac.compare_digest`` is constant-time — keeps the auth check
  147. # from leaking the access code via response timing under network
  148. # jitter. LAN-only threat is marginal; this is the standard fix.
  149. if hmac.compare_digest(arg, self.access_code):
  150. self.authenticated = True
  151. await self.send(230, "Login successful")
  152. logger.info("%sFTP login from %s", self._log_prefix, self.remote_ip)
  153. else:
  154. await self.send(530, "Login incorrect")
  155. logger.warning("%sFTP failed login from %s (access code mismatch)", self._log_prefix, self.remote_ip)
  156. else:
  157. await self.send(503, "Login with USER first")
  158. async def cmd_SYST(self, arg: str) -> None:
  159. """Handle SYST command."""
  160. await self.send(215, "UNIX Type: L8")
  161. async def cmd_FEAT(self, arg: str) -> None:
  162. """Handle FEAT command."""
  163. features = [
  164. "211-Features:",
  165. " PASV",
  166. " EPSV",
  167. " UTF8",
  168. " SIZE",
  169. "211 End",
  170. ]
  171. for line in features[:-1]:
  172. self.writer.write(f"{line}\r\n".encode())
  173. await self.writer.drain()
  174. self.writer.write(f"{features[-1]}\r\n".encode())
  175. await self.writer.drain()
  176. async def cmd_PWD(self, arg: str) -> None:
  177. """Handle PWD command."""
  178. if not self.authenticated:
  179. await self.send(530, "Not logged in")
  180. return
  181. await self.send(257, '"/" is current directory')
  182. async def cmd_CWD(self, arg: str) -> None:
  183. """Handle CWD command."""
  184. if not self.authenticated:
  185. await self.send(530, "Not logged in")
  186. return
  187. # Accept any directory change (we use a flat structure)
  188. await self.send(250, "Directory changed")
  189. async def cmd_TYPE(self, arg: str) -> None:
  190. """Handle TYPE command."""
  191. if not self.authenticated:
  192. await self.send(530, "Not logged in")
  193. return
  194. if arg.upper() in ("A", "I"):
  195. self.transfer_type = arg.upper()
  196. type_name = "ASCII" if arg.upper() == "A" else "Binary"
  197. await self.send(200, f"Type set to {type_name}")
  198. else:
  199. await self.send(504, "Type not supported")
  200. async def _bind_passive_port(self) -> bool:
  201. """Try to bind a passive data port with retries.
  202. Returns True if a port was successfully bound, False otherwise.
  203. Sets self.data_server and self.data_port on success.
  204. """
  205. port_min, port_max = self.passive_port_range
  206. for attempt in range(10):
  207. port = random.randint(port_min, port_max)
  208. try:
  209. self.data_server = await asyncio.start_server(
  210. self._handle_data_connection,
  211. self.bind_address,
  212. port,
  213. ssl=self.ssl_context,
  214. )
  215. self.data_port = port
  216. return True
  217. except OSError:
  218. logger.debug("FTP passive port %s in use, retrying (%s/10)", port, attempt + 1)
  219. return False
  220. async def cmd_EPSV(self, arg: str) -> None:
  221. """Handle EPSV command - Extended Passive Mode (IPv6 compatible)."""
  222. if not self.authenticated:
  223. await self.send(530, "Not logged in")
  224. return
  225. # Close any existing data connection/server
  226. await self._close_data_connection()
  227. # Reset connection state for the new transfer
  228. self._data_connected.clear()
  229. self._data_reader = None
  230. self._data_writer = None
  231. self._transfer_done = asyncio.Event()
  232. if await self._bind_passive_port():
  233. # EPSV response format: 229 Entering Extended Passive Mode (|||port|)
  234. await self.send(229, f"Entering Extended Passive Mode (|||{self.data_port}|)")
  235. logger.info("FTP EPSV listening on port %s", self.data_port)
  236. else:
  237. logger.error("Failed to bind any passive port for EPSV")
  238. await self.send(425, "Cannot open data connection")
  239. async def cmd_PASV(self, arg: str) -> None:
  240. """Handle PASV command - set up passive data connection."""
  241. if not self.authenticated:
  242. await self.send(530, "Not logged in")
  243. return
  244. # Close any existing data connection/server
  245. await self._close_data_connection()
  246. # Reset connection state for the new transfer
  247. self._data_connected.clear()
  248. self._data_reader = None
  249. self._data_writer = None
  250. self._transfer_done = asyncio.Event()
  251. if await self._bind_passive_port():
  252. # Determine the IP to advertise in PASV response
  253. if self.pasv_address:
  254. # Explicit override (e.g., for Docker bridge mode behind NAT)
  255. ip = self.pasv_address
  256. else:
  257. # Use the local IP of the control connection
  258. sockname = self.writer.get_extra_info("sockname")
  259. ip = sockname[0] if sockname else "127.0.0.1"
  260. # 0.0.0.0 is not routable — fall back to control connection IP
  261. if ip == "0.0.0.0": # nosec B104
  262. ip = "127.0.0.1"
  263. # Format IP and port for PASV response
  264. ip_parts = ip.split(".")
  265. port_hi = self.data_port // 256
  266. port_lo = self.data_port % 256
  267. await self.send(
  268. 227,
  269. f"Entering Passive Mode ({ip_parts[0]},{ip_parts[1]},{ip_parts[2]},{ip_parts[3]},{port_hi},{port_lo})",
  270. )
  271. logger.info("FTP PASV listening on %s:%s", ip, self.data_port)
  272. else:
  273. logger.error("Failed to bind any passive port for PASV")
  274. await self.send(425, "Cannot open data connection")
  275. async def _handle_data_connection(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
  276. """Handle incoming data connection (used by PASV/EPSV).
  277. This callback stays alive until the transfer completes to ensure the
  278. asyncio task holds strong references to the reader/writer throughout
  279. the data transfer. If the callback returned immediately, the task
  280. would complete and the StreamReaderProtocol could release its strong
  281. reader reference, potentially destabilising the connection.
  282. """
  283. # Reject duplicate connections — only one data connection per transfer
  284. if self._data_reader is not None:
  285. logger.warning("FTP rejecting duplicate data connection from %s", self.remote_ip)
  286. try:
  287. writer.close()
  288. await writer.wait_closed()
  289. except OSError:
  290. pass
  291. return
  292. # Log TLS details for debugging
  293. ssl_obj = writer.get_extra_info("ssl_object")
  294. if ssl_obj:
  295. logger.info(
  296. f"FTP data TLS from {self.remote_ip}: cipher={ssl_obj.cipher()}, "
  297. f"version={ssl_obj.version()}, session_reused={ssl_obj.session_reused}"
  298. )
  299. else:
  300. logger.warning("FTP data connection from %s has no SSL!", self.remote_ip)
  301. logger.info("FTP data connection established from %s", self.remote_ip)
  302. self._data_reader = reader
  303. self._data_writer = writer
  304. # Stop accepting further connections on the passive port
  305. if self.data_server:
  306. self.data_server.close()
  307. self._data_connected.set()
  308. # Keep this callback alive until the transfer command (STOR/RETR)
  309. # finishes. This ensures the asyncio server-handler task holds strong
  310. # references to reader/writer for the entire transfer lifetime.
  311. await self._transfer_done.wait()
  312. async def _close_data_connection(self) -> None:
  313. """Close the data connection and server."""
  314. had_connection = self._data_writer is not None or self.data_server is not None
  315. # Signal the _handle_data_connection callback to return, allowing
  316. # its asyncio task to complete cleanly.
  317. self._transfer_done.set()
  318. if self._data_writer:
  319. try:
  320. self._data_writer.close()
  321. await self._data_writer.wait_closed()
  322. except OSError:
  323. pass # Best-effort data writer cleanup; peer may have closed already
  324. self._data_writer = None
  325. self._data_reader = None
  326. if self.data_server:
  327. try:
  328. self.data_server.close()
  329. await self.data_server.wait_closed()
  330. except OSError:
  331. pass # Best-effort data server shutdown; port may already be released
  332. self.data_server = None
  333. # Only delay if we actually closed something
  334. if had_connection:
  335. await asyncio.sleep(0.1)
  336. async def cmd_STOR(self, arg: str) -> None:
  337. """Handle STOR command - receive file upload.
  338. Streams each chunk directly to disk inside the receive loop instead
  339. of buffering the whole file in a ``list[bytes]`` and joining at the
  340. end. Wire protocol unchanged — same 150/226/426 sequence, same
  341. single-write target path (no ``.part`` or atomic rename), no new
  342. verbs, no concurrency guard. The visible behaviour difference is
  343. that the destination file grows progressively during upload rather
  344. than appearing all-at-once on completion; slicers don't LIST during
  345. STOR, so this isn't observable. Peak RSS for a multi-GB upload
  346. drops from ~2× file size to one chunk (64 KiB).
  347. ``MAX_UPLOAD_BYTES`` cap kept — purely server-internal DoS guard.
  348. """
  349. if not self.authenticated:
  350. await self.send(530, "Not logged in")
  351. return
  352. if not self.data_server and not self._data_connected.is_set():
  353. await self.send(425, "Use PASV first")
  354. return
  355. filename = Path(arg).name # Sanitize filename
  356. file_path = (
  357. self.upload_dir / filename
  358. ) # SEC-PATH-OK: filename = Path(arg).name strips every path component above
  359. logger.info("FTP receiving file: %s from %s", filename, self.remote_ip)
  360. await self.send(150, f"Opening data connection for {filename}")
  361. # Wait for data connection to be established (client connects after 150)
  362. try:
  363. await asyncio.wait_for(self._data_connected.wait(), timeout=30)
  364. except TimeoutError:
  365. logger.error("FTP data connection timeout - client didn't connect")
  366. await self.send(425, "Data connection timeout")
  367. await self._close_data_connection()
  368. return
  369. if not self._data_reader:
  370. await self.send(425, "Data connection failed")
  371. await self._close_data_connection()
  372. return
  373. # Receive + stream to disk
  374. total_received = 0
  375. write_failed: Exception | None = None
  376. try:
  377. with file_path.open("wb") as f:
  378. while True:
  379. chunk = await asyncio.wait_for(self._data_reader.read(65536), timeout=60)
  380. if not chunk:
  381. break
  382. total_received += len(chunk)
  383. if total_received > MAX_UPLOAD_BYTES:
  384. raise OSError(f"upload exceeded size cap ({total_received} > {MAX_UPLOAD_BYTES} bytes)")
  385. f.write(chunk)
  386. logger.debug("FTP received chunk: %s bytes (total: %s)", len(chunk), total_received)
  387. except TimeoutError:
  388. logger.error("FTP data transfer timeout after %s bytes for %s", total_received, filename)
  389. write_failed = TimeoutError("Transfer timeout")
  390. except Exception as e:
  391. logger.error(
  392. "FTP data transfer error after %s bytes for %s: %s(%s)",
  393. total_received,
  394. filename,
  395. type(e).__name__,
  396. e,
  397. )
  398. write_failed = e
  399. # Close data connection
  400. await self._close_data_connection()
  401. if write_failed is not None:
  402. # Drop the partial file so it doesn't masquerade as a complete
  403. # upload — buffer-then-write never had a partial-file footprint.
  404. try:
  405. file_path.unlink(missing_ok=True)
  406. except OSError:
  407. pass
  408. await self.send(426, f"Transfer failed: {write_failed}")
  409. return
  410. # Confirm + notify
  411. logger.info("FTP saved file: %s (%s bytes)", file_path, total_received)
  412. await self.send(226, "Transfer complete")
  413. if self.on_file_received:
  414. try:
  415. result = self.on_file_received(file_path, self.remote_ip)
  416. if asyncio.iscoroutine(result):
  417. await result
  418. except Exception as e:
  419. logger.error("File received callback error: %s", e)
  420. async def cmd_SIZE(self, arg: str) -> None:
  421. """Handle SIZE command."""
  422. if not self.authenticated:
  423. await self.send(530, "Not logged in")
  424. return
  425. # We don't store files for SIZE queries
  426. await self.send(550, "File not found")
  427. async def cmd_QUIT(self, arg: str) -> None:
  428. """Handle QUIT command."""
  429. await self.send(221, "Goodbye")
  430. raise asyncio.CancelledError()
  431. async def cmd_NOOP(self, arg: str) -> None:
  432. """Handle NOOP command."""
  433. await self.send(200, "OK")
  434. async def cmd_OPTS(self, arg: str) -> None:
  435. """Handle OPTS command."""
  436. if arg.upper().startswith("UTF8"):
  437. await self.send(200, "UTF8 mode enabled")
  438. else:
  439. await self.send(501, "Option not supported")
  440. async def cmd_PBSZ(self, arg: str) -> None:
  441. """Handle PBSZ (Protection Buffer Size) command.
  442. Required for FTP security extensions. With TLS, buffer size is 0.
  443. """
  444. await self.send(200, "PBSZ=0")
  445. async def cmd_PROT(self, arg: str) -> None:
  446. """Handle PROT (Data Channel Protection Level) command.
  447. P = Private (encrypted), which we always use with implicit FTPS.
  448. """
  449. if arg.upper() == "P":
  450. await self.send(200, "Protection level set to Private")
  451. elif arg.upper() == "C":
  452. # Clear (unprotected) - we don't support this
  453. await self.send(536, "Protection level C not supported")
  454. else:
  455. await self.send(504, f"Protection level {arg} not supported")
  456. async def cmd_MKD(self, arg: str) -> None:
  457. """Handle MKD (Make Directory) command."""
  458. if not self.authenticated:
  459. await self.send(530, "Not logged in")
  460. return
  461. # We don't really create directories, just pretend it works
  462. await self.send(257, f'"{arg}" directory created')
  463. async def cmd_LIST(self, arg: str) -> None:
  464. """Handle LIST command - list directory contents.
  465. Intentionally answers 150 + 226 without opening the passive data
  466. channel. Bambuddy is an upload-only VP — no slicer in capture logs
  467. actually issues LIST during the project_file flow, so the
  468. no-data-conn ack is what every observed slicer accepts. A previous
  469. audit recommended opening + closing the data conn for protocol
  470. purity; reverted because (a) the bug was theoretical, (b) slicer
  471. compatibility matters more than RFC purity here, and (c) adding
  472. NLST/MLSD alongside changes the "supported verbs" surface in a way
  473. we cannot regression-test without every supported slicer build.
  474. """
  475. if not self.authenticated:
  476. await self.send(530, "Not logged in")
  477. return
  478. # We don't support listing, return empty
  479. await self.send(150, "Opening data connection")
  480. await self.send(226, "Transfer complete")
  481. PASSIVE_PORT_BASE = 50000
  482. PASSIVE_SLICE_SIZE = 10
  483. PASSIVE_MAX_SLOTS = 100
  484. def compute_passive_port_slice(vp_id: int) -> tuple[int, int]:
  485. """Return the (min, max) passive-mode data port range for VP `vp_id`.
  486. Each VP gets a unique non-overlapping slice so bridge-mode Docker users
  487. only need to expose `PASSIVE_SLICE_SIZE * <vp count>` ports instead of
  488. the full historical 1001-port pool (#1646 — wide pool × Docker's
  489. userland-proxy spawned ~2000 host processes at ~3.5 GB RAM). vp_id is
  490. taken modulo PASSIVE_MAX_SLOTS so installs that have churned through
  491. many VPs over time still produce a valid in-range slice; a same-slot
  492. collision falls back to the existing per-session 10-attempt random
  493. retry, which is the pre-#1646 behaviour and recovers gracefully.
  494. """
  495. slot = (max(vp_id, 1) - 1) % PASSIVE_MAX_SLOTS
  496. port_min = PASSIVE_PORT_BASE + slot * PASSIVE_SLICE_SIZE
  497. port_max = port_min + PASSIVE_SLICE_SIZE - 1
  498. return port_min, port_max
  499. class VirtualPrinterFTPServer:
  500. """Implicit FTPS server that accepts uploads from slicers.
  501. Each VP is given a small non-overlapping passive-mode data-port slice
  502. via `passive_port_min/passive_port_max` (typically computed by
  503. `compute_passive_port_slice(vp_id)` at the call site). The slice is
  504. intentionally narrow — 10 ports per VP fits Bambu-style one-passive-
  505. socket-per-upload sessions with safe headroom, and bridge-mode docker
  506. setups only have to expose `N_vps * 10` ports instead of the historical
  507. 1001-port pool (#1646).
  508. """
  509. def __init__(
  510. self,
  511. upload_dir: Path,
  512. access_code: str,
  513. cert_path: Path,
  514. key_path: Path,
  515. port: int = FTP_PORT,
  516. on_file_received: Callable[[Path, str], None] | None = None,
  517. bind_address: str = "0.0.0.0", # nosec B104
  518. vp_name: str = "",
  519. passive_port_min: int = PASSIVE_PORT_BASE,
  520. passive_port_max: int = PASSIVE_PORT_BASE + PASSIVE_SLICE_SIZE - 1,
  521. ):
  522. """Initialize the FTPS server.
  523. Args:
  524. upload_dir: Directory to store uploaded files
  525. access_code: Password for authentication (bblp user)
  526. cert_path: Path to TLS certificate file
  527. key_path: Path to TLS private key file
  528. port: Port to listen on (default 990)
  529. on_file_received: Callback when file upload completes (path, source_ip)
  530. bind_address: IP address to bind to (default 0.0.0.0)
  531. vp_name: Virtual printer name for log identification
  532. passive_port_min: Low end of this VP's passive-mode data port slice
  533. (inclusive). Per-VP slicing eliminates cross-VP collisions on
  534. shared 0.0.0.0 binds without paying for a 1001-port pool (#1646).
  535. passive_port_max: High end of the slice (inclusive). Defaults
  536. produce a 10-port window starting at PASSIVE_PORT_BASE.
  537. """
  538. self.upload_dir = upload_dir
  539. self.access_code = access_code
  540. self.cert_path = cert_path
  541. self.key_path = key_path
  542. self.port = port
  543. self.on_file_received = on_file_received
  544. self.bind_address = bind_address
  545. self.vp_name = vp_name
  546. self.passive_port_min = passive_port_min
  547. self.passive_port_max = passive_port_max
  548. self._server: asyncio.Server | None = None
  549. self._running = False
  550. # Set after the socket is bound and the server is accepting connections,
  551. # so VirtualPrinterInstance.start_server can wait for readiness before
  552. # reporting is_running=True. Without this, a caller racing the start
  553. # could probe the port and see "connection refused" while is_running
  554. # already says yes.
  555. self.ready = asyncio.Event()
  556. self._ssl_context: ssl.SSLContext | None = None
  557. self._active_sessions: list[asyncio.Task] = []
  558. # Override PASV response IP for Docker bridge mode / NAT environments
  559. self._pasv_address = os.environ.get("VIRTUAL_PRINTER_PASV_ADDRESS", "")
  560. async def start(self) -> None:
  561. """Start the implicit FTPS server."""
  562. if self._running:
  563. return
  564. logger.info("[%s] Starting virtual printer implicit FTPS on %s:%s", self.vp_name, self.bind_address, self.port)
  565. # Ensure upload directory exists
  566. self.upload_dir.mkdir(parents=True, exist_ok=True)
  567. cache_dir = self.upload_dir / "cache"
  568. cache_dir.mkdir(exist_ok=True)
  569. # Create SSL context for implicit FTPS (TLS from byte 0).
  570. # Pinned to TLS 1.2 only. Allowing 1.3 broke BambuStudio mid-upload
  571. # in the field (session_reused=True on data channel via PSK + libcurl
  572. # CURLE_PARTIAL_FILE / RST after ~80 KiB; "server did not report OK,
  573. # got 426"). Real Bambu printers also serve their FTPS at 1.2 only,
  574. # and the slicer expects to match that. A future slicer drop of 1.2
  575. # is a problem to solve when it actually happens; until then 1.2 is
  576. # mandatory for compat.
  577. self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
  578. self._ssl_context.load_cert_chain(str(self.cert_path), str(self.key_path))
  579. self._ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
  580. self._ssl_context.maximum_version = ssl.TLSVersion.TLSv1_2
  581. # Keep the historical `HIGH:!aNULL:!MD5:!RC4` baseline so the cipher
  582. # set stays a strict superset of what shipped before (the previous
  583. # set offered ~58 extra suites — CCM, ARIA, CAMELLIA, DSS variants —
  584. # that no Bambu slicer is known to pick, but the
  585. # [[feedback_dont_remove_compat_pinning]] HARD RULE says don't
  586. # narrow a compat surface without proof). The two explicit additions
  587. # cover the #1610 case on hardened distros (Fedora / RHEL with
  588. # `update-crypto-policies`, hardened Alpine builds) where the system
  589. # policy strips the plain-RSA `AES256-GCM-SHA384` / `AES128-GCM-SHA256`
  590. # suites from `HIGH` — without them present the slicer's FTPS
  591. # ClientHello (which mimics the cipher set real Bambu printers offer)
  592. # finds no overlap and the handshake aborts. Listing them explicitly
  593. # survives any system policy that strips them from `HIGH`.
  594. self._ssl_context.set_ciphers("HIGH:AES256-GCM-SHA384:AES128-GCM-SHA256:!aNULL:!MD5:!RC4")
  595. logger.info("FTP SSL context created with standard settings")
  596. try:
  597. # Create server with SSL - TLS handshake happens before any FTP data
  598. self._server = await asyncio.start_server(
  599. self._handle_client,
  600. self.bind_address,
  601. self.port,
  602. ssl=self._ssl_context, # This makes it implicit FTPS!
  603. )
  604. self._running = True
  605. self.ready.set()
  606. logger.info("Implicit FTPS server started on port %s", self.port)
  607. logger.info(
  608. "FTP passive data port range: %s-%s",
  609. self.passive_port_min,
  610. self.passive_port_max,
  611. )
  612. if self._pasv_address:
  613. logger.info("FTP PASV address override: %s", self._pasv_address)
  614. async with self._server:
  615. await self._server.serve_forever()
  616. except OSError as e:
  617. if e.errno == 98: # Address already in use
  618. logger.error("FTP port %s is already in use", self.port)
  619. else:
  620. logger.error("FTP server error: %s", e)
  621. except asyncio.CancelledError:
  622. logger.debug("FTP server task cancelled")
  623. except Exception as e:
  624. logger.error("FTP server error: %s", e)
  625. finally:
  626. await self.stop()
  627. async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
  628. """Handle a new FTP client connection."""
  629. peername = writer.get_extra_info("peername")
  630. log_prefix = f"[{self.vp_name}] " if self.vp_name else ""
  631. logger.info("%sFTP connection from %s", log_prefix, peername)
  632. session = FTPSession(
  633. reader=reader,
  634. writer=writer,
  635. upload_dir=self.upload_dir,
  636. access_code=self.access_code,
  637. ssl_context=self._ssl_context,
  638. on_file_received=self.on_file_received,
  639. passive_port_range=(self.passive_port_min, self.passive_port_max),
  640. pasv_address=self._pasv_address,
  641. bind_address=self.bind_address,
  642. vp_name=self.vp_name,
  643. )
  644. # Track the session task so we can cancel it on stop
  645. task = asyncio.current_task()
  646. if task:
  647. self._active_sessions.append(task)
  648. try:
  649. await session.handle()
  650. finally:
  651. if task and task in self._active_sessions:
  652. self._active_sessions.remove(task)
  653. async def stop(self) -> None:
  654. """Stop the FTPS server."""
  655. logger.info("Stopping FTP server")
  656. self._running = False
  657. self.ready.clear()
  658. # Cancel all active sessions and AWAIT cancellation. Previously
  659. # this slept 0.1 s and called it good — a session mid-write,
  660. # mid-TLS handshake, or holding a 60 s data-read could easily
  661. # outlive that and then ``_server.close()`` would run while the
  662. # underlying sockets were still in use.
  663. for task in self._active_sessions[:]:
  664. task.cancel()
  665. if self._active_sessions:
  666. await asyncio.gather(*self._active_sessions, return_exceptions=True)
  667. self._active_sessions.clear()
  668. if self._server:
  669. try:
  670. self._server.close()
  671. await self._server.wait_closed()
  672. except OSError as e:
  673. logger.debug("Error closing FTP server: %s", e)
  674. self._server = None