-
Notifications
You must be signed in to change notification settings - Fork 11
/
certificatethread.py
264 lines (222 loc) · 10.6 KB
/
certificatethread.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/python
import codecs
import threading
import select
import os
from PyQt5 import QtCore
from utils import logger
idle_state = 1
datasent_state = 2
DELIMITER = "\r\n"
# PACKET_SIZE = 1200
PACKET_SIZE = 1024
# PACKET_SIZE = 2048
class certificatethread(QtCore.QThread):
uploading_size = QtCore.pyqtSignal(int)
upload_result = QtCore.pyqtSignal(int)
error_flag = QtCore.pyqtSignal(int)
def __init__(self, sock, dest_mac, idcode, set_pw, filename, ipaddr, port, mn_list, cmd):
QtCore.QThread.__init__(self)
self.logger = logger
self.dest_mac = None
self.bin_cert_name = filename
self.fd = None
self.data = None
self.client = None
self.timer1 = None
self.istimeout = 0
self.serverip = ipaddr
self.serverport = port
self.sentbyte = 0
self.dest_mac = dest_mac
self.idcode = idcode
self.error_noresponse = 0
self.retrycheck = 0
self.header = bytearray(128)
self.header_size = 0
self.msg = bytearray(PACKET_SIZE)
self.size = 0
self.sock = sock
self.what_sock = '%s' % self.sock
try:
self.inputs = [self.sock.sock]
except Exception as e:
print('socket error:', e)
self.terminate()
self.outputs = []
self.errors = []
# device name
self.mn_list = mn_list
self.set_pw = set_pw
# self.cert = cert
self.cmd = cmd
self.curr_ptr = 0
# self.cert_size = len(self.cert)
self.remainbytes = 0
self.filesize = 0
self.ip_addr = ipaddr
self.port = port
self.cli_sock = None
def setparam(self):
# Read file data
self.fd = open(self.bin_cert_name, "rb")
self.data = self.fd.read(-1)
self.remainbytes = len(self.data)
self.filesize = len(self.data)
print('remainbytes:', self.remainbytes)
self.curr_ptr = 0
self.fd.close()
def myTimer(self):
# print('timer1 timeout\r\n')
self.istimeout = 1
def make_header_commands(self):
cmd_list = []
cmd_list.append(["MA", self.dest_mac])
cmd_list.append(["PW", self.idcode])
try:
for cmd in cmd_list:
# print('cmd[0]: %s, cmd[1]: %s' % (cmd[0], cmd[1]))
try:
self.msg[self.size:] = str.encode(cmd[0])
except Exception as e:
self.logger.error('[ERROR] make_header_commands() encode:', cmd[0], e)
self.size += len(cmd[0])
if cmd[0] == "MA":
# print('cmd[1]: %r' % cmd[1])
cmd[1] = cmd[1].replace(":", "")
# print(cmd[1])
# hex_string = cmd[1].decode('hex')
try:
hex_string = codecs.decode(cmd[1], 'hex')
except Exception as e:
self.logger.error('[ERROR] make_header_commands() decode:', cmd[0], cmd[1], e)
self.msg[self.size:] = hex_string
self.dest_mac = hex_string
# self.dest_mac = (int(cmd[1], 16)).to_bytes(6, byteorder='big') # Hexadecimal string to hexadecimal binary
# self.msg[self.size:] = self.dest_mac
self.size += 6
else:
try:
self.msg[self.size:] = str.encode(cmd[1])
except Exception as e:
self.logger.error('[ERROR] make_header_commands() encode param:', cmd[0], cmd[1], e)
self.size += len(cmd[1])
if DELIMITER not in cmd[1]:
self.msg[self.size:] = str.encode(DELIMITER)
self.size += 2
# print(self.size, self.msg)
except Exception as e:
self.logger.error(f'[ERROR] WIZMSGHandler make_header_commands(): {str(e)}')
# self.msleep(500)
# self.uploading_size.emit(2)
def add_get_command(self):
try:
cmd = self.cmd + DELIMITER
self.msg[self.size:] = str.encode(cmd)
self.size += len(cmd)
except Exception as e:
self.logger.error(str(e))
def run(self):
self.setparam()
self.logger.info(f'Certificate upload start: {self.mn_list}')
self.make_header_commands()
try:
if self.error_noresponse < 0:
self.logger.error(f'error_noresponse: {self.error_noresponse}')
pass
else:
# print("%r" % self.client.state)
self.logger.info(f"Header: size: {self.size}, msg: {self.msg}")
try:
additional_size = 0
if self.curr_ptr == 0:
# header + getcmd + delemiter
additional_size = self.size + len(self.cmd) + len(DELIMITER)
# print('check size:', PACKET_SIZE - additional_size)
if self.filesize < PACKET_SIZE - additional_size:
self.logger.info(f"Certificate file size: {self.filesize}")
# for WIZ510SSL
cmdset = self.cmd + self.data.decode() + DELIMITER
self.msg[self.size:] = str.encode(cmdset)
self.size += len(cmdset)
self.add_get_command()
self.sock.sendto(self.msg)
self.logger.info('[%s] [%d] bytes sent' % (self.serverip, self.size))
self.remainbytes = 0
else:
while self.remainbytes != 0:
if self.remainbytes >= PACKET_SIZE - additional_size:
# WIZ510SSL
msg = bytearray(PACKET_SIZE)
if self.curr_ptr == 0:
# first packet: command + packet
# msg[:] = self.header + self.cmd.encode() + self.data[PACKET_SIZE - len(self.header) - len(self.cmd)]
msg = self.msg
if self.cmd == "UP":
# MA/PW/cmd/size/bin
datasize = PACKET_SIZE - self.size - len(self.cmd) - len(str(self.filesize)) - len(DELIMITER)
msg[self.size:] = str.encode(self.cmd + str(self.filesize) + DELIMITER) + self.data[self.curr_ptr:self.curr_ptr + datasize]
else:
datasize = PACKET_SIZE - self.size - len(self.cmd)
msg[self.size:] = self.cmd.encode() + self.data[self.curr_ptr:self.curr_ptr + datasize]
# print("[1] msg:", msg)
self.sock.sendto(msg)
self.sentbyte = PACKET_SIZE
# print('PACKET_SIZE bytes sent from at %r' % (self.curr_ptr))
self.logger.info('[1] %d bytes sent from at %r' % (PACKET_SIZE, self.curr_ptr))
self.curr_ptr += datasize
self.remainbytes -= datasize
else:
msg[:] = self.data[self.curr_ptr:self.curr_ptr + PACKET_SIZE]
# print("[2] msg:", msg)
self.sock.sendto(msg)
self.sentbyte = PACKET_SIZE
# print('PACKET_SIZE bytes sent from at %r' % (self.curr_ptr))
self.logger.info('[2] %d bytes sent from at %r' % (PACKET_SIZE, self.curr_ptr))
self.curr_ptr += PACKET_SIZE
self.remainbytes -= PACKET_SIZE
else:
self.uploading_size.emit(6)
msg = bytearray(self.remainbytes + 4)
msg[:] = self.data[
self.curr_ptr:self.curr_ptr + self.remainbytes] + str.encode(self.cmd + DELIMITER)
# self.client.write(msg)
# print("[3] msg:", msg)
self.sock.sendto(msg)
# print('Last %r byte sent from at %r' % (self.remainbytes, self.curr_ptr))
self.logger.info('[3] Last %r byte sent from at %r' % (self.remainbytes, self.curr_ptr))
self.curr_ptr += self.remainbytes
self.remainbytes = 0
self.sentbyte = self.remainbytes
self.timer1 = threading.Timer(2.0, self.myTimer)
self.timer1.start()
self.uploading_size.emit(7)
# Check response
if self.remainbytes == 0:
replylists = []
readready, writeready, errorready = select.select(
self.inputs, self.outputs, self.errors, 2)
# print('readready value: ', len(readready), readready)
for sock in readready:
if sock == self.sock.sock:
data = self.sock.recvfrom()
self.msleep(10)
replylists = data.split(DELIMITER.encode())
self.logger.debug('replylists:', replylists)
if self.cmd.encode() in replylists:
self.uploading_size.emit(8)
self.logger.info('Device [%s] upload success!' % (self.dest_mac))
self.upload_result.emit(1)
else:
self.upload_result.emit(-1)
if len(replylists) == 0:
self.upload_result.emit(-2)
self.uploading_size.emit(8)
except Exception as e:
self.logger.error(str(e))
self.msleep(1)
except Exception as e:
self.error_flag.emit(-3)
self.logger.error(str(e))
finally:
pass