-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcpc_gui_sup.py
427 lines (395 loc) · 16.7 KB
/
dhcpc_gui_sup.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
from tkinter.messagebox import NO
from PyQt6.QtWidgets import QStyledItemDelegate, QComboBox, QSpinBox, QLineEdit, QTableWidgetItem, QTableWidget, QPushButton, QWidget, QFormLayout, QHBoxLayout, QMessageBox, QDialog
from PyQt6.QtCore import QAbstractTableModel, Qt, pyqtSignal
import re
def get_firstint_fromstr(str):
if (str):
l = re.findall("\d+", str)
if l:
return int(l[0])
return None
class DHCPC_OPTION_SET(QDialog):
add_option_signal = pyqtSignal([int, str, str])
del_option_signal = pyqtSignal([int])
def __init__(self, parent=None, conn = None) -> None:
super().__init__(parent)
layout = QFormLayout(self)
btn_layout = QHBoxLayout()
self.setWindowTitle("OPTION")
self.setWindowModality(Qt.WindowModality.WindowModal)
self.option = QSpinBox()
self.option.setMinimum(1)
self.option.setMaximum(254)
self.value_type = QComboBox()
self.value_type.addItems(["hex", "str"])
self.value = QLineEdit()
add_btn = QPushButton("Add")
del_btn = QPushButton("Del")
btn_layout.addWidget(add_btn)
btn_layout.addWidget(del_btn)
layout.addRow("option:", self.option)
layout.addRow("value type:", self.value_type)
layout.addRow("value:", self.value)
layout.addRow(btn_layout)
add_btn.clicked.connect(self.onClickAdd)
del_btn.clicked.connect(self.onClickDel)
# 信号处理
self.add_option_signal[int, str, str].connect(conn.add_option)
self.del_option_signal[int].connect(conn.del_option)
def onClickDel(self):
op = get_firstint_fromstr(self.option.text())
if (op is None):
return
self.del_option_signal.emit(op)
# be_del_data = "是否删除数据option({op})?".format(op=op)
# msg = QMessageBox(QMessageBox.Icon.NoIcon, "提示", be_del_data, QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, None)
# if (msg.exec() == QMessageBox.StandardButton.Yes):
# logger.debug("Del")
def onClickAdd(self):
op = get_firstint_fromstr(self.option.text())
if (op is None):
return
self.add_option_signal.emit(op, self.get_value_type(), self.value.text())
def get_value_type(self):
return self.value_type.currentText()
class DHCPC_PKT_MODEL(QStyledItemDelegate):
def __init__(self, parent, check, conn) -> None:
super().__init__(parent)
self.nondata_ops_check = check
self.conn = conn
def createEditor(self, parent, option, index):
editor = None
if (index.isValid()):
# hops
if (index.row() == 0 and index.column() == 3):
editor = QSpinBox(parent)
editor.setMinimum(0)
editor.setMaximum(255)
# xid
elif (index.row() == 1 and index.column() == 0):
editor = QLineEdit(parent)
editor.setInputMask("\\0\\xHHHHHHHH;0")
# secs
elif (index.row() == 2 and index.column() == 0):
editor = QSpinBox(parent)
editor.setMinimum(0)
editor.setMaximum(2147483647)
# flags
elif (index.row() == 2 and index.column() == 2):
editor = QComboBox(parent)
editor.addItems(["broadcast", "unicast"])
# ciaddr
elif (index.row() == 3 and index.column() == 0):
editor = QLineEdit(parent)
editor.setInputMask("000.000.000.000;0")
# yiaddr
elif (index.row() == 4 and index.column() == 0):
editor = QLineEdit(parent)
editor.setInputMask("000.000.000.000;0")
# giaddr
elif (index.row() == 5 and index.column() == 0):
editor = QLineEdit(parent)
editor.setInputMask("009.009.009.009;0")
# chaddr
elif (index.row() == 6 and index.column() == 0):
editor = QLineEdit(parent)
editor.setInputMask("HH:HH:HH:HH:HH:HH")
# options
elif (index.row() == 9 and index.column() == 0):
editor = DHCPC_OPTION_SET(parent, self.conn)
else:
if (index.data().find("option") == 0):
opint = get_firstint_fromstr(index.data())
if (opint == 53):
editor = QComboBox(parent)
editor.addItems(["discover", "request", "decline", "release", "inform"])
elif (self.nondata_ops_check(opint)):
editor = QLineEdit(parent)
return editor
def setEditorData(self, editor, index):
value = index.data(Qt.ItemDataRole.EditRole)
# hops
if (index.row() == 0 and index.column() == 3):
editor.setValue(int(value.split(":", 1)[1]))
# secs
elif (index.row() == 2 and index.column() == 0):
editor.setValue(int(value.split(":", 1)[1]))
# flags
elif (index.row() == 2 and index.column() == 2):
editor.setCurrentText(value.split(":", 1)[1])
# xid
elif (index.row() == 1 and index.column() == 0):
editor.setText(value.split(":", 1)[1])
# ciaddr
elif (index.row() == 3 and index.column() == 0):
editor.setText(value.split(":", 1)[1])
# yiaddr
elif (index.row() == 4 and index.column() == 0):
editor.setText(value.split(":", 1)[1])
# giaddr
elif (index.row() == 5 and index.column() == 0):
editor.setText(value.split(":", 1)[1])
# chaddr
elif (index.row() == 6 and index.column() == 0):
editor.setText(value.split(":", 1)[1])
# options
elif (index.row() == 9 and index.column() == 0):
pass
else:
opint = get_firstint_fromstr(value)
if (value.find("option") == 0):
if (opint == 53):
editor.setCurrentText(value.split(":", 1)[1])
elif (len(value.split(":", 1)) > 1):
editor.setText(value.split(":", 1)[1])
def point_check(self, value):
ipseg_list = value.split('.')
return '.'.join(['0' if value == '' else value for value in ipseg_list])
def colon_check(self, value):
macseg_list = value.split(':')
return ':'.join(['00' if value == '' else value for value in macseg_list])
def setModelData(self, editor, model, index):
# hops
value = None
if (index.row() == 0 and index.column() == 3):
value = "hops:"+ str(editor.value())
# secs
elif (index.row() == 2 and index.column() == 0):
value = "secs:"+ str(editor.value())
# flags
elif (index.row() == 2 and index.column() == 2):
value = "flags:"+ editor.currentText()
# xid
elif (index.row() == 1 and index.column() == 0):
value = "xid:"+ self.point_check(editor.text())
# ciaddr
elif (index.row() == 3 and index.column() == 0):
value = "ciaddr:"+ self.point_check(editor.text())
# yiaddr
elif (index.row() == 4 and index.column() == 0):
value = "yiaddr:"+ self.point_check(editor.text())
# giaddr
elif (index.row() == 5 and index.column() == 0):
value = "giaddr:"+ self.point_check(editor.text())
# chaddr
elif (index.row() == 6 and index.column() == 0):
value = "chaddr:"+ self.colon_check(editor.text())
# options
elif (index.row() == 9 and index.column() == 0):
value = "options:"
else:
intop = get_firstint_fromstr(index.data())
editable = self.nondata_ops_check(intop)
if (index.data().find("option") == 0):
if (editable):
if (intop == 53):
value = index.data().split(":", 1)[0] + ":" + editor.currentText()
else:
value = index.data().split(":", 1)[0] + ":" + editor.text()
else:
value = "option({op})".format(op = intop)
model.setData(index, value, Qt.ItemDataRole.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class DHCPC_PKT_TABLEWIDGET(QTableWidget):
def __init__(self, row, col, parent=None):
super().__init__(row, col, parent)
self.setWindowTitle("DHCP REQUEST PACKET")
self.zeroip = "0.0.0.0"
self.req_ops_list = [1, 2, 3, 6, 12, 15, 17, 26, 28, 33, 40, 41, 42, 119, 121, 249, 252]
self.nondata_ops_list = [255]
def other(self):
# op
op_item = QTableWidgetItem("op:1")
op_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(0, 0, op_item)
# htype
htype_item = QTableWidgetItem("htype:1")
htype_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(0, 1,htype_item)
# hlen
hlen_item = QTableWidgetItem("hlen:6(bytes)")
hlen_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(0, 2,hlen_item)
# hops
hops_item = QTableWidgetItem("hops:0")
hops_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(0, 3, hops_item)
# xid
xid_item = QTableWidgetItem(self.get_xid())
xid_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
# xid_item.setFlags(Qt.ItemFlag.ItemIsSelectable | Qt.ItemFlag.ItemIsEditable | Qt.ItemFlag.ItemIsEnabled)
self.setItem(1, 0, xid_item)
# secs
secs_item = QTableWidgetItem("secs:0")
secs_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(2, 0, secs_item)
# flags
flags_item = QTableWidgetItem("flags:broadcast")
flags_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(2, 2, flags_item)
# ciaddr
ciaddr_item = QTableWidgetItem(self.get_ciaddr())
ciaddr_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(3, 0, ciaddr_item)
# yiaddr
yiaddr_item = QTableWidgetItem(self.get_yiaddr())
yiaddr_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(4, 0, yiaddr_item)
# giaddr
giaddr_item = QTableWidgetItem(self.get_giaddr())
giaddr_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(5, 0, giaddr_item)
# chaddr
chaddr_item = QTableWidgetItem(self.get_chaddr())
chaddr_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(6, 0, chaddr_item)
# sname
sname_item = QTableWidgetItem(self.get_sname())
sname_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(7, 0, sname_item)
# file
file_item = QTableWidgetItem(self.get_file())
file_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(8, 0, file_item)
# options
option_title_item = QTableWidgetItem("options:")
option_title_item.setTextAlignment(Qt.AlignmentFlag.AlignLeft)
self.setRowHeight(9, 70)
self.setItem(9, 0, option_title_item)
# self.setCornerWidget
# option end
self.insertRow(self.rowCount())
row = self.rowCount() - 1
self.setSpan(row, 0, 1, 4)
end_option_item = QTableWidgetItem("option(255)")
end_option_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(row, 0, end_option_item)
# dhcp message type
self.add_req_option_befend(53, "discover", "Dhcp message type")
self.add_req_option_befend(61, "", "Client identifier")
self.add_req_option_befend(55, ",".join([str(op) for op in self.req_ops_list]), "Parameter Request List")
self.add_req_option_befend(57, "576", "Maximum DHCP Message Size")
self.add_req_option_befend(12, "", "Host Name")
self.setItemDelegate(DHCPC_PKT_MODEL(None, self.nondata_ops_check, self))
def nondata_ops_check(self, op):
if (op in self.nondata_ops_list):
return False
return True
# 去重
def add_option(self, op, type, value):
editable = self.nondata_ops_check(op)
if (not editable and len(value) != 0):
QMessageBox(QMessageBox.Icon.NoIcon, "提示", "该option不可设置value", QMessageBox.StandardButton.Ok, None).exec()
return
self.add_req_option_befend(op, value)
def del_option(self, option):
for row in range(0, self.rowCount()):
for col in range(0, self.columnCount()):
item = self.item(row, col)
if (item):
text = item.text()
if (text.find("option(") == 0 and get_firstint_fromstr(text) == option):
self.removeRow(row)
logger.debug(option, text, row)
break
def add_req_option_befend(self, op, value=None, tips=""):
self.insertRow(self.rowCount() - 1)
row = self.rowCount() - 2
self.setSpan(row, 0, 1, 4)
if tips == "":
separator = ""
else:
separator = "-"
if (value is not None):
option = "option({op}{separator}{tips}):".format(op = op, tips=tips, separator=separator) + value
else:
option = "option({op}{separator}{tips})".format(op = op, tips=tips, separator=separator)
req_option_item = QTableWidgetItem(option)
req_option_item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
self.setItem(row, 0, req_option_item)
def get_xid(self):
xid = 12345678
return "xid:" + "0x" + str(xid)
def get_ciaddr(self):
return "ciaddr:" + self.zeroip
def get_yiaddr(self):
return "yiaddr:" + self.zeroip
def get_giaddr(self):
return "giaddr:" + self.zeroip
def get_chaddr(self):
mac = "10:22:33:44:55:66"
return "chaddr:" + mac
def get_sname(self):
return "sname:" + ''
def get_file(self):
return "file:" + ''
def get_option(self, op):
return "option({op}):".format(op=op) + ''
def write2dhcpc_cfg(self):
return DHCPC_PKT_CFG_FROMUI(self)
class DHCPC_PKT_CFG_FROMUI():
def __init__(self, pkt:DHCPC_PKT_TABLEWIDGET) -> None:
self.ops = [None] * 256
for row in range(0, pkt.rowCount()):
for col in range(0, pkt.columnCount()):
item = pkt.item(row, col)
if (item):
if (item.text().find(":") != -1):
text , value = item.text().split(":", 1)
else:
text = item.text()
opint = get_firstint_fromstr(text)
if (text == "op"):
self.op = int(value)
elif (text == "htype"):
self.htype = int(value)
elif (text == "hops"):
self.hops = int(value)
elif (text == "xid"):
self.xid = int(value, 16)
elif (text == "secs"):
self.secs = int(value)
elif (text == "flags"):
self.flags = value
elif (text == "ciaddr"):
self.ciaddr = value
elif (text == "yiaddr"):
self.yiaddr = value
elif (text == "giaddr"):
self.giaddr = value
elif (text == "chaddr"):
self.chaddr = value
elif (text == "sname"):
self.sname = value
elif (text == "file"):
self.file = value
elif (text.find("option") == 0 and opint):
self.ops[opint] = value
# logger.debug("BB:", type(opint),opint, value)
def get_op(self):
return self.op
def get_htype(self):
return self.htype
def get_hops(self):
return self.hops
def get_xid(self):
return self.xid
def get_secs(self):
return self.secs
def get_flags(self)->str:
return self.flags
def get_ciaddr(self)->str:
return self.ciaddr
def get_yiaddr(self)->str:
return self.yiaddr
def get_giaddr(self)->str:
return self.giaddr
def get_chaddr(self)->str:
return self.chaddr
def get_sname(self):
return self.sname
def get_file(self):
return self.file
def get_ops(self):
return self.ops