-
Notifications
You must be signed in to change notification settings - Fork 5
/
kivy_ui.py
191 lines (156 loc) · 6.55 KB
/
kivy_ui.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
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.scrollview import ScrollView
from kivy.clock import Clock
from kivy.graphics import Color, Rectangle
#import packet_watcher
import packet_limiter
import scapy_watcher as packet_watcher
from kivy.config import Config
Config.set('graphics', 'width', '1000')
Config.set('graphics', 'height', '1000')
class PortInfo(BoxLayout):
def __init__(self,port, item, **kwargs):
super(PortInfo, self).__init__(**kwargs)
i = item#kwargs['item']
self.net_data = i
#port = kwargs['port']
self.port_label = Label(text=str(port))
self.total_label = Label()
self.raw_speed_label = Label()
self.speed_label = Label()
self.add_widget(self.port_label)
self.add_widget(self.total_label)
self.add_widget(self.raw_speed_label)
self.add_widget(self.speed_label)
self.up_limit = TextInput()
self.down_limit = TextInput()
self.down_limit.disabled = True
self.enable_limit = ToggleButton(text="limit?")
self.add_widget(self.up_limit)
self.add_widget(self.down_limit)
self.add_widget(self.enable_limit)
self.Blue = port & 40
self.Green = (port >> 8) & 40
self.Red = (port >> 16) & 40
with self.canvas.before:
Color(self.Red/40.0, self.Green/40.0, self.Blue/40.0, .5) # green; colors range from 0-1 not 0-255
self.rect = Rectangle(size=self.size, pos=self.pos)
self.bind(size=self._update_rect, pos=self._update_rect)
def _update_rect(self, instance, value):
self.rect.pos = instance.pos
self.rect.size = instance.size
def update(self, v):
self.net_data = v
self.speed_label.text = "{:.1f}".format(v['speed'] / 1000.0)
self.total_label.text = "{:.1f}".format(v['total'] / 1000.0)
self.raw_speed_label.text = "{:.1f}".format(v['speed_raw'] / 1000.0)
sort_key = 'speed'
class TableHeader(BoxLayout):
def __init__(self, **kwargs):
super(TableHeader, self).__init__(**kwargs)
self.port_label = Button(text="port", on_release=self.set_sort)
self.port_label.sort_key = "port"
self.total_label = Button(text="total\nkb", on_release=self.set_sort)
self.total_label.sort_key = "total"
self.raw_speed_label = Button(text="raw\nspd kb", on_release=self.set_sort)
self.raw_speed_label.sort_key = "speed_raw"
self.speed_label = Button(text="speed\nkb", on_release=self.set_sort)
self.speed_label.sort_key = "speed"
self.add_widget(self.port_label)
self.add_widget(self.total_label)
self.add_widget(self.raw_speed_label)
self.add_widget(self.speed_label)
self.up_limit_label = Label(text="up\nlimit")
self.down_limit_label = Label(text="down\nlimit")
self.enable_limit_label = Label(text="enable\nlimit")
self.add_widget(self.up_limit_label)
self.add_widget(self.down_limit_label)
self.add_widget(self.enable_limit_label)
self.size_hint_y = 0.1
def set_sort(self, obj):
global sort_key
sort_key = obj.sort_key
print(sort_key)
'''def cmp_PI(ix, iy):
x = ix.net_data[sort_key]
y = iy.net_data[sort_key]
if x > y:
return 1
elif x == y:
return 0
else: # x < y
return -1'''
class MainView(GridLayout):
def __init__(self, **kwargs):
kwargs['cols'] = 1
super(MainView, self).__init__(**kwargs)
self.main_table = BoxLayout(orientation='vertical')
self.main_table.add_widget(TableHeader())
self.main_list = GridLayout(cols=1, spacing=1, row_default_height= '30dp', row_force_default= True, size_hint_y=None)
self.main_list.bind(minimum_height=self.main_list.setter('height'))
self.scroll_view = ScrollView()
self.main_table.add_widget(self.scroll_view)
self.scroll_view.add_widget(self.main_list)
self.add_widget(Label(text="LinNetLim\n press apply to limit selected ports", size_hint_y=.15))
self.add_widget(self.main_table)
Clock.schedule_interval(self.update_cb, 0.5)
self.connected_widgets = {}
self.info_panel = BoxLayout(orientation='vertical', size_hint_x=.2)
applybtn = Button(text='Apply', size_hint_y=.1)
applybtn.bind(on_release=self.apply_limits)
self.add_widget(applybtn)
unapplybtn = Button(text='Un-Apply', size_hint_y=.1)
unapplybtn.bind(on_release=self.clear_limits)
self.add_widget(unapplybtn)
#self.info_panel.add_widget(clearbtn)
#generate a list of ports with up/down limit and pass to the packet limiter
def apply_limits(self, obj):
indata = []
for r in self.main_list.children:
if r.enable_limit.state == 'down':
try:
ul = int(r.up_limit.text)
except:
print("error, missing up limit, setting to 1000000")
ul = 1000000
try:
dl = int(r.down_limit.text)
except:
print("error, missing down limit, setting to 1000000")
dl = 1000000
indata.append({
"port": int(r.port_label.text),
"up_limit": ul,
"down_limit": dl
})
packet_limiter.set_from_ports_list(indata)
def clear_limits(self, obj):
packet_limiter.reset_all()
#read list of ports, with raw_speed, total and speed
def update_cb(self, dt):
for k in list(packet_watcher.portcounts.keys()):
v = packet_watcher.portcounts[k]
w = self.connected_widgets.get(k, None)
if not w:
w = PortInfo(port=k, item=v, height=100)
self.connected_widgets[k] = w
self.main_list.add_widget(w)
w.update(v)
#list(self.main_list.children).sort(key=cmp_PI)
super(self.main_list.children.__class__, self.main_list.children).sort(key=lambda child: child.net_data[sort_key])
class NetLimitApp(App):
def build(self):
parent = MainView()
packet_watcher.start_background_thread()
self.mainwidget = parent
return parent
mainapp = NetLimitApp()
if __name__ == '__main__':
mainapp.run()