darknet-lantern/scripts/utils.py
2025-02-19 09:00:19 +01:00

221 lines
7 KiB
Python

import re
import requests
from PIL import Image
PURPLE = '\033[35;40m'
BOLD_PURPLE = '\033[35;40;1m'
RED = '\033[31;40m'
BOLD_RED = '\033[31;40;1m'
RESET = '\033[m'
#### Checking Functions to validate that links are legit ####
def CheckUrl(url):
"""
Checks if URL is actually reachable via Tor
"""
proxies = {
'http': 'socks5h://127.0.0.1:9050',
'https': 'socks5h://127.0.0.1:9050'
}
try:
status = requests.get(url,proxies=proxies, timeout=5).status_code
if status != 502:
return True
else:
return False
except requests.ConnectionError as e:
return False
except requests.exceptions.ReadTimeout as e:
return False
#### PROTECTIONS AGAINST MALICIOUS CSV INPUTS ####
def CheckBannerURL(path: str) -> str:
"""
In: http://participant.onion/participants/participant.onion/banner
serves to know what the real banner type is, between png, jpg, jpeg and .gif
Out: {png,jpg,jpeg,gif,NOBANNER}
"""
bannercounter=0
# TODO: try to reach (using requests) the URL/banner.{png,jpg,jpeg,gif}
# TODO: if any of them succeed,
# TODO: Check if the banner file is valid using IsBannerValid()
#TODO: if valid: return png/jpg/jpeg/gif
#TODO: if invalid REMOVE the file, and keep going (worst case = return NOBANNER at the bottom)
# TODO: if any of them fail, it should be able to continue
# TODO: if one of them fail, it must keep going and just increment bannercounter once
# TODO: if ALL of them fail, it must return NOBANNER
#TODO in lantern.py: handle when it returns png,jpg,jpeg and gif
#TODO in php: handle when it is not png, how to make sure it finds if its jpg,jpeg or gif
#TODO in lantern.py: handle when it returns NOBANNER
def IsBannerValid(path: str) -> bool:
"""
Checks if the banner.png file has the correct dimensions (240x60)
"""
try:
im = Image.open(path)
except Exception as e:
print("ERROR, EXCEPTION")
return False
width, height = im.size
if width != 240 or height != 60:
print("INVALID BANNER DIMENSIONS, HEIGHT=",height," WIDTH=",width)
return False
# TODO:check the filesize, as you can have a gif that has
# TODO: test it on all 4 filetypes with both correct and incorrect resolutions, and with a gif that has the correct resolutions but is more than 3mb
return True
def IsOnionValid(url: str)-> bool:
"""
Checks if the domain(param) is a valid onion domain and return True else False.
"""
try:
pattern = re.compile("^[A-Za-z0-9.]+(.onion)?$")
url = url.strip().removesuffix('/')
if url.startswith('http://'):
domain = url.split('/')[2]
if pattern.fullmatch(domain) is not None:
if len(domain.split('.')) > 3:
return False
else:
if len(domain) < 62:
return False
return True
elif pattern.fullmatch(domain) is None:
return False
else:
return False
else:
#TODO : edit the url to make sure it has http:// at the beginning, in case if it's missing? (problem is that it only returns true or false)
if pattern.fullmatch(url) is not None:
if len(url.split('.')) > 3:
return False
else:
if len(url) < 62:
return False
return True
elif pattern.fullmatch(url) is None:
return False
else:
return False
except Exception as e:
return False
def IsUrlValid(url:str)->bool:
"""
Check if url is valid both dark net end clearnet.
"""
pattern = re.compile("^[A-Za-z0-9:/.-]+$")
url = str(url)
if len(url) < 4:
return False
if url.endswith('.onion'):
return IsOnionValid(url)
else:
if not url.__contains__('.'):
return False
if pattern.fullmatch(url) is None:
return False
return True
def IsStatusValid(status: str)-> bool:
"""
Checks if status contains only ['YES','NO']. Verbose only if False is returned
"""
pattern = ['YES','NO','✔️','','']
#pattern = ['YES','NO']
status = str(status)
status.strip()
if (status not in pattern):
return False
return True
def IsScoreValid(score:str)->bool:
"""
Check the Score is only "^[0-9.,]+$" with 8 max chars.
"""
pattern = re.compile("^[0-9.,]+$")
score = str(score)
score.strip()
if score in ['','nan']:
return True
if pattern.fullmatch(score) is None:
return False
elif len(score) > 8:
return False
return True
def IsDescriptionValid(desc:str)->bool:
"""
Check the categories are only [a-zA-Z0-9.' ] with 256 max chars.
"""
if desc == "":
return True
pattern = re.compile("^[A-Za-z0-9-.,' \"\(\)\/]+$")
desc = str(desc)
desc.strip()
if pattern.fullmatch(desc) is None:
return False
if desc == "DEFAULT":
return False
elif len(desc) > 256:
return False
return True
def IsCategoryValid(categories: list)-> bool:
"""
Check the categories are only [a-zA-Z0-9 ] with 64 max chars.
"""
pattern = re.compile("^[A-Za-z0-9 ]+$")
for category in categories:
category.strip()
if pattern.fullmatch(category) is None:
return False
elif len(category) > 64:
return False
else:
return True
def IsNameValid(name: str)->bool:
"""
Check the parameter name only contains [a-zA-Z0-9 ] and is 64 chars long.
"""
try:
name = str(name)
except Exception as e:
return False
pattern = re.compile("^[A-Za-z0-9 ]+$")
name = name.strip()
if (pattern.fullmatch(name) is None):
return False
elif len(name) > 64:
return False
return True
def print_colors(s:str=' ', bold:bool=False, is_error:bool = False, default:bool=False):
"""
Helper function to print with colors
"""
if is_error:
print(f"{RED}{s}{RESET}")
elif bold:
print(f"{BOLD_PURPLE}{s}{RESET}")
elif is_error and bold:
print(f"{BOLD_RED}{s}{RESET}")
elif default:
print(f'{s}')
else:
print(f"{PURPLE}{s}{RESET}")