Skip to content

Commit f2ce6a4

Browse files
committed
feat(real-backends): replace all mock logic with real functional engines
1 parent c8f5ae1 commit f2ce6a4

6 files changed

Lines changed: 161 additions & 46 deletions

File tree

‎bigbox/hashopolis.py‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,34 @@ def upload_hash(pcapng_path: Path) -> bool:
2727
return False
2828

2929
try:
30-
# Note: This is a placeholder for the specific Hashopolis / Hashtopolis API
31-
# Typically involves an upload to a task or a pre-configured VOUCHER
32-
files = {'file': open(pcapng_path, 'rb')}
33-
headers = {'X-API-KEY': config.api_key}
34-
response = requests.post(f"{config.api_url}/api/v1/hashes", files=files, headers=headers, timeout=30)
35-
return response.status_code == 200
36-
except Exception:
30+
# Hashtopolis v1 API uses 'hashes' endpoint with specific task IDs or a VOUCHER
31+
# If we have a 'task_id' in config, we use that.
32+
url = f"{config.api_url}/api/v1/hashes"
33+
34+
# Determine hash type based on extension
35+
hash_type = 22000 if pcapng_path.suffix == ".hc22000" else 2500
36+
37+
data = {
38+
"hashTypeId": hash_type,
39+
"name": pcapng_path.name,
40+
}
41+
42+
files = {
43+
'file': (pcapng_path.name, pcapng_path.open('rb'), 'application/octet-stream')
44+
}
45+
46+
headers = {
47+
'X-API-KEY': config.api_key,
48+
'Accept': 'application/json'
49+
}
50+
51+
response = requests.post(url, data=data, files=files, headers=headers, timeout=60)
52+
53+
if response.status_code == 200:
54+
return True
55+
else:
56+
print(f"[hashopolis] upload failed: {response.status_code} - {response.text}")
57+
return False
58+
except Exception as e:
59+
print(f"[hashopolis] error: {e}")
3760
return False

‎bigbox/ui/adsb.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77

88
import time
9+
import threading
910
from dataclasses import dataclass
1011

1112
import pygame
@@ -49,14 +50,25 @@ def handle(self, ev: ButtonEvent, ctx: SectionContext) -> None:
4950
if self.sdr.start():
5051
self.running = True
5152
self.status_msg = "Listening for aircraft..."
53+
# Start reader thread
54+
threading.Thread(target=self._reader, daemon=True).start()
5255
else:
5356
self.status_msg = "Error: dump1090 not found"
5457
else:
5558
self.sdr.stop()
5659
self.running = False
5760
self.status_msg = "Stopped"
5861

62+
def _reader(self) -> None:
63+
while self.running and not self.dismissed:
64+
line = self.sdr.read_line()
65+
if line:
66+
self._parse_line(line)
67+
else:
68+
time.sleep(0.1)
69+
5970
def _shutdown(self) -> None:
71+
self.running = False
6072
self.sdr.stop()
6173
self.dismissed = True
6274

‎bigbox/ui/foxhunter.py‎

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from __future__ import annotations
77

88
import time
9+
import os
10+
import signal
11+
import re
912
import subprocess
1013
import threading
1114
import array
@@ -26,6 +29,8 @@ def __init__(self, target_mac: str, target_type: str = "WIFI") -> None:
2629
self.rssi_history = deque(maxlen=50)
2730
self.current_rssi = -100
2831
self.last_seen = 0.0
32+
self.status_msg = "Starting..."
33+
self.mon_iface = "wlan0mon"
2934
self._stop = False
3035
self._thread: threading.Thread | None = None
3136

@@ -44,21 +49,74 @@ def _start_scan(self) -> None:
4449

4550
def _worker(self) -> None:
4651
if self.type == "WIFI":
47-
# Targeted tshark or airodump-ng is best, but let's use a simpler loop
48-
# with `iw` or `airodump-ng` if available.
49-
# For simplicity, we'll use airodump-ng in a subprocess and parse stdout.
50-
iface = "wlan0mon" # Assuming monitor mode is already enabled by caller or hardware helper
51-
cmd = ["airodump-ng", "--berlin", "2", iface]
52-
# Actually, airodump-ng is hard to parse from stdout.
53-
# Let's use `tcpdump` or `tshark` if possible, or just poll `iw`.
54-
while not self._stop:
55-
# Mocking logic for now; in reality we'd parse real signal
56-
# For Wi-Fi, we'd use:
57-
# tcpdump -i wlan0mon -e -s 0 -l type mgt subtype probe-req or type mgt subtype beacon
58-
time.sleep(1)
52+
# Targeted signal sniffer using tcpdump
53+
# -e: show link-level headers (radiotap)
54+
# -s 0: capture full packet
55+
# -l: line buffered
56+
# -n: don't resolve names
57+
# Filter: match the target MAC in any address field
58+
cmd = [
59+
"tcpdump", "-i", self.mon_iface, "-e", "-s", "256", "-l", "-n",
60+
f"ether host {self.mac}"
61+
]
62+
try:
63+
proc = subprocess.Popen(
64+
cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
65+
text=True, preexec_fn=os.setsid
66+
)
67+
self.status_msg = f"Hunting on {self.mon_iface}..."
68+
69+
# Regex to find signal in Radiotap header: " -75dBm signal" or " 75% signal"
70+
# Different drivers show it differently. Most show -XXdBm.
71+
sig_re = re.compile(r"(-?\d+)dBm")
72+
73+
for line in proc.stdout:
74+
if self._stop: break
75+
match = sig_re.search(line)
76+
if match:
77+
val = int(match.group(1))
78+
self.current_rssi = val
79+
self.last_seen = time.time()
80+
self.rssi_history.append(val)
81+
82+
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
83+
except Exception as e:
84+
self.status_msg = f"Error: {e}"
5985
else:
60-
# Bluetooth: btmon or hcitool rssi
61-
pass
86+
# Bluetooth hunting: use btmon to watch for ADV packets (passive).
87+
cmd = ["btmon"]
88+
try:
89+
proc = subprocess.Popen(
90+
cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
91+
text=True, preexec_fn=os.setsid
92+
)
93+
94+
# bluetoothctl scan must be ON for btmon to see things reliably
95+
subprocess.run(["bluetoothctl", "scan", "le", "on"],
96+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
97+
98+
target_upper = self.mac.upper()
99+
rssi_re = re.compile(r"RSSI: (-?\d+)")
100+
101+
capture_next_rssi = False
102+
for line in proc.stdout:
103+
if self._stop: break
104+
105+
if target_upper in line:
106+
capture_next_rssi = True
107+
continue
108+
109+
if capture_next_rssi:
110+
m = rssi_re.search(line)
111+
if m:
112+
val = int(m.group(1))
113+
self.current_rssi = val
114+
self.last_seen = time.time()
115+
self.rssi_history.append(val)
116+
capture_next_rssi = False
117+
118+
os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
119+
except Exception: pass
62120

63121
def handle(self, ev: ButtonEvent, ctx: SectionContext) -> None:
64122
if not ev.pressed:
@@ -155,3 +213,7 @@ def render(self, surf: pygame.Surface) -> None:
155213
# Hint
156214
hint = self.f_main.render("B: BACK (move around to find peak)", True, theme.FG_DIM)
157215
surf.blit(hint, (theme.SCREEN_W // 2 - hint.get_width() // 2, theme.SCREEN_H - 40))
216+
217+
# Status
218+
status = self.f_main.render(self.status_msg, True, theme.ACCENT)
219+
surf.blit(status, (20, theme.SCREEN_H - 40))

‎bigbox/ui/pager.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import annotations
66

77
import time
8+
import threading
89
from collections import deque
910

1011
import pygame
@@ -37,14 +38,24 @@ def handle(self, ev: ButtonEvent, ctx: SectionContext) -> None:
3738
if self.sdr.start():
3839
self.running = True
3940
self.status_msg = "Listening on 152.0MHz..."
41+
threading.Thread(target=self._reader, daemon=True).start()
4042
else:
4143
self.status_msg = "Error: rtl_fm or multimon-ng missing"
4244
else:
4345
self.sdr.stop()
4446
self.running = False
4547
self.status_msg = "Stopped"
4648

49+
def _reader(self) -> None:
50+
while self.running and not self.dismissed:
51+
line = self.sdr.read_line()
52+
if line:
53+
self.messages.append(line)
54+
else:
55+
time.sleep(0.1)
56+
4757
def _shutdown(self) -> None:
58+
self.running = False
4859
self.sdr.stop()
4960
self.dismissed = True
5061

‎bigbox/ui/signal_scraper.py‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,8 @@ def _create_grid_bg(self) -> pygame.Surface:
104104
return s
105105

106106
def _get_manufacturer(self, mac: str) -> str:
107-
prefix = mac.lower().replace(":", "")[:6]
108-
ouis = {
109-
"4c7403": "Apple", "002500": "Apple", "f01898": "Apple",
110-
"d8bb2c": "Apple", "ac3c0b": "Apple", "64a769": "Apple",
111-
"0017f2": "Apple", "24a074": "Apple", "001c23": "Apple",
112-
"3c5ab4": "Google", "d8eb97": "Google", "705a0f": "Google",
113-
"001a11": "Google", "f4f5d8": "Google/Nest",
114-
"24f5a2": "Samsung", "508569": "Samsung", "948bc1": "Samsung",
115-
"00234a": "Tesla", "404cc1": "Tesla",
116-
"00044b": "NVIDIA", "b827eb": "RaspberryPi", "dca632": "RaspberryPi",
117-
"e45f01": "RaspberryPi", "000c29": "VMware", "005056": "VMware",
118-
"00155d": "Microsoft", "0003ff": "Microsoft",
119-
}
120-
return ouis.get(prefix, "Unknown Vendor")
107+
vendor, _klass = oui.lookup(mac)
108+
return vendor or "Unknown Vendor"
121109

122110
def _start_scanners(self):
123111
self._stop = False

‎bigbox/ui/wardrive.py‎

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ def _start_capture(self) -> None:
255255

256256
# Start one thread per interface
257257
self._wifi_threads = []
258-
for iface in self.ifaces:
259-
t = threading.Thread(target=self._wifi_loop, args=(iface,), daemon=True)
258+
for i, iface in enumerate(self.ifaces):
259+
t = threading.Thread(target=self._wifi_loop, args=(iface, i), daemon=True)
260260
t.start()
261261
self._wifi_threads.append(t)
262262

@@ -444,14 +444,28 @@ def _record(self, obs: _Observation) -> None:
444444
))
445445

446446
# ---------- scan loops ----------
447-
def _wifi_loop(self, iface: str) -> None:
447+
def _wifi_loop(self, iface: str, index: int) -> None:
448+
# 14 channels in 2.4GHz, ~25 in 5GHz.
449+
# If we have 2 adapters, one does 2.4GHz, other does 5GHz.
450+
# Frequencies in MHz
451+
freqs_24 = [2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484]
452+
# 5GHz (Subset of common channels)
453+
freqs_5 = [5180, 5200, 5220, 5240, 5260, 5280, 5300, 5320, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680, 5700, 5745, 5765, 5785, 5805, 5825]
454+
448455
while not self._stop:
449456
try:
457+
cmd = ["iw", "dev", iface, "scan"]
458+
if len(self.ifaces) > 1:
459+
# Split frequencies
460+
target_freqs = freqs_24 if index == 0 else freqs_5
461+
cmd.append("freq")
462+
cmd.extend(map(str, target_freqs))
463+
450464
proc = subprocess.run(
451-
["iw", "dev", iface, "scan"],
465+
cmd,
452466
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
453467
stdin=subprocess.DEVNULL,
454-
text=True, timeout=15,
468+
text=True, timeout=30,
455469
)
456470
if proc.returncode == 0:
457471
for o in _parse_iw_scan(proc.stdout):
@@ -467,9 +481,6 @@ def _wifi_loop(self, iface: str) -> None:
467481

468482
def _bt_loop(self) -> None:
469483
# Start a persistent `scan le on` so the controller keeps probing.
470-
# `select <hci>` over stdin pins this session to the chosen
471-
# controller (UB500 if present); one-shot `bluetoothctl devices`
472-
# below picks up the same default that ensure_bluetooth_on() set.
473484
try:
474485
self._bt_proc = subprocess.Popen(
475486
["bluetoothctl"],
@@ -479,15 +490,22 @@ def _bt_loop(self) -> None:
479490
text=True,
480491
)
481492
assert self._bt_proc.stdin is not None
482-
if self.bt_hci:
483-
self._bt_proc.stdin.write(f"select {self.bt_hci}\n")
493+
if self.locked_hci: # Use the locked controller
494+
self._bt_proc.stdin.write(f"select {self.locked_hci}\n")
484495
self._bt_proc.stdin.write("scan le on\n")
485496
self._bt_proc.stdin.flush()
486497
except Exception:
487498
self._bt_proc = None
488499

489500
while not self._stop:
490501
try:
502+
# Power efficiency: check battery
503+
from bigbox import power
504+
bat = power.battery()
505+
interval = BT_SCAN_INTERVAL
506+
if bat and bat.percent < 15:
507+
interval *= 2 # Slow down BT to save power
508+
491509
proc = subprocess.run(
492510
["bluetoothctl", "devices"],
493511
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
@@ -501,8 +519,9 @@ def _bt_loop(self) -> None:
501519
self._bt_scan_count += 1
502520
except Exception:
503521
pass
522+
# Sleep but stay responsive to stop
504523
t = time.time()
505-
while time.time() - t < BT_SCAN_INTERVAL and not self._stop:
524+
while time.time() - t < interval and not self._stop:
506525
time.sleep(0.1)
507526

508527
# ---------- input ----------

0 commit comments

Comments
 (0)