Update cmd_wdir.py

This commit is contained in:
m5rcel { Marcel }
2025-10-04 11:50:20 +02:00
committed by GitHub
parent dc95f95e53
commit f2495a3765

View File

@@ -4,11 +4,15 @@ from urllib.parse import urljoin
from colorama import Fore, Style
import re
def strip_ansi(text):
return re.sub(r'\x1b\[[0-9;]*m', '', text)
class WdirCommand:
def __init__(self, url):
self.url = url.strip()
def run(self):
box_min = 72
if not self.url:
print(Fore.RED + "Usage: wdir <url>" + Style.RESET_ALL)
return
@@ -33,52 +37,77 @@ class WdirCommand:
href = link.get("href")
if not href:
continue
# Ignore "parent directory" or anchors
if href.startswith("?") or href.startswith("#") or href.startswith("../"):
continue
# Detect if it's a folder
is_dir = href.endswith("/")
filename = href.rstrip("/").split("/")[-1]
# Skip HTML/PHP/ASP/etc. pages that arent real hosted files
if not is_dir and re.search(r"\.(php|html?|asp|aspx|jsp)$", filename, re.I):
continue
# Guess type
if is_dir:
ftype = "Directory"
elif "." in filename:
ftype = f".{filename.split('.')[-1]} file"
else:
ftype = "File"
# Try to extract file size + modified date if available in listing row
row_text = link.parent.get_text(" ", strip=True)
size_match = re.search(r"(\d+(?:\.\d+)?\s*(?:KB|MB|GB|B))", row_text, re.I)
date_match = re.search(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2})", row_text)
size = size_match.group(1) if size_match else "-"
modified = date_match.group(1) if date_match else "-"
files.append((filename, ftype, size, modified))
# Calculate column widths for nice boxed output
col_names = ["Name", "Type", "Size", "Modified"]
pad = [30, 15, 12, 20]
table_width = sum(pad) + len(pad) + 1 # columns + spaces + box
width = max(box_min, table_width + 2)
# If no files, pretty box saying so
if not files:
print(Fore.YELLOW + "No files or directories found (maybe directory listing is disabled)." + Style.RESET_ALL)
print(Fore.MAGENTA + "" + "" * (width - 2) + "")
out = "No files or directories found (maybe directory listing is disabled)."
out = out.center(width - 2)
print(Fore.MAGENTA + "" + Fore.YELLOW + out + Fore.MAGENTA + "")
print(Fore.MAGENTA + "" + "" * (width - 2) + "" + Style.RESET_ALL)
return
print(Fore.GREEN + "\nFiles found:" + Style.RESET_ALL)
print(Fore.MAGENTA + f"{'Name':<30}{'Type':<15}{'Size':<12}{'Modified':<20}" + Style.RESET_ALL)
print(Fore.MAGENTA + "-" * 77 + Style.RESET_ALL)
# Pretty header
print(Fore.MAGENTA + "" + "" * (width - 2) + "")
title = "Web Directory Listing"
print(Fore.MAGENTA + "" + Fore.CYAN + Style.BRIGHT + title.center(width - 2) + Fore.MAGENTA + "")
print(Fore.MAGENTA + "" + "" * (width - 2) + "")
# Table header
header = (
Fore.LIGHTMAGENTA_EX
+ f"{col_names[0]:<{pad[0]}} {col_names[1]:<{pad[1]}} {col_names[2]:<{pad[2]}} {col_names[3]:<{pad[3]}}"
+ Style.RESET_ALL
)
print(
Fore.MAGENTA + ""
+ header
+ " " * (width - 2 - len(strip_ansi(header)))
+ Fore.MAGENTA + ""
)
print(Fore.MAGENTA + "" + "" * (width - 2) + "")
# Table rows
for fname, ftype, size, modified in files:
if ftype == "Directory":
color = Fore.BLUE
color = Fore.BLUE + Style.BRIGHT
elif ftype.endswith("file"):
color = Fore.CYAN
else:
color = Fore.WHITE
print(f"{color}{fname:<30}{Style.RESET_ALL} "
f"{Fore.WHITE}{ftype:<15}{size:<12}{modified:<20}{Style.RESET_ALL}")
filecol = f"{color}{fname:<{pad[0]}}{Style.RESET_ALL}"
typecol = f"{Style.DIM}{Fore.WHITE}{ftype:<{pad[1]}}{Style.RESET_ALL}"
sizecol = f"{Style.DIM}{Fore.LIGHTWHITE_EX}{size:<{pad[2]}}{Style.RESET_ALL}"
modcol = f"{Style.DIM}{Fore.LIGHTWHITE_EX}{modified:<{pad[3]}}{Style.RESET_ALL}"
row = f"{filecol} {typecol} {sizecol} {modcol}"
print(
Fore.MAGENTA + ""
+ row
+ " " * (width - 2 - len(strip_ansi(row)))
+ Fore.MAGENTA + ""
)
# Footer
print(Fore.MAGENTA + "" + "" * (width - 2) + "" + Style.RESET_ALL)