def _start_upstream(self): self._buffer.seek(0) firstline = self._buffer.readline() match = re.match("(?:CONNECT) ([^:]+)(?:[:](\d+))? \w+", firstline) if match is None: return host = match.group(1) port = int(match.group(2) or 443) cachebrowsed = False try: Host.get(Host.hostname == host) cachebrowsed = True except Host.DoesNotExist: pass if cachebrowsed: #logger.info("[HTTPS] %s:%s <REJECTED>" % (host, port)) #self.connection.close_local() logger.info("[HTTPS] %s:%s <CACHEBROWSED>" % (host, port)) return self._connect_upstream(host, port) else: logger.info("[HTTPS] %s:%s <PROXYING>" % (host, port)) return self._connect_upstream(host, port)
def _get_or_create_host(cls, hostname, create=True): try: return Host.get(Host.hostname == hostname), False except DoesNotExist: if create: return Host.create(hostname=hostname), True return None, None
def action_check_host(request): hostname = extract_url_hostname(request['host']) try: is_active = Host.get(Host.hostname == hostname).is_active except DoesNotExist: is_active = False return { 'result': 'active' if is_active else 'inactive', 'host': request['host'] }
def test_database(self): initialize_database(":memory:", reset=True) cdn1 = CDN.create(id="cdn1", name="Sample CDN", edge_server="1.2.3.4") cdn2 = CDN.create(id="cdn2", name="Sample CDN2", edge_server="1.2.3.5") host1 = Host.create(hostname="first.host", cdn=cdn1) host2 = Host.create(hostname="second.host", cdn=cdn2) host3 = Host.create(hostname="third.host", cdn=cdn1) self.assertEqual([host1, host2, host3], list(Host.select())) self.assertEqual([host1, host3], Host.select().where(Host.cdn == cdn1)) self.assertEqual([host1, host3], Host.select().join(CDN).where(CDN.id == 'cdn1')) self.assertEqual(host2, Host.get(Host.cdn == cdn2)) self.assertEqual([], CDN.select().where(CDN.id == "doesntexist")) with self.assertRaises(DoesNotExist): CDN.get(CDN.id == "doesntexist")
def resolve_host(hostname, use_cachebrowser_db=True): # Check if host exists in database if use_cachebrowser_db: try: host = Host.get(hostname=hostname) cdn = host.cdn if cdn is None: _bootstrap_host(host) if not cdn.edge_server: _bootstrap_cdn(cdn) return cdn.edge_server, True except Host.DoesNotExist: pass return _dns_request(hostname), False
def resolve_host(hostname, use_cachebrowser_db=True): # Check if host exists in database if use_cachebrowser_db: try: host = Host.get(hostname) cdn = host.cdn if cdn is None: _bootstrap_host(host) addresses = cdn.addresses if addresses is None or len(addresses) == 0: _bootstrap_cdn(cdn) return cdn.addresses[0], True # make it random? except Host.DoesNotExist: pass return _dns_request(hostname), False
def _start_remote(self): http_request = self.request_builder.http_request url = http_request.path parsed_url = urlparse.urlparse(url) try: host = Host.get(Host.hostname==parsed_url.hostname) if host.uses_ssl: url = url.replace('http', 'https') self.cachebrowsed = True except Host.DoesNotExist: pass logger.info("[%s] %s %s" % (http_request.method, url, '<CACHEBROWSED>' if self.cachebrowsed else '')) request = http_request.get_raw() # request = re.sub(r'^(GET|POST|PUT|DELETE|HEAD) http[s]?://[^/]+/(.+) (\w+)', r'\1 /\2 \3', request) response = http.request(url, raw_request=request) self._connection.start_remote(response)
def _get_or_bootstrap_host(self, hostname): try: return Host.get(Host.hostname == hostname) except DoesNotExist: try: host_data = self.bootstrapper.lookup_host(hostname) except BootstrapError: raise DoesNotExist host = Host(**host_data) try: host.cdn = self._get_or_bootstrap_cdn(host.cdn_id) except DoesNotExist: host.cdn = CDN.create(id=host.cdn_id, valid=False) host.save(force_insert=True) return host
def _start_remote(self): http_request = self.request_builder.http_request url = http_request.path parsed_url = urlparse.urlparse(url) try: host = Host.get(url=parsed_url.hostname) if host.ssl: url = url.replace('http', 'https') self.cachebrowsed = True except Host.DoesNotExist: pass logging.info("[%s] %s %s" % (http_request.method, url, '<CACHEBROWSED>' if self.cachebrowsed else '')) request = http_request.get_raw() # request = re.sub(r'^(GET|POST|PUT|DELETE|HEAD) http[s]?://[^/]+/(.+) (\w+)', r'\1 /\2 \3', request) response = http.request(url, raw_request=request) self._connection.start_remote(response)