|
35 | 35 | import traceback |
36 | 36 | import zlib |
37 | 37 | from collections import deque, namedtuple |
38 | | -from typing import TYPE_CHECKING |
| 38 | +from typing import TYPE_CHECKING, Dict, Any |
39 | 39 |
|
40 | 40 | import aiohttp |
41 | 41 |
|
@@ -924,7 +924,7 @@ async def received_message(self, msg): |
924 | 924 |
|
925 | 925 | await self._hook(self, msg) |
926 | 926 |
|
927 | | - async def initial_connection(self, data): |
| 927 | + async def initial_connection_v1(self, data): |
928 | 928 | state = self._connection |
929 | 929 | state.ssrc = data["ssrc"] |
930 | 930 | state.voice_port = data["port"] |
@@ -967,6 +967,63 @@ async def initial_connection(self, data): |
967 | 967 | await self.select_protocol(state.ip, state.port, mode) |
968 | 968 | _log.info("selected the voice protocol for use (%s)", mode) |
969 | 969 |
|
| 970 | + async def initial_connection(self, data: Dict[str, Any]) -> None: |
| 971 | + state = self._connection |
| 972 | + state.ssrc = data['ssrc'] |
| 973 | + state.voice_port = data['port'] |
| 974 | + state.endpoint_ip = data['ip'] |
| 975 | + |
| 976 | + _log.debug('Connecting to voice socket') |
| 977 | + await self.loop.sock_connect(state.socket, (state.endpoint_ip, state.voice_port)) |
| 978 | + |
| 979 | + state.ip, state.port = await self.discover_ip(state) |
| 980 | + # there *should* always be at least one supported mode (xsalsa20_poly1305) |
| 981 | + modes = [mode for mode in data['modes'] if mode in self._connection.supported_modes] |
| 982 | + _log.debug('received supported encryption modes: %s', ', '.join(modes)) |
| 983 | + |
| 984 | + mode = modes[0] |
| 985 | + await self.select_protocol(state.ip, state.port, mode) |
| 986 | + _log.debug('selected the voice protocol for use (%s)', mode) |
| 987 | + |
| 988 | + async def discover_ip(self, state, max_retries: int = 2, base_timeout: float = 3.0): |
| 989 | + """Send a UDP discovery packet and wait for the correct 74-byte response.""" |
| 990 | + packet = bytearray(74) |
| 991 | + struct.pack_into(">H", packet, 0, 1) |
| 992 | + struct.pack_into(">H", packet, 2, 70) |
| 993 | + struct.pack_into(">I", packet, 4, state.ssrc) |
| 994 | + |
| 995 | + for attempt in range(max_retries): |
| 996 | + increment = 2.0 |
| 997 | + attempt_timeout = base_timeout + (attempt * increment) |
| 998 | + |
| 999 | + await self.loop.sock_sendall(state.socket, packet) |
| 1000 | + |
| 1001 | + try: |
| 1002 | + # wait for discovery reply with timeout |
| 1003 | + data = await asyncio.wait_for(self.loop.sock_recv(state.socket, 74), timeout=attempt_timeout) |
| 1004 | + |
| 1005 | + # validate packet |
| 1006 | + if len(data) == 74 and data[1] == 0x02: |
| 1007 | + ip_start = 8 |
| 1008 | + ip_end = data.index(0, ip_start) |
| 1009 | + ip = data[ip_start:ip_end].decode("ascii") |
| 1010 | + port = struct.unpack_from(">H", data, len(data) - 2)[0] |
| 1011 | + _log.debug("detected ip: %s port: %s", ip, port) |
| 1012 | + return ip, port |
| 1013 | + |
| 1014 | + # wrong packet type → keep waiting |
| 1015 | + _log.debug("Ignored non-discovery packet during handshake.") |
| 1016 | + continue |
| 1017 | + |
| 1018 | + except asyncio.TimeoutError: |
| 1019 | + if attempt + 1 < max_retries: |
| 1020 | + _log.warning("No discovery reply, retrying (%d/%d)...", attempt + 1, max_retries) |
| 1021 | + await asyncio.sleep(0.5) |
| 1022 | + continue |
| 1023 | + else: |
| 1024 | + _log.error("UDP discovery timed out after %d attempts.", max_retries) |
| 1025 | + raise |
| 1026 | + |
970 | 1027 | @property |
971 | 1028 | def latency(self) -> float: |
972 | 1029 | """Latency between a HEARTBEAT and its HEARTBEAT_ACK in seconds.""" |
|
0 commit comments