|
12 | 12 |
|
13 | 13 | """
|
14 | 14 |
|
15 |
| -import os |
| 15 | +import tempfile |
16 | 16 | import time
|
17 | 17 |
|
| 18 | +import httpx |
| 19 | +import rich |
18 | 20 | from docopt import docopt
|
19 | 21 | from redis import Redis
|
| 22 | +from rich import print |
| 23 | +from rich.progress import MofNCompleteColumn, Progress, SpinnerColumn |
20 | 24 |
|
21 | 25 | from src.common import PasswordHasher, PasswordStorage, Settings
|
22 | 26 |
|
| 27 | +ROCKYOU_DOWNLOAD_URL = "https://github.com/brannondorsey/naive-hashcat/releases/download/data/rockyou.txt" |
23 | 28 |
|
24 |
| -def init_db(file_path: str, db_client: Redis) -> int: |
| 29 | + |
| 30 | +def init_db(file_path: str, db_client: Redis, password_count: int) -> int: |
25 | 31 | """Return the number of password inserted in db"""
|
26 |
| - print("") # allow print below to clear this line return |
| 32 | + processed = 0 |
27 | 33 | hasher = PasswordHasher()
|
28 | 34 | storage = PasswordStorage(client=db_client)
|
29 |
| - processed = 0 |
30 |
| - with open(file_path, encoding="latin-1") as file: |
31 |
| - for line in file: |
32 |
| - password = line.strip() |
33 |
| - prefix = hasher.prefix(password) |
34 |
| - storage.add_password(prefix=prefix, password=password) |
35 |
| - processed += 1 |
36 |
| - if processed % 100 == 0: |
37 |
| - print(f"\r{processed=}", end="") |
| 35 | + |
| 36 | + with Progress( |
| 37 | + SpinnerColumn(), |
| 38 | + *Progress.get_default_columns(), |
| 39 | + MofNCompleteColumn(), |
| 40 | + ) as progress: |
| 41 | + rich_task = progress.add_task("[green]Inserting in database...", total=password_count) |
| 42 | + # Actual work |
| 43 | + with open(file_path, encoding="latin-1") as file: |
| 44 | + for line in file: |
| 45 | + password = line.strip() |
| 46 | + prefix = hasher.prefix(password) |
| 47 | + storage.add_password(prefix=prefix, password=password) |
| 48 | + |
| 49 | + processed += 1 |
| 50 | + progress.update(rich_task, advance=1) |
| 51 | + |
38 | 52 | storage.flush()
|
39 |
| - print("\ndone") |
| 53 | + print("[green]done") |
40 | 54 | return processed
|
41 | 55 |
|
42 | 56 |
|
43 | 57 | if __name__ == "__main__":
|
| 58 | + start = time.time() |
44 | 59 | args = docopt(__doc__)
|
45 | 60 | if args.get("--download"):
|
46 |
| - print("not implemented yet") |
47 |
| - exit(0) |
| 61 | + print("Initializing DB with rockyou passwords from internet") |
| 62 | + # Create a temporary file that is deleted automatically on program close |
| 63 | + download_file = tempfile.NamedTemporaryFile(delete=True, delete_on_close=False) |
| 64 | + with httpx.stream("GET", ROCKYOU_DOWNLOAD_URL, follow_redirects=True) as r: |
| 65 | + for data in r.iter_bytes(): |
| 66 | + download_file.write(data) |
| 67 | + download_file.close() |
| 68 | + print("download complete") |
| 69 | + password_path = download_file.name |
48 | 70 | elif password_path := args.get("<passwords-path>"):
|
49 | 71 | print(f"Initializing db with {password_path}")
|
50 |
| - start = time.time() |
51 |
| - settings = Settings() |
52 |
| - redis_client = Redis.from_url(str(settings.kvrocks_url)) |
53 |
| - init_db(password_path, db_client=redis_client) |
54 |
| - duration = time.time() - start |
55 |
| - print(f"Took {duration:.2f}s") |
56 |
| - # else docopt will show help |
| 72 | + else: |
| 73 | + print("Missing arguments") |
| 74 | + exit(1) |
| 75 | + |
| 76 | + settings = Settings() |
| 77 | + password_count = 0 |
| 78 | + with rich.progress.open( |
| 79 | + password_path, encoding="latin-1", description="[orange3]Determining password count...", transient=True, |
| 80 | + ) as file: |
| 81 | + for line in file: |
| 82 | + password_count += 1 |
| 83 | + |
| 84 | + redis_client = Redis.from_url(str(settings.kvrocks_url)) |
| 85 | + init_db(password_path, db_client=redis_client, password_count=password_count) |
| 86 | + duration = time.time() - start |
| 87 | + print(f"Took {duration:.2f}s") |
0 commit comments