2017-09-16 13:58:02 +02:00
|
|
|
import requests
|
|
|
|
import threading
|
2017-09-24 16:43:54 +02:00
|
|
|
import redis as libredis
|
2017-09-16 13:58:02 +02:00
|
|
|
from flask import make_response, abort
|
2017-09-16 18:25:20 +02:00
|
|
|
import secrets
|
|
|
|
import hmac
|
|
|
|
import base64
|
2017-09-24 23:49:08 +02:00
|
|
|
import pickle # nosec
|
2017-09-18 11:23:59 +02:00
|
|
|
import re
|
2017-09-16 13:58:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ImgProxyCache(object):
|
2017-09-23 10:34:01 +02:00
|
|
|
def __init__(self, redis_uri='redis://', timeout=10, expire=60*60,
|
2017-09-16 18:25:20 +02:00
|
|
|
prefix='img_proxy', hmac_hash='sha1'):
|
2017-09-24 16:43:54 +02:00
|
|
|
self._redis = None
|
|
|
|
self._redis_uri = redis_uri
|
2017-09-16 13:58:02 +02:00
|
|
|
self.timeout = timeout
|
|
|
|
self.expire = expire
|
|
|
|
self.prefix = prefix
|
2017-09-16 18:25:20 +02:00
|
|
|
self.hash = hmac_hash
|
2017-09-18 11:23:59 +02:00
|
|
|
self.hmac_key = None
|
2017-09-16 13:58:02 +02:00
|
|
|
|
2017-09-24 16:43:54 +02:00
|
|
|
@property
|
|
|
|
def redis(self):
|
|
|
|
if not self._redis:
|
|
|
|
self._redis = libredis.StrictRedis.from_url(self._redis_uri)
|
|
|
|
self._redis.client_setname('img_proxy')
|
|
|
|
return self._redis
|
|
|
|
|
2017-09-16 18:25:20 +02:00
|
|
|
def key(self, *args):
|
2017-09-18 11:23:59 +02:00
|
|
|
return '{prefix}:1:{args}'.format(
|
2017-09-16 18:25:20 +02:00
|
|
|
prefix=self.prefix, args=":".join(args))
|
|
|
|
|
|
|
|
def token(self):
|
2017-09-18 11:23:59 +02:00
|
|
|
if not self.hmac_key:
|
|
|
|
t = self.redis.get(self.key('hmac_key'))
|
|
|
|
if not t:
|
|
|
|
t = secrets.token_urlsafe().encode('ascii')
|
|
|
|
self.redis.set(self.key('hmac_key'), t)
|
|
|
|
self.hmac_key = t
|
|
|
|
return self.hmac_key
|
2017-09-16 18:25:20 +02:00
|
|
|
|
|
|
|
def identifier_for(self, url):
|
|
|
|
url_hmac = hmac.new(self.token(), url.encode('UTF-8'), self.hash)
|
|
|
|
return base64.urlsafe_b64encode(
|
|
|
|
'{}:{}'.format(url_hmac.hexdigest(), url)
|
|
|
|
.encode('UTF-8')
|
2019-05-02 00:57:45 +02:00
|
|
|
).strip(b'=').decode('UTF-8')
|
2017-09-16 18:25:20 +02:00
|
|
|
|
|
|
|
def url_for(self, identifier):
|
|
|
|
try:
|
|
|
|
padding = (4 - len(identifier)) % 4
|
|
|
|
identifier += padding * '='
|
|
|
|
identifier = base64.urlsafe_b64decode(identifier).decode('UTF-8')
|
|
|
|
received_hmac, url = identifier.split(':', 1)
|
|
|
|
url_hmac = hmac.new(self.token(), url.encode('UTF-8'), self.hash)
|
|
|
|
if not hmac.compare_digest(url_hmac.hexdigest(), received_hmac):
|
|
|
|
return None
|
|
|
|
except Exception:
|
|
|
|
return None
|
|
|
|
return url
|
2017-09-16 13:58:02 +02:00
|
|
|
|
|
|
|
def fetch_and_cache(self, url):
|
|
|
|
resp = requests.get(url)
|
|
|
|
if(resp.status_code != 200):
|
|
|
|
return
|
2017-09-18 11:23:59 +02:00
|
|
|
|
2021-05-14 18:47:46 +02:00
|
|
|
allowed_headers = [
|
2017-09-18 11:23:59 +02:00
|
|
|
'content-type',
|
|
|
|
'cache-control',
|
|
|
|
'etag',
|
|
|
|
'date',
|
|
|
|
'last-modified',
|
|
|
|
]
|
|
|
|
headers = {}
|
|
|
|
|
|
|
|
expire = self.expire
|
|
|
|
if 'cache-control' in resp.headers:
|
|
|
|
for value in resp.headers['cache-control'].split(','):
|
|
|
|
match = re.match(' *max-age *= *([0-9]+) *', value)
|
|
|
|
if match:
|
|
|
|
expire = max(self.expire, int(match.group(1)))
|
|
|
|
|
2021-05-14 18:47:46 +02:00
|
|
|
for key in allowed_headers:
|
2017-09-18 11:23:59 +02:00
|
|
|
if key in resp.headers:
|
|
|
|
headers[key] = resp.headers[key]
|
|
|
|
self.redis.set(self.key('headers', url), pickle.dumps(headers, -1),
|
|
|
|
px=expire*1000)
|
2017-09-16 14:11:17 +02:00
|
|
|
self.redis.set(self.key('body', url),
|
2017-09-18 11:23:59 +02:00
|
|
|
resp.content, px=expire*1000)
|
2017-09-16 13:58:02 +02:00
|
|
|
|
2017-09-16 18:25:20 +02:00
|
|
|
def respond(self, identifier):
|
|
|
|
url = self.url_for(identifier)
|
|
|
|
if not url:
|
|
|
|
return abort(403)
|
|
|
|
|
2017-09-16 13:58:02 +02:00
|
|
|
x_imgproxy_cache = 'HIT'
|
2017-09-18 11:23:59 +02:00
|
|
|
headers = self.redis.get(self.key('headers', url))
|
2017-09-16 13:58:02 +02:00
|
|
|
body = self.redis.get(self.key('body', url))
|
2017-09-18 11:23:59 +02:00
|
|
|
|
|
|
|
if not body or not headers:
|
2017-09-16 13:58:02 +02:00
|
|
|
x_imgproxy_cache = 'MISS'
|
|
|
|
if self.redis.set(
|
|
|
|
self.key('lock', url), 1, nx=True, ex=10*self.timeout):
|
|
|
|
t = threading.Thread(target=self.fetch_and_cache, args=(url,))
|
|
|
|
t.start()
|
|
|
|
t.join(self.timeout)
|
2017-09-18 11:23:59 +02:00
|
|
|
headers = self.redis.get(self.key('headers', url))
|
2017-09-16 13:58:02 +02:00
|
|
|
body = self.redis.get(self.key('body', url))
|
2017-09-18 11:23:59 +02:00
|
|
|
|
|
|
|
try:
|
2017-09-24 23:49:08 +02:00
|
|
|
headers = pickle.loads(headers) # nosec
|
2017-09-24 23:54:03 +02:00
|
|
|
except Exception:
|
2017-09-18 11:23:59 +02:00
|
|
|
self.redis.delete(self.key('headers', url))
|
|
|
|
headers = None
|
|
|
|
|
|
|
|
if not body or not headers:
|
2017-09-16 13:58:02 +02:00
|
|
|
return abort(404)
|
|
|
|
|
|
|
|
resp = make_response(body, 200)
|
2018-11-30 03:25:24 +01:00
|
|
|
resp.headers.set('imgproxy-cache', x_imgproxy_cache)
|
2017-09-16 18:25:20 +02:00
|
|
|
resp.headers.set('cache-control', 'max-age={}'.format(self.expire))
|
2017-09-18 11:23:59 +02:00
|
|
|
for key, value in headers.items():
|
|
|
|
resp.headers.set(key, value)
|
2017-09-16 13:58:02 +02:00
|
|
|
return resp
|