Esempio n. 1
0
    def enable(cls):
        cls._is_enabled = True

        socket.socket = fakesock.socket
        socket._socketobject = fakesock.socket
        if not BAD_SOCKET_SHADOW:
            socket.SocketType = fakesock.socket

        socket.create_connection = create_fake_connection
        socket.gethostname = fake_gethostname
        socket.gethostbyname = fake_gethostbyname
        socket.getaddrinfo = fake_getaddrinfo

        socket.__dict__["socket"] = fakesock.socket
        socket.__dict__["_socketobject"] = fakesock.socket
        if not BAD_SOCKET_SHADOW:
            socket.__dict__["SocketType"] = fakesock.socket

        socket.__dict__["create_connection"] = create_fake_connection
        socket.__dict__["gethostname"] = fake_gethostname
        socket.__dict__["gethostbyname"] = fake_gethostbyname
        socket.__dict__["getaddrinfo"] = fake_getaddrinfo

        if socks:
            socks.socksocket = fakesock.socket
            socks.__dict__["socksocket"] = fakesock.socket

        if ssl:
            ssl.wrap_socket = fake_wrap_socket
            ssl.SSLSocket = FakeSSLSocket

            try:

                def fake_sslcontext_wrap_socket(cls, *args, **kwargs):
                    return fake_wrap_socket(*args, **kwargs)

                ssl.SSLContext.wrap_socket = fake_sslcontext_wrap_socket
            except AttributeError:
                pass

            ssl.__dict__["wrap_socket"] = fake_wrap_socket
            ssl.__dict__["SSLSocket"] = FakeSSLSocket

            if not PY3:
                ssl.sslwrap_simple = fake_wrap_socket
                ssl.__dict__["sslwrap_simple"] = fake_wrap_socket

        if pyopenssl_override:
            extract_from_urllib3()
Esempio n. 2
0
def test_httpretty_fails_when_pyopenssl_is_not_replaced():
    ('HTTPretty should fail if PyOpenSSL is installed and we do not remove the monkey patch')

    # When we don't replace the PyOpenSSL monkeypatch
    inject_into_urllib3()

    # And we use HTTPretty on as ssl site
    HTTPretty.register_uri(HTTPretty.GET, "https://yipit.com/",
                           body="Find the best daily deals")

    # Then we get an SSL error
    requests.get.when.called_with('https://yipit.com').should.throw(requests.exceptions.SSLError)

    # Undo injection after test
    extract_from_urllib3()
Esempio n. 3
0
def test_httpretty_fails_when_pyopenssl_is_not_replaced():
    ('HTTPretty should fail if PyOpenSSL is installed and we do not remove the monkey patch')

    # When we don't replace the PyOpenSSL monkeypatch
    inject_into_urllib3()

    # And we use HTTPretty on as ssl site
    HTTPretty.register_uri(HTTPretty.GET, "https://yipit.com/",
                           body="Find the best daily deals")

    # Then we get an SSL error
    requests.get.when.called_with('https://yipit.com').should.throw(requests.exceptions.SSLError)

    # Undo injection after test
    extract_from_urllib3()
Esempio n. 4
0
    def enable(cls):
        cls._is_enabled = True

        socket.socket = fakesock.socket
        socket._socketobject = fakesock.socket
        if not BAD_SOCKET_SHADOW:
            socket.SocketType = fakesock.socket

        socket.create_connection = create_fake_connection
        socket.gethostname = fake_gethostname
        socket.gethostbyname = fake_gethostbyname
        socket.getaddrinfo = fake_getaddrinfo

        socket.__dict__['socket'] = fakesock.socket
        socket.__dict__['_socketobject'] = fakesock.socket
        if not BAD_SOCKET_SHADOW:
            socket.__dict__['SocketType'] = fakesock.socket

        socket.__dict__['create_connection'] = create_fake_connection
        socket.__dict__['gethostname'] = fake_gethostname
        socket.__dict__['gethostbyname'] = fake_gethostbyname
        socket.__dict__['getaddrinfo'] = fake_getaddrinfo

        if socks:
            socks.socksocket = fakesock.socket
            socks.__dict__['socksocket'] = fakesock.socket

        if ssl:
            ssl.wrap_socket = fake_wrap_socket
            ssl.SSLSocket = FakeSSLSocket

            try:
                def fake_sslcontext_wrap_socket(cls, *args, **kwargs):
                    return fake_wrap_socket(*args, **kwargs)

                ssl.SSLContext.wrap_socket = fake_sslcontext_wrap_socket
            except AttributeError:
                pass

            ssl.__dict__['wrap_socket'] = fake_wrap_socket
            ssl.__dict__['SSLSocket'] = FakeSSLSocket

            if not PY3:
                ssl.sslwrap_simple = fake_wrap_socket
                ssl.__dict__['sslwrap_simple'] = fake_wrap_socket

        if pyopenssl_override:
            extract_from_urllib3()
Esempio n. 5
0
    def enable(cls):
        cls._is_enabled = True
        # Some versions of python internally shadowed the
        # SocketType variable incorrectly https://bugs.python.org/issue20386
        bad_socket_shadow = (socket.socket != socket.SocketType)

        socket.socket = fakesock.socket
        socket._socketobject = fakesock.socket
        if not bad_socket_shadow:
            socket.SocketType = fakesock.socket

        socket.create_connection = create_fake_connection
        socket.gethostname = fake_gethostname
        socket.gethostbyname = fake_gethostbyname
        socket.getaddrinfo = fake_getaddrinfo

        socket.__dict__['socket'] = fakesock.socket
        socket.__dict__['_socketobject'] = fakesock.socket
        if not bad_socket_shadow:
            socket.__dict__['SocketType'] = fakesock.socket

        socket.__dict__['create_connection'] = create_fake_connection
        socket.__dict__['gethostname'] = fake_gethostname
        socket.__dict__['gethostbyname'] = fake_gethostbyname
        socket.__dict__['getaddrinfo'] = fake_getaddrinfo

        if socks:
            socks.socksocket = fakesock.socket
            socks.__dict__['socksocket'] = fakesock.socket

        if ssl:
            ssl.wrap_socket = fake_wrap_socket
            ssl.SSLSocket = FakeSSLSocket

            ssl.__dict__['wrap_socket'] = fake_wrap_socket
            ssl.__dict__['SSLSocket'] = FakeSSLSocket

            if not PY3:
                ssl.sslwrap_simple = fake_wrap_socket
                ssl.__dict__['sslwrap_simple'] = fake_wrap_socket

        if pyopenssl_override:
            # Remove PyOpenSSL monkeypatch - use the default implementation
            extract_from_urllib3()
Esempio n. 6
0
    def enable(cls):
        cls._is_enabled = True
        # Some versions of python internally shadowed the
        # SocketType variable incorrectly https://bugs.python.org/issue20386
        bad_socket_shadow = (socket.socket != socket.SocketType)

        socket.socket = fakesock.socket
        socket._socketobject = fakesock.socket
        if not bad_socket_shadow:
            socket.SocketType = fakesock.socket

        socket.create_connection = create_fake_connection
        socket.gethostname = fake_gethostname
        socket.gethostbyname = fake_gethostbyname
        socket.getaddrinfo = fake_getaddrinfo

        socket.__dict__['socket'] = fakesock.socket
        socket.__dict__['_socketobject'] = fakesock.socket
        if not bad_socket_shadow:
            socket.__dict__['SocketType'] = fakesock.socket

        socket.__dict__['create_connection'] = create_fake_connection
        socket.__dict__['gethostname'] = fake_gethostname
        socket.__dict__['gethostbyname'] = fake_gethostbyname
        socket.__dict__['getaddrinfo'] = fake_getaddrinfo

        if socks:
            socks.socksocket = fakesock.socket
            socks.__dict__['socksocket'] = fakesock.socket

        if ssl:
            ssl.wrap_socket = fake_wrap_socket
            ssl.SSLSocket = FakeSSLSocket

            ssl.__dict__['wrap_socket'] = fake_wrap_socket
            ssl.__dict__['SSLSocket'] = FakeSSLSocket

            if not PY3:
                ssl.sslwrap_simple = fake_wrap_socket
                ssl.__dict__['sslwrap_simple'] = fake_wrap_socket

        if pyopenssl_override:
            # Remove PyOpenSSL monkeypatch - use the default implementation
            extract_from_urllib3()
Esempio n. 7
0
    def enable(cls):
        cls._is_enabled = True

        socket.socket = fakesock.socket
        socket._socketobject = fakesock.socket
        if not BAD_SOCKET_SHADOW:
            socket.SocketType = fakesock.socket

        socket.create_connection = create_fake_connection
        socket.gethostname = fake_gethostname
        socket.gethostbyname = fake_gethostbyname
        socket.getaddrinfo = fake_getaddrinfo

        socket.__dict__['socket'] = fakesock.socket
        socket.__dict__['_socketobject'] = fakesock.socket
        if not BAD_SOCKET_SHADOW:
            socket.__dict__['SocketType'] = fakesock.socket

        socket.__dict__['create_connection'] = create_fake_connection
        socket.__dict__['gethostname'] = fake_gethostname
        socket.__dict__['gethostbyname'] = fake_gethostbyname
        socket.__dict__['getaddrinfo'] = fake_getaddrinfo

        if socks:
            socks.socksocket = fakesock.socket
            socks.__dict__['socksocket'] = fakesock.socket

        if ssl:
            ssl.wrap_socket = fake_wrap_socket
            ssl.SSLSocket = FakeSSLSocket

            ssl.__dict__['wrap_socket'] = fake_wrap_socket
            ssl.__dict__['SSLSocket'] = FakeSSLSocket

            if not PY3:
                ssl.sslwrap_simple = fake_wrap_socket
                ssl.__dict__['sslwrap_simple'] = fake_wrap_socket

        if pyopenssl_override:
            extract_from_urllib3()
Esempio n. 8
0
                setattr(Extr, ckey, prev)

            symtable[Extr.__name__] = prev = Extr


# Reduce strictness of the expected magic string in cookiejar files.
# (This allows the use of Wget-generated cookiejars without modification)
http.cookiejar.MozillaCookieJar.magic_re = re.compile(
    "#( Netscape)? HTTP Cookie File", re.IGNORECASE)

# Undo automatic pyOpenSSL injection by requests
pyopenssl = config.get((), "pyopenssl", False)
if not pyopenssl:
    try:
        from requests.packages.urllib3.contrib import pyopenssl  # noqa
        pyopenssl.extract_from_urllib3()
    except ImportError:
        pass
del pyopenssl

# Replace urllib3's default cipher list to avoid Cloudflare CAPTCHAs
ciphers = config.get((), "ciphers", True)
if ciphers:

    if ciphers is True:
        ciphers = (
            # Firefox's list
            "TLS_AES_128_GCM_SHA256:"
            "TLS_CHACHA20_POLY1305_SHA256:"
            "TLS_AES_256_GCM_SHA384:"
            "ECDHE-ECDSA-AES128-GCM-SHA256:"
Esempio n. 9
0
    sig.connect(cb)


class Spec(semantic_version.Spec):
    
    @staticmethod
    def from_version(version, op='=='):
        return Spec('==' + str(version))


DL_POOL = ResizableSemaphore(10)
HTTP_SESSION.headers['User-Agent'] = get_user_agent()

if not center.DEBUG:
    logging.getLogger('requests.packages.urllib3.connectionpool').propagate = False

if sys.hexversion >= 0x020709F0:
    # In Python 2.7.9 the ssl module from Python 3.x was backported which means we can get rid of
    # the pyOpenSSL monkeypatch in urllib3.
    # On MacOS pyOpenSSL should be avoided at all cost since it's linked (by default) against
    # the system OpenSSL which is *very* old and doesn't support newer features like TLSv1.2.
    # NOTE: It might be better to check ssl.OPENSSL_VERSION
    # See also https://www.python.org/dev/peps/pep-0466/
    
    if 'requests.packages.urllib3.contrib.pyopenssl' in sys.modules:
        logging.info('Deactivating pyOpenSSL...')
        import requests.packages.urllib3.contrib.pyopenssl as pssl
        
        pssl.extract_from_urllib3()
        del pssl
Esempio n. 10
0
    @staticmethod
    def from_version(version, op='=='):
        return Spec('==' + str(version))


DL_POOL = ResizableSemaphore(10)
HTTP_SESSION.headers['User-Agent'] = get_user_agent()
SPEED_LIMIT_BUCKET = BlockingTokenBucket(3 * 1024 * 1024)

if not center.DEBUG:
    logging.getLogger('requests.packages.urllib3.connectionpool').propagate = False

if sys.hexversion >= 0x020709F0:
    # In Python 2.7.9 the ssl module from Python 3.x was backported which means we can get rid of
    # the pyOpenSSL monkeypatch in urllib3.
    # On MacOS pyOpenSSL should be avoided at all cost since it's linked (by default) against
    # the system OpenSSL which is *very* old and doesn't support newer features like TLSv1.2.
    # NOTE: It might be better to check ssl.OPENSSL_VERSION
    # See also https://www.python.org/dev/peps/pep-0466/

    if 'requests.packages.urllib3.contrib.pyopenssl' in sys.modules:
        logging.info('Deactivating pyOpenSSL...')
        import requests.packages.urllib3.contrib.pyopenssl as pssl

        pssl.extract_from_urllib3()
        del pssl

if sys.platform.startswith('win'):
    _HAS_TAR = False
Esempio n. 11
0
 def enable(cls):
     OriginalHTTPretty.enable()
     if pyopenssl_override:
         # Take out the pyopenssl version - use the default implementation
         extract_from_urllib3()
 def enable(cls):
     OriginalHTTPretty.enable()
     if pyopenssl_override:
         # Take out the pyopenssl version - use the default implementation
         extract_from_urllib3()
Esempio n. 13
0
import requests

from .environment import Environment
from .exceptions import SwishError
from .models import Payment, Refund

try:
    from requests.packages.urllib3.contrib import pyopenssl
    pyopenssl.extract_from_urllib3()
except ImportError:
    pass


class SwishClient(object):
    def __init__(self, environment, merchant_swish_number, cert, verify=False):
        self.environment = Environment.parse_environment(environment)
        self.merchant_swish_number = merchant_swish_number
        self.cert = cert
        self.verify = verify

    def post(self, endpoint, payload):
        url = self.environment.base_url + endpoint
        return requests.post(url=url, json=payload, headers={'Content-Type': 'application/json'}, cert=self.cert,
                             verify=self.verify)

    def get(self, endpoint):
        url = self.environment.base_url + endpoint
        return requests.get(url, cert=self.cert, verify=self.verify)

    def create_payment(self, amount, currency, callback_url, payee_payment_reference=None, message=None,
                       payer_alias=None):
Esempio n. 14
0
    ErinaWSGIServer.stop()
    ErinaWSGIServer.close()
    logFile.blocking = True
    log("Erina", "Restarting...")
    os.execl(sys.executable, sys.executable, __file__, *sys.argv[1:])
    

if __name__ == '__main__' or TextFile(erina_dir + "/launch.erina").read().replace(" ", "") in ["", "0"]:
    TextFile(erina_dir + "/launch.erina").read() == "1"

    #### INITIALIZING ERINASERVER --> Manages the whole server
    print("[Erina]", "Initializing ErinaServer")
    import requests
    import requests.packages.urllib3.contrib.pyopenssl as requestsPyOpenSSL
    requestsPyOpenSSL.extract_from_urllib3()
    from ErinaServer.Server import ErinaServer
    from ErinaServer import WebSockets
    from gevent import pywsgi
    from geventwebsocket.handler import WebSocketHandler
    

    from Erina.erina_log import log, logFile
    log("Erina", "Initializing Erina configuration...")
    from Erina.config import Server as ServerConfig
    from Erina.env_information import erina_version

    ## RECORDING Endpoints
    log("Erina", "---> Initializing Static File Endpoints")
    from ErinaServer.Erina import Static
    log("Erina", "---> Initializing API")