mirror of
https://github.com/m4rcel-lol/m5rcode.git
synced 2025-12-06 19:13:57 +05:30
Update cmd_wdir.py
This commit is contained in:
committed by
GitHub
parent
dc95f95e53
commit
f2495a3765
@@ -4,11 +4,15 @@ from urllib.parse import urljoin
|
|||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
def strip_ansi(text):
|
||||||
|
return re.sub(r'\x1b\[[0-9;]*m', '', text)
|
||||||
|
|
||||||
class WdirCommand:
|
class WdirCommand:
|
||||||
def __init__(self, url):
|
def __init__(self, url):
|
||||||
self.url = url.strip()
|
self.url = url.strip()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
box_min = 72
|
||||||
if not self.url:
|
if not self.url:
|
||||||
print(Fore.RED + "Usage: wdir <url>" + Style.RESET_ALL)
|
print(Fore.RED + "Usage: wdir <url>" + Style.RESET_ALL)
|
||||||
return
|
return
|
||||||
@@ -33,52 +37,77 @@ class WdirCommand:
|
|||||||
href = link.get("href")
|
href = link.get("href")
|
||||||
if not href:
|
if not href:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Ignore "parent directory" or anchors
|
|
||||||
if href.startswith("?") or href.startswith("#") or href.startswith("../"):
|
if href.startswith("?") or href.startswith("#") or href.startswith("../"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Detect if it's a folder
|
|
||||||
is_dir = href.endswith("/")
|
is_dir = href.endswith("/")
|
||||||
filename = href.rstrip("/").split("/")[-1]
|
filename = href.rstrip("/").split("/")[-1]
|
||||||
|
|
||||||
# Skip HTML/PHP/ASP/etc. pages that aren’t real hosted files
|
|
||||||
if not is_dir and re.search(r"\.(php|html?|asp|aspx|jsp)$", filename, re.I):
|
if not is_dir and re.search(r"\.(php|html?|asp|aspx|jsp)$", filename, re.I):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Guess type
|
|
||||||
if is_dir:
|
if is_dir:
|
||||||
ftype = "Directory"
|
ftype = "Directory"
|
||||||
elif "." in filename:
|
elif "." in filename:
|
||||||
ftype = f".{filename.split('.')[-1]} file"
|
ftype = f".{filename.split('.')[-1]} file"
|
||||||
else:
|
else:
|
||||||
ftype = "File"
|
ftype = "File"
|
||||||
|
|
||||||
# Try to extract file size + modified date if available in listing row
|
|
||||||
row_text = link.parent.get_text(" ", strip=True)
|
row_text = link.parent.get_text(" ", strip=True)
|
||||||
size_match = re.search(r"(\d+(?:\.\d+)?\s*(?:KB|MB|GB|B))", row_text, re.I)
|
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)
|
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 "-"
|
size = size_match.group(1) if size_match else "-"
|
||||||
modified = date_match.group(1) if date_match else "-"
|
modified = date_match.group(1) if date_match else "-"
|
||||||
|
|
||||||
files.append((filename, ftype, size, modified))
|
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:
|
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
|
return
|
||||||
|
|
||||||
print(Fore.GREEN + "\nFiles found:" + Style.RESET_ALL)
|
# Pretty header
|
||||||
print(Fore.MAGENTA + f"{'Name':<30}{'Type':<15}{'Size':<12}{'Modified':<20}" + Style.RESET_ALL)
|
print(Fore.MAGENTA + "╔" + "═" * (width - 2) + "╗")
|
||||||
print(Fore.MAGENTA + "-" * 77 + Style.RESET_ALL)
|
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:
|
for fname, ftype, size, modified in files:
|
||||||
if ftype == "Directory":
|
if ftype == "Directory":
|
||||||
color = Fore.BLUE
|
color = Fore.BLUE + Style.BRIGHT
|
||||||
elif ftype.endswith("file"):
|
elif ftype.endswith("file"):
|
||||||
color = Fore.CYAN
|
color = Fore.CYAN
|
||||||
else:
|
else:
|
||||||
color = Fore.WHITE
|
color = Fore.WHITE
|
||||||
|
filecol = f"{color}{fname:<{pad[0]}}{Style.RESET_ALL}"
|
||||||
print(f"{color}{fname:<30}{Style.RESET_ALL} "
|
typecol = f"{Style.DIM}{Fore.WHITE}{ftype:<{pad[1]}}{Style.RESET_ALL}"
|
||||||
f"{Fore.WHITE}{ftype:<15}{size:<12}{modified:<20}{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)
|
||||||
|
|||||||
Reference in New Issue
Block a user