Beispiel #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'
Beispiel #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)
Beispiel #3
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])
Beispiel #4
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)
Beispiel #5
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)
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'