From 1c5b548cd94e717f9048657f8275c4eefa9df24e Mon Sep 17 00:00:00 2001 From: mvsrcdst <43897151+mvsrcdst@users.noreply.github.com> Date: Mon, 13 Jul 2026 19:59:15 +0300 Subject: [PATCH] fix: socks5 proxy ipv6 connect sent garbage address --- .../MtProtoKit/Sources/MTTcpConnection.m | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/submodules/MtProtoKit/Sources/MTTcpConnection.m b/submodules/MtProtoKit/Sources/MTTcpConnection.m index 21e8ff013bd..1e9237bda9e 100644 --- a/submodules/MtProtoKit/Sources/MTTcpConnection.m +++ b/submodules/MtProtoKit/Sources/MTTcpConnection.m @@ -1438,15 +1438,37 @@ - (void)connectionInterfaceDidReadPartialDataOfLength:(NSUInteger)partialLength - (void)requestSocksConnection { struct socks5_req req; - + req.Version = 5; req.Cmd = 1; req.Reserved = 0; - req.AddrType = 1; - + + // MARK: Swiftgram + // Upstream hardcoded AddrType=1 and ignored inet_aton's return value, so IPv6 DC + // addresses got sent as ATYP=1 with garbage (uninitialized) bytes. Detect IPv4/IPv6 + // properly, same as -start does for _socksIp/_mtpIp. Domain name (ATYP=3) fallback + // per SOCKS5 spec (RFC 1928 part 4), for non-IP-literal addresses, + // bounds-checked against the 256-byte Domain buffer. struct in_addr ip4; - inet_aton(_scheme.address.ip.UTF8String, &ip4); - req.DestAddr.IPv4 = ip4; + struct in6_addr ip6; + NSString *destAddress = _scheme.address.ip; + if (inet_aton(destAddress.UTF8String, &ip4) != 0) { + req.AddrType = 1; + req.DestAddr.IPv4 = ip4; + } else if (inet_pton(AF_INET6, destAddress.UTF8String, &ip6) != 0) { + req.AddrType = 4; + req.DestAddr.IPv6 = ip6; + } else if (destAddress.length <= 255 && [destAddress getCString:req.DestAddr.Domain maxLength:sizeof(req.DestAddr.Domain) encoding:NSASCIIStringEncoding]) { + req.AddrType = 3; + req.DestAddr.DomainLen = (unsigned char)destAddress.length; + } else { + if (MTLogEnabled()) { + MTLog(@"***** %s: could not parse destination address %@", __PRETTY_FUNCTION__, destAddress); + } + [self closeAndNotifyWithError:true]; + return; + } + req.DestPort = _scheme.address.port; NSMutableData *reqData = [[NSMutableData alloc] init];