-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbinary_camera.py
More file actions
170 lines (145 loc) · 6.69 KB
/
binary_camera.py
File metadata and controls
170 lines (145 loc) · 6.69 KB
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
import asyncio
import struct
import aiopppp.const
from aiopppp.packets import (
make_punch_pkt,
make_p2palive_pkt,
parse_drw_pkt,
make_drw_ack_pkt,
BinaryCmdPkt,
xq_bytes_decode,
DrwPkt,
)
from aiopppp.types import DeviceID
class UDPProtocol(asyncio.DatagramProtocol):
def __init__(self, on_receive):
super().__init__()
self.on_receive = on_receive
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, data, addr):
self.on_receive(data, addr)
async def create_udp_server(port, on_receive):
# Bind to localhost on UDP port
loop = asyncio.get_running_loop()
transport, _ = await loop.create_datagram_endpoint(
lambda: UDPProtocol(on_receive),
local_addr=('127.0.0.1', port),
allow_broadcast=True,
)
return transport
class BinaryCamera:
def __init__(self):
self.transport = None
self.dev_id = DeviceID('TEST',123456, 'CAMERA')
self.input = asyncio.Queue()
self.output = asyncio.Queue()
self.client_addr = None
self.ticket = b'abcd'
self.cmd_idx = 1
def on_receive(self, data, addr):
# print(f"Received {data} from {addr}")
if data[0] == aiopppp.const.CAM_MAGIC:
self.input.put_nowait((data, addr))
async def send_task(self):
while True:
data, addr = await self.output.get()
assert isinstance(data, bytes), 'Data must be bytes'
if data[1] in [aiopppp.const.PacketType.PunchPkt.value, aiopppp.const.PacketType.P2PAlive.value]:
print(f'Send {data} to {addr}')
self.transport.sendto(data, addr)
elif self.client_addr:
print(f'Send {data}')
self.transport.sendto(data, self.client_addr)
self.output.task_done()
async def send_p2p_rdy_set(self):
pkt = make_punch_pkt(self.dev_id)
pkt.type = aiopppp.const.PacketType.P2pRdy
for _ in range(10):
self.output.put_nowait((bytes(pkt), self.client_addr))
await asyncio.sleep(0.1)
async def process_drw(self, data):
cmd_header_len = 12
pkt = parse_drw_pkt(data[4:])
self.output.put_nowait((bytes(make_drw_ack_pkt(pkt)), self.client_addr))
payload = pkt.get_drw_payload()
# print('... DRW payload:', payload)
if payload[:2] == BinaryCmdPkt.START_CMD:
cmd_id = aiopppp.const.BinaryCommands(int.from_bytes(payload[2:4], "little"))
data = payload[cmd_header_len:]
if len(data) > 4:
data = xq_bytes_decode(data, 4)
if cmd_id == aiopppp.const.BinaryCommands.CMD_SYSTEM_USER_CHK:
INCORRECT_USER_RESP = '11 0a 20 11 0c 00 ff 00 00 00 00 00 57 56 6c 37 fe 01 01 01'
CORRECT_USER_RESP = '11 0a 20 11 04 00 ff 00 0e fc ff ff'
username, password = struct.unpack('<32s128s', data)
username = username.decode('utf-8').strip('\x00')
password = password.decode('utf-8').strip('\x00')
resp = CORRECT_USER_RESP if username == 'admin' and password == 'admin' else INCORRECT_USER_RESP
print('... BinaryCommand: cmd_id:', cmd_id, 'data:', data)
await asyncio.sleep(0.3)
print('... send ACK_SYSTEM_USER_CHK')
self.output.put_nowait((bytes(
DrwPkt(cmd_idx=0, channel=0, drw_payload=bytes.fromhex(resp)),
), self.client_addr))
# self.output.put_nowait((bytes(BinaryCmdPkt(
# cmd_idx=self.cmd_idx,
# command=aiopppp.const.BinaryCommands.ACK_SYSTEM_USER_CHK,
# ticket=self.ticket,
# cmd_payload=b'\xff\x00\x00\x00',
# )), self.client_addr))
self.cmd_idx += 1
elif cmd_id == aiopppp.const.BinaryCommands.CMD_SYSTEM_STATUS_GET:
await asyncio.sleep(0.3)
print('... send ACK_SYSTEM_STATUS_GET')
self.output.put_nowait(
(
bytes(
BinaryCmdPkt(
cmd_idx=self.cmd_idx,
command=aiopppp.const.BinaryCommands.ACK_SYSTEM_STATUS_GET,
# token=b'\x0a\xfc\xff\xff',
token=b"\x00\x00\x00\x00",
# cmd_payload=bytes(range(0x15)),
cmd_payload=bytes.fromhex(
"0d 02 01 3d 74 0f 00 00 00 00 00 00 ff ff ff ff bf ff ff ff "
"01 01 00 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 00 00 01 00 00 02 00 00 00 00 00 00 00 "
"00 00 00 00 00 ff ff ff 00 00 00 00 ff ff ff ff 00 00 00 00 "
"00 00 00 00",
),
)
),
self.client_addr,
)
)
async def on_packet(self, data, addr):
print(f"Received packet: {data} {addr}")
if data[1] == aiopppp.const.PacketType.LanSearch.value:
print('Received LanSearch packet')
self.output.put_nowait((bytes(make_punch_pkt(self.dev_id)), addr))
if data[1] == aiopppp.const.PacketType.PunchPkt.value:
self.client_addr = addr
print('Received PunchPkt packet, starting p2prdy sending')
asyncio.create_task(self.send_p2p_rdy_set())
# self.output.put_nowait((bytes(make_punch_pkt(self.dev_id)), addr
elif data[1] == aiopppp.const.PacketType.P2PAlive.value:
self.output.put_nowait((bytes(make_p2palive_pkt()), addr))
elif data[1] == aiopppp.const.PacketType.Drw.value:
asyncio.create_task(self.process_drw(data))
async def receive_task(self):
while True:
data, addr = await self.input.get()
await self.on_packet(data, addr)
self.input.task_done()
async def run(self):
self.transport = await create_udp_server(32108, self.on_receive)
out_t = asyncio.create_task(self.send_task())
in_t = asyncio.create_task(self.receive_task())
await asyncio.gather(*[out_t, in_t])
async def main():
camera = BinaryCamera()
await camera.run()
asyncio.run(main())