Commit 81fdb206 authored by Greg Jurman's avatar Greg Jurman

Socket will find best-case interface to connect/listen to.

IPv6 almost always is priority on newer linux systems. This will get
the socket-type the system says it should be.
parent 97aa6ecd
...@@ -74,11 +74,23 @@ class PyrasiteIPC(object): ...@@ -74,11 +74,23 @@ class PyrasiteIPC(object):
def listen(self): def listen(self):
"""Listen on a random port""" """Listen on a random port"""
self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) for res in socket.getaddrinfo('localhost', None, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, 0):
self.server_sock.settimeout(5) af, socktype, proto, canonname, sa = res
self.server_sock.bind(('localhost', 0)) try:
self.server_sock.listen(1) self.server_sock = socket.socket(af, socktype, proto)
self.port = self.server_sock.getsockname()[1] except socket.error, msg:
self.server_sock = None
continue
try:
self.server_sock.bind(sa)
self.server_sock.listen(1)
except socket.error, msg:
self.server_sock.close()
self.server_sock = None
continue
break
self.hostname, self.port = self.server_sock.getsockname()[0:2]
self.running = True self.running = True
def create_payload(self): def create_payload(self):
......
...@@ -50,8 +50,21 @@ class ReverseConnection(threading.Thread, PyrasiteIPC): ...@@ -50,8 +50,21 @@ class ReverseConnection(threading.Thread, PyrasiteIPC):
running = True running = True
while running: while running:
try: try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
self.sock.connect((self.host, self.port)) af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)
except socket.error, msg:
self.sock = None
continue
try:
self.sock.connect(sa)
except socket.error, msg:
self.sock.close()
self.sock = None
continue
break
self.on_connect() self.on_connect()
while running: while running:
cmd = self.recv() cmd = self.recv()
......
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