2016-04-28 05:40:58 +02:00
|
|
|
import hashlib
|
|
|
|
import hmac
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
|
2018-10-25 01:13:35 +02:00
|
|
|
def generate_camo_url(url: str) -> str:
|
2021-08-02 23:20:39 +02:00
|
|
|
encoded_url = url.encode()
|
2022-06-15 05:17:23 +02:00
|
|
|
assert settings.CAMO_KEY is not None
|
2021-08-02 23:20:39 +02:00
|
|
|
encoded_camo_key = settings.CAMO_KEY.encode()
|
2018-10-25 01:13:35 +02:00
|
|
|
digest = hmac.new(encoded_camo_key, encoded_url, hashlib.sha1).hexdigest()
|
2021-08-02 23:36:06 +02:00
|
|
|
return f"{digest}/{encoded_url.hex()}"
|
2018-10-25 01:13:35 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2016-04-28 05:40:58 +02:00
|
|
|
# Encodes the provided URL using the same algorithm used by the camo
|
|
|
|
# caching https image proxy
|
2018-05-10 19:13:36 +02:00
|
|
|
def get_camo_url(url: str) -> str:
|
2020-10-23 02:43:28 +02:00
|
|
|
# Only encode the URL if Camo is enabled
|
2021-02-12 08:20:45 +01:00
|
|
|
if settings.CAMO_URI == "":
|
2016-04-28 05:40:58 +02:00
|
|
|
return url
|
2020-06-10 06:41:04 +02:00
|
|
|
return f"{settings.CAMO_URI}{generate_camo_url(url)}"
|
2018-12-17 17:27:05 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-12-17 17:27:05 +01:00
|
|
|
def is_camo_url_valid(digest: str, url: str) -> bool:
|
|
|
|
camo_url = generate_camo_url(url)
|
2021-02-12 08:20:45 +01:00
|
|
|
camo_url_digest = camo_url.split("/")[0]
|
2018-12-17 17:27:05 +01:00
|
|
|
return camo_url_digest == digest
|