|
@@ -1,15 +1,17 @@
|
|
|
-"""TLS proxy for slicer-to-printer communication.
|
|
|
|
|
|
|
+"""Proxy for slicer-to-printer communication.
|
|
|
|
|
|
|
|
-This module provides a TLS terminating proxy that forwards data between
|
|
|
|
|
-a slicer and a real Bambu printer, enabling remote printing over
|
|
|
|
|
-any network connection.
|
|
|
|
|
|
|
+This module provides both transparent TCP proxying and TLS-terminating
|
|
|
|
|
+proxying for forwarding data between a slicer and a real Bambu printer,
|
|
|
|
|
+enabling remote printing over any network connection.
|
|
|
|
|
|
|
|
-Unlike a transparent TCP proxy, this terminates TLS on both ends:
|
|
|
|
|
-- Slicer connects to Bambuddy using Bambuddy's certificate
|
|
|
|
|
-- Bambuddy connects to printer using printer's certificate
|
|
|
|
|
-- Data is decrypted, forwarded, and re-encrypted
|
|
|
|
|
|
|
+Most protocols (FTP, FileTransfer, Camera) use transparent TCP proxying —
|
|
|
|
|
+raw bytes are forwarded without decryption, preserving end-to-end TLS
|
|
|
|
|
+between slicer and printer. Only MQTT is TLS-terminated so Bambuddy can
|
|
|
|
|
+rewrite the printer's real IP with the proxy's bind IP in MQTT payloads.
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
|
|
+# ruff: noqa: N801
|
|
|
|
|
+
|
|
|
import asyncio
|
|
import asyncio
|
|
|
import logging
|
|
import logging
|
|
|
import random
|
|
import random
|
|
@@ -22,6 +24,44 @@ from pathlib import Path
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+class _SessionReuseSSLContext:
|
|
|
|
|
+ """Proxy around SSLContext that injects a TLS session into wrap_bio().
|
|
|
|
|
+
|
|
|
|
|
+ vsFTPd (used by some Bambu printers like X1C) requires TLS session reuse
|
|
|
|
|
+ on FTP data channels — the data connection must reuse the TLS session from
|
|
|
|
|
+ the control channel. Without this, the printer rejects the data connection
|
|
|
|
|
+ with "522 SSL connection failed: session reuse required".
|
|
|
|
|
+
|
|
|
|
|
+ asyncio's open_connection() calls SSLContext.wrap_bio() internally but
|
|
|
|
|
+ doesn't expose a session parameter. This wrapper intercepts wrap_bio()
|
|
|
|
|
+ to inject the saved control-channel session, enabling session reuse.
|
|
|
|
|
+ """
|
|
|
|
|
+
|
|
|
|
|
+ def __init__(self, ctx: ssl.SSLContext, session: ssl.SSLSession) -> None:
|
|
|
|
|
+ object.__setattr__(self, "_ctx", ctx)
|
|
|
|
|
+ object.__setattr__(self, "_session", session)
|
|
|
|
|
+
|
|
|
|
|
+ def __getattr__(self, name: str) -> object:
|
|
|
|
|
+ return getattr(self._ctx, name)
|
|
|
|
|
+
|
|
|
|
|
+ def wrap_bio(
|
|
|
|
|
+ self,
|
|
|
|
|
+ incoming: ssl.MemoryBIO,
|
|
|
|
|
+ outgoing: ssl.MemoryBIO,
|
|
|
|
|
+ server_side: bool = False,
|
|
|
|
|
+ server_hostname: str | None = None,
|
|
|
|
|
+ **kwargs: object,
|
|
|
|
|
+ ) -> ssl.SSLObject:
|
|
|
|
|
+ return self._ctx.wrap_bio(
|
|
|
|
|
+ incoming,
|
|
|
|
|
+ outgoing,
|
|
|
|
|
+ server_side=server_side,
|
|
|
|
|
+ server_hostname=server_hostname,
|
|
|
|
|
+ session=self._session,
|
|
|
|
|
+ **kwargs,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def detect_port_redirect(port: int) -> int | None:
|
|
def detect_port_redirect(port: int) -> int | None:
|
|
|
"""Detect if iptables redirects a port to another port.
|
|
"""Detect if iptables redirects a port to another port.
|
|
|
|
|
|
|
@@ -82,6 +122,7 @@ class TLSProxy:
|
|
|
on_connect: Callable[[str], None] | None = None,
|
|
on_connect: Callable[[str], None] | None = None,
|
|
|
on_disconnect: Callable[[str], None] | None = None,
|
|
on_disconnect: Callable[[str], None] | None = None,
|
|
|
bind_address: str = "0.0.0.0", # nosec B104
|
|
bind_address: str = "0.0.0.0", # nosec B104
|
|
|
|
|
+ rewrite_ip: tuple[str, str] | None = None,
|
|
|
):
|
|
):
|
|
|
"""Initialize the TLS proxy.
|
|
"""Initialize the TLS proxy.
|
|
|
|
|
|
|
@@ -95,6 +136,10 @@ class TLSProxy:
|
|
|
on_connect: Optional callback when client connects (receives client_id)
|
|
on_connect: Optional callback when client connects (receives client_id)
|
|
|
on_disconnect: Optional callback when client disconnects (receives client_id)
|
|
on_disconnect: Optional callback when client disconnects (receives client_id)
|
|
|
bind_address: IP address to bind to (default: all interfaces)
|
|
bind_address: IP address to bind to (default: all interfaces)
|
|
|
|
|
+ rewrite_ip: Optional (old_ip, new_ip) tuple — replaces occurrences of
|
|
|
|
|
+ the printer's real IP with the proxy's bind IP in printer→client data.
|
|
|
|
|
+ This prevents the slicer from discovering the printer's real IP
|
|
|
|
|
+ in MQTT payloads (ip_addr, rtsp_url, etc.) and bypassing the proxy.
|
|
|
"""
|
|
"""
|
|
|
self.name = name
|
|
self.name = name
|
|
|
self.listen_port = listen_port
|
|
self.listen_port = listen_port
|
|
@@ -106,12 +151,41 @@ class TLSProxy:
|
|
|
self.on_disconnect = on_disconnect
|
|
self.on_disconnect = on_disconnect
|
|
|
self.bind_address = bind_address
|
|
self.bind_address = bind_address
|
|
|
|
|
|
|
|
|
|
+ # IP rewriting for printer→client direction
|
|
|
|
|
+ if rewrite_ip:
|
|
|
|
|
+ self._rewrite_old = rewrite_ip[0].encode("utf-8")
|
|
|
|
|
+ self._rewrite_new = rewrite_ip[1].encode("utf-8")
|
|
|
|
|
+ # Also rewrite the integer IP in net.info[].ip fields.
|
|
|
|
|
+ # Bambu printers encode their IP as a little-endian uint32 integer
|
|
|
|
|
+ # in the JSON payload. BambuStudio reads this to set dev_ip.
|
|
|
|
|
+ self._rewrite_old_int = self._ip_to_le_int_bytes(rewrite_ip[0])
|
|
|
|
|
+ self._rewrite_new_int = self._ip_to_le_int_bytes(rewrite_ip[1])
|
|
|
|
|
+ else:
|
|
|
|
|
+ self._rewrite_old = None
|
|
|
|
|
+ self._rewrite_new = None
|
|
|
|
|
+ self._rewrite_old_int = None
|
|
|
|
|
+ self._rewrite_new_int = None
|
|
|
|
|
+
|
|
|
self._server: asyncio.Server | None = None
|
|
self._server: asyncio.Server | None = None
|
|
|
self._running = False
|
|
self._running = False
|
|
|
self._active_connections: dict[str, tuple[asyncio.Task, asyncio.Task]] = {}
|
|
self._active_connections: dict[str, tuple[asyncio.Task, asyncio.Task]] = {}
|
|
|
self._server_ssl_context: ssl.SSLContext | None = None
|
|
self._server_ssl_context: ssl.SSLContext | None = None
|
|
|
self._client_ssl_context: ssl.SSLContext | None = None
|
|
self._client_ssl_context: ssl.SSLContext | None = None
|
|
|
|
|
|
|
|
|
|
+ @staticmethod
|
|
|
|
|
+ def _ip_to_le_int_bytes(ip: str) -> bytes:
|
|
|
|
|
+ """Convert an IP address to its little-endian integer JSON representation.
|
|
|
|
|
+
|
|
|
|
|
+ E.g. "192.168.255.16" → b"285190336" (the integer as a decimal string,
|
|
|
|
|
+ as it appears in Bambu MQTT JSON payloads in the net.info[].ip field).
|
|
|
|
|
+ """
|
|
|
|
|
+ import struct as _struct
|
|
|
|
|
+
|
|
|
|
|
+ parts = ip.split(".")
|
|
|
|
|
+ packed = bytes(int(p) for p in parts)
|
|
|
|
|
+ le_int = _struct.unpack("<I", packed)[0]
|
|
|
|
|
+ return str(le_int).encode("utf-8")
|
|
|
|
|
+
|
|
|
def _create_server_ssl_context(self) -> ssl.SSLContext:
|
|
def _create_server_ssl_context(self) -> ssl.SSLContext:
|
|
|
"""Create SSL context for accepting client (slicer) connections."""
|
|
"""Create SSL context for accepting client (slicer) connections."""
|
|
|
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
@@ -263,7 +337,7 @@ class TLSProxy:
|
|
|
name=f"{self.name}_c2p_{client_id}",
|
|
name=f"{self.name}_c2p_{client_id}",
|
|
|
)
|
|
)
|
|
|
printer_to_client = asyncio.create_task(
|
|
printer_to_client = asyncio.create_task(
|
|
|
- self._forward(printer_reader, client_writer, f"printer→{client_id}"),
|
|
|
|
|
|
|
+ self._forward(printer_reader, client_writer, f"printer→{client_id}", rewrite_ip=True),
|
|
|
name=f"{self.name}_p2c_{client_id}",
|
|
name=f"{self.name}_p2c_{client_id}",
|
|
|
)
|
|
)
|
|
|
|
|
|
|
@@ -305,11 +379,157 @@ class TLSProxy:
|
|
|
except Exception:
|
|
except Exception:
|
|
|
pass # Ignore disconnect callback errors; cleanup continues
|
|
pass # Ignore disconnect callback errors; cleanup continues
|
|
|
|
|
|
|
|
|
|
+ @staticmethod
|
|
|
|
|
+ def _rewrite_mqtt_ip(
|
|
|
|
|
+ data: bytes,
|
|
|
|
|
+ old_ip: bytes,
|
|
|
|
|
+ new_ip: bytes,
|
|
|
|
|
+ buffer: bytearray,
|
|
|
|
|
+ extra_replacements: list[tuple[bytes, bytes]] | None = None,
|
|
|
|
|
+ ) -> tuple[bytes, bytearray]:
|
|
|
|
|
+ """Rewrite IP addresses inside MQTT packets, preserving packet framing.
|
|
|
|
|
+
|
|
|
|
|
+ MQTT packets have a variable-length header encoding the remaining
|
|
|
|
|
+ packet length. A naive bytes.replace() would corrupt this framing
|
|
|
|
|
+ when old_ip and new_ip differ in length.
|
|
|
|
|
+
|
|
|
|
|
+ This method parses individual MQTT packets out of the data stream,
|
|
|
|
|
+ performs the replacement only on PUBLISH payloads, and re-encodes
|
|
|
|
|
+ the remaining-length field to match the new size.
|
|
|
|
|
+
|
|
|
|
|
+ Incomplete packets are buffered and returned for the next call.
|
|
|
|
|
+
|
|
|
|
|
+ Args:
|
|
|
|
|
+ extra_replacements: Additional (old, new) byte pairs to replace
|
|
|
|
|
+ (e.g. the integer IP representation in net.info[].ip).
|
|
|
|
|
+
|
|
|
|
|
+ Returns (output_data, remaining_buffer).
|
|
|
|
|
+ """
|
|
|
|
|
+ buffer.extend(data)
|
|
|
|
|
+
|
|
|
|
|
+ # Check if any replacement target exists in the buffer
|
|
|
|
|
+ has_target = old_ip in buffer
|
|
|
|
|
+ if not has_target and extra_replacements:
|
|
|
|
|
+ has_target = any(old in buffer for old, _new in extra_replacements)
|
|
|
|
|
+
|
|
|
|
|
+ if not has_target:
|
|
|
|
|
+ # Fast path: no IP in buffer, but we still need to check for
|
|
|
|
|
+ # incomplete packets at the end that might contain a partial IP.
|
|
|
|
|
+ # For safety, try to parse and emit only complete packets.
|
|
|
|
|
+ result = bytearray()
|
|
|
|
|
+ pos = 0
|
|
|
|
|
+ length = len(buffer)
|
|
|
|
|
+
|
|
|
|
|
+ while pos < length:
|
|
|
|
|
+ packet_start = pos
|
|
|
|
|
+ if pos + 1 >= length:
|
|
|
|
|
+ break
|
|
|
|
|
+ pos += 1 # header byte
|
|
|
|
|
+
|
|
|
|
|
+ # Parse remaining length
|
|
|
|
|
+ remaining_length = 0
|
|
|
|
|
+ multiplier = 1
|
|
|
|
|
+ length_bytes = 0
|
|
|
|
|
+ while pos < length:
|
|
|
|
|
+ encoded_byte = buffer[pos]
|
|
|
|
|
+ pos += 1
|
|
|
|
|
+ remaining_length += (encoded_byte & 0x7F) * multiplier
|
|
|
|
|
+ multiplier *= 128
|
|
|
|
|
+ length_bytes += 1
|
|
|
|
|
+ if (encoded_byte & 0x80) == 0:
|
|
|
|
|
+ break
|
|
|
|
|
+ if length_bytes >= 4:
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ if pos + remaining_length > length:
|
|
|
|
|
+ # Incomplete — keep in buffer
|
|
|
|
|
+ new_buffer = bytearray(buffer[packet_start:])
|
|
|
|
|
+ return bytes(result), new_buffer
|
|
|
|
|
+
|
|
|
|
|
+ pos += remaining_length
|
|
|
|
|
+ result.extend(buffer[packet_start:pos])
|
|
|
|
|
+
|
|
|
|
|
+ # All complete
|
|
|
|
|
+ buffer.clear()
|
|
|
|
|
+ return bytes(result) if result else bytes(data), buffer
|
|
|
|
|
+
|
|
|
|
|
+ # Buffer contains old_ip — parse packets and rewrite
|
|
|
|
|
+ result = bytearray()
|
|
|
|
|
+ pos = 0
|
|
|
|
|
+ length = len(buffer)
|
|
|
|
|
+
|
|
|
|
|
+ while pos < length:
|
|
|
|
|
+ packet_start = pos
|
|
|
|
|
+
|
|
|
|
|
+ if pos >= length:
|
|
|
|
|
+ break
|
|
|
|
|
+ header_byte = buffer[pos]
|
|
|
|
|
+ pos += 1
|
|
|
|
|
+
|
|
|
|
|
+ # Remaining length: variable-length encoding (1-4 bytes)
|
|
|
|
|
+ remaining_length = 0
|
|
|
|
|
+ multiplier = 1
|
|
|
|
|
+ length_bytes = 0
|
|
|
|
|
+ while pos < length:
|
|
|
|
|
+ encoded_byte = buffer[pos]
|
|
|
|
|
+ pos += 1
|
|
|
|
|
+ remaining_length += (encoded_byte & 0x7F) * multiplier
|
|
|
|
|
+ multiplier *= 128
|
|
|
|
|
+ length_bytes += 1
|
|
|
|
|
+ if (encoded_byte & 0x80) == 0:
|
|
|
|
|
+ break
|
|
|
|
|
+ if length_bytes >= 4:
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ # Check if we have enough data for the full packet
|
|
|
|
|
+ if pos + remaining_length > length:
|
|
|
|
|
+ # Incomplete packet — keep in buffer for next call
|
|
|
|
|
+ new_buffer = bytearray(buffer[packet_start:])
|
|
|
|
|
+ return bytes(result), new_buffer
|
|
|
|
|
+
|
|
|
|
|
+ packet_type = (header_byte >> 4) & 0x0F
|
|
|
|
|
+ packet_body = buffer[pos : pos + remaining_length]
|
|
|
|
|
+ pos += remaining_length
|
|
|
|
|
+
|
|
|
|
|
+ # Only rewrite PUBLISH packets (type 3)
|
|
|
|
|
+ needs_rewrite = packet_type == 3 and (
|
|
|
|
|
+ old_ip in packet_body
|
|
|
|
|
+ or (extra_replacements and any(old in packet_body for old, _new in extra_replacements))
|
|
|
|
|
+ )
|
|
|
|
|
+ if needs_rewrite:
|
|
|
|
|
+ new_body = bytes(packet_body).replace(old_ip, new_ip)
|
|
|
|
|
+ if extra_replacements:
|
|
|
|
|
+ for old_val, new_val in extra_replacements:
|
|
|
|
|
+ new_body = new_body.replace(old_val, new_val)
|
|
|
|
|
+
|
|
|
|
|
+ # Re-encode: header byte + new remaining length + new body
|
|
|
|
|
+ result.append(header_byte)
|
|
|
|
|
+
|
|
|
|
|
+ # Encode remaining length (MQTT variable-length encoding)
|
|
|
|
|
+ new_remaining = len(new_body)
|
|
|
|
|
+ while True:
|
|
|
|
|
+ encoded_byte = new_remaining % 128
|
|
|
|
|
+ new_remaining //= 128
|
|
|
|
|
+ if new_remaining > 0:
|
|
|
|
|
+ encoded_byte |= 0x80
|
|
|
|
|
+ result.append(encoded_byte)
|
|
|
|
|
+ if new_remaining == 0:
|
|
|
|
|
+ break
|
|
|
|
|
+
|
|
|
|
|
+ result.extend(new_body)
|
|
|
|
|
+ else:
|
|
|
|
|
+ # Pass through unchanged
|
|
|
|
|
+ result.extend(buffer[packet_start:pos])
|
|
|
|
|
+
|
|
|
|
|
+ buffer.clear()
|
|
|
|
|
+ return bytes(result), buffer
|
|
|
|
|
+
|
|
|
async def _forward(
|
|
async def _forward(
|
|
|
self,
|
|
self,
|
|
|
reader: asyncio.StreamReader,
|
|
reader: asyncio.StreamReader,
|
|
|
writer: asyncio.StreamWriter,
|
|
writer: asyncio.StreamWriter,
|
|
|
direction: str,
|
|
direction: str,
|
|
|
|
|
+ rewrite_ip: bool = False,
|
|
|
) -> None:
|
|
) -> None:
|
|
|
"""Forward data from reader to writer.
|
|
"""Forward data from reader to writer.
|
|
|
|
|
|
|
@@ -317,7 +537,12 @@ class TLSProxy:
|
|
|
reader: Source stream (already TLS-decrypted)
|
|
reader: Source stream (already TLS-decrypted)
|
|
|
writer: Destination stream (will be TLS-encrypted by the stream)
|
|
writer: Destination stream (will be TLS-encrypted by the stream)
|
|
|
direction: Description for logging (e.g., "client→printer")
|
|
direction: Description for logging (e.g., "client→printer")
|
|
|
|
|
+ rewrite_ip: If True and rewrite_ip was configured, replace the
|
|
|
|
|
+ printer's real IP with the proxy's bind IP in the data.
|
|
|
"""
|
|
"""
|
|
|
|
|
+ do_rewrite = rewrite_ip and self._rewrite_old is not None
|
|
|
|
|
+ rewrite_buffer = bytearray() if do_rewrite else None
|
|
|
|
|
+ rewrite_logged = False
|
|
|
total_bytes = 0
|
|
total_bytes = 0
|
|
|
try:
|
|
try:
|
|
|
while self._running:
|
|
while self._running:
|
|
@@ -327,12 +552,40 @@ class TLSProxy:
|
|
|
# Connection closed
|
|
# Connection closed
|
|
|
break
|
|
break
|
|
|
|
|
|
|
|
|
|
+ # Rewrite printer IP → proxy IP in MQTT PUBLISH payloads
|
|
|
|
|
+ # to prevent the slicer from bypassing the proxy.
|
|
|
|
|
+ if do_rewrite:
|
|
|
|
|
+ extra = [(self._rewrite_old_int, self._rewrite_new_int)] if self._rewrite_old_int else None
|
|
|
|
|
+ data, rewrite_buffer = self._rewrite_mqtt_ip(
|
|
|
|
|
+ data,
|
|
|
|
|
+ self._rewrite_old,
|
|
|
|
|
+ self._rewrite_new,
|
|
|
|
|
+ rewrite_buffer,
|
|
|
|
|
+ extra_replacements=extra,
|
|
|
|
|
+ )
|
|
|
|
|
+ if not rewrite_logged and data:
|
|
|
|
|
+ if self._rewrite_old in data:
|
|
|
|
|
+ logger.warning(
|
|
|
|
|
+ "%s proxy IP rewrite FAILED — %s still present after rewrite!",
|
|
|
|
|
+ self.name,
|
|
|
|
|
+ self._rewrite_old.decode(),
|
|
|
|
|
+ )
|
|
|
|
|
+ else:
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "%s proxy IP rewrite active: %s → %s",
|
|
|
|
|
+ self.name,
|
|
|
|
|
+ self._rewrite_old.decode(),
|
|
|
|
|
+ self._rewrite_new.decode(),
|
|
|
|
|
+ )
|
|
|
|
|
+ rewrite_logged = True
|
|
|
|
|
+ if not data:
|
|
|
|
|
+ continue # All data buffered, waiting for more
|
|
|
|
|
+
|
|
|
# Forward to destination
|
|
# Forward to destination
|
|
|
writer.write(data)
|
|
writer.write(data)
|
|
|
await writer.drain()
|
|
await writer.drain()
|
|
|
|
|
|
|
|
total_bytes += len(data)
|
|
total_bytes += len(data)
|
|
|
- logger.debug("%s proxy %s: %s bytes", self.name, direction, len(data))
|
|
|
|
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
except asyncio.CancelledError:
|
|
|
pass # Expected when the other forwarding direction closes first
|
|
pass # Expected when the other forwarding direction closes first
|
|
@@ -628,11 +881,21 @@ class FTPTLSProxy(TLSProxy):
|
|
|
await client_writer.wait_closed()
|
|
await client_writer.wait_closed()
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
+ # Capture the TLS session from the control channel for data channel
|
|
|
|
|
+ # reuse. vsFTPd (X1C) requires require_ssl_reuse — the data connection
|
|
|
|
|
+ # must present the same TLS session as the control channel.
|
|
|
|
|
+ ctrl_ssl_object = printer_writer.get_extra_info("ssl_object")
|
|
|
|
|
+ ctrl_tls_session = ctrl_ssl_object.session if ctrl_ssl_object else None
|
|
|
|
|
+ if ctrl_tls_session:
|
|
|
|
|
+ logger.debug("%s proxy: captured TLS session for data channel reuse", self.name)
|
|
|
|
|
+
|
|
|
# Track data channel protection level per session.
|
|
# Track data channel protection level per session.
|
|
|
# PROT C = cleartext data, PROT P = TLS data.
|
|
# PROT C = cleartext data, PROT P = TLS data.
|
|
|
# Default to cleartext — many Bambu printers (A1, H2D) use PROT C.
|
|
# Default to cleartext — many Bambu printers (A1, H2D) use PROT C.
|
|
|
# If the slicer sends PROT P, we switch to TLS for data connections.
|
|
# If the slicer sends PROT P, we switch to TLS for data connections.
|
|
|
- session_state: dict[str, str] = {"prot": "C"}
|
|
|
|
|
|
|
+ session_state: dict[str, str | ssl.SSLSession] = {"prot": "C"}
|
|
|
|
|
+ if ctrl_tls_session:
|
|
|
|
|
+ session_state["tls_session"] = ctrl_tls_session
|
|
|
|
|
|
|
|
# Client→Printer: intercept EPSV and replace with PASV
|
|
# Client→Printer: intercept EPSV and replace with PASV
|
|
|
# EPSV responses only contain a port (no IP), so the slicer reuses
|
|
# EPSV responses only contain a port (no IP), so the slicer reuses
|
|
@@ -688,7 +951,7 @@ class FTPTLSProxy(TLSProxy):
|
|
|
reader: asyncio.StreamReader,
|
|
reader: asyncio.StreamReader,
|
|
|
writer: asyncio.StreamWriter,
|
|
writer: asyncio.StreamWriter,
|
|
|
direction: str,
|
|
direction: str,
|
|
|
- session_state: dict[str, str],
|
|
|
|
|
|
|
+ session_state: dict[str, str | ssl.SSLSession],
|
|
|
) -> None:
|
|
) -> None:
|
|
|
"""Forward FTP client commands, replacing EPSV with PASV.
|
|
"""Forward FTP client commands, replacing EPSV with PASV.
|
|
|
|
|
|
|
@@ -720,13 +983,8 @@ class FTPTLSProxy(TLSProxy):
|
|
|
|
|
|
|
|
cmd_upper = line.strip().upper()
|
|
cmd_upper = line.strip().upper()
|
|
|
|
|
|
|
|
- # Replace EPSV with PASV so response includes an IP
|
|
|
|
|
- if cmd_upper == b"EPSV":
|
|
|
|
|
- line = b"PASV"
|
|
|
|
|
- logger.info("FTP command rewrite: EPSV → PASV")
|
|
|
|
|
-
|
|
|
|
|
# Track PROT level for data channel encryption
|
|
# Track PROT level for data channel encryption
|
|
|
- elif cmd_upper == b"PROT P":
|
|
|
|
|
|
|
+ if cmd_upper == b"PROT P":
|
|
|
session_state["prot"] = "P"
|
|
session_state["prot"] = "P"
|
|
|
logger.info("FTP data protection: PROT P (TLS)")
|
|
logger.info("FTP data protection: PROT P (TLS)")
|
|
|
elif cmd_upper == b"PROT C":
|
|
elif cmd_upper == b"PROT C":
|
|
@@ -765,7 +1023,7 @@ class FTPTLSProxy(TLSProxy):
|
|
|
writer: asyncio.StreamWriter,
|
|
writer: asyncio.StreamWriter,
|
|
|
direction: str,
|
|
direction: str,
|
|
|
local_ip: str,
|
|
local_ip: str,
|
|
|
- session_state: dict[str, str],
|
|
|
|
|
|
|
+ session_state: dict[str, str | ssl.SSLSession],
|
|
|
) -> None:
|
|
) -> None:
|
|
|
"""Forward FTP control channel responses, rewriting PASV/EPSV.
|
|
"""Forward FTP control channel responses, rewriting PASV/EPSV.
|
|
|
|
|
|
|
@@ -820,7 +1078,9 @@ class FTPTLSProxy(TLSProxy):
|
|
|
|
|
|
|
|
logger.debug("%s proxy %s: total %s bytes", self.name, direction, total_bytes)
|
|
logger.debug("%s proxy %s: total %s bytes", self.name, direction, total_bytes)
|
|
|
|
|
|
|
|
- async def _maybe_rewrite_pasv(self, line: bytes, local_ip: str, session_state: dict[str, str]) -> bytes:
|
|
|
|
|
|
|
+ async def _maybe_rewrite_pasv(
|
|
|
|
|
+ self, line: bytes, local_ip: str, session_state: dict[str, str | ssl.SSLSession]
|
|
|
|
|
+ ) -> bytes:
|
|
|
"""Rewrite PASV/EPSV response to point to a local data proxy."""
|
|
"""Rewrite PASV/EPSV response to point to a local data proxy."""
|
|
|
try:
|
|
try:
|
|
|
text = line.decode("utf-8")
|
|
text = line.decode("utf-8")
|
|
@@ -869,7 +1129,9 @@ class FTPTLSProxy(TLSProxy):
|
|
|
|
|
|
|
|
return line
|
|
return line
|
|
|
|
|
|
|
|
- async def _create_data_proxy(self, printer_ip: str, printer_port: int, session_state: dict[str, str]) -> int | None:
|
|
|
|
|
|
|
+ async def _create_data_proxy(
|
|
|
|
|
+ self, printer_ip: str, printer_port: int, session_state: dict[str, str | ssl.SSLSession]
|
|
|
|
|
+ ) -> int | None:
|
|
|
"""Create a one-shot proxy for an FTP data connection.
|
|
"""Create a one-shot proxy for an FTP data connection.
|
|
|
|
|
|
|
|
Prefers the printer's original passive port so the port number stays
|
|
Prefers the printer's original passive port so the port number stays
|
|
@@ -895,10 +1157,13 @@ class FTPTLSProxy(TLSProxy):
|
|
|
"TLS" if use_tls else "cleartext",
|
|
"TLS" if use_tls else "cleartext",
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ # Get control channel TLS session for data channel reuse
|
|
|
|
|
+ tls_session = session_state.get("tls_session") if use_tls else None
|
|
|
|
|
+
|
|
|
# Try the printer's original port first — this ensures the port
|
|
# Try the printer's original port first — this ensures the port
|
|
|
# matches even when bounce protection or iptables REDIRECT is in play.
|
|
# matches even when bounce protection or iptables REDIRECT is in play.
|
|
|
try:
|
|
try:
|
|
|
- await self._start_data_proxy_server(printer_port, printer_ip, printer_port, use_tls)
|
|
|
|
|
|
|
+ await self._start_data_proxy_server(printer_port, printer_ip, printer_port, use_tls, tls_session)
|
|
|
logger.info("FTP data proxy: using printer's port %s", printer_port)
|
|
logger.info("FTP data proxy: using printer's port %s", printer_port)
|
|
|
return printer_port
|
|
return printer_port
|
|
|
except OSError as e:
|
|
except OSError as e:
|
|
@@ -911,7 +1176,7 @@ class FTPTLSProxy(TLSProxy):
|
|
|
for _attempt in range(10):
|
|
for _attempt in range(10):
|
|
|
port = random.randint(self.PASV_PORT_MIN, self.PASV_PORT_MAX)
|
|
port = random.randint(self.PASV_PORT_MIN, self.PASV_PORT_MAX)
|
|
|
try:
|
|
try:
|
|
|
- await self._start_data_proxy_server(port, printer_ip, printer_port, use_tls)
|
|
|
|
|
|
|
+ await self._start_data_proxy_server(port, printer_ip, printer_port, use_tls, tls_session)
|
|
|
logger.info("FTP data proxy: using random port %s", port)
|
|
logger.info("FTP data proxy: using random port %s", port)
|
|
|
return port
|
|
return port
|
|
|
except OSError:
|
|
except OSError:
|
|
@@ -920,9 +1185,21 @@ class FTPTLSProxy(TLSProxy):
|
|
|
logger.error("Failed to bind FTP data proxy port after 10 attempts")
|
|
logger.error("Failed to bind FTP data proxy port after 10 attempts")
|
|
|
return None
|
|
return None
|
|
|
|
|
|
|
|
- async def _start_data_proxy_server(self, port: int, printer_ip: str, printer_port: int, use_tls: bool) -> None:
|
|
|
|
|
|
|
+ async def _start_data_proxy_server(
|
|
|
|
|
+ self,
|
|
|
|
|
+ port: int,
|
|
|
|
|
+ printer_ip: str,
|
|
|
|
|
+ printer_port: int,
|
|
|
|
|
+ use_tls: bool,
|
|
|
|
|
+ tls_session: ssl.SSLSession | None = None,
|
|
|
|
|
+ ) -> None:
|
|
|
"""Start a one-shot server for one FTP data connection.
|
|
"""Start a one-shot server for one FTP data connection.
|
|
|
|
|
|
|
|
|
|
+ When the slicer connects, immediately connects to the printer's data
|
|
|
|
|
+ port and buffers any slicer data until the printer connection is ready.
|
|
|
|
|
+ This handles zero-byte uploads (verify_job) where the slicer closes
|
|
|
|
|
+ the data channel before a naive proxy would finish its TLS handshake.
|
|
|
|
|
+
|
|
|
The slicer-side listener is ALWAYS cleartext. Even when the slicer
|
|
The slicer-side listener is ALWAYS cleartext. Even when the slicer
|
|
|
sends PROT P on the control channel, Bambu Studio does not perform
|
|
sends PROT P on the control channel, Bambu Studio does not perform
|
|
|
a TLS handshake on the data connection — it relies on the implicit
|
|
a TLS handshake on the data connection — it relies on the implicit
|
|
@@ -942,7 +1219,18 @@ class FTPTLSProxy(TLSProxy):
|
|
|
# Slicer side: ALWAYS cleartext — Bambu Studio does not do TLS on
|
|
# Slicer side: ALWAYS cleartext — Bambu Studio does not do TLS on
|
|
|
# the data channel even after sending PROT P.
|
|
# the data channel even after sending PROT P.
|
|
|
# Printer side: TLS if PROT P, cleartext if PROT C.
|
|
# Printer side: TLS if PROT P, cleartext if PROT C.
|
|
|
- client_ssl = self._client_ssl_context if use_tls else None
|
|
|
|
|
|
|
+ # For TLS data connections, wrap the SSL context to reuse the
|
|
|
|
|
+ # control channel's TLS session if available. vsFTPd (X1C) requires
|
|
|
|
|
+ # require_ssl_reuse — without this, data connections are rejected
|
|
|
|
|
+ # with "522 SSL connection failed: session reuse required".
|
|
|
|
|
+ if use_tls and tls_session:
|
|
|
|
|
+ client_ssl = _SessionReuseSSLContext(self._client_ssl_context, tls_session)
|
|
|
|
|
+ logger.debug("FTP data proxy: using TLS session reuse for port %s", port)
|
|
|
|
|
+ else:
|
|
|
|
|
+ client_ssl = self._client_ssl_context if use_tls else None
|
|
|
|
|
+
|
|
|
|
|
+ # Slicer side is ALWAYS cleartext — Bambu Studio does not do TLS on
|
|
|
|
|
+ # the data channel even after PROT P (confirmed for both H2D and X1C).
|
|
|
printer_mode = "TLS" if use_tls else "cleartext"
|
|
printer_mode = "TLS" if use_tls else "cleartext"
|
|
|
|
|
|
|
|
async def handle_data(
|
|
async def handle_data(
|
|
@@ -967,13 +1255,26 @@ class FTPTLSProxy(TLSProxy):
|
|
|
|
|
|
|
|
printer_writer = None
|
|
printer_writer = None
|
|
|
try:
|
|
try:
|
|
|
|
|
+ # Buffer any slicer data while connecting to printer.
|
|
|
|
|
+ # This handles the race where the slicer sends data (or closes
|
|
|
|
|
+ # for zero-byte files) before the TLS handshake completes.
|
|
|
|
|
+ slicer_buffer = bytearray()
|
|
|
|
|
+ slicer_eof = False
|
|
|
|
|
+
|
|
|
|
|
+ async def buffer_slicer():
|
|
|
|
|
+ nonlocal slicer_eof
|
|
|
|
|
+ while True:
|
|
|
|
|
+ chunk = await client_reader.read(65536)
|
|
|
|
|
+ if not chunk:
|
|
|
|
|
+ slicer_eof = True
|
|
|
|
|
+ return
|
|
|
|
|
+ slicer_buffer.extend(chunk)
|
|
|
|
|
+
|
|
|
|
|
+ buffer_task = asyncio.create_task(buffer_slicer())
|
|
|
|
|
+
|
|
|
# Connect to printer's data port
|
|
# Connect to printer's data port
|
|
|
printer_reader, printer_writer = await asyncio.wait_for(
|
|
printer_reader, printer_writer = await asyncio.wait_for(
|
|
|
- asyncio.open_connection(
|
|
|
|
|
- printer_ip,
|
|
|
|
|
- printer_port,
|
|
|
|
|
- ssl=client_ssl,
|
|
|
|
|
- ),
|
|
|
|
|
|
|
+ asyncio.open_connection(printer_ip, printer_port, ssl=client_ssl),
|
|
|
timeout=10.0,
|
|
timeout=10.0,
|
|
|
)
|
|
)
|
|
|
logger.info(
|
|
logger.info(
|
|
@@ -984,21 +1285,92 @@ class FTPTLSProxy(TLSProxy):
|
|
|
printer_port,
|
|
printer_port,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- # Bidirectional data forwarding
|
|
|
|
|
- c2p = asyncio.create_task(self._forward(client_reader, printer_writer, "data_c2p"))
|
|
|
|
|
- p2c = asyncio.create_task(self._forward(printer_reader, client_writer, "data_p2c"))
|
|
|
|
|
-
|
|
|
|
|
- done, pending = await asyncio.wait([c2p, p2c], return_when=asyncio.FIRST_COMPLETED)
|
|
|
|
|
- for task in pending:
|
|
|
|
|
- task.cancel()
|
|
|
|
|
- try:
|
|
|
|
|
- await task
|
|
|
|
|
- except asyncio.CancelledError:
|
|
|
|
|
- pass # Expected when other data direction closes
|
|
|
|
|
- except TimeoutError:
|
|
|
|
|
- logger.error("FTP data proxy port %s: timeout connecting to printer", port)
|
|
|
|
|
- except ssl.SSLError as e:
|
|
|
|
|
- logger.error("FTP data proxy port %s: SSL error to printer: %s", port, e)
|
|
|
|
|
|
|
+ # Stop buffering
|
|
|
|
|
+ buffer_task.cancel()
|
|
|
|
|
+ try:
|
|
|
|
|
+ await buffer_task
|
|
|
|
|
+ except asyncio.CancelledError:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ # Flush buffered slicer data to printer
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "FTP data proxy port %s: buffer=%s bytes, slicer_eof=%s",
|
|
|
|
|
+ port,
|
|
|
|
|
+ len(slicer_buffer),
|
|
|
|
|
+ slicer_eof,
|
|
|
|
|
+ )
|
|
|
|
|
+ if slicer_buffer:
|
|
|
|
|
+ printer_writer.write(bytes(slicer_buffer))
|
|
|
|
|
+ await printer_writer.drain()
|
|
|
|
|
+
|
|
|
|
|
+ # Forward remaining slicer data to printer, then close the
|
|
|
|
|
+ # printer side to signal upload complete.
|
|
|
|
|
+ #
|
|
|
|
|
+ # Bambu Studio does NOT close the FTP data channel after sending
|
|
|
|
|
+ # STOR data — it keeps the connection open and waits for the
|
|
|
|
|
+ # printer to close its side + send 226 on the control channel.
|
|
|
|
|
+ # A naive bidirectional proxy deadlocks here because the proxy
|
|
|
|
|
+ # waits for the slicer EOF that never comes.
|
|
|
|
|
+ #
|
|
|
|
|
+ # Fix: read slicer data with an idle timeout. Once data has been
|
|
|
|
|
+ # received and the slicer goes quiet, close the printer side so
|
|
|
|
|
+ # the printer can send 226. For RETR (download), the printer
|
|
|
|
|
+ # sends data and closes — the slicer reads until EOF — so this
|
|
|
|
|
+ # unidirectional approach works for both directions.
|
|
|
|
|
+ total_c2p = len(slicer_buffer)
|
|
|
|
|
+ if not slicer_eof:
|
|
|
|
|
+ # Read remaining slicer data with idle detection.
|
|
|
|
|
+ # Must be short — Bambu Studio expects 226 almost instantly
|
|
|
|
|
+ # after sending data. Too long and the slicer times out.
|
|
|
|
|
+ idle_timeout = 0.3
|
|
|
|
|
+ while True:
|
|
|
|
|
+ try:
|
|
|
|
|
+ chunk = await asyncio.wait_for(client_reader.read(65536), timeout=idle_timeout)
|
|
|
|
|
+ except TimeoutError:
|
|
|
|
|
+ if total_c2p > 0:
|
|
|
|
|
+ # Slicer sent data then went idle — upload done
|
|
|
|
|
+ logger.debug(
|
|
|
|
|
+ "FTP data proxy port %s: slicer idle after %s bytes, closing printer side",
|
|
|
|
|
+ port,
|
|
|
|
|
+ total_c2p,
|
|
|
|
|
+ )
|
|
|
|
|
+ break
|
|
|
|
|
+ continue # No data yet, keep waiting
|
|
|
|
|
+ if not chunk:
|
|
|
|
|
+ break # Slicer closed
|
|
|
|
|
+ printer_writer.write(chunk)
|
|
|
|
|
+ await printer_writer.drain()
|
|
|
|
|
+ total_c2p += len(chunk)
|
|
|
|
|
+
|
|
|
|
|
+ logger.debug("FTP proxy data_c2p: total %s bytes", total_c2p)
|
|
|
|
|
+
|
|
|
|
|
+ # Close printer side to signal upload complete.
|
|
|
|
|
+ # For TLS, close() sends close_notify which the printer treats
|
|
|
|
|
+ # as end-of-data. The printer then sends 226 on the control
|
|
|
|
|
+ # channel. For RETR, this is a no-op since the printer closes
|
|
|
|
|
+ # first and we'd have exited the loop above via EOF.
|
|
|
|
|
+ try:
|
|
|
|
|
+ printer_writer.close()
|
|
|
|
|
+ await printer_writer.wait_closed()
|
|
|
|
|
+ except OSError:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
|
|
+ # Wait for 226 response to propagate through the FTP control
|
|
|
|
|
+ # channel before closing the slicer's data channel.
|
|
|
|
|
+ #
|
|
|
|
|
+ # Without this delay, the data channel FIN arrives at the
|
|
|
|
|
+ # slicer before the 226 response on the control channel.
|
|
|
|
|
+ # BambuStudio reacts to the data channel FIN within <1ms
|
|
|
|
|
+ # by sending QUIT + closing the control channel — before
|
|
|
|
|
+ # 226 arrives (~2-3ms network RTT). This causes verify_job
|
|
|
|
|
+ # to be treated as failed and shows the login modal.
|
|
|
|
|
+ #
|
|
|
|
|
+ # In a direct connection, the printer sends 226 AND closes
|
|
|
|
|
+ # the data channel simultaneously, so the slicer gets both
|
|
|
|
|
+ # at once. The delay here emulates that timing.
|
|
|
|
|
+ if total_c2p > 0:
|
|
|
|
|
+ await asyncio.sleep(0.5)
|
|
|
|
|
+
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
logger.error("FTP data proxy port %s: error: %s", port, e)
|
|
logger.error("FTP data proxy port %s: error: %s", port, e)
|
|
|
finally:
|
|
finally:
|
|
@@ -1016,8 +1388,7 @@ class FTPTLSProxy(TLSProxy):
|
|
|
"0.0.0.0", # nosec B104
|
|
"0.0.0.0", # nosec B104
|
|
|
port,
|
|
port,
|
|
|
# No TLS on slicer side — Bambu Studio doesn't do TLS on data
|
|
# No TLS on slicer side — Bambu Studio doesn't do TLS on data
|
|
|
- # channel even after PROT P. The proxy terminates TLS only on
|
|
|
|
|
- # the printer side (inside handle_data).
|
|
|
|
|
|
|
+ # channel even after PROT P (confirmed by connection hang test).
|
|
|
)
|
|
)
|
|
|
server_holder.append(server)
|
|
server_holder.append(server)
|
|
|
self._data_servers.append(server)
|
|
self._data_servers.append(server)
|
|
@@ -1048,6 +1419,8 @@ class SlicerProxyManager:
|
|
|
# Bambu printer ports
|
|
# Bambu printer ports
|
|
|
PRINTER_FTP_PORT = 990
|
|
PRINTER_FTP_PORT = 990
|
|
|
PRINTER_MQTT_PORT = 8883
|
|
PRINTER_MQTT_PORT = 8883
|
|
|
|
|
+ PRINTER_FILE_TRANSFER_PORT = 6000
|
|
|
|
|
+ PRINTER_RTSP_PORT = 322 # X1/H2/P2 series camera (A1/P1 use port 6000)
|
|
|
PRINTER_BIND_PORTS = [3000, 3002]
|
|
PRINTER_BIND_PORTS = [3000, 3002]
|
|
|
|
|
|
|
|
# Local listen ports - must match what Bambu Studio expects
|
|
# Local listen ports - must match what Bambu Studio expects
|
|
@@ -1062,6 +1435,7 @@ class SlicerProxyManager:
|
|
|
key_path: Path,
|
|
key_path: Path,
|
|
|
on_activity: Callable[[str, str], None] | None = None,
|
|
on_activity: Callable[[str, str], None] | None = None,
|
|
|
bind_address: str = "0.0.0.0", # nosec B104
|
|
bind_address: str = "0.0.0.0", # nosec B104
|
|
|
|
|
+ bind_identity: dict[str, str] | None = None,
|
|
|
):
|
|
):
|
|
|
"""Initialize the slicer proxy manager.
|
|
"""Initialize the slicer proxy manager.
|
|
|
|
|
|
|
@@ -1071,25 +1445,46 @@ class SlicerProxyManager:
|
|
|
key_path: Path to server private key
|
|
key_path: Path to server private key
|
|
|
on_activity: Optional callback for activity logging (name, message)
|
|
on_activity: Optional callback for activity logging (name, message)
|
|
|
bind_address: IP address to bind proxy listeners to
|
|
bind_address: IP address to bind proxy listeners to
|
|
|
|
|
+ bind_identity: Optional dict with keys (serial, model, name, version)
|
|
|
|
|
+ for the bind/detect response. When provided, the proxy responds
|
|
|
|
|
+ to detect requests itself instead of forwarding to the printer.
|
|
|
|
|
+ This ensures the slicer sees the VP identity, not the real printer.
|
|
|
"""
|
|
"""
|
|
|
self.target_host = target_host
|
|
self.target_host = target_host
|
|
|
self.cert_path = cert_path
|
|
self.cert_path = cert_path
|
|
|
self.key_path = key_path
|
|
self.key_path = key_path
|
|
|
self.on_activity = on_activity
|
|
self.on_activity = on_activity
|
|
|
self.bind_address = bind_address
|
|
self.bind_address = bind_address
|
|
|
|
|
+ self.bind_identity = bind_identity
|
|
|
|
|
|
|
|
- self._ftp_proxy: TLSProxy | None = None
|
|
|
|
|
|
|
+ self._ftp_proxy: TCPProxy | None = None
|
|
|
self._mqtt_proxy: TLSProxy | None = None
|
|
self._mqtt_proxy: TLSProxy | None = None
|
|
|
|
|
+ self._file_transfer_proxy: TCPProxy | None = None
|
|
|
|
|
+ self._rtsp_proxy: TCPProxy | None = None
|
|
|
self._bind_proxies: list[TCPProxy] = []
|
|
self._bind_proxies: list[TCPProxy] = []
|
|
|
|
|
+ self._bind_server = None
|
|
|
|
|
+ self._probe_servers: list[asyncio.Server] = []
|
|
|
self._tasks: list[asyncio.Task] = []
|
|
self._tasks: list[asyncio.Task] = []
|
|
|
|
|
|
|
|
|
|
+ # FTP passive data port range — Bambu printers typically use ports in
|
|
|
|
|
+ # this range for EPSV/PASV data connections. We pre-listen on all of
|
|
|
|
|
+ # them so EPSV works transparently without decrypting FTP control.
|
|
|
|
|
+ FTP_DATA_PORT_MIN = 50000
|
|
|
|
|
+ FTP_DATA_PORT_MAX = 50100
|
|
|
|
|
+
|
|
|
async def start(self) -> None:
|
|
async def start(self) -> None:
|
|
|
- """Start FTP and MQTT TLS proxies."""
|
|
|
|
|
- logger.info("Starting slicer TLS proxy to %s", self.target_host)
|
|
|
|
|
|
|
+ """Start proxy services.
|
|
|
|
|
+
|
|
|
|
|
+ Uses transparent TCP proxying for most protocols (FTP, FileTransfer,
|
|
|
|
|
+ Camera) — raw bytes are forwarded without TLS termination, so the
|
|
|
|
|
+ slicer gets the printer's real TLS certificate end-to-end.
|
|
|
|
|
+
|
|
|
|
|
+ Only MQTT is TLS-terminated because we must decrypt the payload to
|
|
|
|
|
+ rewrite the printer's real IP with the proxy's bind IP.
|
|
|
|
|
+ """
|
|
|
|
|
+ logger.info("Starting slicer proxy to %s (transparent mode)", self.target_host)
|
|
|
|
|
|
|
|
- # Detect iptables port redirect (e.g. if an external redirect exists).
|
|
|
|
|
- # If active, connections get intercepted by iptables PREROUTING
|
|
|
|
|
- # and sent to the redirect target — our socket never sees them.
|
|
|
|
|
|
|
+ # Detect iptables port redirect for FTP
|
|
|
ftp_listen_port = self.LOCAL_FTP_PORT
|
|
ftp_listen_port = self.LOCAL_FTP_PORT
|
|
|
redirect_target = detect_port_redirect(self.LOCAL_FTP_PORT)
|
|
redirect_target = detect_port_redirect(self.LOCAL_FTP_PORT)
|
|
|
if redirect_target:
|
|
if redirect_target:
|
|
@@ -1101,19 +1496,35 @@ class SlicerProxyManager:
|
|
|
)
|
|
)
|
|
|
ftp_listen_port = redirect_target
|
|
ftp_listen_port = redirect_target
|
|
|
|
|
|
|
|
- # Create FTP proxy with PASV/EPSV awareness for data connections
|
|
|
|
|
- self._ftp_proxy = FTPTLSProxy(
|
|
|
|
|
|
|
+ # FTP control — raw TCP pass-through (end-to-end TLS with printer)
|
|
|
|
|
+ self._ftp_proxy = TCPProxy(
|
|
|
name="FTP",
|
|
name="FTP",
|
|
|
listen_port=ftp_listen_port,
|
|
listen_port=ftp_listen_port,
|
|
|
target_host=self.target_host,
|
|
target_host=self.target_host,
|
|
|
target_port=self.PRINTER_FTP_PORT,
|
|
target_port=self.PRINTER_FTP_PORT,
|
|
|
- server_cert_path=self.cert_path,
|
|
|
|
|
- server_key_path=self.key_path,
|
|
|
|
|
on_connect=lambda cid: self._log_activity("FTP", f"connected: {cid}"),
|
|
on_connect=lambda cid: self._log_activity("FTP", f"connected: {cid}"),
|
|
|
on_disconnect=lambda cid: self._log_activity("FTP", f"disconnected: {cid}"),
|
|
on_disconnect=lambda cid: self._log_activity("FTP", f"disconnected: {cid}"),
|
|
|
bind_address=self.bind_address,
|
|
bind_address=self.bind_address,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ # FTP data ports — pre-listen on the entire passive port range.
|
|
|
|
|
+ # Since FTP control is encrypted end-to-end, we can't read EPSV
|
|
|
|
|
+ # responses to know which port the printer chose. Instead, we
|
|
|
|
|
+ # listen on every port in the range and forward to the same port
|
|
|
|
|
+ # on the printer. The slicer connects to bind_ip:PORT (from EPSV)
|
|
|
|
|
+ # and we transparently relay to printer_ip:PORT.
|
|
|
|
|
+ self._ftp_data_proxies: list[TCPProxy] = []
|
|
|
|
|
+ for port in range(self.FTP_DATA_PORT_MIN, self.FTP_DATA_PORT_MAX + 1):
|
|
|
|
|
+ dp = TCPProxy(
|
|
|
|
|
+ name=f"FTP-Data-{port}",
|
|
|
|
|
+ listen_port=port,
|
|
|
|
|
+ target_host=self.target_host,
|
|
|
|
|
+ target_port=port,
|
|
|
|
|
+ bind_address=self.bind_address,
|
|
|
|
|
+ )
|
|
|
|
|
+ self._ftp_data_proxies.append(dp)
|
|
|
|
|
+
|
|
|
|
|
+ # MQTT — TLS-terminating proxy (must decrypt to rewrite IP addresses)
|
|
|
self._mqtt_proxy = TLSProxy(
|
|
self._mqtt_proxy = TLSProxy(
|
|
|
name="MQTT",
|
|
name="MQTT",
|
|
|
listen_port=self.LOCAL_MQTT_PORT,
|
|
listen_port=self.LOCAL_MQTT_PORT,
|
|
@@ -1124,36 +1535,76 @@ class SlicerProxyManager:
|
|
|
on_connect=lambda cid: self._log_activity("MQTT", f"connected: {cid}"),
|
|
on_connect=lambda cid: self._log_activity("MQTT", f"connected: {cid}"),
|
|
|
on_disconnect=lambda cid: self._log_activity("MQTT", f"disconnected: {cid}"),
|
|
on_disconnect=lambda cid: self._log_activity("MQTT", f"disconnected: {cid}"),
|
|
|
bind_address=self.bind_address,
|
|
bind_address=self.bind_address,
|
|
|
|
|
+ rewrite_ip=(self.target_host, self.bind_address) if self.bind_address != "0.0.0.0" else None, # nosec B104
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- # Bind/auth proxy — port 3000 plain TCP, port 3002 TLS
|
|
|
|
|
- for bind_port in self.PRINTER_BIND_PORTS:
|
|
|
|
|
- if bind_port == 3002:
|
|
|
|
|
- proxy = TLSProxy(
|
|
|
|
|
- name="Bind-TLS",
|
|
|
|
|
- listen_port=bind_port,
|
|
|
|
|
- target_host=self.target_host,
|
|
|
|
|
- target_port=bind_port,
|
|
|
|
|
- server_cert_path=self.cert_path,
|
|
|
|
|
- server_key_path=self.key_path,
|
|
|
|
|
- on_connect=lambda cid: self._log_activity("Bind", f"connected: {cid}"),
|
|
|
|
|
- on_disconnect=lambda cid: self._log_activity("Bind", f"disconnected: {cid}"),
|
|
|
|
|
- bind_address=self.bind_address,
|
|
|
|
|
- )
|
|
|
|
|
- else:
|
|
|
|
|
- proxy = TCPProxy(
|
|
|
|
|
- name="Bind",
|
|
|
|
|
- listen_port=bind_port,
|
|
|
|
|
- target_host=self.target_host,
|
|
|
|
|
- target_port=bind_port,
|
|
|
|
|
- on_connect=lambda cid: self._log_activity("Bind", f"connected: {cid}"),
|
|
|
|
|
- on_disconnect=lambda cid: self._log_activity("Bind", f"disconnected: {cid}"),
|
|
|
|
|
- bind_address=self.bind_address,
|
|
|
|
|
- )
|
|
|
|
|
- self._bind_proxies.append(proxy)
|
|
|
|
|
|
|
+ # File transfer — raw TCP pass-through (port 6000)
|
|
|
|
|
+ self._file_transfer_proxy = TCPProxy(
|
|
|
|
|
+ name="FileTransfer",
|
|
|
|
|
+ listen_port=self.PRINTER_FILE_TRANSFER_PORT,
|
|
|
|
|
+ target_host=self.target_host,
|
|
|
|
|
+ target_port=self.PRINTER_FILE_TRANSFER_PORT,
|
|
|
|
|
+ on_connect=lambda cid: self._log_activity("FileTransfer", f"connected: {cid}"),
|
|
|
|
|
+ on_disconnect=lambda cid: self._log_activity("FileTransfer", f"disconnected: {cid}"),
|
|
|
|
|
+ bind_address=self.bind_address,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # RTSP camera — raw TCP pass-through (port 322)
|
|
|
|
|
+ self._rtsp_proxy = TCPProxy(
|
|
|
|
|
+ name="RTSP",
|
|
|
|
|
+ listen_port=self.PRINTER_RTSP_PORT,
|
|
|
|
|
+ target_host=self.target_host,
|
|
|
|
|
+ target_port=self.PRINTER_RTSP_PORT,
|
|
|
|
|
+ on_connect=lambda cid: self._log_activity("RTSP", f"connected: {cid}"),
|
|
|
|
|
+ on_disconnect=lambda cid: self._log_activity("RTSP", f"disconnected: {cid}"),
|
|
|
|
|
+ bind_address=self.bind_address,
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ # Bind/auth — respond with VP identity instead of proxying to printer.
|
|
|
|
|
+ # The detect response contains the printer name, serial, model, and
|
|
|
|
|
+ # bind status. Proxying it would leak the real printer's identity and
|
|
|
|
|
+ # cause the slicer to treat it as a different device.
|
|
|
|
|
+ if self.bind_identity:
|
|
|
|
|
+ from backend.app.services.virtual_printer.bind_server import BindServer
|
|
|
|
|
+
|
|
|
|
|
+ self._bind_server = BindServer(
|
|
|
|
|
+ serial=self.bind_identity["serial"],
|
|
|
|
|
+ model=self.bind_identity["model"],
|
|
|
|
|
+ name=self.bind_identity["name"],
|
|
|
|
|
+ version=self.bind_identity.get("version", "01.00.00.00"),
|
|
|
|
|
+ bind_address=self.bind_address,
|
|
|
|
|
+ cert_path=self.cert_path,
|
|
|
|
|
+ key_path=self.key_path,
|
|
|
|
|
+ )
|
|
|
|
|
+ else:
|
|
|
|
|
+ # Fallback: proxy bind requests to the real printer
|
|
|
|
|
+ for bind_port in self.PRINTER_BIND_PORTS:
|
|
|
|
|
+ if bind_port == 3002:
|
|
|
|
|
+ proxy = TLSProxy(
|
|
|
|
|
+ name="Bind-TLS",
|
|
|
|
|
+ listen_port=bind_port,
|
|
|
|
|
+ target_host=self.target_host,
|
|
|
|
|
+ target_port=bind_port,
|
|
|
|
|
+ server_cert_path=self.cert_path,
|
|
|
|
|
+ server_key_path=self.key_path,
|
|
|
|
|
+ on_connect=lambda cid: self._log_activity("Bind", f"connected: {cid}"),
|
|
|
|
|
+ on_disconnect=lambda cid: self._log_activity("Bind", f"disconnected: {cid}"),
|
|
|
|
|
+ bind_address=self.bind_address,
|
|
|
|
|
+ )
|
|
|
|
|
+ else:
|
|
|
|
|
+ proxy = TCPProxy(
|
|
|
|
|
+ name="Bind",
|
|
|
|
|
+ listen_port=bind_port,
|
|
|
|
|
+ target_host=self.target_host,
|
|
|
|
|
+ target_port=bind_port,
|
|
|
|
|
+ on_connect=lambda cid: self._log_activity("Bind", f"connected: {cid}"),
|
|
|
|
|
+ on_disconnect=lambda cid: self._log_activity("Bind", f"disconnected: {cid}"),
|
|
|
|
|
+ bind_address=self.bind_address,
|
|
|
|
|
+ )
|
|
|
|
|
+ self._bind_proxies.append(proxy)
|
|
|
|
|
|
|
|
# Start as background tasks
|
|
# Start as background tasks
|
|
|
- async def run_with_logging(proxy: TLSProxy) -> None:
|
|
|
|
|
|
|
+ async def run_with_logging(proxy: TLSProxy | TCPProxy) -> None:
|
|
|
try:
|
|
try:
|
|
|
await proxy.start()
|
|
await proxy.start()
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
@@ -1168,7 +1619,22 @@ class SlicerProxyManager:
|
|
|
run_with_logging(self._mqtt_proxy),
|
|
run_with_logging(self._mqtt_proxy),
|
|
|
name="slicer_proxy_mqtt",
|
|
name="slicer_proxy_mqtt",
|
|
|
),
|
|
),
|
|
|
|
|
+ asyncio.create_task(
|
|
|
|
|
+ run_with_logging(self._file_transfer_proxy),
|
|
|
|
|
+ name="slicer_proxy_file_transfer",
|
|
|
|
|
+ ),
|
|
|
|
|
+ asyncio.create_task(
|
|
|
|
|
+ run_with_logging(self._rtsp_proxy),
|
|
|
|
|
+ name="slicer_proxy_rtsp",
|
|
|
|
|
+ ),
|
|
|
]
|
|
]
|
|
|
|
|
+ if self._bind_server:
|
|
|
|
|
+ self._tasks.append(
|
|
|
|
|
+ asyncio.create_task(
|
|
|
|
|
+ run_with_logging(self._bind_server),
|
|
|
|
|
+ name="slicer_proxy_bind_server",
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
for bp in self._bind_proxies:
|
|
for bp in self._bind_proxies:
|
|
|
self._tasks.append(
|
|
self._tasks.append(
|
|
|
asyncio.create_task(
|
|
asyncio.create_task(
|
|
@@ -1176,8 +1642,37 @@ class SlicerProxyManager:
|
|
|
name=f"slicer_proxy_bind_{bp.listen_port}",
|
|
name=f"slicer_proxy_bind_{bp.listen_port}",
|
|
|
)
|
|
)
|
|
|
)
|
|
)
|
|
|
|
|
+ # FTP data port proxies (50000-50100)
|
|
|
|
|
+ for dp in self._ftp_data_proxies:
|
|
|
|
|
+ self._tasks.append(
|
|
|
|
|
+ asyncio.create_task(
|
|
|
|
|
+ run_with_logging(dp),
|
|
|
|
|
+ name=f"slicer_proxy_ftp_data_{dp.listen_port}",
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
- logger.info("Slicer TLS proxy started for %s", self.target_host)
|
|
|
|
|
|
|
+ # Diagnostic probe: listen on common un-proxied ports to detect
|
|
|
|
|
+ # if the slicer tries to reach a service we don't handle.
|
|
|
|
|
+ if self.bind_address and self.bind_address != "0.0.0.0": # nosec B104
|
|
|
|
|
+ for probe_port in (21, 80, 443):
|
|
|
|
|
+ try:
|
|
|
|
|
+ srv = await asyncio.start_server(
|
|
|
|
|
+ lambda r, w, p=probe_port: self._probe_handler(r, w, p),
|
|
|
|
|
+ self.bind_address,
|
|
|
|
|
+ probe_port,
|
|
|
|
|
+ )
|
|
|
|
|
+ self._probe_servers.append(srv)
|
|
|
|
|
+ except OSError:
|
|
|
|
|
+ pass # Port in use or no permission — skip
|
|
|
|
|
+ if self._probe_servers:
|
|
|
|
|
+ probed = [s.sockets[0].getsockname()[1] for s in self._probe_servers if s.sockets]
|
|
|
|
|
+ logger.info("Proxy diagnostic: probing un-proxied ports %s on %s", probed, self.bind_address)
|
|
|
|
|
+
|
|
|
|
|
+ logger.info(
|
|
|
|
|
+ "Slicer proxy started for %s (transparent TCP + MQTT TLS, %d FTP data ports)",
|
|
|
|
|
+ self.target_host,
|
|
|
|
|
+ len(self._ftp_data_proxies),
|
|
|
|
|
+ )
|
|
|
|
|
|
|
|
# Wait for tasks to complete (they run until cancelled)
|
|
# Wait for tasks to complete (they run until cancelled)
|
|
|
# This keeps the start() coroutine alive so the parent task doesn't complete
|
|
# This keeps the start() coroutine alive so the parent task doesn't complete
|
|
@@ -1199,10 +1694,30 @@ class SlicerProxyManager:
|
|
|
await self._mqtt_proxy.stop()
|
|
await self._mqtt_proxy.stop()
|
|
|
self._mqtt_proxy = None
|
|
self._mqtt_proxy = None
|
|
|
|
|
|
|
|
|
|
+ if self._file_transfer_proxy:
|
|
|
|
|
+ await self._file_transfer_proxy.stop()
|
|
|
|
|
+ self._file_transfer_proxy = None
|
|
|
|
|
+
|
|
|
|
|
+ if self._rtsp_proxy:
|
|
|
|
|
+ await self._rtsp_proxy.stop()
|
|
|
|
|
+ self._rtsp_proxy = None
|
|
|
|
|
+
|
|
|
|
|
+ if self._bind_server:
|
|
|
|
|
+ await self._bind_server.stop()
|
|
|
|
|
+ self._bind_server = None
|
|
|
|
|
+
|
|
|
for bp in self._bind_proxies:
|
|
for bp in self._bind_proxies:
|
|
|
await bp.stop()
|
|
await bp.stop()
|
|
|
self._bind_proxies = []
|
|
self._bind_proxies = []
|
|
|
|
|
|
|
|
|
|
+ for dp in self._ftp_data_proxies:
|
|
|
|
|
+ await dp.stop()
|
|
|
|
|
+ self._ftp_data_proxies = []
|
|
|
|
|
+
|
|
|
|
|
+ for srv in self._probe_servers:
|
|
|
|
|
+ srv.close()
|
|
|
|
|
+ self._probe_servers = []
|
|
|
|
|
+
|
|
|
# Cancel tasks
|
|
# Cancel tasks
|
|
|
for task in self._tasks:
|
|
for task in self._tasks:
|
|
|
task.cancel()
|
|
task.cancel()
|
|
@@ -1219,6 +1734,21 @@ class SlicerProxyManager:
|
|
|
self._tasks = []
|
|
self._tasks = []
|
|
|
logger.info("Slicer proxy stopped")
|
|
logger.info("Slicer proxy stopped")
|
|
|
|
|
|
|
|
|
|
+ async def _probe_handler(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, port: int) -> None:
|
|
|
|
|
+ """Log unexpected connections on un-proxied ports for diagnostics."""
|
|
|
|
|
+ peername = writer.get_extra_info("peername")
|
|
|
|
|
+ client = f"{peername[0]}:{peername[1]}" if peername else "unknown"
|
|
|
|
|
+ logger.warning(
|
|
|
|
|
+ "PROBE: slicer connected to un-proxied port %d from %s — this port may need proxying",
|
|
|
|
|
+ port,
|
|
|
|
|
+ client,
|
|
|
|
|
+ )
|
|
|
|
|
+ writer.close()
|
|
|
|
|
+ try:
|
|
|
|
|
+ await writer.wait_closed()
|
|
|
|
|
+ except OSError:
|
|
|
|
|
+ pass
|
|
|
|
|
+
|
|
|
def _log_activity(self, name: str, message: str) -> None:
|
|
def _log_activity(self, name: str, message: str) -> None:
|
|
|
"""Log activity via callback if configured."""
|
|
"""Log activity via callback if configured."""
|
|
|
if self.on_activity:
|
|
if self.on_activity:
|