Commit 3242a44e authored by Luke Macken's avatar Luke Macken

More robust socket handling

parent 7ccde020
...@@ -83,20 +83,23 @@ class PyrasiteIPC(object): ...@@ -83,20 +83,23 @@ class PyrasiteIPC(object):
af, socktype, proto, canonname, sa = res af, socktype, proto, canonname, sa = res
try: try:
self.server_sock = socket.socket(af, socktype, proto) self.server_sock = socket.socket(af, socktype, proto)
try:
self.server_sock.bind(sa)
self.server_sock.listen(1)
except socket.error:
self.server_sock.close()
self.server_sock = None
continue
except socket.error: except socket.error:
self.server_sock = None self.server_sock = None
continue continue
try:
self.server_sock.bind(sa)
self.server_sock.listen(1)
except socket.error:
self.server_sock.close()
self.server_sock = None
continue
break break
self.hostname, self.port = self.server_sock.getsockname()[0:2] if not self.server_sock:
self.running = True raise Exception('pyrasite was unable to setup a ' +
'local server socket')
else:
self.hostname, self.port = self.server_sock.getsockname()[0:2]
def create_payload(self): def create_payload(self):
"""Write out a reverse python connection payload with a custom port""" """Write out a reverse python connection payload with a custom port"""
......
...@@ -61,19 +61,20 @@ class ReverseConnection(threading.Thread, pyrasite.PyrasiteIPC): ...@@ -61,19 +61,20 @@ class ReverseConnection(threading.Thread, pyrasite.PyrasiteIPC):
af, socktype, proto, canonname, sa = res af, socktype, proto, canonname, sa = res
try: try:
self.sock = socket.socket(af, socktype, proto) self.sock = socket.socket(af, socktype, proto)
try:
self.sock.connect(sa)
except socket.error:
self.sock.close()
self.sock = None
continue
except socket.error: except socket.error:
self.sock = None self.sock = None
continue continue
try:
self.sock.connect(sa)
except socket.error:
self.sock.close()
self.sock = None
continue
break break
if not self.sock: if not self.sock:
raise Exception('pyrasite cannot establish reverse connection to %s:%d' % (self.host, self.port)) raise Exception('pyrasite cannot establish reverse ' +
'connection to %s:%d' % (self.host, self.port))
self.on_connect() self.on_connect()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment