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
This commit is contained in:
Brian May 2020-05-15 07:55:38 +10:00
parent 4b320180c4
commit 6c21addde9
3 changed files with 16 additions and 19 deletions

View File

@ -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'

View File

@ -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')

View File

@ -339,10 +339,10 @@ class Proxy(Handler):
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
@ -439,9 +439,9 @@ class Mux(Handler):
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:]
@ -449,9 +449,9 @@ class Mux(Handler):
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)
@ -481,22 +481,22 @@ class Mux(Handler):
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