Пример #1
0
def test_select():
    sock_r, sock_w = socket.socketpair()

    events = {TorStream: {'data': Event(), 'close': Event()},
              socket.socket: {'data': Event(), 'close': Event()}}

    hostname = 'ifconfig.me'
    tor = TorClient()
    with tor.get_guard() as guard:

        def recv_callback(sock_or_stream, mask):
            kind = type(sock_or_stream)
            data = sock_or_stream.recv(1024)
            logger.info('%s: %r', kind.__name__, data.decode())
            if data:
                events[kind]['data'].set()
            else:
                logger.debug('closing')
                guard.unregister(sock_or_stream)
                events[kind]['close'].set()

        with guard.create_circuit(3) as circuit:
            with circuit.create_stream((hostname, 80)) 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' % hostname.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'
Пример #2
0
def tor_opener(hops_count=3,
               debuglevel=0,
               auth_data=None) -> ContextManager[OpenerDirector]:
    tor = TorClient(auth_data=auth_data)
    with tor.get_guard() as guard:
        yield build_tor_opener(guard,
                               hops_count=hops_count,
                               debuglevel=debuglevel)
Пример #3
0
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'
Пример #4
0
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'
Пример #5
0
    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__)
Пример #6
0
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])
Пример #7
0
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)
Пример #8
0
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)
Пример #9
0
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)
Пример #10
0
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)
Пример #11
0
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)
Пример #12
0
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
Пример #13
0
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'