Skip to content

Commit

Permalink
Fix Python 3.8 file operations
Browse files Browse the repository at this point in the history
Under Python 3.8 we can not wrap a File in a Sock.

Note this currently requires Python >= 3.5
  • Loading branch information
brianmay committed May 28, 2020
1 parent 4b32018 commit 6c21add
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion sshuttle/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
raise Fatal("failed to establish ssh session (1)")
else:
raise
mux = Mux(serversock, serversock)
mux = Mux(serversock.makefile("rb"), serversock.makefile("wb"))
handlers.append(mux)

expected = b'SSHUTTLE0001'
Expand Down
5 changes: 1 addition & 4 deletions sshuttle/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,7 @@ def main(latency_control, auto_hosts, to_nameserver, auto_nets):
sys.stdout.flush()

handlers = []
mux = Mux(socket.fromfd(sys.stdin.fileno(),
socket.AF_INET, socket.SOCK_STREAM),
socket.fromfd(sys.stdout.fileno(),
socket.AF_INET, socket.SOCK_STREAM))
mux = Mux(sys.stdin, sys.stdout)
handlers.append(mux)

debug1('auto-nets:' + str(auto_nets) + '\n')
Expand Down
28 changes: 14 additions & 14 deletions sshuttle/ssnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ def callback(self, sock):

class Mux(Handler):

def __init__(self, rsock, wsock):
Handler.__init__(self, [rsock, wsock])
self.rsock = rsock
self.wsock = wsock
def __init__(self, rfile, wfile):
Handler.__init__(self, [rfile, wfile])
self.rfile = rfile
self.wfile = wfile
self.new_channel = self.got_dns_req = self.got_routes = None
self.got_udp_open = self.got_udp_data = self.got_udp_close = None
self.got_host_req = self.got_host_list = None
Expand Down Expand Up @@ -439,19 +439,19 @@ def got_packet(self, channel, cmd, data):
callback(cmd, data)

def flush(self):
self.wsock.setblocking(False)
os.set_blocking(self.wfile.fileno(), False)
if self.outbuf and self.outbuf[0]:
wrote = _nb_clean(os.write, self.wsock.fileno(), self.outbuf[0])
wrote = _nb_clean(os.write, self.wfile.fileno(), self.outbuf[0])
debug2('mux wrote: %r/%d\n' % (wrote, len(self.outbuf[0])))
if wrote:
self.outbuf[0] = self.outbuf[0][wrote:]
while self.outbuf and not self.outbuf[0]:
self.outbuf[0:1] = []

def fill(self):
self.rsock.setblocking(False)
os.set_blocking(self.rfile.fileno(), False)
try:
read = _nb_clean(os.read, self.rsock.fileno(), LATENCY_BUFFER_SIZE)
read = _nb_clean(os.read, self.rfile.fileno(), LATENCY_BUFFER_SIZE)
except OSError:
_, e = sys.exc_info()[:2]
raise Fatal('other end: %r' % e)
Expand Down Expand Up @@ -481,22 +481,22 @@ def handle(self):
break

def pre_select(self, r, w, x):
_add(r, self.rsock)
_add(r, self.rfile)
if self.outbuf:
_add(w, self.wsock)
_add(w, self.wfile)

def callback(self, sock):
(r, w, _) = select.select([self.rsock], [self.wsock], [], 0)
if self.rsock in r:
(r, w, _) = select.select([self.rfile], [self.wfile], [], 0)
if self.rfile in r:
self.handle()
if self.outbuf and self.wsock in w:
if self.outbuf and self.wfile in w:
self.flush()


class MuxWrapper(SockWrapper):

def __init__(self, mux, channel):
SockWrapper.__init__(self, mux.rsock, mux.wsock)
SockWrapper.__init__(self, mux.rfile, mux.wfile)
self.mux = mux
self.channel = channel
self.mux.channels[channel] = self.got_packet
Expand Down

0 comments on commit 6c21add

Please sign in to comment.