forked from uniflex/module_wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_sniffer.py
79 lines (60 loc) · 2.1 KB
/
packet_sniffer.py
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
import time
import pyshark
import trollius as asyncio # for pyshark comatibility
import threading
__author__ = "Piotr Gawlowicz"
__copyright__ = "Copyright (c) 2015, Technische Universität Berlin"
__version__ = "0.1.0"
__email__ = "{gawlowicz}@tkn.tu-berlin.de"
class Sink(object):
def __init__(self, callback=None):
self.callback = callback
def recv(self, packet):
pass
class RssiSink(Sink):
def __init__(self, callback=None):
super().__init__(callback)
def recv(self, packet):
if 'radiotap' in packet and 'wlan' in packet:
rssi = getattr(packet['radiotap'], 'dbm_antsignal', None)
ta = getattr(packet['wlan'], 'ta_resolved', None)
if rssi and ta:
if self.callback:
self.callback(str(ta), float(rssi))
class PacketSnifferPyShark():
"""docstring for PacketSnifferPyShark"""
def __init__(self, iface):
super().__init__()
self.snifferThread = None
self.iface = iface
self.running = False
self._mySinks = []
def start(self):
self.running = True
self.snifferThread = threading.Thread(target=self.worker,
name="RssiSnifferPyShark")
self.snifferThread.setDaemon(True)
self.snifferThread.start()
@asyncio.coroutine
def handler(self):
capture = pyshark.LiveCapture(interface=self.iface)
while self.running:
if not self._mySinks:
time.sleep(1)
for packet in capture.sniff_continuously():
if not self._mySinks:
break
for sink in self._mySinks:
sink.recv(packet)
def worker(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.handler())
loop.run_forever()
def stop(self):
self.running = False
def add_sink(self, sink):
self._mySinks.append(sink)
def remove_sink(self, sink):
if sink in self._mySinks:
self._mySinks.remove(sink)