FINished adding the trusted and the refactoring of 6

This commit is contained in:
doctor_dev 2025-06-03 16:58:03 +00:00
parent 8b0ba4833f
commit 14231fff92
No known key found for this signature in database
GPG key ID: F12F7F71CB84AEAA
5 changed files with 631 additions and 321 deletions

View file

@ -1,6 +1,7 @@
from utils import *
import logic.lantern_logic as lantern
from dotenv import load_dotenv
import options
import os, pwd
@ -224,6 +225,8 @@ Maintenance:
else:
sensi = 'YES'
#TODO: add blacklisting default to no when refactoring
newrow=[instance,category,name,url,sensi,desc,'YES','100']
print_colors(f"[+] NEWROW= {newrow}")
# (rest is automatic: status, score, instance is = '' because it is your own instance)
@ -529,65 +532,9 @@ Maintenance:
# 6) Trust/UnTrust/Blacklist a webring participant
#####################################################
case 4:
print_colors("4) Synchronize new links from new or existing webring participants, into your local csv files")
try:
print_colors('[+] Syncing official webrings to local webrings')
webring_df = get_local_webring_participants()
current_instance = get_current_instance()
for participant in webring_df.itertuples(index=False, name='columns'):
# Check if the participant is my instance
if current_instance in participant:
continue
if not is_participant_reachable(participant.URL):
print_colors("[-] Webring {participant.URL} isn't reachable, skipping", is_error=True)
continue
print_colors('[+] Downloading participant\'s files to store locally')
lantern.download_participant_data(participant.URL)
print_colors('[+] Reading local blacklist and sensitive words')
local_blacklist, local_sensitive = get_local_blacklist_and_sensitive()
print_colors('[+] Reading local verified and unverified')
local_verified_df, local_unverified_df = get_local_verified_and_unverified()
participant_url = generate_local_participant_dir(participant.URL)
print_colors('[+] Reading webrring participant\'s verified and unverified')
participant_verified_df, participant_unverified_df = get_participant_local_verified_and_unverified(participant_url)
print_colors('[+] Removing unvalidated and blacklisted rows')
participant_verified_df = lantern.clean_csv(participant_verified_df, local_blacklist)
participant_unverified_df = lantern.clean_csv(participant_unverified_df, local_blacklist)
print_colors('[+] Marking sensitive rows')
participant_verified_df = lantern.mark_sensitive(participant_verified_df, local_sensitive)
participant_unverified_df = lantern.mark_sensitive(participant_unverified_df, local_sensitive)
if participant.Trusted == 'YES':
print_colors('[+] This participant is trusted, copying participant\'s verified to local verified')
local_verified_df = merge_verification_df(local_verified_df, participant_verified_df)
else:
print_colors('[+] This participant is not trusted, copying participant\'s verified to local unverified')
local_unverified_df = merge_verification_df(local_unverified_df, participant_verified_df)
print_colors('[+] Copying participant\'s unverified to local unverified')
local_unverified_df = merge_verification_df(local_unverified_df, participant_unverified_df)
print_colors('[+] Saving local verified and unverified')
save_local_verified_and_unverified(local_verified_df, local_unverified_df)
except Exception as err:
print_colors("[-] Option 4 failed suddently, please try again", is_error=True)
options.run_option_4()
break
@ -731,124 +678,10 @@ Maintenance:
##############################################
case 6:
while True:
print_colors("[+] Trust/UnTrust/Blacklist a webring participant (Potentially dangerous)")
webringcsvfile=instancepath+'/'+'webring-participants.csv'
wdf = pd.read_csv(webringcsvfile, on_bad_lines='skip')
print_colors(f'{wdf[["URL","Trusted"]]}')
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
elif index in wdf.index:
choice = int(input("Do you want to 1) Trust, 2) UnTrust, or 3) Blacklist the webring participant?").strip())
while True:
match choice:
case 1:
# trust the webring participant
choice2=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)")
if choice2 == "y":
print_colors(f'[+] Trusting webring participant {wdf.at[index,"URL"]}')
## Warning: In future versions of panda '✔️' will not work. It will show an error.
wdf.at[index,"Trusted"]= 'YES'
wdf.to_csv(webringcsvfile, index=False)
break
else:
print_colors("[-] not trusting webring participant", is_error=True)
break
case 2:
print_colors(f'[+] UnTrusting webring participant {wdf.at[index,"URL"]}')
## Warning: In future versions of panda '' will not work. It will show an error. Maybe change to a 0,1
wdf.at[index,"Trusted"]='NO'
wdf.to_csv(webringcsvfile, index=False)
break
options.run_option_6()
case 3:
print_colors(f'[+] Blacklisting webring participant {wdf.at[index,"URL"]}')
instance2blacklist=wdf.at[index,"URL"]
newrow=[instance2blacklist]
print_colors(f"[+] NEWROW= {newrow}")
# (rest is automatic: status, score, instance is = '' because it is your own instance)
# check if the entry doesn't already exist in verified.csv and in unverified.csv
# if it doesnt exist, add it into unverified.csv
bldf.loc[-1] = newrow # adding a row
bldf.index = bldf.index + 1 # shifting index
bldf = bldf.sort_index() # sorting by index
print_colors("[+] New row added! now writing the csv file:")
bldf.to_csv(blcsvfile, index=False)
# remove all of the entries that came from that participant (drop the lines in your own verified+unverified.csv that have that instance in the instance column)
rows2delete= [] # it is an empty list at first
for i,j in vdf.iterrows():
row=vdf.loc[i,:].values.tolist()
for k,l in bldf.iterrows():
blword=bldf.at[k, 'blacklisted-words']
if any(blword in str(x) for x in row) == True:
if i not in rows2delete:
print_colors(f"Marking row {i} for deletion, as it matches with a blacklisted word")
rows2delete.append(i) #mark the row for deletion if not already done
for i in rows2delete:
row=vdf.loc[i,:].values.tolist()
print_colors(f'[+] REMOVING ROW: {i} {row}')
vdf.drop(i, inplace= True)
vdf.to_csv(verifiedcsvfile, index=False)
print_colors(f"{vdf}")
rows2delete= [] # it is an empty list at first
rows2delete= [] # it is an empty list at first
for i,j in uvdf.iterrows():
row=uvdf.loc[i,:].values.tolist()
for k,l in bldf.iterrows():
blword=bldf.at[k, 'blacklisted-words']
if any(blword in str(x) for x in row) == True:
if i not in rows2delete:
print_colors(f"Marking row {i} for deletion, as it matches with a blacklisted word")
rows2delete.append(i) #mark the row for deletion if not already done
for i in rows2delete:
row=uvdf.loc[i,:].values.tolist()
print_colors(f'[+] REMOVING ROW: {i} {row}')
uvdf.drop(i, inplace= True)
uvdf.to_csv(unverifiedcsvfile, index=False)
print_colors(f"{uvdf}")
rows2delete= [] # it is an empty list at first
# find all rows that match with the instance name in wdf aswell to remove them
for i,j in wdf.iterrows():
row=wdf.loc[i,:].values.tolist()
for k,l in bldf.iterrows():
blword=bldf.at[k, 'blacklisted-words']
if any(blword in str(x) for x in row) == True:
if i not in rows2delete:
print_colors(f"Marking row {i} for deletion, as it matches with a blacklisted word")
rows2delete.append(i) #mark the row for deletion if not already done
for i in rows2delete:
row=wdf.loc[i,:].values.tolist()
print_colors(f'[+] REMOVING ROW: {i} {row}')
wdf.drop(i, inplace= True)
wdf.to_csv(webringcsvfile, index=False)
print_colors(f"{wdf}")
rows2delete= [] # it is an empty list at first
# remove the entire directory in www/participants/INSTANCENAME aswell to get rid of it
instance2blacklistpath=rootpath+'www/participants/'+instance2blacklist
print_colors(f"[+] removing the participant's directory at {instance2blacklistpath}")
shutil.rmtree(instance2blacklistpath)
case _:
break
except Exception:
break
break
@ -1004,50 +837,14 @@ Maintenance:
case 9:
print_colors("[+] 9) Cleaning up all duplicates in your own unverified + verified.csv (based on the url)")
try:
print_colors('[+] Reading local verified and unverified')
verified_df, unverified_df = get_local_verified_and_unverified()
print_colors('[+] Removing cross dataframe replications')
verified_df, unverified_df = remove_cross_dataframe_replications(verified_df, unverified_df)
print_colors('[+] Saving local verified and unverified')
save_local_verified_and_unverified(verified_df, unverified_df)
except Exception as err:
print_colors("[-] Option 9 failed suddently, please try again", is_error=True)
options.run_option_9()
break
case 10:
print_colors("[+] 10) perform sanity checks on all csv files (to mark them as sensitive / or remove the ones that are blacklisted)")
try:
print_colors('[+] Reading local blacklist and sensitive words')
local_blacklist, local_sensitive = get_local_blacklist_and_sensitive()
for participant in os.listdir(conf.PARTICIPANT_DIR):
participant_local_dir = conf.PARTICIPANT_DIR + participant + '/'
print_colors('[+] Reading webrring participant\'s verified and unverified')
participant_verified_df, participant_unverified_df = get_participant_local_verified_and_unverified(participant_local_dir)
print_colors('[+] Removing unverified and blacklisted rows')
participant_verified_df = lantern.clean_csv(participant_verified_df, local_blacklist)
participant_unverified_df = lantern.clean_csv(participant_unverified_df, local_blacklist)
print_colors('[+] Marking sensitive rows')
participant_verified_df = lantern.mark_sensitive(participant_verified_df, local_sensitive)
participant_unverified_df = lantern.mark_sensitive(participant_unverified_df, local_sensitive)
print_colors('[+] Saving local participant verified and unverified')
save_local_participant_verified_and_unverified(participant_verified_df, participant_unverified_df, participant_local_dir)
except Exception as err:
print_colors("[-] Option 10 failed suddently, please try again", is_error=True)
options.run_option_10()
break