forked from K-Francis-H/usbip-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
363 lines (288 loc) · 10.3 KB
/
gui.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
#requires python 3.8+ may work on 3.6, 3.7 definitely broken on <= 3.5 due to subprocess args (text=True)
from tkinter import *
from tkinter.ttk import *
import subprocess
import sys
import re
import time
from urllib.parse import urlparse
DEVICE_COLUMNS = ["bus_id", "manufacturer", "description"]
DEVICE_COLUMN_WIDTHS = [8, 20, 50]
ATTACHED_COLUMNS = ["host", "port", "bus_id", "manufacturer", "description"]
ATTACHED_COLUMN_WIDTHS = [21, 3, 8, 20, 50]
USBIPD_PORT = 3240
attached_devices = {}
def init_kernel_modules():
subprocess.run(["sudo", "modprobe", "usbip_host"])
subprocess.run(["sudo", "modprobe", "usbip_core"])
subprocess.run(["sudo", "modprobe", "vhci_hcd"])
def init_usbip_server():
#TODO log
subprcocess.run(["sudo", "usbipd"])
def scan():
#TODO
return 0
def refresh_local():
local_devices = list_local_usb()
local_listbox.delete(*local_listbox.get_children())
for device in local_devices:
local_listbox.insert('', "end", values=device)
def refresh_remote():
server_ip = remote_ip_input.get()
remote_devices = list_remote_usb(server_ip)
remote_listbox.delete(*remote_listbox.get_children())
for device in remote_devices:
remote_listbox.insert('', "end", values=device)
def refresh_attached():
attached_devices = list_attached_usb()
attached_listbox.delete(*attached_listbox.get_children())
for device in attached_devices:
attached_listbox.insert('', "end", values=device)
#TODO these are both wrong
def bind_local():
selection = local_listbox.selection()
if not selection:
print("no selection to bind")
return
bus_id = local_listbox.item(selection[0])['values'][0]
result = bind_local_usb(bus_id)
if result.returncode == 0:
print(bus_id+" bound successfully")
def unbind_local():
selection = local_listbox.selection()
if not selection:
print("no selection to unbind")
return
bus_id = local_listbox.item(selection[0])['values'][0]
result = unbind_local_usb(bus_id)
if result.returncode == 0:
print(bus_id+" unbound succesfully")
def attach_remote():
server_ip = remote_ip_input.get()
selection = remote_listbox.selection()
if not selection:
print("no selection to attach")
return
print(server_ip)
print(selection[0])
print(selection)
print(remote_listbox.item(selection[0]))
bus_id = remote_listbox.item(selection[0])['values'][0]
manufacturer = remote_listbox.item(selection[0])['values'][1]
description = remote_listbox.item(selection[0])['values'][2]
print(bus_id)
result = attach_remote_usb(server_ip, bus_id)
print(result.returncode)
'''if result.returncode == 0:
attached_devices[bus_id] = {
'bus_id' : bus_id,
'port' : len(attached_devices),
'manufacturer' : manufacturer,
'description' : description
}
print(attached_devices)
'''
time.sleep(0.5)
refresh_remote()
refresh_local()
refresh_attached()
#TODO get selection
def detach_remote():
selection = attached_listbox.selection()
if not selection:
print("no selection to detach")
return # no selected item
print(selection)
port = attached_listbox.item(selection[0])['values'][1]
detach_remote_usb(port)
time.sleep(0.5)
refresh_remote()
refresh_local()
refresh_attached()
#sample output to parse for parse_local_list(text)
'''
- busid 2-3 (1058:25a3)
Western Digital Technologies, Inc. : unknown product (1058:25a3)
- busid 3-3.2 (046d:c52b)
Logitech, Inc. : Unifying Receiver (046d:c52b)
- busid 3-3.3 (046d:c52b)
Logitech, Inc. : Unifying Receiver (046d:c52b)
- busid 3-3.4 (058f:6366)
Alcor Micro Corp. : Multi Flash Reader (058f:6366)
- busid 4-1 (174c:55aa)
ASMedia Technology Inc. : ASM1051E SATA 6Gb/s bridge, ASM1053E SATA 6Gb/s bridge, ASM1153 SATA 3Gb/s bridge (174c:55aa)
- busid 5-1 (054c:0268)
Sony Corp. : Batoh Device / PlayStation 3 Controller (054c:0268)
'''
def parse_local_list(text):
rows = []
devices = text.strip().split("\n\n")
for device in devices:
#print(device)
lines = device.strip().split("\n")
#print(lines)
bus_info = lines[0].split(" ")
man_info = lines[1].split(":")
rows.append((
bus_info[2],
#bus_info[3],
man_info[0],
man_info[1]+":"+man_info[2]
))
#print(rows)
return rows
#sample output to parse for parse_remote(text)
'''
Exportable USB devices
======================
- 192.168.1.103
1-1.3: SanDisk Corp. : Cruzer (0781:5530)
: /sys/devices/platform/soc/20980000.usb/usb1/1-1/1-1.3
: (Defined at Interface level) (00/00/00)
: 0 - Mass Storage / SCSI / Bulk-Only (08/06/50)
'''
def parse_remote_list(text):
if text.__contains__("no exportable devices found on") == True:
return []
rows = []
busid_regex = re.compile("^\d+-\d+$|^\d+-\d+\.\d+$")
lines = text.strip().split("\n")
for line in lines:
vals = line.strip().split(":")
print(vals)
m = busid_regex.match(vals[0])
if m: #the first value is a bus_id, grab the info
rows.append((
vals[0],
vals[1],
vals[2]+":"+vals[3]
))
return rows
#sample output to parse for parse_attached_list(text)
'''
Imported USB devices
====================
Port 00: <Port in Use> at Full Speed(12Mbps)
Sony Corp. : Batoh Device / PlayStation 3 Controller (054c:0268)
5-1 -> usbip://192.168.1.103:3240/1-1.4
-> remote bus/dev 001/005
'''
def parse_attached_list(text):
rows = []
lines = text.strip().split("\n")
index = 0
for i in range(0, len(lines)):
line = lines[i]
if line.__contains__("Port ") == True:
port = int( line.strip().split(":")[0].replace("Port ", "") )
info_line = lines[i+1]
busid_line = lines[i+2]
last_line = lines[i+3]
info = info_line.strip().split(":")
manufacturer = info[0]
description = info[1]+":"+info[2]
businfo = busid_line.strip().split("->")
bus_id = businfo[0].strip()
host = urlparse( businfo[1].strip() )[1] #netloc
rows.append((
host,
port,
bus_id,
manufacturer,
description
))
print(rows)
return rows
def list_local_usb():
result = subprocess.run(["usbip", "list", "--local"], capture_output=True, text=True)
return parse_local_list(result.stdout)
def list_remote_usb(server_ip):
result = subprocess.run(["usbip", "list", "--remote="+server_ip], capture_output=True, text=True)
return parse_remote_list(result.stdout)
def bind_local_usb(bus_id):
result = subprocess.run(["usbip", "bind", "--busid="+bus_id], capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
return result
def unbind_local_usb(bus_id):
result = subprocess.run(["usbip", "unbind", "--busid="+bus_id], capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
return result
def list_attached_usb():
result = subprocess.run(["usbip", "port"], capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
return parse_attached_list(result.stdout)
def attach_remote_usb(server_ip, bus_id):
result = subprocess.run(["usbip", "attach", "--remote="+server_ip, "--busid="+bus_id], capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
return result
def detach_remote_usb(port):
result = subprocess.run(["usbip", "detach", "--port="+str(port)], capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
#def update_list(list, values):
#class UsbIpGui():
# def __init__(self):
root = Tk()
root.wm_title("USB/IP Peer")
root.geometry("1002x842")
#TODO listbox for remote (from entered IP) usb items
remote_control_frame = Frame(root)
remote_list_label = Label(remote_control_frame, text="Remote USB Devices for ")
remote_ip_input = Entry(remote_control_frame)
remote_list_refresh_button = Button(remote_control_frame, text="Refresh", command=refresh_remote)
remote_list_attach_button = Button(remote_control_frame, text="Attach Device", command=attach_remote)
remote_listbox = Treeview(columns=DEVICE_COLUMNS, show="headings")#Listbox(root)
#remote_listbox.pack(side="left")
for col in DEVICE_COLUMNS:
remote_listbox.heading(col, text=col.title())
remote_devices = list_remote_usb('127.0.0.1')#'192.168.1.103')
for device in remote_devices:
remote_listbox.insert("", "end", values=device)
remote_list_label.grid(column=0, row=0, padx=10)
remote_ip_input.grid(column=1, row=0, padx=10)
remote_list_refresh_button.grid(column=2, row=0, padx=10)
remote_list_attach_button.grid(column=3, row=0, padx=10)
remote_control_frame.grid(column=0, row=0, sticky="ew", pady=10)
remote_listbox.grid(column=0, row=1, sticky="ew", pady=10)
#TODO listbox for local items
local_control_frame = Frame(root)
local_list_label = Label(local_control_frame, text="Local USB Devices")
local_list_refresh_button = Button(local_control_frame, text="Refresh", command=refresh_local)
local_list_bind_button = Button(local_control_frame, text="Bind Device", command=bind_local)
local_list_unbind_button = Button(local_control_frame, text="Unbind Device", command=unbind_local)
local_listbox = Treeview(columns=DEVICE_COLUMNS, show="headings")#Listbox(root)
#local_listbox.pack(side="right")
#setup column names
for col in DEVICE_COLUMNS:
local_listbox.heading(col, text=col.title())
#local_listbox.column(col, width=tkFont)
local_devices = list_local_usb()
for device in local_devices:
local_listbox.insert('', "end", values=device)
local_list_label.grid(column=0, row=0, padx=10)
local_list_refresh_button.grid(column=1, row=0, padx=10)
local_list_bind_button.grid(column=3, row=0, padx=10)
local_list_unbind_button.grid(column=4, row=0, padx=10)
local_control_frame.grid(column=0, row=2, sticky="ew", pady=10)
local_listbox.grid(column=0, row=3, sticky="ew", pady=10)
attached_control_frame = Frame(root)
attached_list_label = Label(attached_control_frame, text="Attached Devices")
attached_list_refresh_button = Button(attached_control_frame, text="Refresh", command=refresh_attached)
detach_button = Button(attached_control_frame, text="Detach Device", command=detach_remote)
attached_listbox = Treeview(columns=ATTACHED_COLUMNS, show="headings")
for col in ATTACHED_COLUMNS:
attached_listbox.heading(col, text=col.title())
attached_devices = list_attached_usb()
for device in attached_devices:
attached_listbox.insert("", "end", values=device)
attached_list_label.grid(column=0, row=0, padx=10)
attached_list_refresh_button.grid(column=1, row=0, padx=10)
detach_button.grid(column=2, row=0, padx=10)
attached_control_frame.grid(column=0, row=4, sticky="ew", pady=10)
attached_listbox.grid(column=0, row=5, sticky="ew", pady=10)
#TODO package shit up into classes and functions
root.mainloop()