Blob Blame History Raw
From c12d2ba5c6d0c7287b1e316fec4f9c9bff63c328 Mon Sep 17 00:00:00 2001
From: Joseph Barker <j.barker@leeds.ac.uk>
Date: Fri, 28 Aug 2020 10:04:12 +0900
Subject: [PATCH 1/4] Fix python2 server compatibility

Fixes  #469. We replace python3 exclusive code with a check for python3 and a compatibility fix. Note that the switch from os.set_nonblocking to fcntl.fcntl in 98d052d (fixing #503) also fixes python2 compatibility.
---
 sshuttle/assembler.py |  7 +++++--
 sshuttle/server.py    | 19 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/sshuttle/assembler.py b/sshuttle/assembler.py
index 31900ee..de5730f 100644
--- a/sshuttle/assembler.py
+++ b/sshuttle/assembler.py
@@ -7,8 +7,11 @@
 while 1:
     name = sys.stdin.readline().strip()
     if name:
-        name = name.decode("ASCII")
-
+        # python2 compat: in python2 sys.stdin.readline().strip() -> str
+        #                 in python3 sys.stdin.readline().strip() -> bytes
+        # (see #481)
+        if sys.version_info >= (3, 0):
+            name = name.decode("ASCII")
         nbytes = int(sys.stdin.readline())
         if verbosity >= 2:
             sys.stderr.write('server: assembling %r (%d bytes)\n'
diff --git a/sshuttle/server.py b/sshuttle/server.py
index 7bcd193..fa07b29 100644
--- a/sshuttle/server.py
+++ b/sshuttle/server.py
@@ -6,7 +6,24 @@
 import sys
 import os
 import platform
-from shutil import which
+
+if sys.version_info >= (3, 0):
+    from shutil import which
+else:
+    # python2 compat: shutil.which is not available so we provide our own which command
+    def which(file, mode=os.F_OK | os.X_OK, path=None):
+        if path is not None:
+            search_paths = [path]
+        elif "PATH" in os.environ:
+            search_paths = os.environ["PATH"].split(os.pathsep)
+        else:
+            search_paths = os.defpath.split(os.pathsep)
+
+        for path in search_paths:
+            filepath = os.path.join(path, file)
+            if os.path.exists(filepath) and os.access(filepath, mode):
+                return filepath
+        return None
 
 import sshuttle.ssnet as ssnet
 import sshuttle.helpers as helpers

From 459e573019f5ac636f69e35de60233787c57048b Mon Sep 17 00:00:00 2001
From: Joseph Barker <j.barker@leeds.ac.uk>
Date: Fri, 28 Aug 2020 10:29:12 +0900
Subject: [PATCH 2/4] Fix flake8 line too long

---
 sshuttle/server.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sshuttle/server.py b/sshuttle/server.py
index fa07b29..61e7602 100644
--- a/sshuttle/server.py
+++ b/sshuttle/server.py
@@ -10,7 +10,8 @@
 if sys.version_info >= (3, 0):
     from shutil import which
 else:
-    # python2 compat: shutil.which is not available so we provide our own which command
+    # python2 compat: shutil.which is not available so we provide our
+    # own which command
     def which(file, mode=os.F_OK | os.X_OK, path=None):
         if path is not None:
             search_paths = [path]

From f23510a4fcd924465749911e76a7471799711e0f Mon Sep 17 00:00:00 2001
From: Joseph Barker <j.barker@leeds.ac.uk>
Date: Fri, 28 Aug 2020 10:37:20 +0900
Subject: [PATCH 3/4] Fix Codacy check redefined-argument-from-local

---
 sshuttle/server.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sshuttle/server.py b/sshuttle/server.py
index 61e7602..3073353 100644
--- a/sshuttle/server.py
+++ b/sshuttle/server.py
@@ -20,8 +20,8 @@ def which(file, mode=os.F_OK | os.X_OK, path=None):
         else:
             search_paths = os.defpath.split(os.pathsep)
 
-        for path in search_paths:
-            filepath = os.path.join(path, file)
+        for p in search_paths:
+            filepath = os.path.join(p, file)
             if os.path.exists(filepath) and os.access(filepath, mode):
                 return filepath
         return None

From ec5fb683506f85ec17198d96b1ece851fa3d9325 Mon Sep 17 00:00:00 2001
From: Joseph Barker <j.barker@leeds.ac.uk>
Date: Sat, 29 Aug 2020 21:32:18 +0900
Subject: [PATCH 4/4] Fix python2 client compatibility
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Python2 ignores the byte string qualification (b’foo’)  but falls over for the combination rb for this regexp. Switching the qualification to br appears to fix this and works in both python2 and python3.
---
 sshuttle/client.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sshuttle/client.py b/sshuttle/client.py
index fae2931..5f247f4 100644
--- a/sshuttle/client.py
+++ b/sshuttle/client.py
@@ -299,8 +299,8 @@ def start(self):
             raise Fatal('%r expected STARTED, got %r' % (self.argv, line))
 
     def sethostip(self, hostname, ip):
-        assert(not re.search(rb'[^-\w\.]', hostname))
-        assert(not re.search(rb'[^0-9.]', ip))
+        assert(not re.search(br'[^-\w\.]', hostname))
+        assert(not re.search(br'[^0-9.]', ip))
         self.pfile.write(b'HOST %s,%s\n' % (hostname, ip))
         self.pfile.flush()