Use static IPs only for IPv4 Socks5 proxies.

GitOrigin-RevId: 797d7acdd16323bb61e15b67c13ada216abe31f1
This commit is contained in:
levlam 2018-06-29 20:36:27 +03:00
parent c02ed05cfb
commit cfd1761a00
3 changed files with 24 additions and 14 deletions

View File

@ -753,8 +753,9 @@ Result<mtproto::TransportType> ConnectionCreator::get_transport_type(const Proxy
Result<SocketFd> ConnectionCreator::find_connection(const ProxyInfo &proxy, DcId dc_id, bool allow_media_only, Result<SocketFd> ConnectionCreator::find_connection(const ProxyInfo &proxy, DcId dc_id, bool allow_media_only,
FindConnectionExtra &extra) { FindConnectionExtra &extra) {
extra.debug_str = PSTRING() << "Failed to find valid IP for " << dc_id; extra.debug_str = PSTRING() << "Failed to find valid IP for " << dc_id;
TRY_RESULT(info, TRY_RESULT(info, dc_options_set_.find_connection(
dc_options_set_.find_connection(dc_id, allow_media_only, proxy.use_proxy() && !proxy.use_mtproto_proxy())); dc_id, allow_media_only,
proxy.use_proxy() && proxy.use_socks5_proxy() && proxy.ip_address().is_ipv4()));
extra.stat = info.stat; extra.stat = info.stat;
TRY_RESULT(transport_type, get_transport_type(proxy, info)); TRY_RESULT(transport_type, get_transport_type(proxy, info));
extra.transport_type = std::move(transport_type); extra.transport_type = std::move(transport_type);

View File

@ -178,6 +178,7 @@ bool IPAddress::is_valid() const {
} }
const sockaddr *IPAddress::get_sockaddr() const { const sockaddr *IPAddress::get_sockaddr() const {
CHECK(is_valid());
return &sockaddr_; return &sockaddr_;
} }
@ -199,7 +200,11 @@ int IPAddress::get_address_family() const {
} }
bool IPAddress::is_ipv4() const { bool IPAddress::is_ipv4() const {
return get_address_family() == AF_INET; return is_valid() && get_address_family() == AF_INET;
}
bool IPAddress::is_ipv6() const {
return is_valid() && get_address_family() == AF_INET6;
} }
uint32 IPAddress::get_ipv4() const { uint32 IPAddress::get_ipv4() const {
@ -461,7 +466,7 @@ void IPAddress::set_port(int port) {
bool operator==(const IPAddress &a, const IPAddress &b) { bool operator==(const IPAddress &a, const IPAddress &b) {
if (!a.is_valid() || !b.is_valid()) { if (!a.is_valid() || !b.is_valid()) {
return false; return !a.is_valid() && !b.is_valid();
} }
if (a.get_address_family() != b.get_address_family()) { if (a.get_address_family() != b.get_address_family()) {
return false; return false;
@ -480,8 +485,8 @@ bool operator==(const IPAddress &a, const IPAddress &b) {
} }
bool operator<(const IPAddress &a, const IPAddress &b) { bool operator<(const IPAddress &a, const IPAddress &b) {
if (a.is_valid() != b.is_valid()) { if (!a.is_valid() || !b.is_valid()) {
return a.is_valid() < b.is_valid(); return !a.is_valid() && b.is_valid();
} }
if (a.get_address_family() != b.get_address_family()) { if (a.get_address_family() != b.get_address_family()) {
return a.get_address_family() < b.get_address_family(); return a.get_address_family() < b.get_address_family();

View File

@ -29,17 +29,18 @@ class IPAddress {
IPAddress(); IPAddress();
bool is_valid() const; bool is_valid() const;
const sockaddr *get_sockaddr() const;
size_t get_sockaddr_len() const;
int get_address_family() const;
Slice get_ip_str() const;
bool is_ipv4() const; bool is_ipv4() const;
uint32 get_ipv4() const; bool is_ipv6() const;
Slice get_ipv6() const;
int get_port() const; int get_port() const;
void set_port(int port); void set_port(int port);
uint32 get_ipv4() const;
Slice get_ipv6() const;
Slice get_ip_str() const;
static CSlice ipv4_to_str(int32 ipv4);
IPAddress get_any_addr() const; IPAddress get_any_addr() const;
Status init_ipv6_port(CSlice ipv6, int port) TD_WARN_UNUSED_RESULT; Status init_ipv6_port(CSlice ipv6, int port) TD_WARN_UNUSED_RESULT;
@ -54,7 +55,10 @@ class IPAddress {
friend bool operator==(const IPAddress &a, const IPAddress &b); friend bool operator==(const IPAddress &a, const IPAddress &b);
friend bool operator<(const IPAddress &a, const IPAddress &b); friend bool operator<(const IPAddress &a, const IPAddress &b);
static CSlice ipv4_to_str(int32 ipv4); // for internal usage only
const sockaddr *get_sockaddr() const;
size_t get_sockaddr_len() const;
int get_address_family() const;
private: private:
union { union {