mirror of
http://git.nowherejezfoltodf4jiyl6r56jnzintap5vyjlia7fkirfsnfizflqd.onion/nihilist/darknet-lantern.git
synced 2025-07-01 18:56:40 +00:00
pulling latest code
This commit is contained in:
commit
b234d9d1d0
5 changed files with 305 additions and 477 deletions
523
scripts/utils.py
523
scripts/utils.py
|
@ -10,13 +10,13 @@ from websockets.sync.client import connect
|
|||
import conf
|
||||
import pandas as pd
|
||||
|
||||
PURPLE = '\033[35;40m'
|
||||
|
||||
PURPLE = '\033[35;40m'
|
||||
BOLD_PURPLE = '\033[35;40;1m'
|
||||
RED = '\033[31;40m'
|
||||
BOLD_RED = '\033[31;40;1m'
|
||||
RESET = '\033[m'
|
||||
|
||||
|
||||
def get_current_instance():
|
||||
"""
|
||||
Checks if all URL files are actually reachable via Tor
|
||||
|
@ -36,24 +36,100 @@ conf.LOCAL_DIR = conf.PARTICIPANT_DIR + get_current_instance() + '/'
|
|||
|
||||
###################### Validations ######################
|
||||
|
||||
def IsSimplexChatroomValid(url: str) -> bool:
|
||||
"""
|
||||
Recognizes Simplex Chatroom link.
|
||||
Returns True if URL is a SimpleX chatroom,
|
||||
False otherwise
|
||||
"""
|
||||
return bool(conf.SIMPLEX_CHATROOM_PATTERN.match(url))
|
||||
|
||||
def RecognizeSimplexType(url: str) -> str:
|
||||
"""
|
||||
Recognizes Simplex Server URL, returns smp, xftp or invalid
|
||||
"""
|
||||
match = conf.SIMPLEX_SERVER_PATTERN.match(url)
|
||||
if match:
|
||||
return match.group(1)
|
||||
else:
|
||||
return 'invalid'
|
||||
|
||||
# stub function
|
||||
def IsXFTPServerValid(url: str) -> bool:
|
||||
"""
|
||||
Returns True if URL is a valid SimpleX XFTP Server URL
|
||||
False otherwise
|
||||
"""
|
||||
return conf.RecognizeSimplexType(url) == 'xftp'
|
||||
|
||||
# stub function
|
||||
def IsSMPServerValid(url: str) -> bool:
|
||||
"""
|
||||
Returns True if URL is a valid SimpleX SMP Server URL
|
||||
False otherwise
|
||||
"""
|
||||
return conf.RecognizeSimplexType(url) == 'smp'
|
||||
|
||||
def IsClearnetLinkValid(url: str) -> bool:
|
||||
"""
|
||||
Returns True if URL is a valid clearnet URL
|
||||
False otherwise
|
||||
"""
|
||||
return bool(conf.CLEARNET_URL_PATTERN.match(url))
|
||||
|
||||
def IsOnionLinkValid(url: str) -> bool:
|
||||
"""
|
||||
Returns True if URL is a valid onion URL
|
||||
False otherwise
|
||||
"""
|
||||
return bool(conf.ONION_URL_PATTERN.match(url))
|
||||
|
||||
def RecognizeURLType(url: str) -> str:
|
||||
"""
|
||||
Recognizes URL type, can return:
|
||||
- chatroom - SimpleX chatroom
|
||||
- xftp - XFTP SimpleX server
|
||||
- smp - SMP SimpleX server
|
||||
- onion - onion URL
|
||||
- clearnet - valid clearnet url
|
||||
- invalid - none of the above (probably invalid)
|
||||
"""
|
||||
# order is important here
|
||||
# (ex. simplex chatroom is also valid clearnet link)
|
||||
if IsSimplexChatroomValid(url):
|
||||
return 'chatroom'
|
||||
if IsXFTPServerValid(url):
|
||||
return 'xftp'
|
||||
if IsSMPServerValid(url):
|
||||
return 'smp'
|
||||
if IsOnionLinkValid(url):
|
||||
return 'onion'
|
||||
if IsClearnetLinkValid(url):
|
||||
return 'clearnet'
|
||||
return 'invalid'
|
||||
|
||||
def IsURLValid(url: str) -> bool:
|
||||
"""
|
||||
Checks if given URL is valid (RecognizeURLType recognizes it)
|
||||
"""
|
||||
return RecognizeURLType(url) != 'invalid'
|
||||
|
||||
|
||||
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 == 200:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
except requests.ConnectionError as e:
|
||||
return False
|
||||
except requests.exceptions.ReadTimeout as e:
|
||||
return False
|
||||
"""
|
||||
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
|
||||
return status == 200
|
||||
except requests.ConnectionError:
|
||||
return False
|
||||
except requests.exceptions.ReadTimeout:
|
||||
return False
|
||||
|
||||
###TODO: should replace checkUrl
|
||||
# checks if all the webring participants are reachable
|
||||
|
@ -83,317 +159,110 @@ def is_participant_reachable(instance):
|
|||
|
||||
#### PROTECTIONS AGAINST MALICIOUS CSV INPUTS ####
|
||||
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
|
||||
filesizeMB=os.path.getsize(path)/1024/1024
|
||||
if filesizeMB > 5:
|
||||
print("Banner filesize too large (>5Mb): ",os.path.getsize(path)/1024/1024,"MB")
|
||||
return False
|
||||
return True
|
||||
|
||||
def IsOnionValid(url: str)-> bool:
|
||||
"""
|
||||
Checks if the domain(param) is a valid onion domain and return True else False.
|
||||
Checks if the banner.png file has the correct dimensions (240x60)
|
||||
"""
|
||||
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:
|
||||
im = Image.open(path)
|
||||
except Exception:
|
||||
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
|
||||
filesizeMB = os.path.getsize(path)/1024/1024
|
||||
if filesizeMB > 5:
|
||||
print("Banner filesize too large (>5Mb): ",os.path.getsize(path)/1024/1024,"MB")
|
||||
return False
|
||||
|
||||
def IsSimpleXChatroomValid(url: str) -> bool:
|
||||
"""Validate the SimpleX chatroom URL."""
|
||||
REQUIRED_SUBSTRING = "#/?v=2-7&smp=smp%3A%2F"
|
||||
|
||||
# Step 1: Check if it starts with http://, https://, or simplex:/
|
||||
if url.startswith(('http://', 'https://', 'simplex:/')):
|
||||
# Step 1.5: If http:// or https://, check for valid clearnet or onion domain
|
||||
if url.startswith(('http://', 'https://')) and not IsUrlValid(url):
|
||||
return False
|
||||
elif not url.startswith('simplex:/'):
|
||||
return False # Must start with one of the valid protocols
|
||||
|
||||
# Step 2: Check for the presence of the required substring
|
||||
if REQUIRED_SUBSTRING not in url:
|
||||
return False # Required substring not found
|
||||
|
||||
# Step 3: Extract the part after "smp=smp%3A%2F"
|
||||
smp_start = url.find("smp=smp%3A%2F")
|
||||
if smp_start == -1:
|
||||
return False # Required substring not found
|
||||
|
||||
smp_start += len("smp=smp%3A%2F")
|
||||
smp_end = url.find("&", smp_start)
|
||||
if smp_end == -1:
|
||||
smp_end = len(url) # Take until the end if no "&" is found
|
||||
|
||||
smp_value = urllib.parse.unquote(url[smp_start:smp_end]) # Decode the URL-encoded string
|
||||
|
||||
# Step 3.5: Check if the smp_value contains a valid hostname
|
||||
if '@' not in smp_value:
|
||||
return False # Must contain '@' to separate fingerprint and hostname
|
||||
|
||||
fingerprint, hostname = smp_value.split('@', 1)
|
||||
if not IsUrlValid(hostname):
|
||||
return False # Invalid hostname
|
||||
|
||||
# Step 4: Check for the presence of "%2F" in the original URL
|
||||
if "%2F" not in url:
|
||||
return False # Required substring not found
|
||||
|
||||
# If all checks pass, return True
|
||||
return True
|
||||
|
||||
def IsUrlValid(url:str)->bool:
|
||||
"""
|
||||
Check if url is valid both dark net end clearnet.
|
||||
"""
|
||||
pattern = re.compile(r"^[A-Za-z0-9:/._%-=#?&@]+$")
|
||||
onion_pattern = re.compile(r"^(\w+:)?(?://)?(\w+\.)?[a-z2-7]{56}\.onion")
|
||||
url = str(url)
|
||||
if len(url) < 4:
|
||||
return False
|
||||
if onion_pattern.match(url) is not None:
|
||||
return IsOnionValid(url)
|
||||
else:
|
||||
if not url.__contains__('.'):
|
||||
return False
|
||||
if url.__contains__(';'):
|
||||
return False #required otherwise lantern thinks there are extra columns
|
||||
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 IsSimpleXServerValid(url: str) -> bool:
|
||||
pattern = re.compile('[0-9A-Za-z-_]*')
|
||||
url = url.strip()
|
||||
try:
|
||||
|
||||
if url.startswith(('smp://', 'xftp://')):
|
||||
# Remove the protocol part
|
||||
proless = url.split('//', 1)[-1]
|
||||
# Split the fingerprint and hostname
|
||||
parts = proless.split('@')
|
||||
if len(parts) != 2:
|
||||
return False # Must have exactly one '@' character
|
||||
|
||||
fingerprint = parts[0]
|
||||
hostname = parts[1].split(',')[0] # Get the hostname before any comma
|
||||
|
||||
# Check fingerprint length and pattern
|
||||
if len(fingerprint) == 44 and pattern.match(fingerprint):
|
||||
# Validate the hostname
|
||||
result = IsSimpleXUrlValid(hostname)
|
||||
if result:
|
||||
# Check for an optional comma and a valid onion domain
|
||||
if ',' in proless:
|
||||
onion_part = proless.split(',')[1].strip()
|
||||
if not hostname_pattern.match(onion_part):
|
||||
return False
|
||||
return True
|
||||
return False
|
||||
except Exception as e:
|
||||
print(e)
|
||||
# Any error will be a false
|
||||
return False
|
||||
|
||||
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}")
|
||||
|
||||
def IsSimpleXOnionValid(url: str)-> bool:
|
||||
def IsStatusValid(status: str) -> bool:
|
||||
"""
|
||||
Checks if the domain(param) is a valid onion domain and return True else False.
|
||||
Checks if status contains only ['YES','NO']. Verbose only if False is returned
|
||||
"""
|
||||
try:
|
||||
pattern = re.compile(r"^[A-Za-z0-9:/._%-=#?&@]+(.onion)$")
|
||||
url_pattern = re.compile(r"^(\w+:)?(?://)?(\w+\.)?[a-z2-7]{56}\.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 url_pattern.match(url) is not None:
|
||||
if len(url.split('.')) > 3:
|
||||
return False
|
||||
else:
|
||||
if len(url) < 62:
|
||||
return False
|
||||
return True
|
||||
elif url_pattern.match(url) is None:
|
||||
return False
|
||||
else:
|
||||
return False
|
||||
except Exception as e:
|
||||
pattern = ['YES','NO','']
|
||||
status = status.strip()
|
||||
if status not in pattern:
|
||||
return False
|
||||
|
||||
def IsSimpleXUrlValid(url:str)->bool:
|
||||
"""
|
||||
Check if url is valid both dark net end clearnet.
|
||||
"""
|
||||
pattern = re.compile(r"^[A-Za-z0-9:/._%-=#?&@]+$")
|
||||
onion_pattern = re.compile(r"^(\w+:)?(?://)?(\w+\.)?[a-z2-7]{56}\.onion")
|
||||
url = str(url)
|
||||
if len(url) < 4:
|
||||
return False
|
||||
if onion_pattern.match(url) is not None:
|
||||
return IsSimpleXOnionValid(url)
|
||||
else:
|
||||
if not url.__contains__('.'):
|
||||
return False
|
||||
if pattern.fullmatch(url) is None:
|
||||
return False
|
||||
return True
|
||||
return True
|
||||
|
||||
def send_server_checks(url:str) -> ():
|
||||
"""
|
||||
Sends requests to sxc websocket and retuns
|
||||
response, response type and testFailure or None.
|
||||
"""
|
||||
with connect(f"ws://localhost:3030") as websocket:
|
||||
query = f"/_server test 1 {url}"
|
||||
command = {
|
||||
'corrId': f"id{random.randint(0,999999)}",
|
||||
'cmd': query,
|
||||
}
|
||||
websocket.send(json.dumps(command))
|
||||
message = websocket.recv()
|
||||
response = json.loads(message)
|
||||
resp_type = response["resp"]["type"]
|
||||
failed_response = response['resp'].get('testFailure')
|
||||
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
|
||||
if 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(r"^[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[str]) -> 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:
|
||||
return bool(VALID_NAME_PATTERN.fullmatch(name.strip()))
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def send_server_checks(url: str) -> tuple[str, str, str]:
|
||||
"""
|
||||
Sends requests to sxc websocket and retuns
|
||||
response, response type and testFailure or None.
|
||||
"""
|
||||
with connect(f"ws://localhost:3030") as websocket:
|
||||
query = f"/_server test 1 {url}"
|
||||
command = {
|
||||
'corrId': f"id{random.randint(0,999999)}",
|
||||
'cmd': query,
|
||||
}
|
||||
websocket.send(json.dumps(command))
|
||||
message = websocket.recv()
|
||||
response = json.loads(message)
|
||||
resp_type = response["resp"]["type"]
|
||||
failed_response = response['resp'].get('testFailure')
|
||||
|
||||
return (response, resp_type, failed_response)
|
||||
|
||||
|
@ -676,3 +545,19 @@ def get_local_webring_participants():
|
|||
except Exception:
|
||||
print_colors(f'[-] failed reading webring participants file',is_error=True )
|
||||
return pd.DataFrame()
|
||||
|
||||
|
||||
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}")
|
Loading…
Add table
Add a link
Reference in a new issue