-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
178 lines (147 loc) · 5.89 KB
/
main.py
File metadata and controls
178 lines (147 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
"""
main.py
GUI control for the DarkNetCrawler, using PostgreSQL and Tkinter.
"""
import os
import sys
import threading
import signal
import tkinter as tk
from queue import Queue, Empty
import builtins
import psycopg2
from config import DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS, THREADS
from crawler import run_crawler, shutdown_event, ignore_robots_and_tos
from database import setup_schema
import Crawled_Urls # runs verify_or_rotate() on import
# ─── Override built-in print to also push logs into the GUI queue ────────────
log_queue = Queue()
original_print = builtins.print
def print(*args, **kwargs):
text = " ".join(str(a) for a in args)
original_print(text, **kwargs)
log_queue.put(text)
# ─── Postgres connection helper ──────────────────────────────────────────────
def get_pg_connection():
"""Return a new psycopg2 connection to Postgres."""
return psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME,
user=DB_USER,
password=DB_PASS
)
# ─── GUI helper functions ─────────────────────────────────────────────────────
def load_seeds(seed_path="seeds.txt"):
seeds = []
try:
with open(seed_path, "r") as f:
for raw in f:
line = raw.strip()
if not line or line.startswith("#"):
continue
seeds.append(line)
# Deduplicate seeds
seeds = list(dict.fromkeys(seeds))
except FileNotFoundError:
print(f"[WARN] {seed_path} not found. No seeds loaded.")
return seeds
# ─── GUI CALLBACKS ─────────────────────────────────────────────────────────────
crawler_thread = None
def on_start_button():
global crawler_thread
start_btn.config(state=tk.DISABLED)
stop_btn.config(state=tk.NORMAL)
print("[GUI] Starting crawler…")
shutdown_event.clear()
def target():
# Test Postgres connectivity
try:
pg = get_pg_connection()
print(f"[✔] Successfully connected to PostgreSQL at {DB_HOST}:{DB_PORT}")
pg.close()
except Exception as e:
print(f"[✖] Failed to connect to PostgreSQL: {e}")
sys.exit(1)
print(f"[*] Using {THREADS} threads (from config.py)")
initial_seeds = load_seeds(seed_path="seeds.txt")
if not initial_seeds:
print("[ERROR] No seeds found in seeds.txt. Exiting crawl.")
return
print("[*] Starting crawler with seeds:")
for s in initial_seeds:
print(f" {s}")
# Kick off the crawl
run_crawler(initial_seeds, max_threads=THREADS)
print("[GUI] Crawler thread has finished.")
start_btn.config(state=tk.NORMAL)
stop_btn.config(state=tk.DISABLED)
crawler_thread = threading.Thread(target=target, daemon=True)
crawler_thread.start()
def on_stop_button():
stop_btn.config(state=tk.DISABLED)
print("[GUI] Stop requested. Waiting for crawler to finish current batch...")
shutdown_event.set()
def toggle_robots():
if robots_var.get() == 1:
print("[GUI] Now respecting robots.txt")
else:
ignore_robots_and_tos()
print("[GUI] Now IGNORING robots.txt")
def poll_log_queue():
while True:
try:
line = log_queue.get(block=False)
except Empty:
break
text_widget.configure(state=tk.NORMAL)
text_widget.insert(tk.END, line + "\n")
text_widget.configure(state=tk.DISABLED)
text_widget.yview(tk.END)
root.after(200, poll_log_queue)
# ─── Signal Handling ──────────────────────────────────────────────────────────
def handle_shutdown(signum, frame):
print("[*] Shutting down gracefully...")
shutdown_event.set()
root.quit()
signal.signal(signal.SIGINT, handle_shutdown)
signal.signal(signal.SIGTERM, handle_shutdown)
# ─── MAIN ──────────────────────────────────────────────────────────────────────
if __name__ == "__main__":
# Ensure PostgreSQL schema
setup_schema()
# Build and start the GUI
root = tk.Tk()
root.title("DarkNetCrawler Control")
root.geometry("800x600")
controls_frame = tk.Frame(root)
controls_frame.pack(fill=tk.X, padx=10, pady=5)
start_btn = tk.Button(
controls_frame, text="START", fg="white", bg="green",
width=10, command=on_start_button
)
start_btn.pack(side=tk.LEFT, padx=(0, 10))
stop_btn = tk.Button(
controls_frame, text="STOP", fg="white", bg="red",
width=10, command=on_stop_button, state=tk.DISABLED
)
stop_btn.pack(side=tk.LEFT)
robots_var = tk.IntVar(value=1)
robots_check = tk.Checkbutton(
controls_frame,
text="Respect robots.txt",
variable=robots_var,
command=toggle_robots
)
robots_check.pack(side=tk.LEFT, padx=(20, 0))
log_frame = tk.Frame(root)
log_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=(0, 10))
text_widget = tk.Text(log_frame, wrap=tk.WORD, state=tk.DISABLED)
text_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar = tk.Scrollbar(log_frame, command=text_widget.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_widget.config(yscrollcommand=scrollbar.set)
root.after(200, poll_log_queue)
root.mainloop()
print("[*] Program exit.")