-
Notifications
You must be signed in to change notification settings - Fork 0
/
torscanner.py
executable file
·234 lines (195 loc) · 8.14 KB
/
torscanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/python
'''
Exit statuses:
-1: Module dependency error.
0: Program finished on unexpected point, but not on exception.
1: Program finished on unhandled exception. Please REPORT to author.
42: Program finished succesfully.
'''
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib-ext'))
try:
# Specialities
import TorCtl # https://tor-svn.freehaven.net/svn/torctl/trunk/python/TorCtl/
import GeoIPSupport # -||-
import socks # http://socksipy.sourceforge.net/
import TorUtil
# TorScanner modules
import common
import rpcserver
import urldispatcher
import threaddispatcher
import pathdispatcher
import sockctl
# Standard Python modules
import ConfigParser
import socket
import time
import threading
import time
import optparse
import SimpleXMLRPCServer
import imp
import hashlib
import random
# import pickle
except ImportError, e:
print "%s, please install it and run again." % e
sys.exit(-1)
# Will use first directory with "write" permission as data directory
DATADIR_PATHS = ['/var/lib/torscanner', '~/.torscanner', '.']
DATADIR_USER = '~/.torscanner'
# Config files with priorities. First file with higher priority.
CONFIG_PATHS = ['./torscanner.conf', # Current directory
'~/.torscanner/torscanner.conf', # User directory
'/etc/torscanner.conf'] # System directory
# Use this settings when config file not found or any directive missing.
CONFIG_DEFAULTS = {
'TorCommand': '',
'TorHost': 'localhost',
'TorControlPort': 9051,
'TorControlPwd': '',
'EnableServer': 0,
'ServerHost': 'localhost',
'ServerPort': 8086,
'TorDebug': 'WARN',
'MaxCircuitDirtiness': 500,
'RelayUptime': 86400,
'RelayFlags': 'Running, Valid, Fast',
'ExitFlags': 'Exit, Running, Valid',
'PathLength': 3,
'ConcurrentThreads': 10,
'MinUrls': 10,
'MaxUrls': 100
}
def parseOptions():
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-c", "--config", dest='config',
metavar="FILE", help="Use specific config file"),
parser.add_option("-d", "--data", dest='data',
metavar="FILE", help="Use specific data directory"),
parser.add_option("-s", "--source", dest='source',
metavar="FILE", help="Load URLs from source file"),
parser.add_option("-r", "--dirtiness", dest='maxcircuitdirtiness',
help="How often rotate circuits (MaxCircuitDirtiness)")
parser.add_option("-t", "--threads", dest='concurrentthreads',
help="How many concurrent threads use to scanning")
parser.add_option("-u", "--urls", dest='minurls',
help="Minimum number of urls to vertical analysis of node")
parser.add_option("-m", "--max-urls", dest='maxurls',
help="Maximum number of urls held in memory at the same time")
return parser.parse_args()
if __name__ == '__main__':
# Parsing command line arguments
(options, args) = parseOptions()
common.log('Starting TorScanner at %s UTC...' % \
time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()), 'CMD')
#Joining cmdline param 'config' and list of default places
cfgs = []
if options.config: cfgs.extend([options.config])
cfgs.extend(CONFIG_PATHS)
# Loading configuration file
config = common.parseConfig(cfgs, CONFIG_DEFAULTS)
#Joining cmdline param 'data' and list of default datadir places
datadirs = []
if options.data: datadirs.extend([options.data])
datadirs.extend(DATADIR_PATHS)
# Selecting data directory. Will create in user directory,
# if there is no other location accessible for writing.
try:
datadir = common.selectDatadir(datadirs)
except Exception, e:
# When no datadir exists or exists without write access
common.log(e, 'ERROR')
common.log("Creating new data directory in '%s'" % DATADIR_USER )
try:
os.mkdir(os.path.expanduser(DATADIR_USER))
datadir = DATADIR_USER
except OSError, e:
common.log(e, 'ERROR')
sys.exit(-1)
# Load all plugins
# Load string names of plugins from config and retrieve its object representations
try:
plugins = common.loadPlugins(common.string2list(config.get('global', 'plugins')))
except ConfigParser.NoOptionError:
plugins = {}
opt = common.mergeOptions(config, options)
# DO EVERYTHING HERE
TorUtil.loglevel = opt['tordebug']
# Create URL Queue. Every URL added to queue will be processed by scanner
urldisp = urldispatcher.urlDispatcher(opt)
threaddisp = threaddispatcher.threadDispatcher(opt)
torctl = sockctl.Controller(opt['torhost'], \
int(opt['torcontrolport']), \
opt['torcontrolpwd'])
pathdisp = pathdispatcher.pathDispatcher(opt, torctl)
#FIXME: Temporary contruction to override config settings
options.source='urllist.txt'
sys.exit()
# Run XML-RPC controlling server
server = rpcserver.RPCServer(name='rpcserver')
server.opt = opt
server.urldisp = urldisp
server.threaddisp = threaddisp
server.enabled = bool(int(opt['enableserver']))
if server.enabled:
server.start()
# If given source file, read them and put urls to queue
if options.source:
common.log('Loading URLs from source file...', 'INFO')
sourcefp = open(options.source, 'r')
i = 0
url = None
xxxx = None
while(1):
# Source file is read line by line in each mainloop iteration.
# Don't hurry, processing of one URL takes much more than
# one loop iteration ;-).
if options.source and sourcefp:
if not url: # not None, when last submit was unsuccesfull
url = sourcefp.readline().replace("\n", '')
if url != '':
if urldisp.submit(url):
# Url was accepted to queue
url = None
else:
sourcefp.close()
sourcefp = None
#for xx in urldisp.urls:
# print "%s: %d" % (urldisp.urls[xx].url, urldisp.urls[xx].status)
#print xxxx
# Prototype of removing URLs from queue
# Just for testing purposes.
if xxxx != None: xxxx.setDone()
xxxx = urldisp.getOne()
# Check, if all threads are responding.
# If not, kill them and run new one.
threaddisp.checkThreads()
try:
time.sleep(1)
except KeyboardInterrupt:
common.log('Pressed Ctrl+C, exiting....')
break
if False:
common.log('%03d, TODO: %d, WORK: %d, DONE: %d' % \
(i, urldisp.counter.todo, urldisp.counter.work, \
urldisp.counter.done), 'INFO')
i+=1
# If RPC server was running and currently not,
# shutdown application immediately.
if server.enabled and not server.isAlive():
break;
# If application was not started as daemon (RPC server)
# and there is no jobs in urlQueue (from --source directive),
# shutdown
if not server.enabled and urldisp.allDone():
break;
if server.enabled and server.isAlive():
server.shutdown()
common.log('XML-RPC server shutdown OK.', 'INFO')
common.log('TorScanner succesfully finished at %s UTC.' %\
time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()), 'CMD')
sys.exit(42)