From 02fa49627ff406cb990d0c7653a86bf3f5aa3109 Mon Sep 17 00:00:00 2001 From: Brian May Date: Tue, 15 Dec 2015 12:51:29 +1100 Subject: [PATCH] Fix server side Python3 issues. Closes: #49. --- sshuttle/assembler.py | 8 +++++--- sshuttle/server.py | 21 +++++++++++---------- sshuttle/ssh.py | 3 ++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/sshuttle/assembler.py b/sshuttle/assembler.py index 37cde42..330528d 100644 --- a/sshuttle/assembler.py +++ b/sshuttle/assembler.py @@ -4,13 +4,15 @@ z = zlib.decompressobj() while 1: - name = sys.stdin.readline().strip() + name = stdin.readline().strip() if name: - nbytes = int(sys.stdin.readline()) + name = name.decode("ASCII") + + nbytes = int(stdin.readline()) if verbosity >= 2: sys.stderr.write('server: assembling %r (%d bytes)\n' % (name, nbytes)) - content = z.decompress(sys.stdin.read(nbytes)) + content = z.decompress(stdin.read(nbytes)) module = imp.new_module(name) parent, _, parent_name = name.rpartition(".") diff --git a/sshuttle/server.py b/sshuttle/server.py index 0aee55a..d0c072c 100644 --- a/sshuttle/server.py +++ b/sshuttle/server.py @@ -17,22 +17,23 @@ def _ipmatch(ipstr): - if ipstr == 'default': - ipstr = '0.0.0.0/0' - m = re.match(r'^(\d+(\.\d+(\.\d+(\.\d+)?)?)?)(?:/(\d+))?$', ipstr) + if ipstr == b'default': + ipstr = b'0.0.0.0/0' + m = re.match(b'^(\d+(\.\d+(\.\d+(\.\d+)?)?)?)(?:/(\d+))?$', ipstr) if m: g = m.groups() ips = g[0] width = int(g[4] or 32) if g[1] is None: - ips += '.0.0.0' + ips += b'.0.0.0' width = min(width, 8) elif g[2] is None: - ips += '.0.0' + ips += b'.0.0' width = min(width, 16) elif g[3] is None: - ips += '.0' + ips += b'.0' width = min(width, 24) + ips = ips.decode("ASCII") return (struct.unpack('!I', socket.inet_aton(ips))[0], width) @@ -61,7 +62,7 @@ def _list_routes(): p = ssubprocess.Popen(argv, stdout=ssubprocess.PIPE) routes = [] for line in p.stdout: - cols = re.split(r'\s+', line) + cols = re.split(b'\s+', line) ipw = _ipmatch(cols[0]) if not ipw: continue # some lines won't be parseable; never mind @@ -239,9 +240,9 @@ def main(latency_control): socket.fromfd(sys.stdout.fileno(), socket.AF_INET, socket.SOCK_STREAM)) handlers.append(mux) - routepkt = '' + routepkt = b'' for r in routes: - routepkt += '%d,%s,%d\n' % r + routepkt += b'%d,%s,%d\n' % (r[0], r[1].encode("ASCII"), r[2]) mux.send(0, ssnet.CMD_ROUTES, routepkt) hw = Hostwatch() @@ -270,7 +271,7 @@ def got_host_req(data): mux.got_host_req = got_host_req def new_channel(channel, data): - (family, dstip, dstport) = data.split(',', 2) + (family, dstip, dstport) = data.split(b',', 2) family = int(family) dstport = int(dstport) outwrap = ssnet.connect_dst(family, dstip, dstport) diff --git a/sshuttle/ssh.py b/sshuttle/ssh.py index 29c2c90..7691c80 100644 --- a/sshuttle/ssh.py +++ b/sshuttle/ssh.py @@ -91,7 +91,8 @@ def connect(ssh_cmd, rhostport, python, stderr, options): pyscript = r""" import sys; verbosity=%d; - exec(compile(sys.stdin.read(%d), "assembler.py", "exec")) + stdin=getattr(sys.stdin,"buffer",sys.stdin); + exec(compile(stdin.read(%d), "assembler.py", "exec")) """ % (helpers.verbose or 0, len(content)) pyscript = re.sub(r'\s+', ' ', pyscript.strip())