torproject / stem

Python controller library for Tor
https://stem.torproject.org/
GNU Lesser General Public License v3.0
257 stars 75 forks source link

i am try to run this first tutorial https://stem.torproject.org/tutorials/over_the_river.html , but its not working ? #104

Closed jakbin closed 2 years ago

jakbin commented 3 years ago

https://stem.torproject.org/tutorials/over_the_river.html,

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/stem/connection.py", line 1063, in _connection_for_default_port return stem.socket.ControlPort(address, 9151) File "/usr/lib/python3/dist-packages/stem/socket.py", line 503, in init self.connect() File "/usr/lib/python3/dist-packages/stem/socket.py", line 172, in connect self._socket = self._make_socket() File "/usr/lib/python3/dist-packages/stem/socket.py", line 538, in _make_socket raise stem.SocketError(exc) stem.SocketError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/dj/Desktop/github_pro/flask-tor/f-tor/stem/create_hidden_service.py", line 19, in with Controller.from_port() as controller: File "/usr/lib/python3/dist-packages/stem/control.py", line 1031, in from_port control_port = stem.connection._connection_for_default_port(address) File "/usr/lib/python3/dist-packages/stem/connection.py", line 1065, in _connection_for_default_port raise exc File "/usr/lib/python3/dist-packages/stem/connection.py", line 1060, in _connection_for_default_port return stem.socket.ControlPort(address, 9051) File "/usr/lib/python3/dist-packages/stem/socket.py", line 503, in init self.connect() File "/usr/lib/python3/dist-packages/stem/socket.py", line 172, in connect self._socket = self._make_socket() File "/usr/lib/python3/dist-packages/stem/socket.py", line 538, in _make_socket raise stem.SocketError(exc) stem.SocketError: [Errno 111] Connection refused

0-harshit-0 commented 2 years ago

hello, probably too late but these are the necessary changes to be made, to make it run:

import os
import shutil

from stem.control import Controller
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return "<h1>Hi Grandma!</h1>"

print(' * Connecting to tor')

# if you are on windows use 9151 else 9150
with Controller.from_port(port=9151) as controller:
  controller.authenticate()

  # Create a hidden service where visitors of port 80 get redirected to local
  # port 5000 (this is where Flask runs by default).
  hidden_service_dir = "C:/tmp"
  print(" * Creating our hidden service in %s" % hidden_service_dir)
  result = controller.create_hidden_service(hidden_service_dir, 80, "localhost", target_port = 5000)

  # The hostname is only available when we can read the hidden service
  # directory. This requires us to be running with the same user as tor.

  with open("C:/tmp/hostname", "r") as h:
      print('service established at %s' %h.read())

  try:
    app.run()
  finally:
    # Shut down the hidden service and clean it off disk. Note that you *don't*
    # want to delete the hidden service directory if you'd like to have this
    # same *.onion address in the future.

    print(" * Shutting down our hidden service")
    controller.remove_hidden_service(hidden_service_dir)
    shutil.rmtree(hidden_service_dir)
jakbin commented 2 years ago

this is also giving same error. do i need to start tor service ?

0-harshit-0 commented 2 years ago

yes, if you have a tor installed on windows, just open it and the code should work perfectly

jakbin commented 2 years ago

i am using linux . i run tor on port 9050 and also change port in code, but it is not working.

Traceback (most recent call last):
  File "/home/dj/Desktop/test/stem-example.py", line 19, in <module>
    controller.authenticate()
  File "/usr/lib/python3/dist-packages/stem/control.py", line 1112, in authenticate
    stem.connection.authenticate(self, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/stem/connection.py", line 536, in authenticate
    raise IncorrectSocketType('unable to use the control socket')
stem.connection.IncorrectSocketType: unable to use the control socket
0-harshit-0 commented 2 years ago

I think it should be 9051* sock5 is on 9051, on windows its 9151 ....

jakbin commented 2 years ago

I think it should be 9051* sock5 is on 9051, on windows its 9151 ....

i got this error. i use port 9051.

[warn] Socks version 80 not recognized. (This port is not an HTTP proxy; did you want to use HTTPTunnelPort?)
0-harshit-0 commented 2 years ago

that is something IDK... hope your tor is updated and one more thing. go to /etc/tor > vim torrc and make sure the (controlPort 9051) line is not commented and the (sockport 9050) line as well except for these 2 everything should be commented (that is how I have in mine...) you can uncomment them as you learn more and need more control over tor like authentication, etc. but for now, they are not required.

jakbin commented 2 years ago

this is now working. thanks for reply.

0-harshit-0 commented 2 years ago

so how exactly did you make it work?.. would like to know haha..

jakbin commented 2 years ago

that is something IDK... hope your tor is updated and one more thing. go to /etc/tor > vim torrc and make sure the (controlPort 9051) line is not commented and the (sockport 9050) line as well except for these 2 everything should be commented (that is how I have in mine...) you can uncomment them as you learn more and need more control over tor like authentication, etc. but for now, they are not required.

by using this instructions, and chnage permission of hidden_service_dir to 700

jakbin commented 2 years ago

@0-harshit-0 any example, how to run tor service with tor without interfere with other tor processes on your compute .

0-harshit-0 commented 2 years ago

if I understand correct, you are trying to run 2 tor service on your computer and they are resulting in same address is that it?

jakbin commented 2 years ago

if I understand correct, you are trying to run 2 tor service on your computer and they are resulting in same address is that it?

No , I want to run tor with stem . Like : onionshare .

0-harshit-0 commented 2 years ago

k but you have created a flask application right, just add some good UI and File handling to it ? that should work right?