Skip to content

Commit 3787ecd

Browse files
committed
Use asyncio to avoid blocking sock.getaddrinfo
when DNS resolution fails.
1 parent 3eb690b commit 3787ecd

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

pwnlib/tubes/remote.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22
from __future__ import division
33

4+
import asyncio
45
import socket
56
import socks
67

@@ -109,8 +110,17 @@ def _connect(self, fam, typ):
109110
sock = None
110111
timeout = self.timeout
111112

113+
async def resolve_hostname(host, port, fam=0, typ=0, proto=0, flags=0):
114+
loop = asyncio.get_event_loop()
115+
result = await loop.getaddrinfo(host, port, family=fam, type=typ, proto=proto, flags=flags)
116+
return result
117+
112118
with self.waitfor('Opening connection to %s on port %s' % (self.rhost, self.rport)) as h:
113-
for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE):
119+
# Using asyncio to avoid blocking when DNS resolution fail. It's probably better
120+
# to use async all the ways to `sock.connect`. However, let's keep the changes
121+
# small before we have the needs.
122+
results = asyncio.run(resolve_hostname(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE))
123+
for res in results:
114124
self.family, self.type, self.proto, _canonname, sockaddr = res
115125

116126
if self.type not in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:

0 commit comments

Comments
 (0)