Skip to content

Commit 6c21add

Browse files
committed
Fix Python 3.8 file operations
Under Python 3.8 we can not wrap a File in a Sock. Note this currently requires Python >= 3.5
1 parent 4b32018 commit 6c21add

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

sshuttle/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def _main(tcp_listener, udp_listener, fw, ssh_cmd, remotename,
461461
raise Fatal("failed to establish ssh session (1)")
462462
else:
463463
raise
464-
mux = Mux(serversock, serversock)
464+
mux = Mux(serversock.makefile("rb"), serversock.makefile("wb"))
465465
handlers.append(mux)
466466

467467
expected = b'SSHUTTLE0001'

sshuttle/server.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ def main(latency_control, auto_hosts, to_nameserver, auto_nets):
294294
sys.stdout.flush()
295295

296296
handlers = []
297-
mux = Mux(socket.fromfd(sys.stdin.fileno(),
298-
socket.AF_INET, socket.SOCK_STREAM),
299-
socket.fromfd(sys.stdout.fileno(),
300-
socket.AF_INET, socket.SOCK_STREAM))
297+
mux = Mux(sys.stdin, sys.stdout)
301298
handlers.append(mux)
302299

303300
debug1('auto-nets:' + str(auto_nets) + '\n')

sshuttle/ssnet.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,10 @@ def callback(self, sock):
339339

340340
class Mux(Handler):
341341

342-
def __init__(self, rsock, wsock):
343-
Handler.__init__(self, [rsock, wsock])
344-
self.rsock = rsock
345-
self.wsock = wsock
342+
def __init__(self, rfile, wfile):
343+
Handler.__init__(self, [rfile, wfile])
344+
self.rfile = rfile
345+
self.wfile = wfile
346346
self.new_channel = self.got_dns_req = self.got_routes = None
347347
self.got_udp_open = self.got_udp_data = self.got_udp_close = None
348348
self.got_host_req = self.got_host_list = None
@@ -439,19 +439,19 @@ def got_packet(self, channel, cmd, data):
439439
callback(cmd, data)
440440

441441
def flush(self):
442-
self.wsock.setblocking(False)
442+
os.set_blocking(self.wfile.fileno(), False)
443443
if self.outbuf and self.outbuf[0]:
444-
wrote = _nb_clean(os.write, self.wsock.fileno(), self.outbuf[0])
444+
wrote = _nb_clean(os.write, self.wfile.fileno(), self.outbuf[0])
445445
debug2('mux wrote: %r/%d\n' % (wrote, len(self.outbuf[0])))
446446
if wrote:
447447
self.outbuf[0] = self.outbuf[0][wrote:]
448448
while self.outbuf and not self.outbuf[0]:
449449
self.outbuf[0:1] = []
450450

451451
def fill(self):
452-
self.rsock.setblocking(False)
452+
os.set_blocking(self.rfile.fileno(), False)
453453
try:
454-
read = _nb_clean(os.read, self.rsock.fileno(), LATENCY_BUFFER_SIZE)
454+
read = _nb_clean(os.read, self.rfile.fileno(), LATENCY_BUFFER_SIZE)
455455
except OSError:
456456
_, e = sys.exc_info()[:2]
457457
raise Fatal('other end: %r' % e)
@@ -481,22 +481,22 @@ def handle(self):
481481
break
482482

483483
def pre_select(self, r, w, x):
484-
_add(r, self.rsock)
484+
_add(r, self.rfile)
485485
if self.outbuf:
486-
_add(w, self.wsock)
486+
_add(w, self.wfile)
487487

488488
def callback(self, sock):
489-
(r, w, _) = select.select([self.rsock], [self.wsock], [], 0)
490-
if self.rsock in r:
489+
(r, w, _) = select.select([self.rfile], [self.wfile], [], 0)
490+
if self.rfile in r:
491491
self.handle()
492-
if self.outbuf and self.wsock in w:
492+
if self.outbuf and self.wfile in w:
493493
self.flush()
494494

495495

496496
class MuxWrapper(SockWrapper):
497497

498498
def __init__(self, mux, channel):
499-
SockWrapper.__init__(self, mux.rsock, mux.wsock)
499+
SockWrapper.__init__(self, mux.rfile, mux.wfile)
500500
self.mux = mux
501501
self.channel = channel
502502
self.mux.channels[channel] = self.got_packet

0 commit comments

Comments
 (0)