-
Notifications
You must be signed in to change notification settings - Fork 0
/
HARtree.py
executable file
·189 lines (138 loc) · 5.78 KB
/
HARtree.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
#!/usr/bin/python2
# -*- coding: utf-8 -*-
"""
HARtree
author: René Kliment < rene (at) renekliment.cz >
"""
import sys
import json
import os.path
import argparse
import tempfile
from operator import itemgetter
from HARtree.fetcher import HARfetcher
from HARtree.helperFile import HARhelperFile
from HARtree.easyList import HAReasyList
from HARtree.loadAndDisplay import *
try:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
except ImportError:
sys.exit("PyQt5 not found!")
with open('config.json') as config_file:
config = json.load(config_file)
if not config['fetchEngines']['defaultHarStorage']:
config['fetchEngines']['defaultHarStorage'] = tempfile.gettempdir()
def on_keyHighlightPressed():
global helperFile
id = int(view.selectionModel().currentIndex().sibling(view.selectionModel().currentIndex().row(), 0).data())
if id in helperFile.data["highlightedEntries"]:
helperFile.data["highlightedEntries"].remove(id)
idToIdWidget[id].setBackground(QBrush(QColor("white")))
else:
helperFile.data["highlightedEntries"].append(id)
idToIdWidget[id].setBackground(QBrush(QColor("gray")))
helperFile.save()
class customQTreeView(QTreeView):
keyHighlightPressed = pyqtSignal()
def keyPressEvent(self, event):
super(customQTreeView, self).keyPressEvent(event)
if (event.key() == Qt.Key_Space):
self.keyHighlightPressed.emit()
execfile("./HARtree/displayDetail.py")
if __name__ == '__main__':
# command line arguments parsing
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-f", "--file", help='path to a HAR file', default="")
group.add_argument("-u", "--url", help='generate a HAR file, save it and display it', default="")
group.add_argument('resource', nargs='?')
parser.add_argument("-e", "--fetch-engine", help='engine for fetching HARs (ph, bs)', default="browsermobproxy+selenium")
parser.add_argument("-b", "--engine-browser", help='browser for the engine (ff, ch)', default="firefox")
parser.add_argument("-l", "--use-easy-list", help='use an easylist to highlight URLs', action="store_true")
parser.add_argument("-d", "--save-directory", help='directory for saving HAR files, defaults to system temp directory or a configured directory (' + config['fetchEngines']['defaultHarStorage'] + ')', default=config['fetchEngines']['defaultHarStorage'])
args = parser.parse_args()
harFileName = ""
if (args.resource):
if os.path.isfile(args.resource):
harFileName = args.resource
elif args.resource.startswith("http://") or args.resource.startswith("https://"):
if args.fetch_engine in HARfetcher.supportedEngines and args.engine_browser in HARfetcher.supportedBrowsers:
harFileName = HARfetcher.fetch(args.resource, config, args.save_directory, args.fetch_engine, args.engine_browser)
else:
sys.exit("Unsupported engine.")
else:
sys.exit("Resource not recognized / found - use proper command line arguments.")
elif (args.file):
if (os.path.isfile(args.file)):
harFileName = args.file
else:
sys.exit("File " + args.file + " does not exist!");
elif (args.url):
if (args.fetch_engine in HARfetcher.supportedEngines) and (args.engine_browser in HARfetcher.supportedBrowsers):
harFileName = HARfetcher.fetch(args.url, config, args.save_directory, args.fetch_engine, args.engine_browser)
else:
sys.exit("Unsupported engine / browser.")
if not harFileName:
sys.exit("No HAR file. Use some params ...")
useEasyList = args.use_easy_list
# GUI
app = QApplication(sys.argv)
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['#', 'Status', 'MIME', 'URL', 'redirectURL', 'Referer'])
view = customQTreeView()
view.setSelectionBehavior(QAbstractItemView.SelectRows)
view.setModel(model)
view.setUniformRowHeights(True)
layout = QSplitter()
layout.addWidget(view)
layout.setOrientation(Qt.Vertical)
layout.setWindowTitle('HARtree')
layout.showMaximized()
# display data
har = HARtree_loadFile(harFileName)
sortedEntries = sorted(har['log']['entries'], key=itemgetter('startedDateTime'))
idToIdWidget = {}
easyList = None
if (useEasyList):
easyList = HAReasyList(config['adblockparser']['easylist_file'])
HARtree_populateData(har, sortedEntries, model, idToIdWidget, useEasyList, easyList)
helperFile = HARhelperFile(harFileName + ".hh")
for id in helperFile.data['highlightedEntries']:
idToIdWidget[id].setBackground(QBrush(QColor("gray")))
view.expandAll()
view.resizeColumnToContents(0)
view.resizeColumnToContents(1)
view.resizeColumnToContents(2)
view.setColumnWidth(3, 300)
view.setColumnWidth(4, 300)
view.setColumnWidth(5, 300)
selmod = view.selectionModel()
detailModels = {}
detailViews = {}
detailParts = ("request", "response", "timings")
for item in detailParts:
detailModels[item] = QStandardItemModel()
detailModels[item].setHorizontalHeaderLabels(['Name', 'Value'])
detailViews[item] = QTreeView()
detailViews[item].setModel(detailModels[item])
responseContentWidget = QWidget()
responseContentLayout = QHBoxLayout()
responseContentWidget.setLayout(responseContentLayout)
responseContentTreeModel = QStandardItemModel()
responseContentTreeModel.setHorizontalHeaderLabels(['Name', 'Value'])
responseContentTree = QTreeView()
responseContentTree.setModel(responseContentTreeModel)
responseContentLayout.addWidget(responseContentTree)
tabs = QTabWidget()
tabs.addTab(detailViews["request"], "Request")
tabs.addTab(detailViews["response"], "Response")
tabs.addTab(responseContentWidget ,"Response Content")
tabs.addTab(detailViews["timings"], "Timings")
layout.addWidget(tabs)
layout.setStretchFactor(0, 1);
layout.setStretchFactor(1, 0);
selmod.currentRowChanged.connect(HARtree_displayDetail)
view.keyHighlightPressed.connect(on_keyHighlightPressed)
sys.exit(app.exec_())