fix resolve_host

This commit is contained in:
Jack Robison 2019-02-05 13:31:57 -05:00
parent 43d424978d
commit 7fe211d134
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 8 additions and 5 deletions

View file

@ -127,7 +127,7 @@ class Node:
if known_node_urls: if known_node_urls:
for host, port in known_node_urls: for host, port in known_node_urls:
address = await resolve_host(host) address = await resolve_host(host, port, proto='udp')
if (address, port) not in known_node_addresses: if (address, port) not in known_node_addresses:
known_node_addresses.append((address, port)) known_node_addresses.append((address, port))
url_to_addr[address] = host url_to_addr[address] = host

View file

@ -70,7 +70,7 @@ class StreamDownloader(StreamAssembler):
def add_fixed_peers(self): def add_fixed_peers(self):
async def _add_fixed_peers(): async def _add_fixed_peers():
self.peer_queue.put_nowait([ self.peer_queue.put_nowait([
KademliaPeer(self.loop, address=(await resolve_host(url)), tcp_port=port + 1) KademliaPeer(self.loop, address=(await resolve_host(url, port + 1, proto='tcp')), tcp_port=port + 1)
for url, port in self.config.reflector_servers for url, port in self.config.reflector_servers
]) ])
if self.config.reflector_servers: if self.config.reflector_servers:

View file

@ -142,7 +142,9 @@ def drain_tasks(tasks: typing.List[typing.Optional[asyncio.Task]]):
cancel_task(tasks.pop()) cancel_task(tasks.pop())
async def resolve_host(url: str) -> str: async def resolve_host(url: str, port: int, proto: str) -> str:
if proto not in ['udp', 'tcp']:
raise Exception("invalid protocol")
try: try:
if ipaddress.ip_address(url): if ipaddress.ip_address(url):
return url return url
@ -150,6 +152,7 @@ async def resolve_host(url: str) -> str:
pass pass
loop = asyncio.get_running_loop() loop = asyncio.get_running_loop()
return (await loop.getaddrinfo( return (await loop.getaddrinfo(
url, 'https', url, port,
proto=socket.IPPROTO_TCP, proto=socket.IPPROTO_TCP if proto == 'tcp' else socket.SOCK_DGRAM,
type=socket.SOCK_STREAM
))[0][4][0] ))[0][4][0]