darknet-lantern/scripts/logic/options.py
2025-06-07 19:07:58 +00:00

234 lines
11 KiB
Python

import shutil
import os
import utils
import conf
import logic.lantern_logic as lantern
def run_option_4():
"""
Running option 4: syncing all links from official and registered webrings
"""
try:
utils.print_colors("4) Synchronize new links from new or existing webring participants, into your local csv files")
utils.print_colors('[+] Syncing official webrings to local webrings')
webring_df = utils.get_local_webring_participants()
current_instance = utils.get_current_instance()
utils.print_colors('[+] Reading local blacklist and sensitive words')
local_blacklist_df = utils.get_local_blacklist()
local_sensitive_df = utils.get_local_sensitive()
utils.print_colors('[+] Reading local verified and unverified')
local_verified_df, local_unverified_df = utils.get_local_verified_and_unverified()
#Remove all rows
local_unverified_df = local_unverified_df[0:0]
local_verified_df = local_verified_df[0:0]
for participant in webring_df.itertuples(index=False, name='columns'):
# Check if the participant is my instance
if current_instance in participant:
continue
if participant.Blacklisted == 'YES':
continue
if not utils.is_participant_reachable(participant.URL):
utils.print_colors(f"[-] Webring {participant.URL} isn't reachable, skipping", is_error=True)
continue
utils.print_colors('[+] Downloading participant\'s files to store locally')
lantern.download_participant_data(participant.URL)
participant_url = utils.generate_local_participant_dir(participant.URL)
utils.print_colors('[+] Reading webring participant\'s verified and unverified')
participant_verified_df, participant_unverified_df = utils.get_participant_local_verified_and_unverified(participant_url)
utils.print_colors('[+] Removing unvalidated and blacklisted rows')
participant_verified_df = lantern.clean_csv(participant_verified_df, local_blacklist_df['blacklisted-words'].tolist())
participant_unverified_df = lantern.clean_csv(participant_unverified_df, local_blacklist_df['blacklisted-words'].tolist())
utils.print_colors('[+] Marking sensitive rows')
participant_verified_df = lantern.mark_sensitive(participant_verified_df, local_sensitive_df['sensitive-words'].tolist())
participant_unverified_df = lantern.mark_sensitive(participant_unverified_df, local_sensitive_df['sensitive-words'].tolist())
if participant.Trusted == 'YES':
utils.print_colors('[+] This participant is trusted, copying participant\'s verified to local verified')
local_verified_df = utils.merge_verification_df(local_verified_df, participant_verified_df)
else:
utils.print_colors('[+] This participant is not trusted, copying participant\'s verified to local unverified')
local_unverified_df = utils.merge_verification_df(local_unverified_df, participant_verified_df)
utils.print_colors('[+] Copying participant\'s unverified to local unverified')
local_unverified_df = utils.merge_verification_df(local_unverified_df, participant_unverified_df)
utils.print_colors('[+] Saving local verified and unverified')
utils.save_local_verified_and_unverified(local_verified_df, local_unverified_df)
except Exception as err:
utils.print_colors("[-] Option 4 failed suddenly, please try again", is_error=True)
def run_option_6():
"""
Running option 6: Trusting/Untrusting/Blacklisting a webring participant
"""
while True:
utils.print_colors("[+] Trust/UnTrust/Blacklist a webring participant (Potentially dangerous)")
webring_df = utils.get_local_webring_participants()
webring_path = conf.LOCAL_DIR + conf.WEBRING_CSV_FILE
utils.print_colors(f'{webring_df[["URL","Trusted", "Blacklisted"]]}')
try:
index = int(input('What is the index of the webring participant that you want to edit? -1 to exit ').strip())
if index == -1:
break
if index in webring_df.index:
choice = input('Do you want to 1) Trust, 2) UnTrust, or 3) Blacklist the webring participant?').strip()
utils.print_colors('[+] Reading local verified and unverified')
local_verified_df, local_unverified_df = utils.get_local_verified_and_unverified()
participant_instance = webring_df.iloc[index, webring_df.columns.get_loc("URL")]
match choice:
case '1':
# trust the webring participant
approve=input('You\'re about to trust another peer, this means that you\'re going to automatically trust all of the links they have in their verified.csv file! If this is a malicious peer, this action might be potentially risky! Do you want to continue ? (y/n)')
# to lower case incase someone enters Y instead of y
if approve.lower() == 'y':
try:
utils.print_colors(f'[+] Trusting webring participant {participant_instance}')
webring_df = lantern.mark_webring_participant_trusted(webring_df, index, True)
webring_df = lantern.mark_webring_participant_blacklist(webring_df, participant_instance, index, False)
except Exception as err:
utils.print_colors('[-] Trusting webring participant failed', is_error=True)
else:
utils.print_colors('[-] not trusting webring participant', is_error=True)
case '2':
try:
utils.print_colors(f'[+] Untrusting webring participant {participant_instance}')
webring_df = lantern.mark_webring_participant_trusted(webring_df, index, False)
webring_df = lantern.mark_webring_participant_blacklist(webring_df, participant_instance, index, False)
except Exception as err:
utils.print_colors('[-] Untrusting webring participant failed', is_error=True)
case '3':
try:
utils.print_colors(f'[+] Blacklisting webring participant {participant_instance}')
webring_df = lantern.mark_webring_participant_trusted(webring_df, index, False)
webring_df = lantern.mark_webring_participant_blacklist(webring_df, participant_instance, index, True)
local_blacklist_df = utils.get_local_blacklist()
utils.print_colors('[+] Removing unvalidated and blacklisted rows')
local_verified_df = lantern.clean_csv(local_verified_df, local_blacklist_df['blacklisted-words'].tolist())
local_unverified_df = lantern.clean_csv(local_verified_df, local_blacklist_df['blacklisted-words'].tolist())
participant_dir = f'{conf.PARTICIPANT_DIR}{participant_instance}'
utils.print_colors(f"[+] removing the participant's directory at {participant_dir}")
shutil.rmtree(participant_dir)
except FileNotFoundError as err:
utils.print_colors('[-] File already blacklisted', is_error=True)
except Exception as err:
utils.print_colors('[-] Blacklisting webring participant failed', is_error=True)
utils.save_dataframe(webring_df, webring_path)
utils.print_colors('[+] Saving local verified and unverified')
utils.save_local_verified_and_unverified(local_verified_df, local_unverified_df)
except Exception as err:
utils.print_colors("[-] Option 6 failed suddently, please try again", is_error=True)
def run_option_9():
"""
Running option 9: cleans duplications in local instance verified and unverified csv files
"""
utils.print_colors("[+] 9) Cleaning up all duplicates in your own unverified + verified.csv (based on the url)")
try:
utils.print_colors('[+] Reading local verified and unverified')
verified_df, unverified_df = utils.get_local_verified_and_unverified()
utils.print_colors('[+] Removing cross dataframe replications')
verified_df, unverified_df = utils.remove_cross_dataframe_replications(verified_df, unverified_df)
utils.print_colors('[+] Saving local verified and unverified')
utils.save_local_verified_and_unverified(verified_df, unverified_df)
except Exception as err:
utils.print_colors("[-] Option 9 failed suddenly, please try again", is_error=True)
def run_option_10():
"""
Running option 10: go over all verified and unverified participants csv files
"""
utils.print_colors("[+] 10) perform sanity checks on all csv files (to mark them as sensitive / or remove the ones that are blacklisted)")
try:
utils.print_colors('[+] Reading local blacklist and sensitive words')
local_blacklist_df = utils.get_local_blacklist()
local_sensitive_df = utils.get_local_sensitive()
for participant in os.listdir(conf.PARTICIPANT_DIR):
participant_local_dir = conf.PARTICIPANT_DIR + participant + '/'
if not os.path.exists(f'{participant_local_dir}verified.csv'):
continue
utils.print_colors('[+] Reading webrring participant\'s verified and unverified')
participant_verified_df, participant_unverified_df = utils.get_participant_local_verified_and_unverified(participant_local_dir)
utils.print_colors('[+] Removing unverified and blacklisted rows')
participant_verified_df = lantern.clean_csv(participant_verified_df, local_blacklist_df['blacklisted-words'].tolist())
participant_unverified_df = lantern.clean_csv(participant_unverified_df, local_blacklist_df['blacklisted-words'].tolist())
utils.print_colors('[+] Marking sensitive rows')
participant_verified_df = lantern.mark_sensitive(participant_verified_df, local_sensitive_df['sensitive-words'].tolist())
participant_unverified_df = lantern.mark_sensitive(participant_unverified_df, local_sensitive_df['sensitive-words'].tolist())
utils.print_colors('[+] Saving local participant verified and unverified')
utils.save_local_participant_verified_and_unverified(participant_verified_df, participant_unverified_df, participant_local_dir)
except Exception as err:
utils.print_colors("[-] Option 10 failed suddenly, please try again", is_error=True)