Пример #1
0
    def testProxyBypass(self):
        if not os.path.exists(rephelp.HTTPProxy.proxyBinPath):
            raise testhelp.SkipTestException(
                'testProxyBypass depends on squid being installed')

        class Controller(ServerController):
            class XMLRPCHandler:
                def ping(self):
                    return [True]

            def handlerFactory(self):
                return self.XMLRPCHandler()

        sc = Controller()
        h = None
        oldLocalHosts = httputils.LocalHosts
        try:
            h = rephelp.HTTPProxy(os.path.join(self.workDir, "http-cache"))
            proxyUri = h.start()

            proxyMap = proxy_map.ProxyMap()
            proxyMap.addStrategy('*', ['http://' + proxyUri])

            tsp = transport.Transport(proxyMap=proxyMap)
            sp = util.ServerProxy(url=sc.url(), transport=tsp)

            # Make sure we're proxying
            logsz0 = h.getAccessLogSize()
            self.assertEqual(sp.ping(), ([True], ))
            logEntry = h.getAccessLogEntry(logsz0)
            self.assertTrue(logEntry)

            # Now mangle localhosts. Proxy is 127.0.0.1, server is on
            # localhost.
            # Make the proxy appear to be remote

            localhosts = set(httputils.LocalHosts)
            localhosts.remove('127.0.0.1')
            httputils.LocalHosts = localhosts

            logsz0 = h.getAccessLogSize()
            sp.ping()
            self.sleep(1)
            logsz1 = h.getAccessLogSize()

            self.assertEqual(logsz1, logsz0)
        finally:
            httputils.LocalHosts = oldLocalHosts
            sc.kill()
            if h:
                h.stop()
Пример #2
0
    def __init__(self,
                 proxyMap=None,
                 caCerts=None,
                 persist=False,
                 connectAttempts=None):
        if proxyMap is None:
            proxyMap = proxy_map.ProxyMap()
        self.proxyMap = proxyMap
        self.caCerts = caCerts
        self.persist = persist
        if connectAttempts:
            self.connectAttempts = connectAttempts

        self.connectionCache = {}
        self.lastProxy = None
Пример #3
0
class CfgProxyMap(CfgType):
    default = proxy_map.ProxyMap()

    def updateFromString(self, val, string):
        parts = string.split()
        if parts == ['[]']:
            val.clear()
            return val
        if len(parts) < 2:
            raise ParseError("Expected: proxyMap <pattern> "
                             "[http://proxy1|DIRECT] [proxy2 ...]")
        pattern, targets = parts[0], parts[1:]
        val.addStrategy(pattern, targets)
        return val

    def setFromString(self, val, string):
        pm = val.__class__()
        return self.updateFromString(pm, string)

    def toStrings(self, value, displayOptions):
        for pattern, targets in value.filterList:
            yield ' '.join([str(pattern)] + [str(x) for x in targets])
Пример #4
0
    def testProxyBypassNoProxy(self):
        environ = os.environ.copy()
        proxyPlain = request.URL("http://*****:*****@host.example.com:1234")
        proxyConary = request.URL("conary://*****:*****@host.example.com:1234")
        url = request.URL("http://rpath.com")

        # We should not hit the proxy at all
        self.mock(os, 'environ', environ)

        # All flavors of localhost should be direct
        proxyMap = proxy_map.ProxyMap()
        opener = opmod.URLOpener(proxyMap)

        # Normal hosts will proxy through
        self.assertEqual(opener._shouldBypass(url, proxyPlain), False)
        self.assertEqual(opener._shouldBypass(url, proxyConary), False)
        for h in httputils.LocalHosts:
            self.assertEqual(
                opener._shouldBypass(request.URL("http://%s:33" % h),
                                     proxyPlain), True)
            self.assertEqual(
                opener._shouldBypass(request.URL("http://%s:33" % h),
                                     proxyConary), True)

        # Force direct for everything on HTTP proxies
        environ['no_proxy'] = "*"
        self.assertEqual(opener._shouldBypass(url, proxyPlain), True)
        # environment variable should not affect the selection of conary proxy
        self.assertEqual(opener._shouldBypass(url, proxyConary), False)

        # Test that NO_PROXY is also used, not just no_proxy
        del environ['no_proxy']
        self.assertEqual(opener._shouldBypass(url, proxyPlain), False)
        environ['NO_PROXY'] = "*"
        self.assertEqual(opener._shouldBypass(url, proxyPlain), True)
        self.assertEqual(opener._shouldBypass(url, proxyConary), False)
        # no_proxy takes precedence over NO_PROXY
        environ['no_proxy'] = "host.com"
        self.assertEqual(opener._shouldBypass(url, proxyPlain), False)
        self.assertEqual(
            opener._shouldBypass(request.URL("http://host.com"), proxyPlain),
            True)
        # http://lynx.isc.org/lynx2.8.5/lynx2-8-5/lynx_help/keystrokes/environments.html - comma-separated list of domains (with a trailing, to force empty domain)
        environ['no_proxy'] = "host.domain.dom, domain1.dom, domain2, "
        tests = [
            ('rpath.com:80', False),
            ('domain.com:80', False),
            ('host.domain.dom:80', True),
            ('hosthost.domain.dom:80', True),
            ('sub.host.domain.dom:80', True),
            ('www.domain1.dom:80', True),
            # Hmm. We will assume the domain is qualified up to ., the domain2
            # example probably assumes it matches any domain2 in the domain
            # search path, but we don't handle that.
            ('domain2:80', True),
            ('www.domain2:80', True),
        ]
        for h, expected in tests:
            self.assertEqual(
                opener._shouldBypass(request.URL('http://' + h), proxyPlain),
                expected)
Пример #5
0
    def testProxyBypassConary(self):
        """Test that no_proxy does not affect conary proxies (CNY-3349)"""
        if not os.path.exists(rephelp.HTTPProxy.proxyBinPath):
            raise testhelp.SkipTestException(
                'testProxyBypass depends on squid being installed')

        class Controller(ServerController):
            class XMLRPCHandler:
                def ping(self):
                    return [True]

            def handlerFactory(self):
                return self.XMLRPCHandler()

        environ = os.environ.copy()
        self.mock(os, 'environ', environ)

        sc = Controller()
        h = None
        oldLocalHosts = httputils.LocalHosts
        try:
            h = rephelp.HTTPProxy(os.path.join(self.workDir, "http-cache"))
            proxyUri = h.start()

            proxyMap = proxy_map.ProxyMap()
            proxyMap.addStrategy('*', ['conary://' + proxyUri])

            tsp = transport.Transport(proxyMap=proxyMap)
            sp = util.ServerProxy(url=sc.url(), transport=tsp)

            # Now mangle localhosts. Proxy is 127.0.0.1, server is on
            # localhost.
            # Make the proxy appear to be remote
            localhosts = set(httputils.LocalHosts)
            localhosts.remove('127.0.0.1')
            httputils.LocalHosts = localhosts

            # Remote proxy, local host. Go direct
            logsz0 = h.getAccessLogSize()
            sp.ping()
            self.sleep(1)
            logsz1 = h.getAccessLogSize()
            self.assertEqual(logsz1, logsz0)

            # Local proxy, remote host. Proxy
            localhosts.add('127.0.0.1')
            localhosts.remove('localhost')

            logsz0 = h.getAccessLogSize()
            sp.ping()
            self.sleep(1)
            logsz1 = h.getAccessLogSize()
            self.assertTrue(logsz1 > logsz0, "Proxy should have been used")

            environ['no_proxy'] = '*'
            # Proxy should still be used, the environemnt variable should
            # not affect the result since it's a Conary proxy
            logsz0 = h.getAccessLogSize()
            sp.ping()
            self.sleep(1)
            logsz1 = h.getAccessLogSize()
            self.assertTrue(logsz1 > logsz0, "Proxy should have been used")
        finally:
            httputils.LocalHosts = oldLocalHosts
            sc.kill()
            if h:
                h.stop()
Пример #6
0
 def getProxyMap(self):
     return proxy_map.ProxyMap()