88 lines
3.3 KiB
Python
88 lines
3.3 KiB
Python
#!/usr/bin/env python
|
|
|
|
import time, random, sys, os, string
|
|
#import sockschain as socks
|
|
sys.path.append('./includes')
|
|
import rocksock
|
|
|
|
""" return formatted timestamp """
|
|
def timestamp():
|
|
return time.strftime('%H:%M:%S', time.gmtime())
|
|
|
|
""" return some random string """
|
|
def random_string(strlen=20):
|
|
return ''.join([random.choice(string.letters) for x in xrange(strlen)])
|
|
|
|
def _log(strng, level='info'):
|
|
print '%s/%s\t%s' % (timestamp(), level, strng)
|
|
|
|
def option_matches_options(strng, items):
|
|
try: return [item for item in items if re.match(strng, item)]
|
|
except: return False
|
|
|
|
def prepare_socksocket(self, destination, path, path_item):
|
|
if path_item in self.paths and self.paths[path_item]['path'] == path:
|
|
self.paths[path_item]['path'] = False
|
|
|
|
#socks.setdefaultproxy()
|
|
# relay to i2p http proxy if *.i2p domain
|
|
if destination.endswith('i2p'):
|
|
proxy = random.choice(self.i2p_host).split(':')
|
|
path = False
|
|
# or go with tor
|
|
else:
|
|
proxies = [ rocksock.RocksockProxyFromURL('socks5://%s' % random.choice(self.tor_host)) ]
|
|
#socks.adddefaultproxy(*socks.parseproxy('tor://%s' % random.choice(self.tor_host)))
|
|
# add 'clearnet' proxies to the chain ?
|
|
if self.proxify and (not destination.endswith('onion') and not destination.endswith('.exit')):
|
|
|
|
# get a proxy path
|
|
path = build_path(self, path_item, path)
|
|
|
|
# if path isn't long enough, break
|
|
if not len(path): return False, False, False
|
|
|
|
# add chain...
|
|
#for inc in xrange(len(path) - 1): socks.adddefaultproxy(*socks.parseproxy('http://%s' % path[inc]))
|
|
#for inc in xrange(len(path) - 1): socks.adddefaultproxy(*socks.parseproxy('%s://%s' % (path[inc][1], path[inc][0])))
|
|
for inc in xrange(len(path)): proxies.append( rocksock.RocksockProxyFromURL('%s://%s' % (path[inc][1], path[inc][0])))
|
|
|
|
#return True, socks.socksocket, path
|
|
return True, proxies, path
|
|
|
|
def build_path(self, path_item, path):
|
|
|
|
chainlen = random.randint( self.path_len, (self.path_len + self.path_randomlen))
|
|
# if not enough proxies
|
|
# FIXME: try to get a proxylist from database
|
|
if len(self.proxylist) < chainlen: return []
|
|
|
|
# valid path already available
|
|
elif (path_item in self.paths and
|
|
self.paths[path_item]['path'] and
|
|
(time.time() - self.paths[path_item]['ticks']) < self.path_duration):
|
|
|
|
# take available path if any
|
|
if path != self.paths[path_item]['path']: path = self.paths[path_item]['path']
|
|
|
|
# or nope, none available
|
|
# build a new one from scratch
|
|
else:
|
|
path = []
|
|
avail = []
|
|
|
|
# dec chainlen if we have to select the exit proxy
|
|
if self.exitcountry is not None: chainlen -= 1
|
|
#avail = [item[0] for item in self.proxylist if not item[0] in avail and item[1] != str(self.exitcountry).upper()]
|
|
avail = [[item[0],item[2]] for item in self.proxylist if not item[0] in avail and item[1] != str(self.exitcountry).upper()]
|
|
path = random.sample(avail, chainlen)
|
|
|
|
# choose the exit proxy
|
|
if self.exitcountry is not None:
|
|
#avail = [item[0] for item in self.proxylist if not item[0] in path and item[1] == str(self.exitcountry).upper()]
|
|
avail = [[item[0],item[2]] for item in self.proxylist if not item[0] in path and item[1] == str(self.exitcountry).upper()]
|
|
if not len(avail): return []
|
|
path.append(random.choice(avail))
|
|
|
|
return path
|