Skip to content

Commit

Permalink
Encode urls to ascii ignoring errors and log issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jadolg committed Dec 13, 2024
1 parent 6370766 commit 8b4329a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
13 changes: 11 additions & 2 deletions proxylist/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import base64
import logging
import re

from django.core.cache import cache
Expand All @@ -12,6 +13,8 @@
from proxylist.base64_decoder import decode_base64
from proxylist.proxy import update_proxy_status, get_proxy_location

log = logging.getLogger("django")


def validate_sip002(value) -> None:
if get_sip002(instance_url=value) == "":
Expand Down Expand Up @@ -76,7 +79,7 @@ def get_sip002(instance_url):
url = url.replace("=", "")
if "@" not in url:
url = url.replace("ss://", "")
decoded_url = decode_base64(url.encode("ascii"))
decoded_url = decode_base64(url.encode("ascii", errors="ignore"))
if decoded_url:
encoded_bits = (
base64.b64encode(decoded_url.split(b"@")[0])
Expand All @@ -88,7 +91,13 @@ def get_sip002(instance_url):
)
else:
return ""
except IndexError:
except (IndexError, UnicodeEncodeError):
log.warning(
"Unable to decode SIP002",
extra={
"url": instance_url,
},
)
return ""

return url
Expand Down
10 changes: 5 additions & 5 deletions proxylist/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def decode_line(line):
return decode_base64(line).decode("utf-8").split("\n")
except UnicodeDecodeError:
log.error(
f"Failed decoding line",
"Failed decoding line",
extra={"task": inspect.currentframe().f_code.co_name, "line": line},
)

Expand All @@ -138,7 +138,7 @@ def poll_subscriptions() -> None:
subscriptions = Subscription.objects.filter(enabled=True)
for subscription in subscriptions:
log.info(
f"Testing subscription",
"Testing subscription",
extra={
"task": inspect.currentframe().f_code.co_name,
"subscription": subscription.url,
Expand Down Expand Up @@ -189,7 +189,7 @@ def poll_subscriptions() -> None:
requests.exceptions.ReadTimeout,
) as e:
log.error(
f"Failed to get subscription",
"Failed to get subscription",
extra={
"error": f"{e}",
"task": inspect.currentframe().f_code.co_name,
Expand All @@ -200,7 +200,7 @@ def poll_subscriptions() -> None:
subscription.alive = False
except AttributeError as e:
log.warning(
f"Error decoding subscription",
"Error decoding subscription",
extra={
"task": inspect.currentframe().f_code.co_name,
"error": f"{e}",
Expand Down Expand Up @@ -247,7 +247,7 @@ def save_proxies(proxies_lists):
saved_proxies += 1
except Exception as e:
log.warning(
f"Failed to save proxy",
"Failed to save proxy",
extra={
"task": inspect.currentframe().f_code.co_name,
"proxy": proxy,
Expand Down

0 comments on commit 8b4329a

Please sign in to comment.