forked from eReader/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
executable file
·131 lines (106 loc) · 3.9 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
# Copyright (C) 2014 Claudio Guarnieri.
# This file is part of Detekt - https://github.com/botherder/detekt
# See the file 'LICENSE' for copying permission.
import sys
import Queue
import random
import threading
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from bottle import *
from utils import get_resource, check_connection
import detector
# Add the gui folder to the template path of bottle.py.
TEMPLATE_PATH.insert(0, get_resource('gui'))
# Instatiate the Bottle application.
webapp = Bottle()
# This queue will contain the list of matches retrieved from the yara scan.
queue_results = Queue.Queue()
# This queue will contain the list of errors generated by the detector.
queue_errors = Queue.Queue()
# Instatiate the thread that will run the detector.
scanner = threading.Thread(target=detector.main, args=(queue_results, queue_errors))
scanner.daemon = True
# Enabled language.
lang = 'en'
# Port for the web server.
web_port = random.randint(1024, 65535)
# This route serves static content such as images, css and js files.
@webapp.route('/static/<path:path>')
def static(path):
return static_file(path, get_resource('gui/static/'))
# This route returns the start page.
@webapp.route('/')
def index():
connection = check_connection()
return template('index.{0}'.format(lang), language=lang, action='start', connection=connection)
@webapp.route('/language', method='POST')
def language():
global lang
lang = request.forms.get('language')
redirect('/')
# This route triggers the execution of the detector then returns the running
# page which will then start refreshing to /check.
@webapp.route('/scan')
def scan():
scanner.start()
return template('index.{0}'.format(lang), action='running')
# This route checks whether the scanner thread has finished, and if so it
# will collect errors and results and return the results page.
@webapp.route('/check')
def check():
if scanner.isAlive():
return template('index.{0}'.format(lang), action='running')
else:
# Flag if the detector generated any matches.
infected = False
if queue_results.qsize() > 0:
infected = True
# Populate the list of errors from the queue.
errors = []
while True:
try:
errors.append(queue_errors.get(block=False))
except Queue.Empty:
break
# Populate the list of results from the queue.
results = []
while True:
try:
# Retrieve next entry in the results queue.
entry = queue_results.get(block=False)
# Only add the detection name to the list of results.
# This is to avoid to have the rules names in there, which
# will mostly be meaningless to a regular user.
if entry['detection'] not in results:
results.append(entry['detection'])
except Queue.Empty:
break
return template('index.{0}'.format(lang), action='results', infected=infected,
errors=errors, results=results)
# This thread will run the bottle.py web app.
class WebApp(QThread):
def __init__(self):
QThread.__init__(self)
def run(self):
run(webapp, host='localhost', port=web_port, quiet=True)
# Define the Qt window, resizable and that connect to the bottle.py app.
class Window(QWebView):
def __init__(self):
QWebView.__init__(self)
self.setWindowTitle('Detekt')
self.resize(640, 500)
self.load(QUrl('http://localhost:{0}/'.format(web_port)))
def main():
# Initiate the Qt application.
app = QApplication(sys.argv)
# Instantiate and start the bottle.py application.
web = WebApp()
web.start()
# Instantiate and show the Qt window.
win = Window()
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()