66from __future__ import annotations
77
88import time
9+ import os
10+ import signal
11+ import re
912import subprocess
1013import threading
1114import 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 ))
0 commit comments