def test_requests_session2(): tor = TorClient() with tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3, retries=RETRIES) with requests.Session() as s: s.headers.update({'User-Agent': 'Mozilla/5.0'}) s.mount('http://', adapter) s.mount('https://', adapter) #r = s.get('https://google.com', timeout=30) r = s.get('http://u7spnj3dmwumzoa4.onion', timeout=30) print(r) print(r.text[:1000])
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) search_ip = '.'.join( circuit.last_node.router.ip.split('.')[:-1]) + '.' assert search_ip 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)
def test_adapter(): tor = TorClient() with tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3) with requests.Session() as s: s.headers.update({'User-Agent': 'Mozilla/5.0'}) s.mount('http://', adapter) s.mount('https://', adapter) r = s.get('https://google.com', timeout=30) logger.warning(r) logger.warning(r.text) assert "</body></html>" in r.text r = s.get('https://cryptowat.ch/assets/btc') logger.warning(r) logger.warning(r.text)
def test_requests_session(): tor = TorClient() with tor.get_guard() as guard: adapter = TorHttpAdapter(guard, 3, retries=RETRIES) with requests.Session() as s: s.headers.update({'User-Agent': 'Mozilla/5.0'}) s.mount('http://', adapter) s.mount('https://', adapter) r = s.get('https://google.com', timeout=30) logger.warning(r) logger.warning(r.text) assert r.text.rstrip().endswith('</html>') r = s.get('https://stackoverflow.com/questions/tagged/python') assert r.text.rstrip().endswith('</html>') logger.warning(r) logger.warning(r.text)
Support v2 hidden services (v2 specification) Support Basic and Stealth authorization protocol Provide simple TorHttpAdapter for requests library Provide simple urllib tor_opener for making requests without any dependencies Provide simple Socks5 proxy """ # Console examples check on Github page PyPi page !!! # Usage examples: # A basic example of how to send some data to a clearnet host or a hidden service from torpy import TorClient hostname = 'ifconfig.me' # It's possible use onion hostname here as well with TorClient() as tor: # 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: # Now we can communicate with host stream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % hostname.encode()) recv = stream.recv(1024) # TorHttpAdapter is a convenient Tor adapter for the requests library. # Requests Adapters: https://2.python-requests.org/en/master/user/advanced/#transport-adapters # The following example shows the usage of TorHttpAdapter for multi-threaded HTTP requests: from multiprocessing.pool import ThreadPool from torpy.http.requests import tor_requests_session