I'm writing a little program trying to connect to Tor (v0.2.9.10) using the stem library (v1.5.4).
I've taken inspiration from this page : https://stem.torproject.org/api/control.html
For now it looks like this :
#!/usr/bin/python
from __future__ import print_function
import os
import sys
import getpass
import stem.connection
import stem.socket
if __name__ == '__main__':
try:
controller = stem.control.Controller.from_port()
except stem.SocketError as exc:
print("Unable to connect to tor on port 9051: %s" % exc)
sys.exit(1)
try:
controller.authenticate()
except stem.connection.MissingPassword:
try:
pw = os.environ['torpassword']
except KeyError:
print("Please configure the 'torpassword' environment variable", file=sys.stderr)
sys.exit(1)
try:
controller.authenticate(password = pw)
except stem.connection.PasswordAuthFailed:
print("Unable to authenticate, password is incorrect")
sys.exit(1)
except stem.connection.AuthenticationFailure as exc:
print("Unable to authenticate: %s" % exc)
sys.exit(1)
print("Tor is running version %s" % controller.get_version())
controller.close()
When the right password is given, it works, it displays the Tor version as it's supposed to do. When there's no password I get the message I'm expecting : Please configure the 'torpassword' environment variable
The problem is when I'm trying with wrong passwords to see how it reacts, but I get different messages everytime and I'm bit confused about that. For example :
[dev@l]#$ torpassword=test ./test.py
Traceback (most recent call last):
File "./test.py", line 28, in <module>
controller.authenticate(password = pw)
File "/usr/lib/python2.7/site-packages/stem/control.py", line 1071, in authenticate
stem.connection.authenticate(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/stem/connection.py", line 618, in authenticate
raise auth_exc
stem.connection.AuthenticationFailure: Received empty socket content.
[dev@l]$ torpassword=test ./test.py
Traceback (most recent call last):
File "./test.py", line 28, in <module>
controller.authenticate(password = pw)
File "/usr/lib/python2.7/site-packages/stem/control.py", line 1071, in authenticate
stem.connection.authenticate(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/stem/connection.py", line 618, in authenticate
raise auth_exc
stem.connection.AuthenticationFailure: [Errno 104] Connection reset by peer
[dev@l]$ torpassword=test ./test.py
Traceback (most recent call last):
File "./test.py", line 28, in <module>
controller.authenticate(password = pw)
File "/usr/lib/python2.7/site-packages/stem/control.py", line 1071, in authenticate
stem.connection.authenticate(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/stem/connection.py", line 618, in authenticate
raise auth_exc
stem.connection.AuthenticationFailure: [Errno 104] Connection reset by peer
[dev@l]$ torpassword=test ./test.py
Traceback (most recent call last):
File "./test.py", line 28, in <module>
controller.authenticate(password = pw)
File "/usr/lib/python2.7/site-packages/stem/control.py", line 1071, in authenticate
stem.connection.authenticate(self, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/stem/connection.py", line 618, in authenticate
raise auth_exc
stem.connection.AuthenticationFailure: [Errno 104] Connection reset by peer
[dev@l]$ torpassword=test ./test.py
Unable to authenticate, password is incorrect
In my opinion the last message should be the one I get all the time. Is it a problem in the Tor server ? Not handling a wrong password the right way ? (not so likely in my opinion) Is it a problem with the stem library ? (could be, but I doubt it) Is it a problem with my code ? (much more likely, but I can't see why)
Thanks for your time. Regards,
# Edit following canonizing ironize answer :
I did try creating a new controller before the second attempt to authenticate, I did try to use the reconnect method Finally I tried to only let the second authenticate part alone. All these produced no effect.
# Edit 2:
#!/usr/bin/python
from __future__ import print_function
import os
import sys
import getpass
import stem.connection
import stem.socket
if __name__ == '__main__':
try:
controller = stem.control.Controller.from_port()
except stem.SocketError as exc:
print("Unable to connect to tor on port 9051: %s" % exc)
sys.exit(1)
try:
try:
controller.authenticate(password = "test")
except stem.connection.PasswordAuthFailed:
print("Unable to authenticate, password is incorrect")
sys.exit(1)
except stem.connection.AuthenticationFailure as exc:
print("Unable to authenticate: %s" % exc)
sys.exit(1)
print("Tor is running version %s" % controller.get_version())
controller.close()
Even a simpler piece of code like this one, with an obvious wrong password show different results when called.
[dev@l]$ ./test2.py
Unable to authenticate: [Errno 104] Connection reset by peer
Exception in thread Event Notifier (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 811, in __bootstrap_inner
File "/usr/lib64/python2.7/threading.py", line 764, in run
File "/usr/lib/python2.7/site-packages/stem/control.py", line 958, in _event_loop
File "/usr/lib64/python2.7/threading.py", line 621, in wait
File "/usr/lib64/python2.7/threading.py", line 332, in wait
<type 'exceptions.TypeError'>: 'NoneType' object is not callable
[dev@l]$ ./test2.py
Unable to authenticate: [Errno 104] Connection reset by peer
[dev@l]$ ./test2.py
Unable to authenticate: [Errno 104] Connection reset by peer
[dev@l]$ ./test2.py
Unable to authenticate: [Errno 104] Connection reset by peer
[dev@l]$ ./test2.py
Unable to authenticate: Received empty socket content.