def test_clearnet_raw(): hostname = 'ifconfig.me' tor = TorClient() # Choose random guard node and create 3-hops circuit with tor.create_circuit(3) as circuit: # Create tor stream to host with circuit.create_stream((hostname, 80)) as stream: # Send some data to it stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostname.encode()) recv = recv_all(stream).decode() logger.warning('recv: %s', recv) assert circuit.last_node.router.ip in recv, 'wrong data received'
def test_onion_raw(): hostname = 'nzxj65x32vh2fkhk.onion' tor = TorClient() # Choose random guard node and create 3-hops circuit with tor.create_circuit(3) as circuit: # Create tor stream to host with circuit.create_stream((hostname, 80)) as stream: # Send some data to it stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostname.encode()) recv = recv_all(stream).decode() logger.warning('recv: %s', recv) assert 'StickyNotes' in recv, 'wrong data received'
def test_torpy(): hostname = COMRAD_ONION from torpy import TorClient tor = TorClient() # Choose random guard node and create 3-hops circuit with tor.create_circuit(3) as circuit: # Create tor stream to host with circuit.create_stream((hostname, 80)) as stream: # Send some data to it stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostname.encode()) recv = recv_all(stream).decode() #logger.warning('recv: %s', recv) print('RECEIVED:', recv)
def test_stealth_auth(): """Connecting to Hidden Service with 'Stealth' authorization.""" if not HS_STEALTH_HOST or not HS_STEALTH_AUTH: logger.warning('Skip test_stealth_auth()') return hs = HiddenService(HS_STEALTH_HOST, HS_STEALTH_AUTH, AuthType.Stealth) tor = TorClient() # Choose random guard node and create 3-hops circuit with tor.create_circuit(3) as circuit: # Create tor stream to host with circuit.create_stream((hs, 80)) as stream: # Send some data to it stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hs.hostname.encode()) recv = recv_all(stream).decode() logger.warning('recv: %s', recv)
def test_basic_auth_pre(): """Using pre-defined authorization data for making HTTP requests.""" if not HS_BASIC_HOST or not HS_BASIC_AUTH: logger.warning('Skip test_basic_auth()') return hidden_service = HS_BASIC_HOST auth_data = {HS_BASIC_HOST: (HS_BASIC_AUTH, AuthType.Basic)} tor = TorClient(auth_data=auth_data) # Choose random guard node and create 3-hops circuit with tor.create_circuit(3) as circuit: # Create tor stream to host with circuit.create_stream((hidden_service, 80)) as stream: # Send some data to it stream.send(b'GET / HTTP/1.0\r\nHost: %s.onion\r\n\r\n' % hidden_service.encode()) recv = recv_all(stream).decode() logger.warning('recv: %s', recv)
class TorConnection: def __init__(self, host, port, hops, type, data, agent=None): self.host = host self.port = port self.hops = hops self.type = type self.data = data self.agent = agent self.tor = TorClient() logging.getLogger('requests').setLevel(logging.CRITICAL) logging.basicConfig( format='[%(asctime)s] [%(threadName)-16s] %(message)s', level=logging.DEBUG) self.logger = logging.getLogger(__name__) def test_request(self): if type is "GET": request_data = b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % self.host.encode( ) else: request_data = b'POST / HTTP/1.0\r\nHost: %s\r\n\r\n%s\r\n' % ( self.host.encode(), self.data.encode()) with self.tor.create_circuit(self.hops) as circuit: with circuit.create_stream((self.host, self.port)) as stream: stream.send(request_data) recv = stream.recv(1024) print(recv) def test_session(self): with self.tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3) with requests.Session() as s: s.headers.update({'User-Agent': self.agent}) s.mount('http://', adapter) s.mount('https://', adapter) r = s.get(self.host, timeout=30) self.logger.warning(r) self.logger.warning(r.text) assert r.text.rstrip().endswith('</html>') r = s.get(self.host) assert r.text.rstrip().endswith('</html>') self.logger.warning(r) self.logger.warning(r.text) def test_select(self): sock_r, sock_w = socket.socketpair() events = { TorStream: { 'data': Event(), 'close': Event() }, socket.socket: { 'data': Event(), 'close': Event() } } with self.tor.get_guard() as guard: def recv_callback(sock_or_stream, mask): kind = type(sock_or_stream) data = sock_or_stream.recv(1024) self.logger.info('%s: %r', kind.__name__, data.decode()) if data: events[kind]['data'].set() else: self.logger.debug('closing') guard.unregister(sock_or_stream) events[kind]['close'].set() with guard.create_circuit(3) as circuit: with circuit.create_stream((self.host, self.port)) as stream: guard.register(sock_r, EVENT_READ, recv_callback) guard.register(stream, EVENT_READ, recv_callback) stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % self.host.encode()) sock_w.send(b'some data') assert events[socket.socket]['data'].wait( 10), 'no sock data received' assert events[TorStream]['data'].wait( 30), 'no stream data received' sock_w.close() assert events[socket.socket]['close'].wait( 10), 'no sock close received' assert events[TorStream]['close'].wait( 10), 'no stream close received'