Get referer domain from user agent

main
Bob Mottram 2021-09-12 13:37:28 +01:00
parent 336a71f162
commit f3aac82b56
1 changed files with 39 additions and 12 deletions

View File

@ -502,16 +502,10 @@ class PubServer(BaseHTTPRequestHandler):
else: else:
print('ERROR: unable to create vote') print('ERROR: unable to create vote')
def _blockedUserAgent(self, callingDomain: str) -> bool: def _blockedUserAgent(self, callingDomain: str, agentStr: str) -> bool:
"""Should a GET or POST be blocked based upon its user agent? """Should a GET or POST be blocked based upon its user agent?
""" """
agentDomain = None agentDomain = None
agentStr = None
if self.headers.get('User-Agent'):
agentStr = self.headers['User-Agent']
elif self.headers.get('user-agent'):
agentStr = self.headers['user-agent']
if agentStr: if agentStr:
# is this a web crawler? If so the block it # is this a web crawler? If so the block it
@ -11627,6 +11621,19 @@ class PubServer(BaseHTTPRequestHandler):
self._400() self._400()
return return
# get the user agent
uaStr = None
if self.headers.get('User-Agent'):
uaStr = self.headers['User-Agent']
elif self.headers.get('user-agent'):
uaStr = self.headers['user-agent']
elif self.headers.get('User-agent'):
uaStr = self.headers['User-agent']
if self._blockedUserAgent(callingDomain, uaStr):
self._400()
return
# which domain is the GET request coming from? # which domain is the GET request coming from?
refererDomain = None refererDomain = None
if self.headers.get('referer'): if self.headers.get('referer'):
@ -11637,10 +11644,21 @@ class PubServer(BaseHTTPRequestHandler):
refererDomain, refererPort = \ refererDomain, refererPort = \
getDomainFromActor(self.headers['Referer']) getDomainFromActor(self.headers['Referer'])
refererDomain = getFullDomain(refererDomain, refererPort) refererDomain = getFullDomain(refererDomain, refererPort)
elif uaStr:
if self._blockedUserAgent(callingDomain): if '+https://' in uaStr:
self._400() refererDomain = \
return self.headers['User-Agent'].split('+https://')[1]
if '/' in refererDomain:
refererDomain = refererDomain.split('/')[0]
elif ')' in refererDomain:
refererDomain = refererDomain.split(')')[0]
elif '+http://' in uaStr:
refererDomain = \
self.headers['User-Agent'].split('+http://')[1]
if '/' in refererDomain:
refererDomain = refererDomain.split('/')[0]
elif ')' in refererDomain:
refererDomain = refererDomain.split(')')[0]
GETstartTime = time.time() GETstartTime = time.time()
GETtimings = {} GETtimings = {}
@ -15526,7 +15544,16 @@ class PubServer(BaseHTTPRequestHandler):
self._400() self._400()
return return
if self._blockedUserAgent(callingDomain): # get the user agent
uaStr = None
if self.headers.get('User-Agent'):
uaStr = self.headers['User-Agent']
elif self.headers.get('user-agent'):
uaStr = self.headers['user-agent']
elif self.headers.get('User-agent'):
uaStr = self.headers['User-agent']
if self._blockedUserAgent(callingDomain, uaStr):
self._400() self._400()
return return