diff --git a/td/telegram/net/ConnectionCreator.cpp b/td/telegram/net/ConnectionCreator.cpp index 57d4bc88c..47e319967 100644 --- a/td/telegram/net/ConnectionCreator.cpp +++ b/td/telegram/net/ConnectionCreator.cpp @@ -661,9 +661,9 @@ Result ConnectionCreator::get_transport_type(const Proxy if (!proxy.user().empty() || !proxy.password().empty()) { proxy_authorization = "|basic " + base64_encode(PSLICE() << proxy.user() << ':' << proxy.password()); } - return mtproto::TransportType{ - mtproto::TransportType::Http, 0, - mtproto::ProxySecret::from_raw(PSTRING() << info.option->get_ip_address().get_ip_str() << proxy_authorization)}; + return mtproto::TransportType{mtproto::TransportType::Http, 0, + mtproto::ProxySecret::from_raw( + PSTRING() << info.option->get_ip_address().get_ip_host() << proxy_authorization)}; } if (info.use_http) { diff --git a/tdnet/td/net/HttpProxy.cpp b/tdnet/td/net/HttpProxy.cpp index b604e2e38..87c34f9b8 100644 --- a/tdnet/td/net/HttpProxy.cpp +++ b/tdnet/td/net/HttpProxy.cpp @@ -47,7 +47,7 @@ Status HttpProxy::wait_connect_response() { size_t len = min(sizeof(buf), it.size()); it.advance(len, MutableSlice{buf, sizeof(buf)}); VLOG(proxy) << "Failed to connect: " << format::escaped(Slice(buf, len)); - return Status::Error(PSLICE() << "Failed to connect to " << ip_address_.get_ip_str() << ':' + return Status::Error(PSLICE() << "Failed to connect to " << ip_address_.get_ip_host() << ':' << ip_address_.get_port()); } diff --git a/tdutils/td/utils/port/IPAddress.cpp b/tdutils/td/utils/port/IPAddress.cpp index 32b1acef8..275184c61 100644 --- a/tdutils/td/utils/port/IPAddress.cpp +++ b/tdutils/td/utils/port/IPAddress.cpp @@ -545,6 +545,22 @@ CSlice IPAddress::get_ip_str() const { } } +string IPAddress::get_ip_host() const { + if (!is_valid()) { + return "0.0.0.0"; + } + + switch (get_address_family()) { + case AF_INET6: + return PSTRING() << '[' << ::td::get_ip_str(AF_INET6, &ipv6_addr_.sin6_addr) << ']'; + case AF_INET: + return ::td::get_ip_str(AF_INET, &ipv4_addr_.sin_addr).str(); + default: + UNREACHABLE(); + return string(); + } +} + int IPAddress::get_port() const { if (!is_valid()) { return 0; @@ -624,12 +640,7 @@ StringBuilder &operator<<(StringBuilder &builder, const IPAddress &address) { if (!address.is_valid()) { return builder << "[invalid]"; } - if (address.is_ipv4()) { - return builder << "[" << address.get_ip_str() << ":" << address.get_port() << "]"; - } else { - CHECK(address.is_ipv6()); - return builder << "[[" << address.get_ip_str() << "]:" << address.get_port() << "]"; - } + return builder << "[" << address.get_ip_host() << ":" << address.get_port() << "]"; } } // namespace td diff --git a/tdutils/td/utils/port/IPAddress.h b/tdutils/td/utils/port/IPAddress.h index 4af10c112..7673d1b16 100644 --- a/tdutils/td/utils/port/IPAddress.h +++ b/tdutils/td/utils/port/IPAddress.h @@ -44,6 +44,9 @@ class IPAddress { // returns result in a static thread-local buffer, which may be overwritten by any subsequent method call CSlice get_ip_str() const; + // returns IP address as a host, i.e. IPv4 or [IPv6] + string get_ip_host() const; + static string ipv4_to_str(uint32 ipv4); static string ipv6_to_str(Slice ipv6);