-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdump_packets.py
244 lines (198 loc) · 6.79 KB
/
dump_packets.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import asyncio
import logging
import sys
from argparse import ArgumentParser
from typing import Tuple
import aiohttp
from siobrultech_protocols.gem.packets import PacketFormatType
from greeneye.monitor import (
Aux,
Channel,
Monitor,
Monitors,
MonitorType,
PulseCounter,
TemperatureSensor,
VoltageSensor,
)
num_packets = 0
async def main():
parser = ArgumentParser()
subcommands = parser.add_subparsers(dest="subcommand")
listen_command = subcommands.add_parser(
"listen", description="Listens for incoming connections from GEMs."
)
listen_command.add_argument(
"port", help="Port on which to listen for incoming GEM packets."
)
spy_command = subcommands.add_parser(
"spy",
description=(
"Connects to a GEM at the given IP and spies on whatever packets it is"
" sending (regardless of their destination)."
),
)
spy_command.add_argument(
"host", help="Hostname or IP address of the GEM to which to connect."
)
redirect_command = subcommands.add_parser(
"redirect",
description=(
"Connects to a GEM and redirects its packets to this machine, redirecting"
" back afterwards."
),
)
redirect_command.add_argument(
"--gem", required=True, help="Hostname or IP address of the GEM."
)
redirect_command.add_argument(
"--redirect-to-host",
required=True,
help="Hostname of the machine to redirect packets to.",
)
redirect_command.add_argument(
"--redirect-to-port",
type=int,
required=True,
help="Port of the machine to redirect packets to.",
)
redirect_command.add_argument(
"--restore-to-host",
required=True,
help="Hostname of the target machine to restore after the run.",
)
redirect_command.add_argument(
"--restore-to-port",
type=int,
required=True,
help="Port of the target machine to restore after the run.",
)
args = parser.parse_args()
if args.subcommand == "listen":
await listen(args.port)
elif args.subcommand == "spy":
await spy(args.host)
elif args.subcommand == "redirect":
await redirect(
gem=args.gem,
original=(args.restore_to_host, args.restore_to_port),
redirect=(args.redirect_to_host, args.redirect_to_port),
)
else:
print(f"Unknown subcommand: {args.subcommand}")
sys.exit(-1)
async def listen(port: int) -> None:
async with Monitors() as monitors:
monitors.add_listener(on_new_monitor)
await monitors.start_server(port)
while True:
try:
await asyncio.sleep(60)
except asyncio.CancelledError:
break
async def spy(host: str) -> None:
async with Monitors() as monitors:
monitors.add_listener(on_new_monitor)
_monitor = await monitors.connect(host)
while True:
try:
await asyncio.sleep(60)
except asyncio.CancelledError:
break
async def redirect(
gem: str, original: Tuple[str, int], redirect: Tuple[str, int]
) -> None:
async with aiohttp.ClientSession() as session:
async with Monitors() as monitors:
monitor = await monitors.connect(gem)
old_packet_format = monitor.packet_format
assert old_packet_format
if not monitor.control:
raise Exception("Could not get monitor control interface")
await monitor.control.set_packet_format(PacketFormatType.BIN48_NET_TIME)
await monitor.control.set_packet_destination(
redirect[0], redirect[1], session
)
await listen(redirect[1])
async with Monitors() as monitors:
monitor = await monitors.connect(gem)
if not monitor.control:
raise Exception("Could not get monitor control interface")
await monitor.control.set_packet_format(old_packet_format)
await monitor.control.set_packet_destination(
original[0], original[1], session
)
def on_new_monitor(monitor: Monitor):
monitor.add_listener(lambda: print_monitor(monitor))
monitor.voltage_sensor.add_listener(lambda: print_voltage(monitor.voltage_sensor))
if monitor.type != MonitorType.GEM:
for channel in monitor.channels:
channel.net_metering = False
for channel in monitor.channels:
on_new_channel(channel)
for counter in monitor.pulse_counters:
on_new_counter(counter)
for temp in monitor.temperature_sensors:
on_new_temperature_sensor(temp)
for aux in monitor.aux:
on_new_aux(aux)
def on_new_channel(channel: Channel):
channel.add_listener(lambda: print_channel(channel))
def on_new_counter(counter: PulseCounter):
counter.add_listener(lambda: print_counter(counter))
def on_new_temperature_sensor(temp: TemperatureSensor):
temp.add_listener(lambda: print_temperature(temp))
def on_new_aux(aux: Channel | Aux):
if isinstance(aux, Channel):
on_new_channel(aux)
else:
on_new_channel(aux.channel)
on_new_counter(aux.pulse_counter)
def print_monitor(monitor: Monitor):
print(
f"Monitor {monitor.serial_number} ({monitor.type}) sending packets every"
f" {monitor.packet_send_interval}"
)
def print_voltage(voltage_sensor: VoltageSensor):
print(f"Voltage: {voltage_sensor.voltage} V")
def print_channel(channel: Channel):
if channel.watts and channel.kilowatt_hours:
print(
"Channel {0} (type={4} range={5}): {1:.0f} W ({2:.3f} kWh {3})".format(
channel.number,
channel.watts,
channel.kilowatt_hours,
"net" if channel.net_metering else "abs",
channel.ct_type,
channel.ct_range,
)
)
def print_counter(counter: PulseCounter):
print(
"Pulse counter {0}: {1} ({2}/sec)".format(
counter.number, counter.pulses, counter.pulses_per_second
)
)
def print_temperature(sensor: TemperatureSensor):
print(
"Temperature sensor {0}: {1} {2}".format(
sensor.number, sensor.temperature, sensor.unit
)
)
if __name__ == "__main__":
logging.basicConfig(
stream=sys.stderr,
level=logging.DEBUG,
format="%(asctime)s [%(name)s](%(levelname)s) %(message)s",
)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(main())
try:
loop.run_until_complete(task)
except KeyboardInterrupt:
task.cancel()
try:
loop.run_until_complete(task)
except asyncio.CancelledError:
pass
loop.close()