Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tor version parser broken? #756

Closed
adrelanos opened this issue Jan 10, 2017 · 19 comments · Fixed by #784
Closed

Tor version parser broken? #756

adrelanos opened this issue Jan 10, 2017 · 19 comments · Fixed by #784

Comments

@adrelanos
Copy link
Contributor

zeronet --tor always does not work for me.

[22:17:05] TorManager Tor controller connect error: AttributeError: 'NoneType' object has no attribute 'group' in TorManager.py line 160


250-VERSION Tor="0.2.9.8 (git-a0df013ea241b026)"

https://github.com/HelloZeroNet/ZeroNet/blob/master/src/Tor/TorManager.py#L160

@HelloZeroNet
Copy link
Owner

well, i don't think if thats the problem:

>>> re.search('Tor="([0-9\.]+)', '250-VERSION Tor="0.2.9.8 (git-a0df013ea241b026)"').group(1)
'0.2.9.8'

Can you please add a self.log.debug("Tor protocol: %s" % res_protocol) before that line?

@adrelanos
Copy link
Contributor Author

Took the liberty to make that self.log.info so I don't have to enable --debug if that is okay.

[23:00:51] TorManager Tor protocol: 250-PROTOCOLINFO 1
250-AUTH METHODS=NULL

[23:00:51] TorManager Tor controller connect error: AttributeError: 'NoneType' object has no attribute 'group' in TorManager.py line 161

An issue with line handling?


Could also be an issue with our control-port-filter-python.

It receives:

PROTOCOLINFO

It replies:

250-PROTOCOLINFO 1
250-AUTH METHODS=NULL
250-VERSION Tor="0.2.9.8 (git-a0df013ea241b026)"
250 OK

@MuxZeroNet
Copy link
Contributor

Could you please find your TorManager.py file and add this line under line 158?

print "------", repr(res_protocol)

@HelloZeroNet
Copy link
Owner

HelloZeroNet commented Jan 11, 2017

@MuxZeroNet he already did that, I think the problem is at https://github.com/HelloZeroNet/ZeroNet/blob/master/src/Tor/TorManager.py#L243, have to put that line to a while not data: loop

I will try to reproduce the problem

@adrelanos
Copy link
Contributor Author

Another option, if yo want the Tor version number, send GETINFO version, not ask for protocolinfo.

Yet another option would be porting to python-stem, which I created a separate ticket for just now:
#758

@HelloZeroNet
Copy link
Owner

I have added a modification that queries version using GETINFO, please try it: 77e07dd

@adrelanos
Copy link
Contributor Author

No, doesn't work for me.

Added a log line below.

                res_version = self.send("GETINFO version", conn)
                self.log.info("res_version: %s" % res_version)
[19:27:27] TorManager res_version: 250 OK
[19:27:27] TorManager Tor controller connect error: AttributeError: 'NoneType' object has no attribute 'group' in TorManager.py line 176

@adrelanos
Copy link
Contributor Author

Tor is getting:

GETINFO version

Reply:

250-version=0.2.9.8 (git-a0df013ea241b026)
250 OK

@HelloZeroNet
Copy link
Owner

Hm I don't have other ideas yet. I tried to reproduce it under windows, but no luck. Going to again with a VPS.

@HelloZeroNet
Copy link
Owner

HelloZeroNet commented Jan 11, 2017

I have just installed on Debian 8 and works for me:

# ./ZeroNet.sh --debug
...
[21:29:16] TorManager > PROTOCOLINFO
[21:29:16] TorManager < 250-PROTOCOLINFO 1
250-AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE="/var/run/tor/control.authcookie"
250-VERSION Tor="0.2.9.8"
250 OK
...
[21:29:16] TorManager > GETINFO version
[21:29:16] TorManager < 250-version=0.2.9.8 (git-a0df013ea241b026)
250 OK

(same with experimental 0.3.x)

do you have any other ideas to reproduce it?

please try this one:

# python --version
Python 2.7.9
# python -c "import gevent; gevent.version_info"
version_info(major=1, minor=2, micro=0, releaselevel='final', serial=1)
# python -c "import gevent; import gevent.monkey; gevent.monkey.patch_all(); import socket; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.connect(('127.0.0.1', 9051)); s.send('PROTOCOLINFO\r\n'); print s.recv(1024)"
250-PROTOCOLINFO 1
250-AUTH METHODS=COOKIE,SAFECOOKIE COOKIEFILE="/var/run/tor/control.authcookie"
250-VERSION Tor="0.2.9.8"
250 OK

@MuxZeroNet
Copy link
Contributor

I have no idea either. (Better use repr strings)

>>> import socket
>>> conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> conn.connect(('127.0.0.1', 9151))
>>> conn.sendall('authenticate c5e2d6b540e0b8ea29702e0112740e64f056751d661d03ba0d00d399262e6f77\r\n')
>>> conn.recv(1024*32)
'250 OK\r\n'

>>> conn.sendall('getinfo version\r\n')
>>> conn.recv(1024*32)
'250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK\r\n'

>>> raw_reply = '250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK\r\n'
>>> raw_reply.decode('utf-8')
u'250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK\r\n'
>>> raw_reply.decode('utf-8', 'ignore')
u'250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK\r\n'
>>> raw_reply.decode('utf-8', 'ignore').strip()
u'250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK'
>>> decoded_reply = _
>>> decoded_reply
u'250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK'
>>> import re
>>> re.search('version=([0-9\.]+)', decoded_reply)
<_sre.SRE_Match object at 0x7fc300f3d030>
>>> re.search('version=([0-9\.]+)', decoded_reply).group(1)
u'0.2.8.11'

@adrelanos
Copy link
Contributor Author

Reproduction is a bit hard at the moment.

The communication is as follows.

(1)

PROTOCOLINFO

250-PROTOCOLINFO 1
250-AUTH METHODS=NULL
250-VERSION Tor="0.2.9.8 (git-a0df013ea241b026)"
250 OK

(2)

AUTHENTICATE

250 OK

(3)

GETINFO version

250-version=0.2.9.8 (git-a0df013ea241b026)
250 OK

This is what ZeroNet gets:

[22:07:54] TorManager res_protocol: 250-PROTOCOLINFO 1
250-AUTH METHODS=NULL
250-VERSION Tor="0.2.9.8 (git-a0df013ea241b026)"
250 OK

[22:07:54] TorManager res_auth: 250 OK
[22:07:54] TorManager res_version: 

[22:07:54] TorManager Tor controller connect error: AttributeError: 'NoneType' object has no attribute 'group' in TorManager.py line 184

I am not sure yet we gotta blame this on control-port-filter-python.

@MuxZeroNet
Copy link
Contributor

MuxZeroNet commented Jan 11, 2017

To figure out what Python actually got, could you please try this?

hex_cookie = 'your hex encoded cookie here'
import socket
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect(('127.0.0.1', 9151))
conn.sendall('authenticate %s\r\n' % hex_cookie)
print repr(conn.recv(1024*32))

conn.sendall('getinfo version\r\n')
raw_reply = conn.recv(1024*32)

print repr(raw_reply)

decoded_reply = raw_reply.decode('utf-8', 'ignore').strip()
print repr(decoded_reply)

import re
print repr(re.search('version=([0-9\.]+)', decoded_reply).group(1))

On my computer:

'250 OK\r\n'
'250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK\r\n'
u'250-version=0.2.8.11 (git-31e7b47fbebe8caf)\r\n250 OK'
u'0.2.8.11'

@adrelanos
Copy link
Contributor Author

Thank you for looking into this so quickly and diligently!

I've put that into a text file, added #!/usr/bin/env python, made executable and run it.

'250 OK'
'\r\n'
u''
Traceback (most recent call last):
  File "./pytest.py", line 18, in <module>
    print repr(re.search('version=([0-9\.]+)', decoded_reply).group(1))
AttributeError: 'NoneType' object has no attribute 'group'

Communication went as follow:

authenticate your hex encoded cookie here

250 OK

getinfo version

250-version=0.2.9.8 (git-a0df013ea241b026)
250 OK

@MuxZeroNet
Copy link
Contributor

We should probably blame the control port firewall. 🔓

anonym added a commit to anonym/ZeroNet that referenced this issue Jan 20, 2017
Tor command responses terminate with "250 OK\r\n" so we have to read
until that sequence is encountered.

The previous implementation is racy: after sending a command it would
accept whatever that is found on the socket as its response, no matter
if it is correctly terminated or not.

This commit fixes: HelloZeroNet#756
@anonym
Copy link
Contributor

anonym commented Jan 20, 2017

Actually, to me (anonym, author of the new tor-controlport-filter) it looks like ZeroNet violates Tor's control language specification. See the PR. I can confirm it fixes this issue for me.

@anonym
Copy link
Contributor

anonym commented Jan 20, 2017

Also note that TorManager.send() cannot be relied upon as soon as any asynchronous feature of Tor control port is used, e.g. subscribing to some event. You don't use that, but it's worth mentioning. Long term, dropping your own implementation and using stem seems like a fine goal.

@anonym
Copy link
Contributor

anonym commented Jan 22, 2017

@adrelanos, did this fix your problem?

@adrelanos
Copy link
Contributor Author

adrelanos commented Jan 24, 2017 via email

dmcAdmin pushed a commit to dmcAdmin/splitDemo2 that referenced this issue Apr 15, 2023
Tor command responses terminate with "250 OK\r\n" so we have to read
until that sequence is encountered.

The previous implementation is racy: after sending a command it would
accept whatever that is found on the socket as its response, no matter
if it is correctly terminated or not.

This commit fixes: HelloZeroNet/ZeroNet#756
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 a pull request may close this issue.

4 participants