ftp_server.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 = self.upload_dir / filename
  357. logger.info("FTP receiving file: %s from %s", filename, self.remote_ip)
  358. await self.send(150, f"Opening data connection for {filename}")
  359. # Wait for data connection to be established (client connects after 150)
  360. try:
  361. await asyncio.wait_for(self._data_connected.wait(), timeout=30)
  362. except TimeoutError:
  363. logger.error("FTP data connection timeout - client didn't connect")
  364. await self.send(425, "Data connection timeout")
  365. await self._close_data_connection()
  366. return
  367. if not self._data_reader:
  368. await self.send(425, "Data connection failed")
  369. await self._close_data_connection()
  370. return
  371. # Receive + stream to disk
  372. total_received = 0
  373. write_failed: Exception | None = None
  374. try:
  375. with file_path.open("wb") as f:
  376. while True:
  377. chunk = await asyncio.wait_for(self._data_reader.read(65536), timeout=60)
  378. if not chunk:
  379. break
  380. total_received += len(chunk)
  381. if total_received > MAX_UPLOAD_BYTES:
  382. raise OSError(f"upload exceeded size cap ({total_received} > {MAX_UPLOAD_BYTES} bytes)")
  383. f.write(chunk)
  384. logger.debug("FTP received chunk: %s bytes (total: %s)", len(chunk), total_received)
  385. except TimeoutError:
  386. logger.error("FTP data transfer timeout after %s bytes for %s", total_received, filename)
  387. write_failed = TimeoutError("Transfer timeout")
  388. except Exception as e:
  389. logger.error(
  390. "FTP data transfer error after %s bytes for %s: %s(%s)",
  391. total_received,
  392. filename,
  393. type(e).__name__,
  394. e,
  395. )
  396. write_failed = e
  397. # Close data connection
  398. await self._close_data_connection()
  399. if write_failed is not None:
  400. # Drop the partial file so it doesn't masquerade as a complete
  401. # upload — buffer-then-write never had a partial-file footprint.
  402. try:
  403. file_path.unlink(missing_ok=True)
  404. except OSError:
  405. pass
  406. await self.send(426, f"Transfer failed: {write_failed}")
  407. return
  408. # Confirm + notify
  409. logger.info("FTP saved file: %s (%s bytes)", file_path, total_received)
  410. await self.send(226, "Transfer complete")
  411. if self.on_file_received:
  412. try:
  413. result = self.on_file_received(file_path, self.remote_ip)
  414. if asyncio.iscoroutine(result):
  415. await result
  416. except Exception as e:
  417. logger.error("File received callback error: %s", e)
  418. async def cmd_SIZE(self, arg: str) -> None:
  419. """Handle SIZE command."""
  420. if not self.authenticated:
  421. await self.send(530, "Not logged in")
  422. return
  423. # We don't store files for SIZE queries
  424. await self.send(550, "File not found")
  425. async def cmd_QUIT(self, arg: str) -> None:
  426. """Handle QUIT command."""
  427. await self.send(221, "Goodbye")
  428. raise asyncio.CancelledError()
  429. async def cmd_NOOP(self, arg: str) -> None:
  430. """Handle NOOP command."""
  431. await self.send(200, "OK")
  432. async def cmd_OPTS(self, arg: str) -> None:
  433. """Handle OPTS command."""
  434. if arg.upper().startswith("UTF8"):
  435. await self.send(200, "UTF8 mode enabled")
  436. else:
  437. await self.send(501, "Option not supported")
  438. async def cmd_PBSZ(self, arg: str) -> None:
  439. """Handle PBSZ (Protection Buffer Size) command.
  440. Required for FTP security extensions. With TLS, buffer size is 0.
  441. """
  442. await self.send(200, "PBSZ=0")
  443. async def cmd_PROT(self, arg: str) -> None:
  444. """Handle PROT (Data Channel Protection Level) command.
  445. P = Private (encrypted), which we always use with implicit FTPS.
  446. """
  447. if arg.upper() == "P":
  448. await self.send(200, "Protection level set to Private")
  449. elif arg.upper() == "C":
  450. # Clear (unprotected) - we don't support this
  451. await self.send(536, "Protection level C not supported")
  452. else:
  453. await self.send(504, f"Protection level {arg} not supported")
  454. async def cmd_MKD(self, arg: str) -> None:
  455. """Handle MKD (Make Directory) command."""
  456. if not self.authenticated:
  457. await self.send(530, "Not logged in")
  458. return
  459. # We don't really create directories, just pretend it works
  460. await self.send(257, f'"{arg}" directory created')
  461. async def cmd_LIST(self, arg: str) -> None:
  462. """Handle LIST command - list directory contents.
  463. Intentionally answers 150 + 226 without opening the passive data
  464. channel. Bambuddy is an upload-only VP — no slicer in capture logs
  465. actually issues LIST during the project_file flow, so the
  466. no-data-conn ack is what every observed slicer accepts. A previous
  467. audit recommended opening + closing the data conn for protocol
  468. purity; reverted because (a) the bug was theoretical, (b) slicer
  469. compatibility matters more than RFC purity here, and (c) adding
  470. NLST/MLSD alongside changes the "supported verbs" surface in a way
  471. we cannot regression-test without every supported slicer build.
  472. """
  473. if not self.authenticated:
  474. await self.send(530, "Not logged in")
  475. return
  476. # We don't support listing, return empty
  477. await self.send(150, "Opening data connection")
  478. await self.send(226, "Transfer complete")
  479. class VirtualPrinterFTPServer:
  480. """Implicit FTPS server that accepts uploads from slicers."""
  481. # Passive-mode data port range. Widened from 50000-50100 (101 ports) to
  482. # 50000-51000 (1001 ports) so concurrent transfers across multiple VPs
  483. # — particularly when a VP falls back to bind 0.0.0.0 (manager.py picks
  484. # this when bind_ip is unset) — don't collide. With 101 ports and 10
  485. # random pick attempts per session, birthday-style collisions hit
  486. # under load; 1001 ports gives multi-VP setups headroom.
  487. PASSIVE_PORT_MIN = 50000
  488. PASSIVE_PORT_MAX = 51000
  489. def __init__(
  490. self,
  491. upload_dir: Path,
  492. access_code: str,
  493. cert_path: Path,
  494. key_path: Path,
  495. port: int = FTP_PORT,
  496. on_file_received: Callable[[Path, str], None] | None = None,
  497. bind_address: str = "0.0.0.0", # nosec B104
  498. vp_name: str = "",
  499. ):
  500. """Initialize the FTPS server.
  501. Args:
  502. upload_dir: Directory to store uploaded files
  503. access_code: Password for authentication (bblp user)
  504. cert_path: Path to TLS certificate file
  505. key_path: Path to TLS private key file
  506. port: Port to listen on (default 990)
  507. on_file_received: Callback when file upload completes (path, source_ip)
  508. bind_address: IP address to bind to (default 0.0.0.0)
  509. vp_name: Virtual printer name for log identification
  510. """
  511. self.upload_dir = upload_dir
  512. self.access_code = access_code
  513. self.cert_path = cert_path
  514. self.key_path = key_path
  515. self.port = port
  516. self.on_file_received = on_file_received
  517. self.bind_address = bind_address
  518. self.vp_name = vp_name
  519. self._server: asyncio.Server | None = None
  520. self._running = False
  521. # Set after the socket is bound and the server is accepting connections,
  522. # so VirtualPrinterInstance.start_server can wait for readiness before
  523. # reporting is_running=True. Without this, a caller racing the start
  524. # could probe the port and see "connection refused" while is_running
  525. # already says yes.
  526. self.ready = asyncio.Event()
  527. self._ssl_context: ssl.SSLContext | None = None
  528. self._active_sessions: list[asyncio.Task] = []
  529. # Override PASV response IP for Docker bridge mode / NAT environments
  530. self._pasv_address = os.environ.get("VIRTUAL_PRINTER_PASV_ADDRESS", "")
  531. async def start(self) -> None:
  532. """Start the implicit FTPS server."""
  533. if self._running:
  534. return
  535. logger.info("[%s] Starting virtual printer implicit FTPS on %s:%s", self.vp_name, self.bind_address, self.port)
  536. # Ensure upload directory exists
  537. self.upload_dir.mkdir(parents=True, exist_ok=True)
  538. cache_dir = self.upload_dir / "cache"
  539. cache_dir.mkdir(exist_ok=True)
  540. # Create SSL context for implicit FTPS (TLS from byte 0).
  541. # Pinned to TLS 1.2 only. Allowing 1.3 broke BambuStudio mid-upload
  542. # in the field (session_reused=True on data channel via PSK + libcurl
  543. # CURLE_PARTIAL_FILE / RST after ~80 KiB; "server did not report OK,
  544. # got 426"). Real Bambu printers also serve their FTPS at 1.2 only,
  545. # and the slicer expects to match that. A future slicer drop of 1.2
  546. # is a problem to solve when it actually happens; until then 1.2 is
  547. # mandatory for compat.
  548. self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
  549. self._ssl_context.load_cert_chain(str(self.cert_path), str(self.key_path))
  550. self._ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
  551. self._ssl_context.maximum_version = ssl.TLSVersion.TLSv1_2
  552. # Use standard TLS settings for compatibility
  553. self._ssl_context.set_ciphers("HIGH:!aNULL:!MD5:!RC4")
  554. logger.info("FTP SSL context created with standard settings")
  555. try:
  556. # Create server with SSL - TLS handshake happens before any FTP data
  557. self._server = await asyncio.start_server(
  558. self._handle_client,
  559. self.bind_address,
  560. self.port,
  561. ssl=self._ssl_context, # This makes it implicit FTPS!
  562. )
  563. self._running = True
  564. self.ready.set()
  565. logger.info("Implicit FTPS server started on port %s", self.port)
  566. logger.info(
  567. "FTP passive data port range: %s-%s",
  568. self.PASSIVE_PORT_MIN,
  569. self.PASSIVE_PORT_MAX,
  570. )
  571. if self._pasv_address:
  572. logger.info("FTP PASV address override: %s", self._pasv_address)
  573. async with self._server:
  574. await self._server.serve_forever()
  575. except OSError as e:
  576. if e.errno == 98: # Address already in use
  577. logger.error("FTP port %s is already in use", self.port)
  578. else:
  579. logger.error("FTP server error: %s", e)
  580. except asyncio.CancelledError:
  581. logger.debug("FTP server task cancelled")
  582. except Exception as e:
  583. logger.error("FTP server error: %s", e)
  584. finally:
  585. await self.stop()
  586. async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
  587. """Handle a new FTP client connection."""
  588. peername = writer.get_extra_info("peername")
  589. log_prefix = f"[{self.vp_name}] " if self.vp_name else ""
  590. logger.info("%sFTP connection from %s", log_prefix, peername)
  591. session = FTPSession(
  592. reader=reader,
  593. writer=writer,
  594. upload_dir=self.upload_dir,
  595. access_code=self.access_code,
  596. ssl_context=self._ssl_context,
  597. on_file_received=self.on_file_received,
  598. passive_port_range=(self.PASSIVE_PORT_MIN, self.PASSIVE_PORT_MAX),
  599. pasv_address=self._pasv_address,
  600. bind_address=self.bind_address,
  601. vp_name=self.vp_name,
  602. )
  603. # Track the session task so we can cancel it on stop
  604. task = asyncio.current_task()
  605. if task:
  606. self._active_sessions.append(task)
  607. try:
  608. await session.handle()
  609. finally:
  610. if task and task in self._active_sessions:
  611. self._active_sessions.remove(task)
  612. async def stop(self) -> None:
  613. """Stop the FTPS server."""
  614. logger.info("Stopping FTP server")
  615. self._running = False
  616. self.ready.clear()
  617. # Cancel all active sessions and AWAIT cancellation. Previously
  618. # this slept 0.1 s and called it good — a session mid-write,
  619. # mid-TLS handshake, or holding a 60 s data-read could easily
  620. # outlive that and then ``_server.close()`` would run while the
  621. # underlying sockets were still in use.
  622. for task in self._active_sessions[:]:
  623. task.cancel()
  624. if self._active_sessions:
  625. await asyncio.gather(*self._active_sessions, return_exceptions=True)
  626. self._active_sessions.clear()
  627. if self._server:
  628. try:
  629. self._server.close()
  630. await self._server.wait_closed()
  631. except OSError as e:
  632. logger.debug("Error closing FTP server: %s", e)
  633. self._server = None