Skip to content

Commit

Permalink
Fix server side Python3 issues.
Browse files Browse the repository at this point in the history
Closes: apenwarr#49.
  • Loading branch information
brianmay committed Dec 15, 2015
1 parent ce51871 commit 02fa496
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
8 changes: 5 additions & 3 deletions sshuttle/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")
Expand Down
21 changes: 11 additions & 10 deletions sshuttle/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion sshuttle/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 02fa496

Please sign in to comment.