Skip to content

Fix python2 server compatibility#513

Merged
brianmay merged 4 commits intosshuttle:masterfrom
drjbarker:python2-compat
Aug 29, 2020
Merged

Fix python2 server compatibility#513
brianmay merged 4 commits intosshuttle:masterfrom
drjbarker:python2-compat

Conversation

@drjbarker
Copy link
Contributor

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.

@brianmay I don't know if you like where I put the definition of the 'which' function. Maybe that should be moved? I think the changes are relatively minor and fix python2 compatibility with the server.

sshuttle is now working for my with a python2 server, but I only have a single setup I can test. Could you or someone else test with a python3 server?

I marked the compatibility code with #python2 compat comments to make it clear in the future.

Fixes  sshuttle#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 sshuttle#503) also fixes python2 compatibility.
@brianmay
Copy link
Member

I think these two lines need fixing also:

sshuttle/sshuttle/client.py

Lines 302 to 303 in 45f8cce

assert(not re.search(rb'[^-\w\.]', hostname))
assert(not re.search(rb'[^0-9.]', ip))

Python 2.7 doesn't support the rb string prefix.

@drjbarker
Copy link
Contributor Author

Do they get called on the server? I thought that was client side.

My intention was just to patch the server side code for python2 compatibility. Then we might manage to maintain it for a while. Trying keep the whole client python2 compatible seems like too much work.

Either way, I think the 'b' can be safely removed right? Just testing those asserts in python2 and python3 everything seems to work fine as just r-strings. If you agree then I push a change to remove the 'b'.

@brianmay
Copy link
Member

OK, sounds good to me.

No,unfortunately, you can't remove the b prefix, it will break under python3 because the string is a byte value:

>>> import re
>>> re.search(rb'[^-\w\.]', b'abc')
>>> re.search(r'[^-\w\.]', b'abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/re.py", line 201, in search
    return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object

@drjbarker
Copy link
Contributor Author

drjbarker commented Aug 29, 2020

Ah yes, I didn't think about the case the string being searched was byte string. Let me see if I can work around it.

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.
@drjbarker
Copy link
Contributor Author

Haha, how about that for a fix! I made a minimal test outside of sshuttle and it seemed to be fine once I switched the order of rb to br! Not sure how to test within sshuttle because I don't think my usage ever hits those lines.

@brianmay
Copy link
Member

Wow. I never thought of switching it to 'br'. Anyway, thanks for you work here.

@brianmay brianmay merged commit 19f653d into sshuttle:master Aug 29, 2020
@leiserfg
Copy link

Any clue bout when will it be released?

@skuhl
Copy link
Contributor

skuhl commented Dec 30, 2020

@leiserfg sshuttle 1.0.5 was recently released with this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Removing python2 support makes sshuttle fail with old remotes

4 participants