Пример #1
0
def _get_upstream_proxy_args(options):
    args = []
    proxy_config = get_upstream_proxy(options)

    http_proxy = proxy_config.get('http')
    https_proxy = proxy_config.get('https')
    conf = None

    if http_proxy and https_proxy:
        if http_proxy.hostport != https_proxy.hostport:
            # We only support a single upstream proxy server
            raise ValueError('Cannot specify both http AND https '
                             'proxy settings with mitmproxy backend')

        conf = https_proxy
    elif http_proxy:
        conf = http_proxy
    elif https_proxy:
        conf = https_proxy

    if conf:
        scheme, username, password, hostport = conf

        args += ['--set', 'mode=upstream:{}://{}'.format(scheme, hostport)]

        if username and password:
            args += ['--set', 'upstream_auth={}:{}'.format(username, password)]

    return args
Пример #2
0
    def test_merge(self):
        options = {
            'proxy': {
                'https': 'https://*****:*****@server3:8888',
                'no_proxy': 'localhost'
            }
        }

        with self.set_env(
                HTTP_PROXY='http://*****:*****@server1:8888',
                HTTPS_PROXY='https://*****:*****@server2:8888',
                NO_PROXY='127.0.0.1'):

            proxy = get_upstream_proxy(options)

            http = proxy['http']
            self.assertEqual('http', http.scheme)
            self.assertEqual('username1', http.username)
            self.assertEqual('password1', http.password)
            self.assertEqual('server1:8888', http.hostport)

            # The dict config overrides that defined in env variables
            https = proxy['https']
            self.assertEqual('https', https.scheme)
            self.assertEqual('username3', https.username)
            self.assertEqual('password3', https.password)
            self.assertEqual('server3:8888', https.hostport)

            self.assertEqual('localhost', proxy['no_proxy'])
Пример #3
0
    def test_get_from_env(self):
        with self.set_env(
                HTTP_PROXY='http://*****:*****@server1:8888',
                HTTPS_PROXY='https://*****:*****@server2:8888',
                NO_PROXY='localhost'):

            proxy = get_upstream_proxy({})

            http = proxy['http']
            self.assertEqual('http', http.scheme)
            self.assertEqual('username1', http.username)
            self.assertEqual('password1', http.password)
            self.assertEqual('server1:8888', http.hostport)

            https = proxy['https']
            self.assertEqual('https', https.scheme)
            self.assertEqual('username2', https.username)
            self.assertEqual('password2', https.password)
            self.assertEqual('server2:8888', https.hostport)

            self.assertEqual('localhost', proxy['no_proxy'])
Пример #4
0
    def test_get_config(self):
        options = {
            'proxy': {
                'http': 'http://*****:*****@server1:8888',
                'https': 'https://*****:*****@server2:8888',
                'no_proxy': 'localhost'
            }
        }

        proxy = get_upstream_proxy(options)

        http = proxy['http']
        self.assertEqual('http', http.scheme)
        self.assertEqual('username1', http.username)
        self.assertEqual('password1', http.password)
        self.assertEqual('server1:8888', http.hostport)

        https = proxy['https']
        self.assertEqual('https', https.scheme)
        self.assertEqual('username2', https.username)
        self.assertEqual('password2', https.password)
        self.assertEqual('server2:8888', https.hostport)

        self.assertEqual('localhost', proxy['no_proxy'])
Пример #5
0
    def test_none(self):
        options = None

        proxy = get_upstream_proxy(options)

        self.assertEqual({}, proxy)