A Python port of the Invisible Internet Project (I2P)
1"""SAM raw session -- raw I2P protocol delivery.
2
3Ported from net.i2p.sam.SAMRawSession.
4"""
5
6import logging
7
8logger = logging.getLogger(__name__)
9
10
11class SAMRawSession:
12 """Raw protocol session (no wrapper, max 32KB).
13
14 Raw sessions send data without the streaming or datagram wrappers.
15 The protocol field specifies the I2P protocol number.
16 """
17
18 MAX_SIZE = 32 * 1024 # 32KB maximum payload
19
20 def __init__(
21 self,
22 nickname: str,
23 destination_b64: str,
24 protocol: int = 18,
25 ) -> None:
26 self._nickname = nickname
27 self._destination_b64 = destination_b64
28 self._protocol = protocol
29
30 async def send(
31 self,
32 target_dest: str,
33 data: bytes,
34 protocol: int = 18,
35 from_port: int = 0,
36 to_port: int = 0,
37 ) -> None:
38 """Send raw data to the target destination.
39
40 Args:
41 target_dest: Base64-encoded target destination.
42 data: Payload bytes (max 32KB).
43 protocol: I2P protocol number.
44 from_port: Source port (virtual).
45 to_port: Destination port (virtual).
46
47 Raises:
48 ValueError: If data exceeds MAX_SIZE.
49 """
50 if len(data) > self.MAX_SIZE:
51 raise ValueError(
52 f"Raw payload exceeds maximum size: {len(data)} > {self.MAX_SIZE}"
53 )
54 logger.debug("RAW SEND to %s...%s (%d bytes, proto=%d) from session %s",
55 target_dest[:16], target_dest[-8:], len(data),
56 protocol, self._nickname)
57 # In production, this would send raw data through I2P tunnels.