-
Notifications
You must be signed in to change notification settings - Fork 9
/
gpsd.py
166 lines (152 loc) · 5.31 KB
/
gpsd.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
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
# Based on the GPS plugin from https://github.com/evilsocket
#
# Requires https://github.com/MartijnBraam/gpsd-py3
import json
import logging
import pwnagotchi.plugins as plugins
import pwnagotchi.ui.fonts as fonts
from pwnagotchi.ui.components import LabeledValue
from pwnagotchi.ui.view import BLACK
import pwnagotchi
import gpsd
class GPSD:
def __init__(self, gpsdhost, gpsdport):
gpsd.connect(host=gpsdhost, port=gpsdport)
self.running = True
self.coords = {
"Latitude": None,
"Longitude": None,
"Altitude": None,
"Date": None
}
def update_gps(self):
if self.running:
packet = gpsd.get_current()
if packet.mode >= 2:
self.coords = {
"Latitude": packet.lat,
"Longitude": packet.lon,
"Altitude": packet.alt if packet.mode > 2 else None,
"Date": packet.time
}
return self.coords
class gpsd_coord(plugins.Plugin):
__author__ = "[email protected]"
__version__ = "1.0.0"
__license__ = "GPL3"
__description__ = "Talk to GPSD and save coordinates whenever a handshake is captured."
def __init__(self):
self.gpsd = None
def on_loaded(self):
self.gpsd = GPSD(self.options['gpsdhost'], self.options['gpsdport'])
logging.info("[gpsd] plugin loaded")
def on_ready(self, agent):
if (self.options["gpsdhost"]):
logging.info(f"enabling bettercap's gps module for {self.options['gpsdhost']}:{self.options['gpsdport']}")
try:
agent.run("gps off")
except Exception:
logging.info(f"bettercap gps was already off")
pass
agent.run("set gps.device 127.0.0.1:2947; set gps.baudrate 9600; gps on")
logging.info("bettercap set and on")
self.running = True
else:
logging.warning("no GPS detected")
def on_handshake(self, agent, filename, access_point, client_station):
coords = self.gpsd.update_gps()
if coords and all([
# avoid 0.000... measurements
coords["Latitude"], coords["Longitude"]
]):
gps_filename = filename.replace(".pcap", ".gps.json")
logging.info(f"[gpsd] saving GPS to {gps_filename} ({coords})")
with open(gps_filename, "w+t") as fp:
json.dump(coords, fp)
else:
logging.info("[gpsd] not saving GPS: no fix")
def on_ui_setup(self, ui):
# add coordinates for other displays
if ui.is_waveshare_v2():
lat_pos = (127, 75)
lon_pos = (122, 84)
alt_pos = (127, 94)
elif ui.is_waveshare_v1():
lat_pos = (130, 70)
lon_pos = (125, 80)
alt_pos = (130, 90)
elif ui.is_inky():
lat_pos = (127, 60)
lon_pos = (127, 70)
alt_pos = (127, 80)
elif ui.is_waveshare144lcd():
# guessed values, add tested ones if you can
lat_pos = (67, 73)
lon_pos = (62, 83)
alt_pos = (67, 93)
elif ui.is_dfrobot_v2:
lat_pos = (127, 75)
lon_pos = (122, 84)
alt_pos = (127, 94)
elif ui.is_waveshare27inch():
lat_pos = (6,120)
lon_pos = (1,135)
alt_pos = (6,150)
else:
# guessed values, add tested ones if you can
lat_pos = (127, 51)
lon_pos = (127, 56)
alt_pos = (102, 71)
label_spacing = 0
ui.add_element(
"latitude",
LabeledValue(
color=BLACK,
label="lat:",
value="-",
position=lat_pos,
label_font=fonts.Small,
text_font=fonts.Small,
label_spacing=label_spacing,
),
)
ui.add_element(
"longitude",
LabeledValue(
color=BLACK,
label="long:",
value="-",
position=lon_pos,
label_font=fonts.Small,
text_font=fonts.Small,
label_spacing=label_spacing,
),
)
ui.add_element(
"altitude",
LabeledValue(
color=BLACK,
label="alt:",
value="-",
position=alt_pos,
label_font=fonts.Small,
text_font=fonts.Small,
label_spacing=label_spacing,
),
)
def on_unload(self, ui):
with ui._lock:
ui.remove_element('latitude')
ui.remove_element('longitude')
ui.remove_element('altitude')
def on_ui_update(self, ui):
coords = self.gpsd.update_gps()
if coords and all([
# avoid 0.000... measurements
coords["Latitude"], coords["Longitude"]
]):
# last char is sometimes not completely drawn
# using an ending-whitespace as workaround on each line
ui.set("latitude", f"{coords['Latitude']:.4f} ")
ui.set("longitude", f" {coords['Longitude']:.4f} ")
ui.set("altitude", f" {coords['Altitude']:.1f}m ")