[tor-commits] [stem/master] Offer to start tor when running prompt

atagar at torproject.org atagar at torproject.org
Sun May 11 01:02:50 UTC 2014


commit 8c3ada016b02bf19eed54e18c7cfed03ecacd52a
Author: Damian Johnson <atagar at torproject.org>
Date:   Sat May 10 17:46:29 2014 -0700

    Offer to start tor when running prompt
    
    If tor isn't running then offering to start an instance for our prompt to
    connect to. This only lasts for the duration of the interpretor session.
---
 stem/connection.py            |    6 ++----
 stem/interpretor/__init__.py  |   30 ++++++++++++++++++++++++++++++
 stem/interpretor/settings.cfg |   11 +++++++++++
 3 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index b9f6c0d..ad04aa7 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -266,10 +266,8 @@ def connect(control_port = ('127.0.0.1', 9051), control_socket = '/var/run/tor/c
 
   if not control_connection:
     if control_socket and control_port:
-      if not stem.util.system.is_running('tor') and not stem.util.system.is_running('tor.real'):
-        error_msg = CONNECT_MESSAGES['tor_isnt_running']
-      else:
-        error_msg = CONNECT_MESSAGES['no_control_port']
+      is_tor_running = stem.util.system.is_running('tor') or stem.util.system.is_running('tor.real')
+      error_msg = CONNECT_MESSAGES['no_control_port'] if is_tor_running else CONNECT_MESSAGES['tor_isnt_running']
 
     print error_msg
     return None
diff --git a/stem/interpretor/__init__.py b/stem/interpretor/__init__.py
index 9ebcea7..5850364 100644
--- a/stem/interpretor/__init__.py
+++ b/stem/interpretor/__init__.py
@@ -14,7 +14,9 @@ import sys
 
 import stem
 import stem.connection
+import stem.process
 import stem.util.conf
+import stem.util.system
 
 from stem.util.term import RESET, Attr, Color, format
 
@@ -57,6 +59,34 @@ def main():
     print stem.interpretor.arguments.get_help()
     sys.exit()
 
+  # If the user isn't connecting to something in particular then offer to start
+  # tor if it isn't running.
+
+  if not (args.user_provided_port or args.user_provided_socket):
+    is_tor_running = stem.util.system.is_running('tor') or stem.util.system.is_running('tor.real')
+
+    if not is_tor_running:
+      if not stem.util.system.is_available('tor'):
+        print msg('msg.tor_unavailable')
+        sys.exit(1)
+      else:
+        user_input = raw_input(msg('msg.start_tor_prompt'))
+        print  # extra newline
+
+        if user_input.lower() not in ('y', 'yes'):
+          sys.exit()
+
+        stem.process.launch_tor_with_config(
+          config = {
+            'SocksPort': '0',
+            'ControlPort': str(args.control_port),
+            'CookieAuthentication': '1',
+            'ExitPolicy': 'reject *:*',
+          },
+          completion_percent = 5,
+          take_ownership = True,
+        )
+
   control_port = None if args.user_provided_socket else (args.control_address, args.control_port)
   control_socket = None if args.user_provided_port else args.control_socket
 
diff --git a/stem/interpretor/settings.cfg b/stem/interpretor/settings.cfg
index 73fb067..444936a 100644
--- a/stem/interpretor/settings.cfg
+++ b/stem/interpretor/settings.cfg
@@ -56,6 +56,17 @@ msg.python_banner
 |  {version}
 |
 
+msg.tor_unavailable Tor isn't running and the command presently isn't in your PATH.
+
+msg.start_tor_prompt
+|Tor isn't running. Would you like to start up an instance for our interpretor
+|to connect to?
+|
+|Tor will have a minimal, non-relaying configuration and be shut down when
+|you're done.
+|
+|Start Tor (yes / no)? 
+
  #################
 # OUTPUT OF /HELP #
  #################





More information about the tor-commits mailing list