def hostip(self) -> str: """In case a server still has active connections, we use the same IP again If new connection then based on value of load_balancing() and self.info: 0 - return the first entry, so all threads use the same IP 1 - and self.info has more than 1 entry (read: IP address): Return a random entry from the possible IPs 2 - and self.info has more than 1 entry (read: IP address): Return the quickest IP based on the happyeyeballs algorithm In case of problems: return the host name itself """ # Check if already a successful ongoing connection if self.busy_threads and self.busy_threads[0].nntp: # Re-use that IP logging.debug("%s: Re-using address %s", self.host, self.busy_threads[0].nntp.host) return self.busy_threads[0].nntp.host # Determine IP ip = self.host if self.info: if cfg.load_balancing() == 0 or len(self.info) == 1: # Just return the first one, so all next threads use the same IP ip = self.info[0][4][0] logging.debug("%s: Connecting to address %s", self.host, ip) elif cfg.load_balancing() == 1: # Return a random entry from the possible IPs rnd = random.randint(0, len(self.info) - 1) ip = self.info[rnd][4][0] logging.debug("%s: Connecting to address %s", self.host, ip) elif cfg.load_balancing() == 2: # RFC6555 / Happy Eyeballs: ip = happyeyeballs(self.host, port=self.port, ssl=self.ssl) if ip: logging.debug("%s: Connecting to address %s", self.host, ip) else: # nothing returned, so there was a connection problem logging.debug("%s: No successful IP connection was possible", self.host) return ip
def hostip(self): """ based on value of load_balancing() and self.info: 0: return the host name itself (so: do nothing) 1 and self.info has more than 1 entry (read: IP address): Return a random entry from the possible IPs 2 and self.info has more than 1 entry (read: IP address): Return the quickest IP based on the happyeyeballs algorithm In case of problems: return the host name itself """ if cfg.load_balancing() == 1 and self.info and len(self.info) > 1: # Return a random entry from the possible IPs rnd = random.randint(0, len(self.info) - 1) ip = self.info[rnd][4][0] logging.debug('For server %s, using IP %s', self.host, ip) elif cfg.load_balancing() == 2 and self.info and len(self.info) > 1: # RFC6555 / Happy Eyeballs: ip = happyeyeballs(self.host, port=self.port, ssl=self.ssl) if ip: logging.debug('For server %s, using IP %s as server', self.host, ip) else: # nothing returned, so there was a connection problem ip = self.host logging.debug( 'For server %s, no successful IP connection possible', self.host) else: ip = self.host return ip
def hostip(self): """ In case a server still has active connections, we use the same IP again If new connection then based on value of load_balancing() and self.info: 0 - return the first entry, so all threads use the same IP 1 - and self.info has more than 1 entry (read: IP address): Return a random entry from the possible IPs 2 - and self.info has more than 1 entry (read: IP address): Return the quickest IP based on the happyeyeballs algorithm In case of problems: return the host name itself """ # Check if already a successful ongoing connection if self.busy_threads and self.busy_threads[0].nntp: # Re-use that IP logging.debug('%s: Re-using address %s', self.host, self.busy_threads[0].nntp.host) return self.busy_threads[0].nntp.host # Determine new IP if cfg.load_balancing() == 0 and self.info: # Just return the first one, so all next threads use the same IP ip = self.info[0][4][0] logging.debug('%s: Connecting to address %s', self.host, ip) elif cfg.load_balancing() == 1 and self.info and len(self.info) > 1: # Return a random entry from the possible IPs rnd = random.randint(0, len(self.info) - 1) ip = self.info[rnd][4][0] logging.debug('%s: Connecting to address %s', self.host, ip) elif cfg.load_balancing() == 2 and self.info and len(self.info) > 1: # RFC6555 / Happy Eyeballs: ip = happyeyeballs(self.host, port=self.port, ssl=self.ssl) if ip: logging.debug('%s: Connecting to address %s', self.host, ip) else: # nothing returned, so there was a connection problem ip = self.host logging.debug('%s: No successful IP connection was possible', self.host) else: ip = self.host return ip
def hostip(self): """ based on value of load_balancing() and self.info: 0: return the host name itself (so: do nothing) 1 and self.info has more than 1 entry (read: IP address): Return a random entry from the possible IPs 2 and self.info has more than 1 entry (read: IP address): Return the quickest IP based on the happyeyeballs algorithm In case of problems: return the host name itself """ if cfg.load_balancing() == 1 and self.info and len(self.info) > 1: # Return a random entry from the possible IPs rnd = random.randint(0, len(self.info) - 1) ip = self.info[rnd][4][0] logging.debug('For server %s, using IP %s', self.host, ip) elif cfg.load_balancing() == 2 and self.info and len(self.info) > 1: # RFC6555 / Happy Eyeballs: ip = happyeyeballs(self.host, port=self.port, ssl=self.ssl) if ip: logging.debug('For server %s, using IP %s as server', self.host, ip) else: # nothing returned, so there was a connection problem ip = self.host logging.debug('For server %s, no successful IP connection possible', self.host) else: ip = self.host return ip
def test_newszilla_nttp(self): ip = happyeyeballs("newszilla.xs4all.nl", port=119) assert "." in ip or ":" in ip
def test_google_unreachable_port(self): ip = happyeyeballs("www.google.com", port=33333) assert ip is None
def test_ipv6_only(self): ip = happyeyeballs("ipv6.google.com") assert ip is None or ":" in ip
def test_not_resolvable(self): ip = happyeyeballs("not.resolvable.invalid") assert ip is None
def test_google_https(self): ip = happyeyeballs("www.google.com", port=443, ssl=True) assert "." in ip or ":" in ip
def test_google_http(self): ip = happyeyeballs("www.google.com") assert "." in ip or ":" in ip