ftp_server.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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 logging
  9. import os
  10. import random
  11. import ssl
  12. from collections.abc import Callable
  13. from pathlib import Path
  14. logger = logging.getLogger(__name__)
  15. # Default FTP port for Bambu printers (implicit FTPS).
  16. # Must be 990 (same as real printers) to avoid iptables REDIRECT,
  17. # which rewrites the destination IP to the incoming interface's primary
  18. # address — breaking multi-VP setups with different bind IPs.
  19. # Requires CAP_NET_BIND_SERVICE or root.
  20. FTP_PORT = 990
  21. class FTPSession:
  22. """Handles a single FTP client session."""
  23. def __init__(
  24. self,
  25. reader: asyncio.StreamReader,
  26. writer: asyncio.StreamWriter,
  27. upload_dir: Path,
  28. access_code: str,
  29. ssl_context: ssl.SSLContext,
  30. on_file_received: Callable[[Path, str], None] | None,
  31. passive_port_range: tuple[int, int] = (50000, 50100),
  32. pasv_address: str = "",
  33. bind_address: str = "0.0.0.0", # nosec B104
  34. vp_name: str = "",
  35. ):
  36. self.reader = reader
  37. self.writer = writer
  38. self.upload_dir = upload_dir
  39. self.access_code = access_code
  40. self.ssl_context = ssl_context
  41. self.on_file_received = on_file_received
  42. self.passive_port_range = passive_port_range
  43. self.pasv_address = pasv_address
  44. self.bind_address = bind_address
  45. self.vp_name = vp_name
  46. self._log_prefix = f"[{vp_name}] " if vp_name else ""
  47. self.authenticated = False
  48. self.username: str | None = None
  49. self.current_dir = upload_dir
  50. self.transfer_type = "A" # ASCII by default
  51. self.data_server: asyncio.Server | None = None
  52. self.data_port: int | None = None
  53. # For data transfer coordination
  54. self._data_reader: asyncio.StreamReader | None = None
  55. self._data_writer: asyncio.StreamWriter | None = None
  56. self._data_connected = asyncio.Event()
  57. self._transfer_done = asyncio.Event()
  58. peername = writer.get_extra_info("peername")
  59. self.remote_ip = peername[0] if peername else "unknown"
  60. async def send(self, code: int, message: str) -> None:
  61. """Send an FTP response."""
  62. response = f"{code} {message}\r\n"
  63. logger.debug("%sFTP -> %s: %s", self._log_prefix, self.remote_ip, response.strip())
  64. self.writer.write(response.encode("utf-8"))
  65. await self.writer.drain()
  66. async def handle(self) -> None:
  67. """Handle the FTP session."""
  68. try:
  69. # Send welcome banner
  70. await self.send(220, "Bambuddy Virtual Printer FTP ready")
  71. while True:
  72. try:
  73. line = await asyncio.wait_for(
  74. self.reader.readline(),
  75. timeout=300, # 5 minute timeout
  76. )
  77. except TimeoutError:
  78. logger.debug("%sFTP session timeout from %s", self._log_prefix, self.remote_ip)
  79. break
  80. if not line:
  81. break
  82. try:
  83. command_line = line.decode("utf-8").strip()
  84. except UnicodeDecodeError:
  85. command_line = line.decode("latin-1").strip()
  86. if not command_line:
  87. continue
  88. # Never log passwords
  89. if command_line.upper().startswith("PASS"):
  90. logger.debug("%sFTP <- %s: PASS ********", self._log_prefix, self.remote_ip)
  91. else:
  92. logger.debug("%sFTP <- %s: %s", self._log_prefix, self.remote_ip, command_line)
  93. # Parse command and argument
  94. parts = command_line.split(" ", 1)
  95. cmd = parts[0].upper()
  96. arg = parts[1] if len(parts) > 1 else ""
  97. # Dispatch command
  98. handler = getattr(self, f"cmd_{cmd}", None)
  99. if handler:
  100. await handler(arg)
  101. else:
  102. logger.debug("%sFTP command not implemented: %s", self._log_prefix, cmd)
  103. await self.send(502, f"Command {cmd} not implemented")
  104. except asyncio.CancelledError:
  105. logger.info("%sFTP session cancelled from %s", self._log_prefix, self.remote_ip)
  106. except Exception as e:
  107. logger.error("%sFTP session error from %s: %s", self._log_prefix, self.remote_ip, e)
  108. finally:
  109. logger.info("%sFTP session ended from %s", self._log_prefix, self.remote_ip)
  110. await self._cleanup()
  111. async def _cleanup(self) -> None:
  112. """Clean up session resources."""
  113. # Release any waiting data connection callback
  114. self._transfer_done.set()
  115. if self.data_server:
  116. self.data_server.close()
  117. try:
  118. await self.data_server.wait_closed()
  119. except OSError:
  120. pass # Best-effort data server cleanup; may already be closed
  121. self.data_server = None
  122. try:
  123. self.writer.close()
  124. await self.writer.wait_closed()
  125. except OSError:
  126. pass # Best-effort control connection cleanup; client may have disconnected
  127. # FTP Commands
  128. async def cmd_USER(self, arg: str) -> None:
  129. """Handle USER command."""
  130. self.username = arg
  131. if arg.lower() == "bblp":
  132. await self.send(331, "Password required")
  133. else:
  134. await self.send(530, "Invalid user")
  135. async def cmd_PASS(self, arg: str) -> None:
  136. """Handle PASS command."""
  137. if self.username and self.username.lower() == "bblp":
  138. if arg == self.access_code:
  139. self.authenticated = True
  140. await self.send(230, "Login successful")
  141. logger.info("%sFTP login from %s", self._log_prefix, self.remote_ip)
  142. else:
  143. await self.send(530, "Login incorrect")
  144. logger.warning("%sFTP failed login from %s (access code mismatch)", self._log_prefix, self.remote_ip)
  145. else:
  146. await self.send(503, "Login with USER first")
  147. async def cmd_SYST(self, arg: str) -> None:
  148. """Handle SYST command."""
  149. await self.send(215, "UNIX Type: L8")
  150. async def cmd_FEAT(self, arg: str) -> None:
  151. """Handle FEAT command."""
  152. features = [
  153. "211-Features:",
  154. " PASV",
  155. " EPSV",
  156. " UTF8",
  157. " SIZE",
  158. "211 End",
  159. ]
  160. for line in features[:-1]:
  161. self.writer.write(f"{line}\r\n".encode())
  162. await self.writer.drain()
  163. self.writer.write(f"{features[-1]}\r\n".encode())
  164. await self.writer.drain()
  165. async def cmd_PWD(self, arg: str) -> None:
  166. """Handle PWD command."""
  167. if not self.authenticated:
  168. await self.send(530, "Not logged in")
  169. return
  170. await self.send(257, '"/" is current directory')
  171. async def cmd_CWD(self, arg: str) -> None:
  172. """Handle CWD command."""
  173. if not self.authenticated:
  174. await self.send(530, "Not logged in")
  175. return
  176. # Accept any directory change (we use a flat structure)
  177. await self.send(250, "Directory changed")
  178. async def cmd_TYPE(self, arg: str) -> None:
  179. """Handle TYPE command."""
  180. if not self.authenticated:
  181. await self.send(530, "Not logged in")
  182. return
  183. if arg.upper() in ("A", "I"):
  184. self.transfer_type = arg.upper()
  185. type_name = "ASCII" if arg.upper() == "A" else "Binary"
  186. await self.send(200, f"Type set to {type_name}")
  187. else:
  188. await self.send(504, "Type not supported")
  189. async def _bind_passive_port(self) -> bool:
  190. """Try to bind a passive data port with retries.
  191. Returns True if a port was successfully bound, False otherwise.
  192. Sets self.data_server and self.data_port on success.
  193. """
  194. port_min, port_max = self.passive_port_range
  195. for attempt in range(10):
  196. port = random.randint(port_min, port_max)
  197. try:
  198. self.data_server = await asyncio.start_server(
  199. self._handle_data_connection,
  200. self.bind_address,
  201. port,
  202. ssl=self.ssl_context,
  203. )
  204. self.data_port = port
  205. return True
  206. except OSError:
  207. logger.debug("FTP passive port %s in use, retrying (%s/10)", port, attempt + 1)
  208. return False
  209. async def cmd_EPSV(self, arg: str) -> None:
  210. """Handle EPSV command - Extended Passive Mode (IPv6 compatible)."""
  211. if not self.authenticated:
  212. await self.send(530, "Not logged in")
  213. return
  214. # Close any existing data connection/server
  215. await self._close_data_connection()
  216. # Reset connection state for the new transfer
  217. self._data_connected.clear()
  218. self._data_reader = None
  219. self._data_writer = None
  220. self._transfer_done = asyncio.Event()
  221. if await self._bind_passive_port():
  222. # EPSV response format: 229 Entering Extended Passive Mode (|||port|)
  223. await self.send(229, f"Entering Extended Passive Mode (|||{self.data_port}|)")
  224. logger.info("FTP EPSV listening on port %s", self.data_port)
  225. else:
  226. logger.error("Failed to bind any passive port for EPSV")
  227. await self.send(425, "Cannot open data connection")
  228. async def cmd_PASV(self, arg: str) -> None:
  229. """Handle PASV command - set up passive data connection."""
  230. if not self.authenticated:
  231. await self.send(530, "Not logged in")
  232. return
  233. # Close any existing data connection/server
  234. await self._close_data_connection()
  235. # Reset connection state for the new transfer
  236. self._data_connected.clear()
  237. self._data_reader = None
  238. self._data_writer = None
  239. self._transfer_done = asyncio.Event()
  240. if await self._bind_passive_port():
  241. # Determine the IP to advertise in PASV response
  242. if self.pasv_address:
  243. # Explicit override (e.g., for Docker bridge mode behind NAT)
  244. ip = self.pasv_address
  245. else:
  246. # Use the local IP of the control connection
  247. sockname = self.writer.get_extra_info("sockname")
  248. ip = sockname[0] if sockname else "127.0.0.1"
  249. # 0.0.0.0 is not routable — fall back to control connection IP
  250. if ip == "0.0.0.0": # nosec B104
  251. ip = "127.0.0.1"
  252. # Format IP and port for PASV response
  253. ip_parts = ip.split(".")
  254. port_hi = self.data_port // 256
  255. port_lo = self.data_port % 256
  256. await self.send(
  257. 227,
  258. f"Entering Passive Mode ({ip_parts[0]},{ip_parts[1]},{ip_parts[2]},{ip_parts[3]},{port_hi},{port_lo})",
  259. )
  260. logger.info("FTP PASV listening on %s:%s", ip, self.data_port)
  261. else:
  262. logger.error("Failed to bind any passive port for PASV")
  263. await self.send(425, "Cannot open data connection")
  264. async def _handle_data_connection(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
  265. """Handle incoming data connection (used by PASV/EPSV).
  266. This callback stays alive until the transfer completes to ensure the
  267. asyncio task holds strong references to the reader/writer throughout
  268. the data transfer. If the callback returned immediately, the task
  269. would complete and the StreamReaderProtocol could release its strong
  270. reader reference, potentially destabilising the connection.
  271. """
  272. # Reject duplicate connections — only one data connection per transfer
  273. if self._data_reader is not None:
  274. logger.warning("FTP rejecting duplicate data connection from %s", self.remote_ip)
  275. try:
  276. writer.close()
  277. await writer.wait_closed()
  278. except OSError:
  279. pass
  280. return
  281. # Log TLS details for debugging
  282. ssl_obj = writer.get_extra_info("ssl_object")
  283. if ssl_obj:
  284. logger.info(
  285. f"FTP data TLS from {self.remote_ip}: cipher={ssl_obj.cipher()}, "
  286. f"version={ssl_obj.version()}, session_reused={ssl_obj.session_reused}"
  287. )
  288. else:
  289. logger.warning("FTP data connection from %s has no SSL!", self.remote_ip)
  290. logger.info("FTP data connection established from %s", self.remote_ip)
  291. self._data_reader = reader
  292. self._data_writer = writer
  293. # Stop accepting further connections on the passive port
  294. if self.data_server:
  295. self.data_server.close()
  296. self._data_connected.set()
  297. # Keep this callback alive until the transfer command (STOR/RETR)
  298. # finishes. This ensures the asyncio server-handler task holds strong
  299. # references to reader/writer for the entire transfer lifetime.
  300. await self._transfer_done.wait()
  301. async def _close_data_connection(self) -> None:
  302. """Close the data connection and server."""
  303. had_connection = self._data_writer is not None or self.data_server is not None
  304. # Signal the _handle_data_connection callback to return, allowing
  305. # its asyncio task to complete cleanly.
  306. self._transfer_done.set()
  307. if self._data_writer:
  308. try:
  309. self._data_writer.close()
  310. await self._data_writer.wait_closed()
  311. except OSError:
  312. pass # Best-effort data writer cleanup; peer may have closed already
  313. self._data_writer = None
  314. self._data_reader = None
  315. if self.data_server:
  316. try:
  317. self.data_server.close()
  318. await self.data_server.wait_closed()
  319. except OSError:
  320. pass # Best-effort data server shutdown; port may already be released
  321. self.data_server = None
  322. # Only delay if we actually closed something
  323. if had_connection:
  324. await asyncio.sleep(0.1)
  325. async def cmd_STOR(self, arg: str) -> None:
  326. """Handle STOR command - receive file upload."""
  327. if not self.authenticated:
  328. await self.send(530, "Not logged in")
  329. return
  330. if not self.data_server and not self._data_connected.is_set():
  331. await self.send(425, "Use PASV first")
  332. return
  333. filename = Path(arg).name # Sanitize filename
  334. file_path = self.upload_dir / filename
  335. logger.info("FTP receiving file: %s from %s", filename, self.remote_ip)
  336. await self.send(150, f"Opening data connection for {filename}")
  337. # Wait for data connection to be established (client connects after 150)
  338. try:
  339. await asyncio.wait_for(self._data_connected.wait(), timeout=30)
  340. except TimeoutError:
  341. logger.error("FTP data connection timeout - client didn't connect")
  342. await self.send(425, "Data connection timeout")
  343. await self._close_data_connection()
  344. return
  345. if not self._data_reader:
  346. await self.send(425, "Data connection failed")
  347. await self._close_data_connection()
  348. return
  349. # Receive data
  350. data_content: list[bytes] = []
  351. total_received = 0
  352. try:
  353. while True:
  354. chunk = await asyncio.wait_for(self._data_reader.read(65536), timeout=60)
  355. if not chunk:
  356. break
  357. data_content.append(chunk)
  358. total_received += len(chunk)
  359. logger.debug("FTP received chunk: %s bytes (total: %s)", len(chunk), total_received)
  360. except TimeoutError:
  361. logger.error("FTP data transfer timeout after %s bytes for %s", total_received, filename)
  362. await self.send(426, "Transfer timeout")
  363. await self._close_data_connection()
  364. return
  365. except Exception as e:
  366. logger.error(
  367. "FTP data transfer error after %s bytes for %s: %s(%s)",
  368. total_received,
  369. filename,
  370. type(e).__name__,
  371. e,
  372. )
  373. await self.send(426, f"Transfer failed: {e}")
  374. await self._close_data_connection()
  375. return
  376. # Close data connection
  377. await self._close_data_connection()
  378. # Write file
  379. try:
  380. total_size = sum(len(c) for c in data_content)
  381. file_path.write_bytes(b"".join(data_content))
  382. logger.info("FTP saved file: %s (%s bytes)", file_path, total_size)
  383. await self.send(226, "Transfer complete")
  384. # Notify callback
  385. if self.on_file_received:
  386. try:
  387. result = self.on_file_received(file_path, self.remote_ip)
  388. if asyncio.iscoroutine(result):
  389. await result
  390. except Exception as e:
  391. logger.error("File received callback error: %s", e)
  392. except Exception as e:
  393. logger.error("Failed to save file %s: %s", file_path, e)
  394. await self.send(550, "Failed to save file")
  395. async def cmd_SIZE(self, arg: str) -> None:
  396. """Handle SIZE command."""
  397. if not self.authenticated:
  398. await self.send(530, "Not logged in")
  399. return
  400. # We don't store files for SIZE queries
  401. await self.send(550, "File not found")
  402. async def cmd_QUIT(self, arg: str) -> None:
  403. """Handle QUIT command."""
  404. await self.send(221, "Goodbye")
  405. raise asyncio.CancelledError()
  406. async def cmd_NOOP(self, arg: str) -> None:
  407. """Handle NOOP command."""
  408. await self.send(200, "OK")
  409. async def cmd_OPTS(self, arg: str) -> None:
  410. """Handle OPTS command."""
  411. if arg.upper().startswith("UTF8"):
  412. await self.send(200, "UTF8 mode enabled")
  413. else:
  414. await self.send(501, "Option not supported")
  415. async def cmd_PBSZ(self, arg: str) -> None:
  416. """Handle PBSZ (Protection Buffer Size) command.
  417. Required for FTP security extensions. With TLS, buffer size is 0.
  418. """
  419. await self.send(200, "PBSZ=0")
  420. async def cmd_PROT(self, arg: str) -> None:
  421. """Handle PROT (Data Channel Protection Level) command.
  422. P = Private (encrypted), which we always use with implicit FTPS.
  423. """
  424. if arg.upper() == "P":
  425. await self.send(200, "Protection level set to Private")
  426. elif arg.upper() == "C":
  427. # Clear (unprotected) - we don't support this
  428. await self.send(536, "Protection level C not supported")
  429. else:
  430. await self.send(504, f"Protection level {arg} not supported")
  431. async def cmd_MKD(self, arg: str) -> None:
  432. """Handle MKD (Make Directory) command."""
  433. if not self.authenticated:
  434. await self.send(530, "Not logged in")
  435. return
  436. # We don't really create directories, just pretend it works
  437. await self.send(257, f'"{arg}" directory created')
  438. async def cmd_LIST(self, arg: str) -> None:
  439. """Handle LIST command - list directory contents."""
  440. if not self.authenticated:
  441. await self.send(530, "Not logged in")
  442. return
  443. # We don't support listing, return empty
  444. await self.send(150, "Opening data connection")
  445. await self.send(226, "Transfer complete")
  446. class VirtualPrinterFTPServer:
  447. """Implicit FTPS server that accepts uploads from slicers."""
  448. PASSIVE_PORT_MIN = 50000
  449. PASSIVE_PORT_MAX = 50100
  450. def __init__(
  451. self,
  452. upload_dir: Path,
  453. access_code: str,
  454. cert_path: Path,
  455. key_path: Path,
  456. port: int = FTP_PORT,
  457. on_file_received: Callable[[Path, str], None] | None = None,
  458. bind_address: str = "0.0.0.0", # nosec B104
  459. vp_name: str = "",
  460. ):
  461. """Initialize the FTPS server.
  462. Args:
  463. upload_dir: Directory to store uploaded files
  464. access_code: Password for authentication (bblp user)
  465. cert_path: Path to TLS certificate file
  466. key_path: Path to TLS private key file
  467. port: Port to listen on (default 990)
  468. on_file_received: Callback when file upload completes (path, source_ip)
  469. bind_address: IP address to bind to (default 0.0.0.0)
  470. vp_name: Virtual printer name for log identification
  471. """
  472. self.upload_dir = upload_dir
  473. self.access_code = access_code
  474. self.cert_path = cert_path
  475. self.key_path = key_path
  476. self.port = port
  477. self.on_file_received = on_file_received
  478. self.bind_address = bind_address
  479. self.vp_name = vp_name
  480. self._server: asyncio.Server | None = None
  481. self._running = False
  482. self._ssl_context: ssl.SSLContext | None = None
  483. self._active_sessions: list[asyncio.Task] = []
  484. # Override PASV response IP for Docker bridge mode / NAT environments
  485. self._pasv_address = os.environ.get("VIRTUAL_PRINTER_PASV_ADDRESS", "")
  486. async def start(self) -> None:
  487. """Start the implicit FTPS server."""
  488. if self._running:
  489. return
  490. logger.info("[%s] Starting virtual printer implicit FTPS on %s:%s", self.vp_name, self.bind_address, self.port)
  491. # Ensure upload directory exists
  492. self.upload_dir.mkdir(parents=True, exist_ok=True)
  493. cache_dir = self.upload_dir / "cache"
  494. cache_dir.mkdir(exist_ok=True)
  495. # Create SSL context for implicit FTPS (TLS from byte 0)
  496. self._ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
  497. self._ssl_context.load_cert_chain(str(self.cert_path), str(self.key_path))
  498. self._ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2
  499. self._ssl_context.maximum_version = ssl.TLSVersion.TLSv1_2
  500. # Use standard TLS settings for compatibility
  501. self._ssl_context.set_ciphers("HIGH:!aNULL:!MD5:!RC4")
  502. logger.info("FTP SSL context created with standard settings")
  503. try:
  504. # Create server with SSL - TLS handshake happens before any FTP data
  505. self._server = await asyncio.start_server(
  506. self._handle_client,
  507. self.bind_address,
  508. self.port,
  509. ssl=self._ssl_context, # This makes it implicit FTPS!
  510. )
  511. self._running = True
  512. logger.info("Implicit FTPS server started on port %s", self.port)
  513. logger.info(
  514. "FTP passive data port range: %s-%s",
  515. self.PASSIVE_PORT_MIN,
  516. self.PASSIVE_PORT_MAX,
  517. )
  518. if self._pasv_address:
  519. logger.info("FTP PASV address override: %s", self._pasv_address)
  520. async with self._server:
  521. await self._server.serve_forever()
  522. except OSError as e:
  523. if e.errno == 98: # Address already in use
  524. logger.error("FTP port %s is already in use", self.port)
  525. else:
  526. logger.error("FTP server error: %s", e)
  527. except asyncio.CancelledError:
  528. logger.debug("FTP server task cancelled")
  529. except Exception as e:
  530. logger.error("FTP server error: %s", e)
  531. finally:
  532. await self.stop()
  533. async def _handle_client(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
  534. """Handle a new FTP client connection."""
  535. peername = writer.get_extra_info("peername")
  536. log_prefix = f"[{self.vp_name}] " if self.vp_name else ""
  537. logger.info("%sFTP connection from %s", log_prefix, peername)
  538. session = FTPSession(
  539. reader=reader,
  540. writer=writer,
  541. upload_dir=self.upload_dir,
  542. access_code=self.access_code,
  543. ssl_context=self._ssl_context,
  544. on_file_received=self.on_file_received,
  545. passive_port_range=(self.PASSIVE_PORT_MIN, self.PASSIVE_PORT_MAX),
  546. pasv_address=self._pasv_address,
  547. bind_address=self.bind_address,
  548. vp_name=self.vp_name,
  549. )
  550. # Track the session task so we can cancel it on stop
  551. task = asyncio.current_task()
  552. if task:
  553. self._active_sessions.append(task)
  554. try:
  555. await session.handle()
  556. finally:
  557. if task and task in self._active_sessions:
  558. self._active_sessions.remove(task)
  559. async def stop(self) -> None:
  560. """Stop the FTPS server."""
  561. logger.info("Stopping FTP server")
  562. self._running = False
  563. # Cancel all active sessions first
  564. for task in self._active_sessions[:]: # Copy list to avoid modification during iteration
  565. task.cancel()
  566. # Wait briefly for sessions to clean up
  567. if self._active_sessions:
  568. await asyncio.sleep(0.1)
  569. self._active_sessions.clear()
  570. if self._server:
  571. try:
  572. self._server.close()
  573. await self._server.wait_closed()
  574. except OSError as e:
  575. logger.debug("Error closing FTP server: %s", e)
  576. self._server = None