2018-05-22 17:36:06 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2018-10-22 04:31:32 +02:00
|
|
|
import errno
|
2018-11-04 17:09:31 +01:00
|
|
|
import logging
|
2018-05-17 16:01:52 +02:00
|
|
|
import os
|
2018-07-21 11:13:58 +02:00
|
|
|
import random
|
2018-10-22 04:31:32 +02:00
|
|
|
import socket
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2018-05-17 16:01:52 +02:00
|
|
|
|
2018-10-06 23:10:37 +02:00
|
|
|
LOG_TIMEOUT = 60.0
|
|
|
|
LOG_WAIT = 0.1
|
|
|
|
LOG_CHECK_LIMIT = LOG_TIMEOUT / LOG_WAIT
|
|
|
|
|
2018-10-06 23:09:20 +02:00
|
|
|
def fail(msg):
|
2018-11-04 17:09:31 +01:00
|
|
|
logging.error('FAIL')
|
2018-10-06 23:09:20 +02:00
|
|
|
sys.exit(msg)
|
|
|
|
|
2018-05-17 16:01:52 +02:00
|
|
|
def try_connecting_to_socksport():
|
|
|
|
socks_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2018-07-21 11:13:58 +02:00
|
|
|
if socks_socket.connect_ex(('127.0.0.1', socks_port)):
|
2018-05-17 16:01:52 +02:00
|
|
|
tor_process.terminate()
|
2018-10-06 23:09:20 +02:00
|
|
|
fail('Cannot connect to SOCKSPort')
|
2018-05-17 16:01:52 +02:00
|
|
|
socks_socket.close()
|
|
|
|
|
|
|
|
def wait_for_log(s):
|
2018-10-06 23:10:37 +02:00
|
|
|
log_checked = 0
|
|
|
|
while log_checked < LOG_CHECK_LIMIT:
|
2018-05-17 16:01:52 +02:00
|
|
|
l = tor_process.stdout.readline()
|
2018-10-06 23:10:37 +02:00
|
|
|
l = l.decode('utf8')
|
|
|
|
if s in l:
|
2018-11-13 10:38:51 +01:00
|
|
|
logging.info('Tor logged: "{}"'.format(l.strip()))
|
2018-05-17 16:01:52 +02:00
|
|
|
return
|
2018-11-04 17:09:31 +01:00
|
|
|
logging.info('Tor logged: "{}", waiting for "{}"'.format(l.strip(), s))
|
2018-10-06 23:10:37 +02:00
|
|
|
# readline() returns a blank string when there is no output
|
|
|
|
# avoid busy-waiting
|
|
|
|
if len(s) == 0:
|
|
|
|
time.sleep(LOG_WAIT)
|
|
|
|
log_checked += 1
|
|
|
|
fail('Could not find "{}" in logs after {} seconds'.format(s, LOG_TIMEOUT))
|
2018-05-17 16:01:52 +02:00
|
|
|
|
2018-07-21 11:13:58 +02:00
|
|
|
def pick_random_port():
|
|
|
|
port = 0
|
|
|
|
random.seed()
|
|
|
|
|
2018-08-05 15:27:49 +02:00
|
|
|
for i in range(8):
|
2018-07-21 11:13:58 +02:00
|
|
|
port = random.randint(10000, 60000)
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
if s.connect_ex(('127.0.0.1', port)) == 0:
|
|
|
|
s.close()
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2018-10-06 23:09:20 +02:00
|
|
|
if port == 0:
|
|
|
|
fail('Could not find a random free port between 10000 and 60000')
|
|
|
|
|
2018-07-21 11:13:58 +02:00
|
|
|
return port
|
|
|
|
|
2018-11-04 17:09:31 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
|
|
format='%(asctime)s.%(msecs)03d %(message)s',
|
|
|
|
datefmt='%Y-%m-%d %H:%M:%S')
|
|
|
|
|
2018-09-14 19:13:23 +02:00
|
|
|
if sys.hexversion < 0x02070000:
|
2018-10-06 23:09:20 +02:00
|
|
|
fail("ERROR: unsupported Python version (should be >= 2.7)")
|
2018-09-14 19:13:23 +02:00
|
|
|
|
|
|
|
if sys.hexversion > 0x03000000 and sys.hexversion < 0x03010000:
|
2018-10-06 23:09:20 +02:00
|
|
|
fail("ERROR: unsupported Python3 version (should be >= 3.1)")
|
2018-09-14 19:13:23 +02:00
|
|
|
|
2018-07-21 11:13:58 +02:00
|
|
|
control_port = pick_random_port()
|
|
|
|
socks_port = pick_random_port()
|
|
|
|
|
|
|
|
assert control_port != 0
|
|
|
|
assert socks_port != 0
|
|
|
|
|
2018-11-22 01:53:57 +01:00
|
|
|
if len(sys.argv) < 3:
|
|
|
|
fail('Usage: %s <path-to-tor> <data-dir>' % sys.argv[0])
|
|
|
|
|
2018-05-17 16:01:52 +02:00
|
|
|
if not os.path.exists(sys.argv[1]):
|
2018-10-06 23:09:20 +02:00
|
|
|
fail('ERROR: cannot find tor at %s' % sys.argv[1])
|
2018-11-22 01:53:57 +01:00
|
|
|
if not os.path.exists(sys.argv[2]):
|
|
|
|
fail('ERROR: cannot find datadir at %s' % sys.argv[2])
|
2018-05-17 16:01:52 +02:00
|
|
|
|
|
|
|
tor_path = sys.argv[1]
|
2018-11-22 01:53:57 +01:00
|
|
|
data_dir = sys.argv[2]
|
2018-05-17 16:01:52 +02:00
|
|
|
|
|
|
|
tor_process = subprocess.Popen([tor_path,
|
2018-11-22 01:53:57 +01:00
|
|
|
'-DataDirectory', data_dir,
|
2018-07-21 11:13:58 +02:00
|
|
|
'-ControlPort', '127.0.0.1:{}'.format(control_port),
|
|
|
|
'-SOCKSPort', '127.0.0.1:{}'.format(socks_port),
|
2018-11-04 18:01:00 +01:00
|
|
|
'-Log', 'debug stdout',
|
2018-11-13 10:15:40 +01:00
|
|
|
'-LogTimeGranularity', '1',
|
2018-05-17 16:01:52 +02:00
|
|
|
'-FetchServerDescriptors', '0'],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
if tor_process == None:
|
2018-10-06 23:09:20 +02:00
|
|
|
fail('ERROR: running tor failed')
|
2018-05-17 16:01:52 +02:00
|
|
|
|
|
|
|
wait_for_log('Opened Control listener on')
|
|
|
|
|
|
|
|
try_connecting_to_socksport()
|
|
|
|
|
|
|
|
control_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2018-07-21 11:13:58 +02:00
|
|
|
if control_socket.connect_ex(('127.0.0.1', control_port)):
|
2018-05-17 16:01:52 +02:00
|
|
|
tor_process.terminate()
|
2018-10-06 23:09:20 +02:00
|
|
|
fail('Cannot connect to ControlPort')
|
2018-05-17 16:01:52 +02:00
|
|
|
|
2018-08-05 15:27:49 +02:00
|
|
|
control_socket.sendall('AUTHENTICATE \r\n'.encode('utf8'))
|
|
|
|
control_socket.sendall('SETCONF SOCKSPort=0.0.0.0:{}\r\n'.format(socks_port).encode('utf8'))
|
2018-05-17 16:01:52 +02:00
|
|
|
wait_for_log('Opened Socks listener')
|
|
|
|
|
|
|
|
try_connecting_to_socksport()
|
|
|
|
|
2018-08-05 15:27:49 +02:00
|
|
|
control_socket.sendall('SETCONF SOCKSPort=127.0.0.1:{}\r\n'.format(socks_port).encode('utf8'))
|
2018-05-17 16:01:52 +02:00
|
|
|
wait_for_log('Opened Socks listener')
|
|
|
|
|
|
|
|
try_connecting_to_socksport()
|
|
|
|
|
2018-08-05 15:27:49 +02:00
|
|
|
control_socket.sendall('SIGNAL HALT\r\n'.encode('utf8'))
|
2018-05-17 16:01:52 +02:00
|
|
|
|
2018-10-06 23:05:04 +02:00
|
|
|
wait_for_log('exiting cleanly')
|
2018-11-04 17:09:31 +01:00
|
|
|
logging.info('OK')
|
2018-10-06 23:05:04 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
tor_process.terminate()
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno == errno.ESRCH: # errno 3: No such process
|
|
|
|
# assume tor has already exited due to SIGNAL HALT
|
2018-11-04 17:09:31 +01:00
|
|
|
logging.warn("Tor has already exited")
|
2018-10-06 23:05:04 +02:00
|
|
|
else:
|
|
|
|
raise
|