-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
189 lines (152 loc) · 5.54 KB
/
main.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
import sys
import re
import tools
from log import LogViewer
from torrc_viewer import TorrcViewer
from PySide import QtGui, QtCore
class Communicate(QtCore.QObject):
# msg displayed in the status bar
status_msg = QtCore.Signal(str)
# output from tor process
bootstrap_msg = QtCore.Signal(str)
class Cipollini(QtGui.QMainWindow):
def __init__(self):
super(Cipollini, self).__init__()
# intialise widgets
self.main_widget = MainWidget(self)
self.setCentralWidget(self.main_widget)
# intialise status bar
self.status_bar = self.statusBar()
self.main_widget.btn_frame.comms.status_msg.connect(self.status_bar.showMessage)
# fix size and position
self.setGeometry(500, 500, 350, 250)
self.center()
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('CDE'))
self.setWindowTitle('Cipollini')
self.draw_bg()
self.show()
def center(self):
screen = QtGui.QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width()-size.width())/2,
(screen.height()-size.height())/2)
def draw_bg(self):
bg = QtGui.QImage('./images/bg.png')
palette = QtGui.QPalette()
palette.setBrush(QtGui.QPalette.Background, bg)
self.setPalette(palette)
class MainWidget(QtGui.QWidget):
def __init__(self, parent):
super(MainWidget, self).__init__()
self.init()
def init(self):
# initialise tool tips
QtGui.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
self.status_frame = StatusFrame(self)
self.btn_frame = BtnFrame(self)
self.btn_frame.comms.bootstrap_msg.connect(self.status_frame.update_progressbar)
self.btn_frame.comms.bootstrap_msg.connect(self.btn_frame.log_viewer.update_log)
# place them all vertically
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.status_frame)
vbox.addWidget(self.btn_frame)
self.setLayout(vbox)
class StatusFrame(QtGui.QFrame):
def __init__(self, parent):
super(StatusFrame, self).__init__()
self.init()
def init(self):
# progress bar
self.pbar = QtGui.QProgressBar(self)
self.pbar.resize(self.pbar.sizeHint())
self.pbar.setValue(0)
self.pbar.setTextVisible(True)
self.setFrameStyle(QtGui.QFrame.StyledPanel)
# place them all vertically
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.pbar)
self.setLayout(vbox)
@QtCore.Slot(str)
def update_progressbar(self, msg):
if msg == "Stopped Tor":
self.pbar.setValue(0)
else:
progress = tools.parse_bootstrap_msg(msg)
if progress:
self.pbar.setValue(progress)
class BtnFrame(QtGui.QFrame):
def __init__(self, parent):
self.tor_process = None
self.torrc_path = None
self.tor_start = False
self.log_viewer = LogViewer(self)
self.comms = Communicate()
super(BtnFrame, self).__init__()
self.init()
def init(self):
# button to start/stop Tor
main_btn = QtGui.QPushButton('Start Tor', self)
main_btn.clicked.connect(self.main_btn_clicked)
main_btn.resize(main_btn.sizeHint())
# show logs
log_btn = QtGui.QPushButton('Show Logs', self)
log_btn.clicked.connect(self.show_logs)
log_btn.resize(log_btn.sizeHint())
# show torrc
torrc_btn = QtGui.QPushButton('Show Torrc', self)
torrc_btn.clicked.connect(self.show_torrc)
torrc_btn.resize(torrc_btn.sizeHint())
# button to kill cipollini
quit_btn = QtGui.QPushButton('Quit', self)
quit_btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
quit_btn.resize(quit_btn.sizeHint())
# initialise a horizontal box layout
hbox = QtGui.QHBoxLayout()
hbox.addWidget(main_btn)
hbox.addWidget(log_btn)
hbox.addWidget(torrc_btn)
hbox.addWidget(quit_btn)
self.setLayout(hbox)
self.setFrameStyle(QtGui.QFrame.StyledPanel)
def read_tor_msg(self):
msg = str(self.tor_process.readAll())
self.comms.bootstrap_msg.emit(msg.strip())
if "100%" in msg:
self.comms.status_msg.emit("Started Tor")
def start_tor(self, main_btn):
self.tor_start = True
self.tor_process, self.torrc_path = tools.launch_tor()
self.tor_process.readyReadStandardOutput.connect(self.read_tor_msg)
main_btn.setText("Stop Tor")
self.comms.status_msg.emit("Bootstrapping Tor")
def stop_tor(self, main_btn):
# stop Tor
self.tor_process.kill()
self.tor_start = False
self.comms.status_msg.emit("Stopped Tor")
self.comms.bootstrap_msg.emit("Stopped Tor")
main_btn.setText("Start Tor")
def main_btn_clicked(self):
main_btn = self.sender()
if not self.tor_start:
self.start_tor(main_btn)
else:
self.stop_tor(main_btn)
def show_logs(self):
#XXX: maybe create log_viewer only
# after log_btn is clicked?
# memory wastage?
if self.log_viewer:
self.log_viewer.show()
else:
# raise Exception
pass
def show_torrc(self):
self.torrc_viewer = TorrcViewer(self.torrc_path)
self.torrc_viewer.show()
def main():
app = QtGui.QApplication(sys.argv)
cipollini = Cipollini()
sys.exit(app.exec_())
if __name__ == '__main__':
main()