tcp_proxy.py 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  1. """Proxy for slicer-to-printer communication.
  2. This module provides both transparent TCP proxying and TLS-terminating
  3. proxying for forwarding data between a slicer and a real Bambu printer,
  4. enabling remote printing over any network connection.
  5. Most protocols (FTP, FileTransfer, Camera) use transparent TCP proxying —
  6. raw bytes are forwarded without decryption, preserving end-to-end TLS
  7. between slicer and printer. Only MQTT is TLS-terminated so Bambuddy can
  8. rewrite the printer's real IP with the proxy's bind IP in MQTT payloads.
  9. """
  10. # ruff: noqa: N801
  11. import asyncio
  12. import logging
  13. import random
  14. import re
  15. import ssl
  16. import subprocess
  17. from collections.abc import Callable
  18. from pathlib import Path
  19. logger = logging.getLogger(__name__)
  20. class _SessionReuseSSLContext:
  21. """Proxy around SSLContext that injects a TLS session into wrap_bio().
  22. vsFTPd (used by some Bambu printers like X1C) requires TLS session reuse
  23. on FTP data channels — the data connection must reuse the TLS session from
  24. the control channel. Without this, the printer rejects the data connection
  25. with "522 SSL connection failed: session reuse required".
  26. asyncio's open_connection() calls SSLContext.wrap_bio() internally but
  27. doesn't expose a session parameter. This wrapper intercepts wrap_bio()
  28. to inject the saved control-channel session, enabling session reuse.
  29. """
  30. def __init__(self, ctx: ssl.SSLContext, session: ssl.SSLSession) -> None:
  31. object.__setattr__(self, "_ctx", ctx)
  32. object.__setattr__(self, "_session", session)
  33. def __getattr__(self, name: str) -> object:
  34. return getattr(self._ctx, name)
  35. def wrap_bio(
  36. self,
  37. incoming: ssl.MemoryBIO,
  38. outgoing: ssl.MemoryBIO,
  39. server_side: bool = False,
  40. server_hostname: str | None = None,
  41. **kwargs: object,
  42. ) -> ssl.SSLObject:
  43. return self._ctx.wrap_bio(
  44. incoming,
  45. outgoing,
  46. server_side=server_side,
  47. server_hostname=server_hostname,
  48. session=self._session,
  49. **kwargs,
  50. )
  51. def detect_port_redirect(port: int) -> int | None:
  52. """Detect if iptables redirects a port to another port.
  53. When iptables NAT REDIRECT rules exist (e.g. port redirects), connections
  54. to the original port never reach our socket because iptables intercepts
  55. them in PREROUTING. We must listen on the redirect target instead.
  56. Returns the redirect target port, or None if no redirect is active.
  57. """
  58. # Method 1: Read persistent rules file (doesn't require root)
  59. for rules_path in ("/etc/iptables/rules.v4", "/etc/iptables.rules"):
  60. try:
  61. with open(rules_path) as f:
  62. content = f.read()
  63. match = re.search(rf"--dport {port}\b.*?--to-ports\s+(\d+)", content)
  64. if match:
  65. target = int(match.group(1))
  66. if target != port:
  67. return target
  68. except (FileNotFoundError, PermissionError, OSError):
  69. continue
  70. # Method 2: Query live iptables rules (may require root)
  71. try:
  72. result = subprocess.run( # noqa: S603, S607
  73. ["iptables-save", "-t", "nat"],
  74. capture_output=True,
  75. text=True,
  76. timeout=5,
  77. )
  78. if result.returncode == 0:
  79. match = re.search(rf"--dport {port}\b.*?--to-ports\s+(\d+)", result.stdout)
  80. if match:
  81. target = int(match.group(1))
  82. if target != port:
  83. return target
  84. except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
  85. pass
  86. return None
  87. class TLSProxy:
  88. """TLS terminating proxy that forwards data between client and target.
  89. This proxy terminates TLS on both ends, allowing the slicer to connect
  90. to Bambuddy's certificate while Bambuddy connects to the real printer.
  91. """
  92. def __init__(
  93. self,
  94. name: str,
  95. listen_port: int,
  96. target_host: str,
  97. target_port: int,
  98. server_cert_path: Path,
  99. server_key_path: Path,
  100. on_connect: Callable[[str], None] | None = None,
  101. on_disconnect: Callable[[str], None] | None = None,
  102. bind_address: str = "0.0.0.0", # nosec B104
  103. rewrite_ip: tuple[str, str] | None = None,
  104. ):
  105. """Initialize the TLS proxy.
  106. Args:
  107. name: Friendly name for logging (e.g., "FTP", "MQTT")
  108. listen_port: Port to listen on for incoming connections
  109. target_host: Target printer IP/hostname
  110. target_port: Target printer port
  111. server_cert_path: Path to server certificate (for accepting slicer connections)
  112. server_key_path: Path to server private key
  113. on_connect: Optional callback when client connects (receives client_id)
  114. on_disconnect: Optional callback when client disconnects (receives client_id)
  115. bind_address: IP address to bind to (default: all interfaces)
  116. rewrite_ip: Optional (old_ip, new_ip) tuple — replaces occurrences of
  117. the printer's real IP with the proxy's bind IP in printer→client data.
  118. This prevents the slicer from discovering the printer's real IP
  119. in MQTT payloads (ip_addr, rtsp_url, etc.) and bypassing the proxy.
  120. """
  121. self.name = name
  122. self.listen_port = listen_port
  123. self.target_host = target_host
  124. self.target_port = target_port
  125. self.server_cert_path = server_cert_path
  126. self.server_key_path = server_key_path
  127. self.on_connect = on_connect
  128. self.on_disconnect = on_disconnect
  129. self.bind_address = bind_address
  130. # IP rewriting for printer→client direction
  131. if rewrite_ip:
  132. self._rewrite_old = rewrite_ip[0].encode("utf-8")
  133. self._rewrite_new = rewrite_ip[1].encode("utf-8")
  134. # Also rewrite the integer IP in net.info[].ip fields.
  135. # Bambu printers encode their IP as a little-endian uint32 integer
  136. # in the JSON payload. BambuStudio reads this to set dev_ip.
  137. self._rewrite_old_int = self._ip_to_le_int_bytes(rewrite_ip[0])
  138. self._rewrite_new_int = self._ip_to_le_int_bytes(rewrite_ip[1])
  139. else:
  140. self._rewrite_old = None
  141. self._rewrite_new = None
  142. self._rewrite_old_int = None
  143. self._rewrite_new_int = None
  144. self._server: asyncio.Server | None = None
  145. self._running = False
  146. self._active_connections: dict[str, tuple[asyncio.Task, asyncio.Task]] = {}
  147. self._server_ssl_context: ssl.SSLContext | None = None
  148. self._client_ssl_context: ssl.SSLContext | None = None
  149. @staticmethod
  150. def _ip_to_le_int_bytes(ip: str) -> bytes:
  151. """Convert an IP address to its little-endian integer JSON representation.
  152. E.g. "192.168.255.16" → b"285190336" (the integer as a decimal string,
  153. as it appears in Bambu MQTT JSON payloads in the net.info[].ip field).
  154. """
  155. import struct as _struct
  156. parts = ip.split(".")
  157. packed = bytes(int(p) for p in parts)
  158. le_int = _struct.unpack("<I", packed)[0]
  159. return str(le_int).encode("utf-8")
  160. def _create_server_ssl_context(self) -> ssl.SSLContext:
  161. """Create SSL context for accepting client (slicer) connections."""
  162. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
  163. ctx.load_cert_chain(self.server_cert_path, self.server_key_path)
  164. # Allow older TLS versions for compatibility with slicers
  165. ctx.minimum_version = ssl.TLSVersion.TLSv1_2
  166. # Don't require client certificates
  167. ctx.verify_mode = ssl.CERT_NONE
  168. return ctx
  169. def _create_client_ssl_context(self) -> ssl.SSLContext:
  170. """Create SSL context for connecting to printer."""
  171. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
  172. # Don't verify printer's certificate (self-signed)
  173. ctx.check_hostname = False
  174. ctx.verify_mode = ssl.CERT_NONE
  175. ctx.minimum_version = ssl.TLSVersion.TLSv1_2
  176. # Bambu printers use plain RSA key exchange (no ECDHE/DHE),
  177. # which modern OpenSSL 3.x defaults exclude. Add them back.
  178. ctx.set_ciphers("DEFAULT:AES256-GCM-SHA384:AES128-GCM-SHA256")
  179. return ctx
  180. async def start(self) -> None:
  181. """Start the TLS proxy server."""
  182. if self._running:
  183. return
  184. logger.info(
  185. f"Starting {self.name} TLS proxy: {self.bind_address}:{self.listen_port} → {self.target_host}:{self.target_port}"
  186. )
  187. try:
  188. self._running = True
  189. # Create SSL contexts
  190. self._server_ssl_context = self._create_server_ssl_context()
  191. self._client_ssl_context = self._create_client_ssl_context()
  192. # Start server with TLS
  193. self._server = await asyncio.start_server(
  194. self._handle_client,
  195. self.bind_address,
  196. self.listen_port,
  197. ssl=self._server_ssl_context,
  198. )
  199. logger.info("%s TLS proxy listening on port %s", self.name, self.listen_port)
  200. async with self._server:
  201. await self._server.serve_forever()
  202. except OSError as e:
  203. if e.errno == 98: # Address already in use
  204. logger.error("%s proxy port %s is already in use", self.name, self.listen_port)
  205. elif e.errno == 13: # Permission denied
  206. logger.error(
  207. "%s proxy: cannot bind to port %s (permission denied). "
  208. "Port %s requires root or CAP_NET_BIND_SERVICE. "
  209. "Docker: add 'cap_add: [NET_BIND_SERVICE]' to docker-compose.yml. "
  210. "Native: use 'sudo setcap cap_net_bind_service=+ep $(which python3)' "
  211. "or redirect with iptables.",
  212. self.name,
  213. self.listen_port,
  214. self.listen_port,
  215. )
  216. else:
  217. logger.error("%s proxy error: %s", self.name, e)
  218. except asyncio.CancelledError:
  219. logger.debug("%s proxy task cancelled", self.name)
  220. except Exception as e:
  221. logger.error("%s proxy error: %s", self.name, e)
  222. finally:
  223. await self.stop()
  224. async def stop(self) -> None:
  225. """Stop the TLS proxy server."""
  226. logger.info("Stopping %s proxy", self.name)
  227. self._running = False
  228. # Cancel all active connection tasks
  229. for client_id, (task1, task2) in list(self._active_connections.items()):
  230. task1.cancel()
  231. task2.cancel()
  232. if self.on_disconnect:
  233. try:
  234. self.on_disconnect(client_id)
  235. except Exception:
  236. pass # Ignore disconnect callback errors during shutdown
  237. self._active_connections.clear()
  238. if self._server:
  239. try:
  240. self._server.close()
  241. await self._server.wait_closed()
  242. except OSError as e:
  243. logger.debug("Error closing %s proxy server: %s", self.name, e)
  244. self._server = None
  245. async def _handle_client(
  246. self,
  247. client_reader: asyncio.StreamReader,
  248. client_writer: asyncio.StreamWriter,
  249. ) -> None:
  250. """Handle a new client connection by proxying to target."""
  251. peername = client_writer.get_extra_info("peername")
  252. client_id = f"{peername[0]}:{peername[1]}" if peername else "unknown"
  253. logger.info("%s proxy: client connected from %s", self.name, client_id)
  254. if self.on_connect:
  255. try:
  256. self.on_connect(client_id)
  257. except Exception:
  258. pass # Ignore connect callback errors; connection proceeds regardless
  259. # Connect to target printer with TLS
  260. try:
  261. printer_reader, printer_writer = await asyncio.wait_for(
  262. asyncio.open_connection(
  263. self.target_host,
  264. self.target_port,
  265. ssl=self._client_ssl_context,
  266. ),
  267. timeout=10.0,
  268. )
  269. logger.info("%s proxy: connected to printer %s:%s", self.name, self.target_host, self.target_port)
  270. except TimeoutError:
  271. logger.error("%s proxy: timeout connecting to %s:%s", self.name, self.target_host, self.target_port)
  272. client_writer.close()
  273. await client_writer.wait_closed()
  274. return
  275. except ssl.SSLError as e:
  276. logger.error(
  277. "%s proxy: SSL error connecting to %s:%s: %s", self.name, self.target_host, self.target_port, e
  278. )
  279. client_writer.close()
  280. await client_writer.wait_closed()
  281. return
  282. except OSError as e:
  283. logger.error("%s proxy: failed to connect to %s:%s: %s", self.name, self.target_host, self.target_port, e)
  284. client_writer.close()
  285. await client_writer.wait_closed()
  286. return
  287. # Create bidirectional forwarding tasks
  288. client_to_printer = asyncio.create_task(
  289. self._forward(client_reader, printer_writer, f"{client_id}→printer"),
  290. name=f"{self.name}_c2p_{client_id}",
  291. )
  292. printer_to_client = asyncio.create_task(
  293. self._forward(printer_reader, client_writer, f"printer→{client_id}", rewrite_ip=True),
  294. name=f"{self.name}_p2c_{client_id}",
  295. )
  296. self._active_connections[client_id] = (client_to_printer, printer_to_client)
  297. try:
  298. # Wait for either direction to complete (connection closed)
  299. done, pending = await asyncio.wait(
  300. [client_to_printer, printer_to_client],
  301. return_when=asyncio.FIRST_COMPLETED,
  302. )
  303. # Cancel the other direction
  304. for task in pending:
  305. task.cancel()
  306. try:
  307. await task
  308. except asyncio.CancelledError:
  309. pass # Expected when cancelling the other forwarding direction
  310. except Exception as e:
  311. logger.debug("%s proxy connection error: %s", self.name, e)
  312. finally:
  313. # Clean up
  314. self._active_connections.pop(client_id, None)
  315. for writer in [client_writer, printer_writer]:
  316. try:
  317. writer.close()
  318. await writer.wait_closed()
  319. except OSError:
  320. pass # Best-effort connection cleanup; peer may have disconnected
  321. logger.info("%s proxy: client %s disconnected", self.name, client_id)
  322. if self.on_disconnect:
  323. try:
  324. self.on_disconnect(client_id)
  325. except Exception:
  326. pass # Ignore disconnect callback errors; cleanup continues
  327. @staticmethod
  328. def _rewrite_mqtt_ip(
  329. data: bytes,
  330. old_ip: bytes,
  331. new_ip: bytes,
  332. buffer: bytearray,
  333. extra_replacements: list[tuple[bytes, bytes]] | None = None,
  334. ) -> tuple[bytes, bytearray]:
  335. """Rewrite IP addresses inside MQTT packets, preserving packet framing.
  336. MQTT packets have a variable-length header encoding the remaining
  337. packet length. A naive bytes.replace() would corrupt this framing
  338. when old_ip and new_ip differ in length.
  339. This method parses individual MQTT packets out of the data stream,
  340. performs the replacement only on PUBLISH payloads, and re-encodes
  341. the remaining-length field to match the new size.
  342. Incomplete packets are buffered and returned for the next call.
  343. Args:
  344. extra_replacements: Additional (old, new) byte pairs to replace
  345. (e.g. the integer IP representation in net.info[].ip).
  346. Returns (output_data, remaining_buffer).
  347. """
  348. buffer.extend(data)
  349. # Check if any replacement target exists in the buffer
  350. has_target = old_ip in buffer
  351. if not has_target and extra_replacements:
  352. has_target = any(old in buffer for old, _new in extra_replacements)
  353. if not has_target:
  354. # Fast path: no IP in buffer, but we still need to check for
  355. # incomplete packets at the end that might contain a partial IP.
  356. # For safety, try to parse and emit only complete packets.
  357. result = bytearray()
  358. pos = 0
  359. length = len(buffer)
  360. while pos < length:
  361. packet_start = pos
  362. if pos + 1 >= length:
  363. break
  364. pos += 1 # header byte
  365. # Parse remaining length
  366. remaining_length = 0
  367. multiplier = 1
  368. length_bytes = 0
  369. while pos < length:
  370. encoded_byte = buffer[pos]
  371. pos += 1
  372. remaining_length += (encoded_byte & 0x7F) * multiplier
  373. multiplier *= 128
  374. length_bytes += 1
  375. if (encoded_byte & 0x80) == 0:
  376. break
  377. if length_bytes >= 4:
  378. break
  379. if pos + remaining_length > length:
  380. # Incomplete — keep in buffer
  381. new_buffer = bytearray(buffer[packet_start:])
  382. return bytes(result), new_buffer
  383. pos += remaining_length
  384. result.extend(buffer[packet_start:pos])
  385. # All complete
  386. buffer.clear()
  387. return bytes(result) if result else bytes(data), buffer
  388. # Buffer contains old_ip — parse packets and rewrite
  389. result = bytearray()
  390. pos = 0
  391. length = len(buffer)
  392. while pos < length:
  393. packet_start = pos
  394. if pos >= length:
  395. break
  396. header_byte = buffer[pos]
  397. pos += 1
  398. # Remaining length: variable-length encoding (1-4 bytes)
  399. remaining_length = 0
  400. multiplier = 1
  401. length_bytes = 0
  402. while pos < length:
  403. encoded_byte = buffer[pos]
  404. pos += 1
  405. remaining_length += (encoded_byte & 0x7F) * multiplier
  406. multiplier *= 128
  407. length_bytes += 1
  408. if (encoded_byte & 0x80) == 0:
  409. break
  410. if length_bytes >= 4:
  411. break
  412. # Check if we have enough data for the full packet
  413. if pos + remaining_length > length:
  414. # Incomplete packet — keep in buffer for next call
  415. new_buffer = bytearray(buffer[packet_start:])
  416. return bytes(result), new_buffer
  417. packet_type = (header_byte >> 4) & 0x0F
  418. packet_body = buffer[pos : pos + remaining_length]
  419. pos += remaining_length
  420. # Only rewrite PUBLISH packets (type 3)
  421. needs_rewrite = packet_type == 3 and (
  422. old_ip in packet_body
  423. or (extra_replacements and any(old in packet_body for old, _new in extra_replacements))
  424. )
  425. if needs_rewrite:
  426. new_body = bytes(packet_body).replace(old_ip, new_ip)
  427. if extra_replacements:
  428. for old_val, new_val in extra_replacements:
  429. new_body = new_body.replace(old_val, new_val)
  430. # Re-encode: header byte + new remaining length + new body
  431. result.append(header_byte)
  432. # Encode remaining length (MQTT variable-length encoding)
  433. new_remaining = len(new_body)
  434. while True:
  435. encoded_byte = new_remaining % 128
  436. new_remaining //= 128
  437. if new_remaining > 0:
  438. encoded_byte |= 0x80
  439. result.append(encoded_byte)
  440. if new_remaining == 0:
  441. break
  442. result.extend(new_body)
  443. else:
  444. # Pass through unchanged
  445. result.extend(buffer[packet_start:pos])
  446. buffer.clear()
  447. return bytes(result), buffer
  448. async def _forward(
  449. self,
  450. reader: asyncio.StreamReader,
  451. writer: asyncio.StreamWriter,
  452. direction: str,
  453. rewrite_ip: bool = False,
  454. ) -> None:
  455. """Forward data from reader to writer.
  456. Args:
  457. reader: Source stream (already TLS-decrypted)
  458. writer: Destination stream (will be TLS-encrypted by the stream)
  459. direction: Description for logging (e.g., "client→printer")
  460. rewrite_ip: If True and rewrite_ip was configured, replace the
  461. printer's real IP with the proxy's bind IP in the data.
  462. """
  463. do_rewrite = rewrite_ip and self._rewrite_old is not None
  464. rewrite_buffer = bytearray() if do_rewrite else None
  465. rewrite_logged = False
  466. total_bytes = 0
  467. try:
  468. while self._running:
  469. # Read chunk - use reasonable buffer size
  470. data = await reader.read(65536)
  471. if not data:
  472. # Connection closed
  473. break
  474. # Rewrite printer IP → proxy IP in MQTT PUBLISH payloads
  475. # to prevent the slicer from bypassing the proxy.
  476. if do_rewrite:
  477. extra = [(self._rewrite_old_int, self._rewrite_new_int)] if self._rewrite_old_int else None
  478. data, rewrite_buffer = self._rewrite_mqtt_ip(
  479. data,
  480. self._rewrite_old,
  481. self._rewrite_new,
  482. rewrite_buffer,
  483. extra_replacements=extra,
  484. )
  485. if not rewrite_logged and data:
  486. if self._rewrite_old in data:
  487. logger.warning(
  488. "%s proxy IP rewrite FAILED — %s still present after rewrite!",
  489. self.name,
  490. self._rewrite_old.decode(),
  491. )
  492. else:
  493. logger.info(
  494. "%s proxy IP rewrite active: %s → %s",
  495. self.name,
  496. self._rewrite_old.decode(),
  497. self._rewrite_new.decode(),
  498. )
  499. rewrite_logged = True
  500. if not data:
  501. continue # All data buffered, waiting for more
  502. # Forward to destination
  503. writer.write(data)
  504. await writer.drain()
  505. total_bytes += len(data)
  506. except asyncio.CancelledError:
  507. pass # Expected when the other forwarding direction closes first
  508. except ConnectionResetError:
  509. logger.debug("%s proxy %s: connection reset", self.name, direction)
  510. except BrokenPipeError:
  511. logger.debug("%s proxy %s: broken pipe", self.name, direction)
  512. except OSError as e:
  513. logger.debug("%s proxy %s error: %s", self.name, direction, e)
  514. logger.debug("%s proxy %s: total %s bytes", self.name, direction, total_bytes)
  515. class TCPProxy:
  516. """Raw TCP proxy that forwards data without TLS termination.
  517. Used for protocols where the printer doesn't use TLS (e.g., port 3000
  518. binding/authentication protocol).
  519. """
  520. def __init__(
  521. self,
  522. name: str,
  523. listen_port: int,
  524. target_host: str,
  525. target_port: int,
  526. on_connect: Callable[[str], None] | None = None,
  527. on_disconnect: Callable[[str], None] | None = None,
  528. bind_address: str = "0.0.0.0", # nosec B104
  529. ):
  530. self.name = name
  531. self.listen_port = listen_port
  532. self.target_host = target_host
  533. self.target_port = target_port
  534. self.on_connect = on_connect
  535. self.on_disconnect = on_disconnect
  536. self.bind_address = bind_address
  537. self._server: asyncio.Server | None = None
  538. self._running = False
  539. self._active_connections: dict[str, tuple[asyncio.Task, asyncio.Task]] = {}
  540. async def start(self) -> None:
  541. """Start the TCP proxy server."""
  542. if self._running:
  543. return
  544. logger.info(
  545. "Starting %s TCP proxy: %s:%s → %s:%s",
  546. self.name,
  547. self.bind_address,
  548. self.listen_port,
  549. self.target_host,
  550. self.target_port,
  551. )
  552. try:
  553. self._running = True
  554. self._server = await asyncio.start_server(
  555. self._handle_client,
  556. self.bind_address,
  557. self.listen_port,
  558. )
  559. logger.info("%s TCP proxy listening on port %s", self.name, self.listen_port)
  560. async with self._server:
  561. await self._server.serve_forever()
  562. except OSError as e:
  563. if e.errno == 98: # Address already in use
  564. logger.error("%s proxy port %s is already in use", self.name, self.listen_port)
  565. else:
  566. logger.error("%s proxy error: %s", self.name, e)
  567. except asyncio.CancelledError:
  568. logger.debug("%s proxy task cancelled", self.name)
  569. except Exception as e:
  570. logger.error("%s proxy error: %s", self.name, e)
  571. finally:
  572. await self.stop()
  573. async def stop(self) -> None:
  574. """Stop the TCP proxy server."""
  575. logger.info("Stopping %s proxy", self.name)
  576. self._running = False
  577. for client_id, (task1, task2) in list(self._active_connections.items()):
  578. task1.cancel()
  579. task2.cancel()
  580. if self.on_disconnect:
  581. try:
  582. self.on_disconnect(client_id)
  583. except Exception:
  584. pass
  585. self._active_connections.clear()
  586. if self._server:
  587. try:
  588. self._server.close()
  589. await self._server.wait_closed()
  590. except OSError as e:
  591. logger.debug("Error closing %s proxy server: %s", self.name, e)
  592. self._server = None
  593. async def _handle_client(
  594. self,
  595. client_reader: asyncio.StreamReader,
  596. client_writer: asyncio.StreamWriter,
  597. ) -> None:
  598. """Handle a new client connection by proxying to target."""
  599. peername = client_writer.get_extra_info("peername")
  600. client_id = f"{peername[0]}:{peername[1]}" if peername else "unknown"
  601. logger.info("%s proxy: client connected from %s", self.name, client_id)
  602. if self.on_connect:
  603. try:
  604. self.on_connect(client_id)
  605. except Exception:
  606. pass
  607. try:
  608. printer_reader, printer_writer = await asyncio.wait_for(
  609. asyncio.open_connection(self.target_host, self.target_port),
  610. timeout=10.0,
  611. )
  612. logger.info("%s proxy: connected to printer %s:%s", self.name, self.target_host, self.target_port)
  613. except TimeoutError:
  614. logger.error("%s proxy: timeout connecting to %s:%s", self.name, self.target_host, self.target_port)
  615. client_writer.close()
  616. await client_writer.wait_closed()
  617. return
  618. except OSError as e:
  619. logger.error("%s proxy: failed to connect to %s:%s: %s", self.name, self.target_host, self.target_port, e)
  620. client_writer.close()
  621. await client_writer.wait_closed()
  622. return
  623. client_to_printer = asyncio.create_task(
  624. self._forward(client_reader, printer_writer, f"{client_id}→printer"),
  625. name=f"{self.name}_c2p_{client_id}",
  626. )
  627. printer_to_client = asyncio.create_task(
  628. self._forward(printer_reader, client_writer, f"printer→{client_id}"),
  629. name=f"{self.name}_p2c_{client_id}",
  630. )
  631. self._active_connections[client_id] = (client_to_printer, printer_to_client)
  632. try:
  633. done, pending = await asyncio.wait(
  634. [client_to_printer, printer_to_client],
  635. return_when=asyncio.FIRST_COMPLETED,
  636. )
  637. for task in pending:
  638. task.cancel()
  639. try:
  640. await task
  641. except asyncio.CancelledError:
  642. pass
  643. except Exception as e:
  644. logger.debug("%s proxy connection error: %s", self.name, e)
  645. finally:
  646. self._active_connections.pop(client_id, None)
  647. for writer in [client_writer, printer_writer]:
  648. try:
  649. writer.close()
  650. await writer.wait_closed()
  651. except OSError:
  652. pass
  653. logger.info("%s proxy: client %s disconnected", self.name, client_id)
  654. if self.on_disconnect:
  655. try:
  656. self.on_disconnect(client_id)
  657. except Exception:
  658. pass
  659. async def _forward(
  660. self,
  661. reader: asyncio.StreamReader,
  662. writer: asyncio.StreamWriter,
  663. direction: str,
  664. ) -> None:
  665. """Forward data from reader to writer."""
  666. total_bytes = 0
  667. try:
  668. while self._running:
  669. data = await reader.read(65536)
  670. if not data:
  671. break
  672. writer.write(data)
  673. await writer.drain()
  674. total_bytes += len(data)
  675. logger.debug("%s proxy %s: %s bytes", self.name, direction, len(data))
  676. except asyncio.CancelledError:
  677. pass
  678. except ConnectionResetError:
  679. logger.debug("%s proxy %s: connection reset", self.name, direction)
  680. except BrokenPipeError:
  681. logger.debug("%s proxy %s: broken pipe", self.name, direction)
  682. except OSError as e:
  683. logger.debug("%s proxy %s error: %s", self.name, direction, e)
  684. logger.debug("%s proxy %s: total %s bytes", self.name, direction, total_bytes)
  685. class FTPTLSProxy(TLSProxy):
  686. """FTP-aware TLS proxy that handles passive data connections.
  687. Extends TLSProxy to intercept PASV/EPSV responses on the FTP control
  688. channel, dynamically create TLS data proxies on local ports, and rewrite
  689. the responses so the slicer connects to the proxy instead of the printer.
  690. Without this, FTP passive data connections bypass the proxy and go directly
  691. to the printer, which fails when the slicer can't reach the printer's IP.
  692. """
  693. PASV_PORT_MIN = 50000
  694. PASV_PORT_MAX = 50100
  695. async def stop(self) -> None:
  696. """Stop proxy and clean up data connection servers."""
  697. # Cancel any pending auto_close timeouts so they don't outlive the
  698. # proxy holding a dead server reference. Without this, up to 101
  699. # tasks lingered for ~60 s after stop(), each gripping a server
  700. # ref + an active_connections slot; under rapid mode-switch the
  701. # ports stayed bound long enough to fail the next start().
  702. for task in list(self._auto_close_tasks):
  703. task.cancel()
  704. if self._auto_close_tasks:
  705. await asyncio.gather(*self._auto_close_tasks, return_exceptions=True)
  706. self._auto_close_tasks.clear()
  707. # Close all data servers
  708. for server in list(self._data_servers):
  709. try:
  710. server.close()
  711. await server.wait_closed()
  712. except OSError:
  713. pass # Best-effort cleanup of data proxy servers
  714. self._data_servers.clear()
  715. await super().stop()
  716. async def start(self) -> None:
  717. """Start the FTP TLS proxy."""
  718. self._data_servers: list[asyncio.Server] = []
  719. self._auto_close_tasks: list[asyncio.Task] = []
  720. await super().start()
  721. async def _handle_client(
  722. self,
  723. client_reader: asyncio.StreamReader,
  724. client_writer: asyncio.StreamWriter,
  725. ) -> None:
  726. """Handle FTP client with PASV/EPSV-aware response forwarding."""
  727. peername = client_writer.get_extra_info("peername")
  728. client_id = f"{peername[0]}:{peername[1]}" if peername else "unknown"
  729. logger.info("%s proxy: client connected from %s", self.name, client_id)
  730. if self.on_connect:
  731. try:
  732. self.on_connect(client_id)
  733. except Exception:
  734. pass # Ignore connect callback errors; connection proceeds regardless
  735. # Determine our local IP from the control connection socket
  736. sockname = client_writer.get_extra_info("sockname")
  737. local_ip = sockname[0] if sockname else "0.0.0.0" # nosec B104
  738. if local_ip in ("0.0.0.0", "::"): # nosec B104
  739. local_ip = "127.0.0.1"
  740. # Connect to target printer with TLS
  741. try:
  742. printer_reader, printer_writer = await asyncio.wait_for(
  743. asyncio.open_connection(
  744. self.target_host,
  745. self.target_port,
  746. ssl=self._client_ssl_context,
  747. ),
  748. timeout=10.0,
  749. )
  750. logger.info("%s proxy: connected to printer %s:%s", self.name, self.target_host, self.target_port)
  751. except TimeoutError:
  752. logger.error("%s proxy: timeout connecting to %s:%s", self.name, self.target_host, self.target_port)
  753. client_writer.close()
  754. await client_writer.wait_closed()
  755. return
  756. except ssl.SSLError as e:
  757. logger.error(
  758. "%s proxy: SSL error connecting to %s:%s: %s", self.name, self.target_host, self.target_port, e
  759. )
  760. client_writer.close()
  761. await client_writer.wait_closed()
  762. return
  763. except OSError as e:
  764. logger.error("%s proxy: failed to connect to %s:%s: %s", self.name, self.target_host, self.target_port, e)
  765. client_writer.close()
  766. await client_writer.wait_closed()
  767. return
  768. # Capture the TLS session from the control channel for data channel
  769. # reuse. vsFTPd (X1C) requires require_ssl_reuse — the data connection
  770. # must present the same TLS session as the control channel.
  771. ctrl_ssl_object = printer_writer.get_extra_info("ssl_object")
  772. ctrl_tls_session = ctrl_ssl_object.session if ctrl_ssl_object else None
  773. if ctrl_tls_session:
  774. logger.debug("%s proxy: captured TLS session for data channel reuse", self.name)
  775. # Track data channel protection level per session.
  776. # PROT C = cleartext data, PROT P = TLS data.
  777. # Default to cleartext — many Bambu printers (A1, H2D) use PROT C.
  778. # If the slicer sends PROT P, we switch to TLS for data connections.
  779. session_state: dict[str, str | ssl.SSLSession] = {"prot": "C"}
  780. if ctrl_tls_session:
  781. session_state["tls_session"] = ctrl_tls_session
  782. # Client→Printer: intercept EPSV and replace with PASV
  783. # EPSV responses only contain a port (no IP), so the slicer reuses
  784. # the control connection IP. If that IP is the real printer (via
  785. # iptables REDIRECT), the data connection bypasses the proxy.
  786. # PASV responses include an explicit IP that we can rewrite.
  787. client_to_printer = asyncio.create_task(
  788. self._forward_ftp_commands(client_reader, printer_writer, f"{client_id}→printer", session_state),
  789. name=f"{self.name}_c2p_{client_id}",
  790. )
  791. # Printer→Client: intercept PASV/EPSV responses
  792. printer_to_client = asyncio.create_task(
  793. self._forward_ftp_control(printer_reader, client_writer, f"printer→{client_id}", local_ip, session_state),
  794. name=f"{self.name}_p2c_{client_id}",
  795. )
  796. self._active_connections[client_id] = (client_to_printer, printer_to_client)
  797. try:
  798. done, pending = await asyncio.wait(
  799. [client_to_printer, printer_to_client],
  800. return_when=asyncio.FIRST_COMPLETED,
  801. )
  802. for task in pending:
  803. task.cancel()
  804. try:
  805. await task
  806. except asyncio.CancelledError:
  807. pass # Expected when cancelling the other forwarding direction
  808. except Exception as e:
  809. logger.debug("%s proxy connection error: %s", self.name, e)
  810. finally:
  811. self._active_connections.pop(client_id, None)
  812. for writer in [client_writer, printer_writer]:
  813. try:
  814. writer.close()
  815. await writer.wait_closed()
  816. except OSError:
  817. pass # Best-effort connection cleanup; peer may have disconnected
  818. logger.info("%s proxy: client %s disconnected", self.name, client_id)
  819. if self.on_disconnect:
  820. try:
  821. self.on_disconnect(client_id)
  822. except Exception:
  823. pass # Ignore disconnect callback errors; cleanup continues
  824. async def _forward_ftp_commands(
  825. self,
  826. reader: asyncio.StreamReader,
  827. writer: asyncio.StreamWriter,
  828. direction: str,
  829. session_state: dict[str, str | ssl.SSLSession],
  830. ) -> None:
  831. """Forward FTP client commands, replacing EPSV with PASV.
  832. EPSV responses only contain a port number — the client reuses the
  833. control connection IP for data. When the control IP is the real
  834. printer (due to iptables REDIRECT), EPSV data connections bypass
  835. the proxy. PASV responses include an explicit IP that the proxy
  836. can rewrite to its own address.
  837. Also tracks PROT P/C commands to know whether data connections
  838. should use TLS or cleartext.
  839. """
  840. buffer = b""
  841. total_bytes = 0
  842. try:
  843. while self._running:
  844. data = await reader.read(65536)
  845. if not data:
  846. break
  847. total_bytes += len(data)
  848. buffer += data
  849. output = b""
  850. while b"\r\n" in buffer:
  851. idx = buffer.index(b"\r\n")
  852. line = buffer[:idx]
  853. buffer = buffer[idx + 2 :]
  854. cmd_upper = line.strip().upper()
  855. # Track PROT level for data channel encryption
  856. if cmd_upper == b"PROT P":
  857. session_state["prot"] = "P"
  858. logger.info("FTP data protection: PROT P (TLS)")
  859. elif cmd_upper == b"PROT C":
  860. session_state["prot"] = "C"
  861. logger.info("FTP data protection: PROT C (cleartext)")
  862. output += line + b"\r\n"
  863. if output:
  864. writer.write(output)
  865. await writer.drain()
  866. logger.debug("%s proxy %s: %s bytes", self.name, direction, len(data))
  867. except asyncio.CancelledError:
  868. pass # Expected when the other forwarding direction closes first
  869. except ConnectionResetError:
  870. logger.debug("%s proxy %s: connection reset", self.name, direction)
  871. except BrokenPipeError:
  872. logger.debug("%s proxy %s: broken pipe", self.name, direction)
  873. except OSError as e:
  874. logger.debug("%s proxy %s error: %s", self.name, direction, e)
  875. if buffer:
  876. try:
  877. writer.write(buffer)
  878. await writer.drain()
  879. except OSError:
  880. pass # Best-effort flush of remaining FTP command data
  881. logger.debug("%s proxy %s: total %s bytes", self.name, direction, total_bytes)
  882. async def _forward_ftp_control(
  883. self,
  884. reader: asyncio.StreamReader,
  885. writer: asyncio.StreamWriter,
  886. direction: str,
  887. local_ip: str,
  888. session_state: dict[str, str | ssl.SSLSession],
  889. ) -> None:
  890. """Forward FTP control channel responses, rewriting PASV/EPSV.
  891. FTP control channel is line-based (\\r\\n terminated). We buffer data
  892. and process complete lines, intercepting 227 (PASV) and 229 (EPSV)
  893. responses to create local data proxies.
  894. """
  895. buffer = b""
  896. total_bytes = 0
  897. try:
  898. while self._running:
  899. data = await reader.read(65536)
  900. if not data:
  901. break
  902. total_bytes += len(data)
  903. buffer += data
  904. output = b""
  905. # Process all complete lines
  906. while b"\r\n" in buffer:
  907. idx = buffer.index(b"\r\n")
  908. line = buffer[:idx]
  909. buffer = buffer[idx + 2 :]
  910. rewritten = await self._maybe_rewrite_pasv(line, local_ip, session_state)
  911. output += rewritten + b"\r\n"
  912. if output:
  913. writer.write(output)
  914. await writer.drain()
  915. logger.debug("%s proxy %s: %s bytes", self.name, direction, len(data))
  916. except asyncio.CancelledError:
  917. pass # Expected when the other forwarding direction closes first
  918. except ConnectionResetError:
  919. logger.debug("%s proxy %s: connection reset", self.name, direction)
  920. except BrokenPipeError:
  921. logger.debug("%s proxy %s: broken pipe", self.name, direction)
  922. except OSError as e:
  923. logger.debug("%s proxy %s error: %s", self.name, direction, e)
  924. # Flush any remaining buffered data
  925. if buffer:
  926. try:
  927. writer.write(buffer)
  928. await writer.drain()
  929. except OSError:
  930. pass # Best-effort flush of remaining FTP control data
  931. logger.debug("%s proxy %s: total %s bytes", self.name, direction, total_bytes)
  932. async def _maybe_rewrite_pasv(
  933. self, line: bytes, local_ip: str, session_state: dict[str, str | ssl.SSLSession]
  934. ) -> bytes:
  935. """Rewrite PASV/EPSV response to point to a local data proxy."""
  936. try:
  937. text = line.decode("utf-8")
  938. except UnicodeDecodeError:
  939. return line
  940. # 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)
  941. if text.startswith("227 "):
  942. match = re.search(r"\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)", text)
  943. if match:
  944. h1, h2, h3, h4, p1, p2 = (int(x) for x in match.groups())
  945. printer_ip = f"{h1}.{h2}.{h3}.{h4}"
  946. printer_port = p1 * 256 + p2
  947. local_port = await self._create_data_proxy(printer_ip, printer_port, session_state)
  948. if local_port:
  949. ip_parts = local_ip.split(".")
  950. lp1 = local_port // 256
  951. lp2 = local_port % 256
  952. rewritten = (
  953. f"227 Entering Passive Mode "
  954. f"({ip_parts[0]},{ip_parts[1]},{ip_parts[2]},{ip_parts[3]},{lp1},{lp2})"
  955. )
  956. logger.info("FTP PASV rewrite: %s:%s → %s:%s", printer_ip, printer_port, local_ip, local_port)
  957. return rewritten.encode("utf-8")
  958. else:
  959. logger.error("FTP PASV: failed to create data proxy for %s:%s", printer_ip, printer_port)
  960. else:
  961. logger.warning("FTP PASV: 227 response didn't match expected format: %s", text[:100])
  962. # 229 Entering Extended Passive Mode (|||port|)
  963. elif text.startswith("229 "):
  964. match = re.search(r"\(\|\|\|(\d+)\|\)", text)
  965. if match:
  966. printer_port = int(match.group(1))
  967. local_port = await self._create_data_proxy(self.target_host, printer_port, session_state)
  968. if local_port:
  969. rewritten = f"229 Entering Extended Passive Mode (|||{local_port}|)"
  970. logger.info("FTP EPSV rewrite: port %s → %s", printer_port, local_port)
  971. return rewritten.encode("utf-8")
  972. else:
  973. logger.error("FTP EPSV: failed to create data proxy for port %s", printer_port)
  974. else:
  975. logger.warning("FTP EPSV: 229 response didn't match expected format: %s", text[:100])
  976. return line
  977. async def _create_data_proxy(
  978. self, printer_ip: str, printer_port: int, session_state: dict[str, str | ssl.SSLSession]
  979. ) -> int | None:
  980. """Create a one-shot proxy for an FTP data connection.
  981. Prefers the printer's original passive port so the port number stays
  982. the same in the rewritten PASV/EPSV response. This is critical when
  983. the slicer's FTP bounce-attack protection overrides the IP in the PASV
  984. response: the slicer connects to <control_IP>:<port>, and if iptables
  985. REDIRECT maps that port to the local machine, the data proxy must be
  986. listening on the *same* port number.
  987. Falls back to a random port if the original is unavailable.
  988. Uses TLS or cleartext based on the session's PROT level:
  989. - PROT P: TLS on both slicer and printer data connections
  990. - PROT C: cleartext on both sides (common for A1/H2D printers)
  991. Returns the local port number, or None if binding failed.
  992. """
  993. use_tls = session_state.get("prot") == "P"
  994. logger.info(
  995. "FTP data proxy: creating data proxy for %s:%s (printer-side %s)",
  996. printer_ip,
  997. printer_port,
  998. "TLS" if use_tls else "cleartext",
  999. )
  1000. # Get control channel TLS session for data channel reuse
  1001. tls_session = session_state.get("tls_session") if use_tls else None
  1002. # Try the printer's original port first — this ensures the port
  1003. # matches even when bounce protection or iptables REDIRECT is in play.
  1004. try:
  1005. await self._start_data_proxy_server(printer_port, printer_ip, printer_port, use_tls, tls_session)
  1006. logger.info("FTP data proxy: using printer's port %s", printer_port)
  1007. return printer_port
  1008. except OSError as e:
  1009. logger.debug(
  1010. "FTP data proxy: printer port %s unavailable (%s), trying random",
  1011. printer_port,
  1012. e,
  1013. )
  1014. for _attempt in range(10):
  1015. port = random.randint(self.PASV_PORT_MIN, self.PASV_PORT_MAX)
  1016. try:
  1017. await self._start_data_proxy_server(port, printer_ip, printer_port, use_tls, tls_session)
  1018. logger.info("FTP data proxy: using random port %s", port)
  1019. return port
  1020. except OSError:
  1021. continue
  1022. logger.error("Failed to bind FTP data proxy port after 10 attempts")
  1023. return None
  1024. async def _start_data_proxy_server(
  1025. self,
  1026. port: int,
  1027. printer_ip: str,
  1028. printer_port: int,
  1029. use_tls: bool,
  1030. tls_session: ssl.SSLSession | None = None,
  1031. ) -> None:
  1032. """Start a one-shot server for one FTP data connection.
  1033. When the slicer connects, immediately connects to the printer's data
  1034. port and buffers any slicer data until the printer connection is ready.
  1035. This handles zero-byte uploads (verify_job) where the slicer closes
  1036. the data channel before a naive proxy would finish its TLS handshake.
  1037. The slicer-side listener is ALWAYS cleartext. Even when the slicer
  1038. sends PROT P on the control channel, Bambu Studio does not perform
  1039. a TLS handshake on the data connection — it relies on the implicit
  1040. FTPS control channel for authentication and sends data unencrypted.
  1041. The printer-side outbound connection follows the PROT level:
  1042. - PROT P (use_tls=True): TLS to the printer's data port
  1043. - PROT C (use_tls=False): cleartext to the printer's data port
  1044. This mirrors the control channel's TLS-termination architecture.
  1045. Raises OSError if the port is already in use.
  1046. """
  1047. connected = asyncio.Event()
  1048. server_holder: list[asyncio.Server] = []
  1049. # Slicer side: ALWAYS cleartext — Bambu Studio does not do TLS on
  1050. # the data channel even after sending PROT P.
  1051. # Printer side: TLS if PROT P, cleartext if PROT C.
  1052. # For TLS data connections, wrap the SSL context to reuse the
  1053. # control channel's TLS session if available. vsFTPd (X1C) requires
  1054. # require_ssl_reuse — without this, data connections are rejected
  1055. # with "522 SSL connection failed: session reuse required".
  1056. if use_tls and tls_session:
  1057. client_ssl = _SessionReuseSSLContext(self._client_ssl_context, tls_session)
  1058. logger.debug("FTP data proxy: using TLS session reuse for port %s", port)
  1059. else:
  1060. client_ssl = self._client_ssl_context if use_tls else None
  1061. # Slicer side is ALWAYS cleartext — Bambu Studio does not do TLS on
  1062. # the data channel even after PROT P (confirmed for both H2D and X1C).
  1063. printer_mode = "TLS" if use_tls else "cleartext"
  1064. async def handle_data(
  1065. client_reader: asyncio.StreamReader,
  1066. client_writer: asyncio.StreamWriter,
  1067. ) -> None:
  1068. """Handle one FTP data connection, then close the server."""
  1069. peername = client_writer.get_extra_info("peername")
  1070. data_client = f"{peername[0]}:{peername[1]}" if peername else "unknown"
  1071. logger.info(
  1072. "FTP data proxy port %s (slicer=cleartext, printer=%s): client connected from %s, bridging to %s:%s",
  1073. port,
  1074. printer_mode,
  1075. data_client,
  1076. printer_ip,
  1077. printer_port,
  1078. )
  1079. connected.set()
  1080. # One-shot: close server after accepting first connection
  1081. if server_holder:
  1082. server_holder[0].close()
  1083. printer_writer = None
  1084. try:
  1085. # Buffer any slicer data while connecting to printer.
  1086. # This handles the race where the slicer sends data (or closes
  1087. # for zero-byte files) before the TLS handshake completes.
  1088. slicer_buffer = bytearray()
  1089. slicer_eof = False
  1090. async def buffer_slicer():
  1091. nonlocal slicer_eof
  1092. while True:
  1093. chunk = await client_reader.read(65536)
  1094. if not chunk:
  1095. slicer_eof = True
  1096. return
  1097. slicer_buffer.extend(chunk)
  1098. buffer_task = asyncio.create_task(buffer_slicer())
  1099. # Connect to printer's data port
  1100. printer_reader, printer_writer = await asyncio.wait_for(
  1101. asyncio.open_connection(printer_ip, printer_port, ssl=client_ssl),
  1102. timeout=10.0,
  1103. )
  1104. logger.info(
  1105. "FTP data proxy port %s (printer=%s): connected to printer %s:%s",
  1106. port,
  1107. printer_mode,
  1108. printer_ip,
  1109. printer_port,
  1110. )
  1111. # Stop buffering
  1112. buffer_task.cancel()
  1113. try:
  1114. await buffer_task
  1115. except asyncio.CancelledError:
  1116. pass
  1117. # Flush buffered slicer data to printer
  1118. logger.info(
  1119. "FTP data proxy port %s: buffer=%s bytes, slicer_eof=%s",
  1120. port,
  1121. len(slicer_buffer),
  1122. slicer_eof,
  1123. )
  1124. if slicer_buffer:
  1125. printer_writer.write(bytes(slicer_buffer))
  1126. await printer_writer.drain()
  1127. # Forward remaining slicer data to printer, then close the
  1128. # printer side to signal upload complete.
  1129. #
  1130. # Bambu Studio does NOT close the FTP data channel after sending
  1131. # STOR data — it keeps the connection open and waits for the
  1132. # printer to close its side + send 226 on the control channel.
  1133. # A naive bidirectional proxy deadlocks here because the proxy
  1134. # waits for the slicer EOF that never comes.
  1135. #
  1136. # Fix: read slicer data with an idle timeout. Once data has been
  1137. # received and the slicer goes quiet, close the printer side so
  1138. # the printer can send 226. For RETR (download), the printer
  1139. # sends data and closes — the slicer reads until EOF — so this
  1140. # unidirectional approach works for both directions.
  1141. total_c2p = len(slicer_buffer)
  1142. if not slicer_eof:
  1143. # Read remaining slicer data with idle detection.
  1144. # Must be short — Bambu Studio expects 226 almost instantly
  1145. # after sending data. Too long and the slicer times out.
  1146. idle_timeout = 0.3
  1147. while True:
  1148. try:
  1149. chunk = await asyncio.wait_for(client_reader.read(65536), timeout=idle_timeout)
  1150. except TimeoutError:
  1151. if total_c2p > 0:
  1152. # Slicer sent data then went idle — upload done
  1153. logger.debug(
  1154. "FTP data proxy port %s: slicer idle after %s bytes, closing printer side",
  1155. port,
  1156. total_c2p,
  1157. )
  1158. break
  1159. continue # No data yet, keep waiting
  1160. if not chunk:
  1161. break # Slicer closed
  1162. printer_writer.write(chunk)
  1163. await printer_writer.drain()
  1164. total_c2p += len(chunk)
  1165. logger.debug("FTP proxy data_c2p: total %s bytes", total_c2p)
  1166. # Close printer side to signal upload complete.
  1167. # For TLS, close() sends close_notify which the printer treats
  1168. # as end-of-data. The printer then sends 226 on the control
  1169. # channel. For RETR, this is a no-op since the printer closes
  1170. # first and we'd have exited the loop above via EOF.
  1171. try:
  1172. printer_writer.close()
  1173. await printer_writer.wait_closed()
  1174. except OSError:
  1175. pass
  1176. # Wait for 226 response to propagate through the FTP control
  1177. # channel before closing the slicer's data channel.
  1178. #
  1179. # Without this delay, the data channel FIN arrives at the
  1180. # slicer before the 226 response on the control channel.
  1181. # BambuStudio reacts to the data channel FIN within <1ms
  1182. # by sending QUIT + closing the control channel — before
  1183. # 226 arrives (~2-3ms network RTT). This causes verify_job
  1184. # to be treated as failed and shows the login modal.
  1185. #
  1186. # In a direct connection, the printer sends 226 AND closes
  1187. # the data channel simultaneously, so the slicer gets both
  1188. # at once. The delay here emulates that timing.
  1189. if total_c2p > 0:
  1190. await asyncio.sleep(0.5)
  1191. except Exception as e:
  1192. logger.error("FTP data proxy port %s: error: %s", port, e)
  1193. finally:
  1194. for w in [client_writer, printer_writer]:
  1195. if w:
  1196. try:
  1197. w.close()
  1198. await w.wait_closed()
  1199. except OSError:
  1200. pass # Best-effort data connection cleanup
  1201. logger.info("FTP data proxy port %s: connection closed", port)
  1202. server = await asyncio.start_server(
  1203. handle_data,
  1204. "0.0.0.0", # nosec B104
  1205. port,
  1206. # No TLS on slicer side — Bambu Studio doesn't do TLS on data
  1207. # channel even after PROT P (confirmed by connection hang test).
  1208. )
  1209. server_holder.append(server)
  1210. self._data_servers.append(server)
  1211. # Auto-close after 60s if no connection arrives
  1212. async def auto_close() -> None:
  1213. try:
  1214. await asyncio.wait_for(connected.wait(), timeout=60.0)
  1215. except TimeoutError:
  1216. logger.debug("FTP data proxy on port %s timed out, closing", port)
  1217. try:
  1218. server.close()
  1219. await server.wait_closed()
  1220. except OSError:
  1221. pass # Best-effort timeout cleanup
  1222. finally:
  1223. if server in self._data_servers:
  1224. self._data_servers.remove(server)
  1225. # Track the auto_close task so stop() can cancel it; otherwise the
  1226. # 60 s timeout would hold a server reference past proxy teardown.
  1227. ac_task = asyncio.create_task(auto_close(), name=f"ftp_data_timeout_{port}")
  1228. self._auto_close_tasks.append(ac_task)
  1229. ac_task.add_done_callback(lambda t, tasks=self._auto_close_tasks: tasks.remove(t) if t in tasks else None)
  1230. logger.debug("FTP data proxy: port %s → %s:%s", port, printer_ip, printer_port)
  1231. class SlicerProxyManager:
  1232. """Manages FTP and MQTT TLS proxies for a single printer target."""
  1233. # Bambu printer ports
  1234. PRINTER_FTP_PORT = 990
  1235. PRINTER_MQTT_PORT = 8883
  1236. PRINTER_FILE_TRANSFER_PORT = 6000
  1237. PRINTER_RTSP_PORT = 322 # X1/H2/P2 series camera (A1/P1 use port 6000)
  1238. # Undocumented proprietary ports used by some models (A1, P1S, etc.)
  1239. # BambuStudio requires port 2024 for printing; OrcaSlicer also needs 2025.
  1240. PRINTER_AUX_PORTS = [2024, 2025, 2026]
  1241. PRINTER_BIND_PORTS = [3000, 3002]
  1242. # Local listen ports - must match what Bambu Studio expects
  1243. # Note: Port 990 requires root or CAP_NET_BIND_SERVICE capability
  1244. LOCAL_FTP_PORT = 990
  1245. LOCAL_MQTT_PORT = 8883
  1246. def __init__(
  1247. self,
  1248. target_host: str,
  1249. cert_path: Path,
  1250. key_path: Path,
  1251. on_activity: Callable[[str, str], None] | None = None,
  1252. bind_address: str = "0.0.0.0", # nosec B104
  1253. bind_identity: dict[str, str] | None = None,
  1254. ):
  1255. """Initialize the slicer proxy manager.
  1256. Args:
  1257. target_host: Target printer IP address
  1258. cert_path: Path to server certificate
  1259. key_path: Path to server private key
  1260. on_activity: Optional callback for activity logging (name, message)
  1261. bind_address: IP address to bind proxy listeners to
  1262. bind_identity: Optional dict with keys (serial, model, name, version)
  1263. for the bind/detect response. When provided, the proxy responds
  1264. to detect requests itself instead of forwarding to the printer.
  1265. This ensures the slicer sees the VP identity, not the real printer.
  1266. """
  1267. self.target_host = target_host
  1268. self.cert_path = cert_path
  1269. self.key_path = key_path
  1270. self.on_activity = on_activity
  1271. self.bind_address = bind_address
  1272. self.bind_identity = bind_identity
  1273. self._ftp_proxy: TCPProxy | None = None
  1274. self._mqtt_proxy: TLSProxy | None = None
  1275. self._file_transfer_proxy: TCPProxy | None = None
  1276. self._rtsp_proxy: TCPProxy | None = None
  1277. self._aux_proxies: list[TCPProxy] = []
  1278. self._bind_proxies: list[TCPProxy] = []
  1279. self._bind_server = None
  1280. self._probe_servers: list[asyncio.Server] = []
  1281. self._tasks: list[asyncio.Task] = []
  1282. # Pre-bind the lifetime-coupled collections so ``stop()`` works if
  1283. # called before ``start()`` finishes (rapid mode-switch races).
  1284. # Previously ``_ftp_data_proxies`` was first assigned inside ``start()``
  1285. # at line ~1520, so an early stop hit AttributeError and left
  1286. # sockets stranded.
  1287. self._ftp_data_proxies: list[TCPProxy] = []
  1288. # Actual FTP listen port — class constant by default; ``start()``
  1289. # overwrites with the redirect target when iptables-redirect is
  1290. # active. ``get_status()`` reads this so diagnostics probe the
  1291. # port that actually has a listener, not the static LOCAL_FTP_PORT.
  1292. self._actual_ftp_port: int = self.LOCAL_FTP_PORT
  1293. # FTP passive data port range — Bambu printers typically use ports in
  1294. # this range for EPSV/PASV data connections. We pre-listen on all of
  1295. # them so EPSV works transparently without decrypting FTP control.
  1296. FTP_DATA_PORT_MIN = 50000
  1297. FTP_DATA_PORT_MAX = 50100
  1298. async def start(self) -> None:
  1299. """Start proxy services.
  1300. Uses transparent TCP proxying for most protocols (FTP, FileTransfer,
  1301. Camera) — raw bytes are forwarded without TLS termination, so the
  1302. slicer gets the printer's real TLS certificate end-to-end.
  1303. Only MQTT is TLS-terminated because we must decrypt the payload to
  1304. rewrite the printer's real IP with the proxy's bind IP.
  1305. """
  1306. logger.info("Starting slicer proxy to %s (transparent mode)", self.target_host)
  1307. # Detect iptables port redirect for FTP
  1308. ftp_listen_port = self.LOCAL_FTP_PORT
  1309. redirect_target = detect_port_redirect(self.LOCAL_FTP_PORT)
  1310. if redirect_target:
  1311. logger.info(
  1312. "Detected iptables redirect: port %d → %d. FTP proxy will listen on %d.",
  1313. self.LOCAL_FTP_PORT,
  1314. redirect_target,
  1315. redirect_target,
  1316. )
  1317. ftp_listen_port = redirect_target
  1318. # Cache the actual listen port for get_status() / diagnostic so the
  1319. # port_ftps check probes the port that actually has a socket.
  1320. self._actual_ftp_port = ftp_listen_port
  1321. # FTP control — raw TCP pass-through (end-to-end TLS with printer).
  1322. # A TLS 1.3 → 1.2 ClientHello-rewrite was attempted to work around
  1323. # BambuStudio's libcurl bug on the X1C FTPS data channel (PSK
  1324. # session-resumption + CURLE_PARTIAL_FILE). Reverted because the
  1325. # rewrite broke the control-channel TLS handshake itself: replacing
  1326. # 0x0304 with a duplicate 0x0303 in supported_versions while leaving
  1327. # the TLS-1.3-only extensions (key_share, psk_key_exchange_modes,
  1328. # signature_algorithms_cert) in place produced a malformed ClientHello
  1329. # that the printer or slicer rejected, and the connection closed
  1330. # before any data channel was opened. A proper fix needs full TLS
  1331. # bumping (terminate + re-establish) with packet-capture work
  1332. # that's out of scope for now. X1C proxy-mode FTP uploads remain
  1333. # broken — users with X1C should use the non-proxy modes (immediate
  1334. # / review / print_queue) which work end-to-end via the VP's own
  1335. # FTP server on TLS 1.2.
  1336. self._ftp_proxy = TCPProxy(
  1337. name="FTP",
  1338. listen_port=ftp_listen_port,
  1339. target_host=self.target_host,
  1340. target_port=self.PRINTER_FTP_PORT,
  1341. on_connect=lambda cid: self._log_activity("FTP", f"connected: {cid}"),
  1342. on_disconnect=lambda cid: self._log_activity("FTP", f"disconnected: {cid}"),
  1343. bind_address=self.bind_address,
  1344. )
  1345. # FTP data ports — pre-listen on the entire passive port range.
  1346. # Since FTP control is encrypted end-to-end, we can't read EPSV
  1347. # responses to know which port the printer chose. Instead, we
  1348. # listen on every port in the range and forward to the same port
  1349. # on the printer. The slicer connects to bind_ip:PORT (from EPSV)
  1350. # and we transparently relay to printer_ip:PORT.
  1351. self._ftp_data_proxies: list[TCPProxy] = []
  1352. for port in range(self.FTP_DATA_PORT_MIN, self.FTP_DATA_PORT_MAX + 1):
  1353. dp = TCPProxy(
  1354. name=f"FTP-Data-{port}",
  1355. listen_port=port,
  1356. target_host=self.target_host,
  1357. target_port=port,
  1358. bind_address=self.bind_address,
  1359. )
  1360. self._ftp_data_proxies.append(dp)
  1361. # MQTT — TLS-terminating proxy (must decrypt to rewrite IP addresses)
  1362. self._mqtt_proxy = TLSProxy(
  1363. name="MQTT",
  1364. listen_port=self.LOCAL_MQTT_PORT,
  1365. target_host=self.target_host,
  1366. target_port=self.PRINTER_MQTT_PORT,
  1367. server_cert_path=self.cert_path,
  1368. server_key_path=self.key_path,
  1369. on_connect=lambda cid: self._log_activity("MQTT", f"connected: {cid}"),
  1370. on_disconnect=lambda cid: self._log_activity("MQTT", f"disconnected: {cid}"),
  1371. bind_address=self.bind_address,
  1372. rewrite_ip=(self.target_host, self.bind_address) if self.bind_address != "0.0.0.0" else None, # nosec B104
  1373. )
  1374. # File transfer — raw TCP pass-through (port 6000)
  1375. self._file_transfer_proxy = TCPProxy(
  1376. name="FileTransfer",
  1377. listen_port=self.PRINTER_FILE_TRANSFER_PORT,
  1378. target_host=self.target_host,
  1379. target_port=self.PRINTER_FILE_TRANSFER_PORT,
  1380. on_connect=lambda cid: self._log_activity("FileTransfer", f"connected: {cid}"),
  1381. on_disconnect=lambda cid: self._log_activity("FileTransfer", f"disconnected: {cid}"),
  1382. bind_address=self.bind_address,
  1383. )
  1384. # RTSP camera — raw TCP pass-through (port 322)
  1385. self._rtsp_proxy = TCPProxy(
  1386. name="RTSP",
  1387. listen_port=self.PRINTER_RTSP_PORT,
  1388. target_host=self.target_host,
  1389. target_port=self.PRINTER_RTSP_PORT,
  1390. on_connect=lambda cid: self._log_activity("RTSP", f"connected: {cid}"),
  1391. on_disconnect=lambda cid: self._log_activity("RTSP", f"disconnected: {cid}"),
  1392. bind_address=self.bind_address,
  1393. )
  1394. # Auxiliary ports (2024-2026) — raw TCP pass-through for undocumented
  1395. # proprietary services. Required by BambuStudio/OrcaSlicer for some
  1396. # models (A1, P1S). Silently ignored if the printer doesn't listen.
  1397. for aux_port in self.PRINTER_AUX_PORTS:
  1398. self._aux_proxies.append(
  1399. TCPProxy(
  1400. name=f"Aux-{aux_port}",
  1401. listen_port=aux_port,
  1402. target_host=self.target_host,
  1403. target_port=aux_port,
  1404. on_connect=lambda cid, p=aux_port: self._log_activity(f"Aux-{p}", f"connected: {cid}"),
  1405. on_disconnect=lambda cid, p=aux_port: self._log_activity(f"Aux-{p}", f"disconnected: {cid}"),
  1406. bind_address=self.bind_address,
  1407. )
  1408. )
  1409. # Bind/auth — respond with VP identity instead of proxying to printer.
  1410. # The detect response contains the printer name, serial, model, and
  1411. # bind status. Proxying it would leak the real printer's identity and
  1412. # cause the slicer to treat it as a different device.
  1413. if self.bind_identity:
  1414. from backend.app.services.virtual_printer.bind_server import BindServer
  1415. self._bind_server = BindServer(
  1416. serial=self.bind_identity["serial"],
  1417. model=self.bind_identity["model"],
  1418. name=self.bind_identity["name"],
  1419. version=self.bind_identity.get("version", "01.00.00.00"),
  1420. bind_address=self.bind_address,
  1421. cert_path=self.cert_path,
  1422. key_path=self.key_path,
  1423. )
  1424. else:
  1425. # Fallback: proxy bind requests to the real printer
  1426. for bind_port in self.PRINTER_BIND_PORTS:
  1427. if bind_port == 3002:
  1428. proxy = TLSProxy(
  1429. name="Bind-TLS",
  1430. listen_port=bind_port,
  1431. target_host=self.target_host,
  1432. target_port=bind_port,
  1433. server_cert_path=self.cert_path,
  1434. server_key_path=self.key_path,
  1435. on_connect=lambda cid: self._log_activity("Bind", f"connected: {cid}"),
  1436. on_disconnect=lambda cid: self._log_activity("Bind", f"disconnected: {cid}"),
  1437. bind_address=self.bind_address,
  1438. )
  1439. else:
  1440. proxy = TCPProxy(
  1441. name="Bind",
  1442. listen_port=bind_port,
  1443. target_host=self.target_host,
  1444. target_port=bind_port,
  1445. on_connect=lambda cid: self._log_activity("Bind", f"connected: {cid}"),
  1446. on_disconnect=lambda cid: self._log_activity("Bind", f"disconnected: {cid}"),
  1447. bind_address=self.bind_address,
  1448. )
  1449. self._bind_proxies.append(proxy)
  1450. # Start as background tasks
  1451. async def run_with_logging(proxy: TLSProxy | TCPProxy) -> None:
  1452. try:
  1453. await proxy.start()
  1454. except Exception as e:
  1455. logger.error("Slicer proxy %s failed: %s", proxy.name, e)
  1456. self._tasks = [
  1457. asyncio.create_task(
  1458. run_with_logging(self._ftp_proxy),
  1459. name="slicer_proxy_ftp",
  1460. ),
  1461. asyncio.create_task(
  1462. run_with_logging(self._mqtt_proxy),
  1463. name="slicer_proxy_mqtt",
  1464. ),
  1465. asyncio.create_task(
  1466. run_with_logging(self._file_transfer_proxy),
  1467. name="slicer_proxy_file_transfer",
  1468. ),
  1469. asyncio.create_task(
  1470. run_with_logging(self._rtsp_proxy),
  1471. name="slicer_proxy_rtsp",
  1472. ),
  1473. ]
  1474. for ap in self._aux_proxies:
  1475. self._tasks.append(
  1476. asyncio.create_task(
  1477. run_with_logging(ap),
  1478. name=f"slicer_proxy_aux_{ap.listen_port}",
  1479. )
  1480. )
  1481. if self._bind_server:
  1482. self._tasks.append(
  1483. asyncio.create_task(
  1484. run_with_logging(self._bind_server),
  1485. name="slicer_proxy_bind_server",
  1486. )
  1487. )
  1488. for bp in self._bind_proxies:
  1489. self._tasks.append(
  1490. asyncio.create_task(
  1491. run_with_logging(bp),
  1492. name=f"slicer_proxy_bind_{bp.listen_port}",
  1493. )
  1494. )
  1495. # FTP data port proxies (50000-50100)
  1496. for dp in self._ftp_data_proxies:
  1497. self._tasks.append(
  1498. asyncio.create_task(
  1499. run_with_logging(dp),
  1500. name=f"slicer_proxy_ftp_data_{dp.listen_port}",
  1501. )
  1502. )
  1503. # Diagnostic probe: listen on common un-proxied ports to detect
  1504. # if the slicer tries to reach a service we don't handle.
  1505. if self.bind_address and self.bind_address != "0.0.0.0": # nosec B104
  1506. for probe_port in (21, 80, 443):
  1507. try:
  1508. srv = await asyncio.start_server(
  1509. lambda r, w, p=probe_port: self._probe_handler(r, w, p),
  1510. self.bind_address,
  1511. probe_port,
  1512. )
  1513. self._probe_servers.append(srv)
  1514. except OSError:
  1515. pass # Port in use or no permission — skip
  1516. if self._probe_servers:
  1517. probed = [s.sockets[0].getsockname()[1] for s in self._probe_servers if s.sockets]
  1518. logger.info("Proxy diagnostic: probing un-proxied ports %s on %s", probed, self.bind_address)
  1519. logger.info(
  1520. "Slicer proxy started for %s (transparent TCP + MQTT TLS, %d FTP data ports)",
  1521. self.target_host,
  1522. len(self._ftp_data_proxies),
  1523. )
  1524. # Wait for tasks to complete (they run until cancelled)
  1525. # This keeps the start() coroutine alive so the parent task doesn't complete
  1526. try:
  1527. await asyncio.gather(*self._tasks)
  1528. except asyncio.CancelledError:
  1529. logger.debug("Slicer proxy start cancelled")
  1530. async def stop(self) -> None:
  1531. """Stop all proxies."""
  1532. logger.info("Stopping slicer proxy")
  1533. # Stop proxies
  1534. if self._ftp_proxy:
  1535. await self._ftp_proxy.stop()
  1536. self._ftp_proxy = None
  1537. if self._mqtt_proxy:
  1538. await self._mqtt_proxy.stop()
  1539. self._mqtt_proxy = None
  1540. if self._file_transfer_proxy:
  1541. await self._file_transfer_proxy.stop()
  1542. self._file_transfer_proxy = None
  1543. if self._rtsp_proxy:
  1544. await self._rtsp_proxy.stop()
  1545. self._rtsp_proxy = None
  1546. for ap in self._aux_proxies:
  1547. await ap.stop()
  1548. self._aux_proxies = []
  1549. if self._bind_server:
  1550. await self._bind_server.stop()
  1551. self._bind_server = None
  1552. for bp in self._bind_proxies:
  1553. await bp.stop()
  1554. self._bind_proxies = []
  1555. for dp in self._ftp_data_proxies:
  1556. await dp.stop()
  1557. self._ftp_data_proxies = []
  1558. # Probe servers need wait_closed — without it the OS releases the
  1559. # bind socket asynchronously and a rapid stop+start cycle (e.g.
  1560. # config-change-driven mode switch) can race "address already in
  1561. # use" on the probe ports.
  1562. for srv in self._probe_servers:
  1563. try:
  1564. srv.close()
  1565. await srv.wait_closed()
  1566. except OSError:
  1567. pass # Best-effort — port may already be released
  1568. self._probe_servers = []
  1569. # Cancel tasks
  1570. for task in self._tasks:
  1571. task.cancel()
  1572. if self._tasks:
  1573. try:
  1574. await asyncio.wait_for(
  1575. asyncio.gather(*self._tasks, return_exceptions=True),
  1576. timeout=2.0,
  1577. )
  1578. except TimeoutError:
  1579. logger.debug("Some proxy tasks didn't stop in time")
  1580. self._tasks = []
  1581. logger.info("Slicer proxy stopped")
  1582. async def _probe_handler(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, port: int) -> None:
  1583. """Log unexpected connections on un-proxied ports for diagnostics."""
  1584. peername = writer.get_extra_info("peername")
  1585. client = f"{peername[0]}:{peername[1]}" if peername else "unknown"
  1586. logger.warning(
  1587. "PROBE: slicer connected to un-proxied port %d from %s — this port may need proxying",
  1588. port,
  1589. client,
  1590. )
  1591. writer.close()
  1592. try:
  1593. await writer.wait_closed()
  1594. except OSError:
  1595. pass
  1596. def _log_activity(self, name: str, message: str) -> None:
  1597. """Log activity via callback if configured."""
  1598. if self.on_activity:
  1599. try:
  1600. self.on_activity(name, message)
  1601. except Exception:
  1602. pass # Ignore activity callback errors; logging is non-critical
  1603. @property
  1604. def is_running(self) -> bool:
  1605. """Check if proxies are running."""
  1606. return len(self._tasks) > 0 and all(not t.done() for t in self._tasks)
  1607. def get_status(self) -> dict:
  1608. """Get proxy status."""
  1609. return {
  1610. "running": self.is_running,
  1611. "target_host": self.target_host,
  1612. # ``_actual_ftp_port`` reflects the iptables-redirected listen
  1613. # port when the docker-host deployment uses
  1614. # ``iptables -t nat -A PREROUTING ... REDIRECT --to-port`` to
  1615. # let non-root containers serve on the printer's 990. Returning
  1616. # the class constant here made the diagnostic probe a port
  1617. # nothing was listening on and report a false fail on every
  1618. # working redirect deployment.
  1619. "ftp_port": self._actual_ftp_port,
  1620. "mqtt_port": self.LOCAL_MQTT_PORT,
  1621. "bind_ports": self.PRINTER_BIND_PORTS,
  1622. "ftp_connections": (len(self._ftp_proxy._active_connections) if self._ftp_proxy else 0),
  1623. "mqtt_connections": (len(self._mqtt_proxy._active_connections) if self._mqtt_proxy else 0),
  1624. "bind_connections": sum(len(bp._active_connections) for bp in self._bind_proxies),
  1625. }