forked from mirrors/bookwyrm
Class method for checking if urls are blocked
This commit is contained in:
parent
0caeb3ac33
commit
1903812b1d
3 changed files with 17 additions and 15 deletions
|
@ -219,6 +219,12 @@ def dict_from_mappings(data, mappings):
|
||||||
|
|
||||||
def get_data(url, params=None):
|
def get_data(url, params=None):
|
||||||
""" wrapper for request.get """
|
""" wrapper for request.get """
|
||||||
|
# check if the url is blocked
|
||||||
|
if models.FederatedServer.is_blocked(url):
|
||||||
|
raise ConnectorException(
|
||||||
|
"Attempting to load data from blocked url: {:s}".format(url)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resp = requests.get(
|
resp = requests.get(
|
||||||
url,
|
url,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
""" connections to external ActivityPub servers """
|
""" connections to external ActivityPub servers """
|
||||||
|
from urllib.parse import urlparse
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from .base_model import BookWyrmModel
|
from .base_model import BookWyrmModel
|
||||||
|
|
||||||
|
@ -38,3 +39,10 @@ class FederatedServer(BookWyrmModel):
|
||||||
|
|
||||||
# TODO: only reactivate users as appropriate
|
# TODO: only reactivate users as appropriate
|
||||||
self.user_set.update(is_active=True)
|
self.user_set.update(is_active=True)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def is_blocked(cls, url):
|
||||||
|
""" look up if a domain is blocked """
|
||||||
|
url = urlparse(url)
|
||||||
|
domain = url.netloc
|
||||||
|
return cls.objects.filter(server_name=domain, status="blocked").exists()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
""" incoming activities """
|
""" incoming activities """
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from urllib.parse import urldefrag, urlparse
|
from urllib.parse import urldefrag
|
||||||
|
|
||||||
from django.http import HttpResponse, HttpResponseNotFound
|
from django.http import HttpResponse, HttpResponseNotFound
|
||||||
from django.http import HttpResponseBadRequest, HttpResponseForbidden
|
from django.http import HttpResponseBadRequest, HttpResponseForbidden
|
||||||
|
@ -71,11 +71,7 @@ def is_blocked_user_agent(request):
|
||||||
if not user_agent:
|
if not user_agent:
|
||||||
return False
|
return False
|
||||||
url = re.search(r"https?://{:s}/?".format(regex.domain), user_agent).group()
|
url = re.search(r"https?://{:s}/?".format(regex.domain), user_agent).group()
|
||||||
domain = urlparse(url).netloc
|
return models.FederatedServer.is_blocked(url)
|
||||||
if not domain:
|
|
||||||
# idk, we'll try again later with the actor
|
|
||||||
return False
|
|
||||||
return is_blocked(domain)
|
|
||||||
|
|
||||||
|
|
||||||
def is_blocked_activity(activity_json):
|
def is_blocked_activity(activity_json):
|
||||||
|
@ -84,15 +80,7 @@ def is_blocked_activity(activity_json):
|
||||||
if not actor:
|
if not actor:
|
||||||
# well I guess it's not even a valid activity so who knows
|
# well I guess it's not even a valid activity so who knows
|
||||||
return False
|
return False
|
||||||
url = urlparse(actor)
|
return models.FederatedServer.is_blocked(actor)
|
||||||
return is_blocked(url.netloc)
|
|
||||||
|
|
||||||
|
|
||||||
def is_blocked(domain):
|
|
||||||
""" is this domain blocked? """
|
|
||||||
return models.FederatedServer.objects.filter(
|
|
||||||
server_name=domain, status="blocked"
|
|
||||||
).exists()
|
|
||||||
|
|
||||||
|
|
||||||
@app.task
|
@app.task
|
||||||
|
|
Loading…
Reference in a new issue