Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit aff07b6

Browse files
committed
addons update
1 parent e18730a commit aff07b6

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

browserup-proxy-core/src/main/resources/mitmproxy/har_dump.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ def __init__(self):
187187
self.response_receive_started_nanos = 0
188188
self.http_connect_timings = {}
189189

190+
def get_har_entry(self, flow):
191+
return flow.request.har_entry
192+
190193
def get_har(self, clean_har):
191194
if clean_har:
192195
return self.new_har(DEFAULT_PAGE_REF, DEFAULT_PAGE_TITLE)
@@ -511,9 +514,9 @@ def consume_http_connect_timing(self, client_conn):
511514
def populate_har_entry_with_default_response(self, flow):
512515
full_url = self.get_full_url(flow.request)
513516

514-
ctx.log.debug('Creating new har entry for request: {}'.format(full_url))
517+
ctx.log.debug('Populating har entry for request: {}'.format(full_url))
515518

516-
har_entry = flow.metadata['har_entry']
519+
har_entry = self.get_har_entry(flow)
517520

518521
har_entry['pageref'] = self.get_current_page_ref()
519522
har_entry['startedDateTime'] = datetime.fromtimestamp(flow.request.timestamp_start, timezone.utc).isoformat()
@@ -536,6 +539,8 @@ def request(self, flow):
536539
if 'WhiteListFiltered' in flow.metadata or 'BlackListFiltered' in flow.metadata:
537540
return
538541

542+
har_entry = self.get_har_entry(flow)
543+
539544
self.populate_har_entry_with_default_response(flow)
540545

541546
req_url = 'none'
@@ -555,11 +560,10 @@ def request(self, flow):
555560
if HarCaptureTypes.RESPONSE_CONTENT in self.har_capture_types:
556561
self.capture_request_content(flow)
557562

558-
har_entry = flow.metadata['har_entry']
559563
har_entry['request']['bodySize'] = \
560564
len(flow.request.raw_content) if flow.request.raw_content else 0
561565

562-
connect_timing = self.consume_http_connect_timing(flow.client_conn)
566+
connect_timing = self.consume_http_connect_timing(flow)
563567
if connect_timing is not None:
564568
har_entry['timings']['sslNanos'] = connect_timing['sslHandshakeTimeNanos']
565569
har_entry['timings']['connectNanos'] = connect_timing['connectTimeNanos']
@@ -589,7 +593,7 @@ def capture_request_content(self, flow):
589593
}
590594

591595
def response(self, flow):
592-
har_entry = flow.metadata['har_entry']
596+
har_entry = self.get_har_entry(flow)
593597

594598
ctx.log.debug('Incoming response for request to url: {}'.format(flow.request.url))
595599

@@ -669,6 +673,8 @@ def response(self, flow):
669673
har_entry["serverIPAddress"] = str(
670674
flow.server_conn.ip_address[0])
671675

676+
ctx.log.debug('Populated har entry for response: {}, entry: {}'.format(flow.request.url, str(har_entry)))
677+
672678
def calculate_timings(self, connect_time, flow, ssl_time):
673679
timings_raw = {
674680
'sendNanos': flow.request.timestamp_end - flow.request.timestamp_start,

browserup-proxy-core/src/main/resources/mitmproxy/http_connect_capture.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
import json
33
import asyncio
44

5+
import typing
6+
57
from mitmproxy import ctx
8+
from mitmproxy import connections
69
from mitmproxy.exceptions import TcpTimeout
710

811
RESOLUTION_FAILED_ERROR_MESSAGE = "Unable to resolve host: "
912
CONNECTION_FAILED_ERROR_MESSAGE = "Unable to connect to host"
1013
RESPONSE_TIMED_OUT_ERROR_MESSAGE = "Response timed out"
1114

15+
# A list of server seen till now is maintained so we can avoid
16+
# using 'connect' time for entries that use an existing connection.
17+
SERVERS_SEEN: typing.Set[connections.ServerConnection] = set()
1218

1319
class HttpConnectCaptureResource:
1420

@@ -56,7 +62,7 @@ def generate_http_connect_timing(self):
5662
# TCP Callbacks
5763

5864
def tcp_resolving_server_address_finished(self, flow):
59-
if 'har_entry' not in flow.metadata:
65+
if not hasattr(flow.request, 'har_entry'):
6066
return
6167
self.populate_dns_timings(flow)
6268
self.dns_resolution_finished_nanos = self.now_time_nanos()
@@ -119,7 +125,15 @@ def proxy_to_server_connection_succeeded(self, f):
119125
else:
120126
self.get_http_connect_timing()['sslHandshakeTimeNanos'] = 0
121127

128+
def init_har_entry(self, flow):
129+
ctx.log.debug("Initializing har entry for flow request: {}".format(str(flow.request)))
130+
setattr(flow.request, 'har_entry', self.har_dump_addon.generate_har_entry())
131+
self.har_dump_addon.append_har_entry(flow.request.har_entry)
132+
122133
def error(self, flow):
134+
if not hasattr(flow.request, 'har_entry'):
135+
self.init_har_entry(flow)
136+
123137
req_host_port = flow.request.host
124138
if flow.request.port != 80:
125139
req_host_port = req_host_port + ':' + str(flow.request.port)
@@ -155,6 +169,25 @@ def populate_server_ip_address(self, flow, original_error):
155169
self.get_har_entry(flow)['serverIPAddress'] = str(
156170
flow.server_conn.ip_address[0])
157171

172+
def populate_connect_and_ssl_timings(self, flow):
173+
ssl_time = -1
174+
connect_time = -1
175+
176+
if flow.server_conn and flow.server_conn not in SERVERS_SEEN:
177+
connect_time = (flow.server_conn.timestamp_tcp_setup -
178+
flow.server_conn.timestamp_start)
179+
connect_time = self.timestamp_to_nanos(connect_time)
180+
181+
if flow.server_conn.timestamp_tls_setup is not None:
182+
ssl_time = (flow.server_conn.timestamp_tls_setup -
183+
flow.server_conn.timestamp_tcp_setup)
184+
ssl_time = self.timestamp_to_nanos(ssl_time)
185+
186+
SERVERS_SEEN.add(flow.server_conn)
187+
har_entry = self.get_har_entry(flow)
188+
har_entry['timings']['sslNanos'] = ssl_time
189+
har_entry['timings']['connectNanos'] = connect_time
190+
158191
def get_resource(self):
159192
return HttpConnectCaptureResource(self)
160193

@@ -179,6 +212,7 @@ def server_to_proxy_response_timed_out(self, flow, req_host_port, original_error
179212
self.create_har_entry_for_failed_connect(flow, msg)
180213
self.populate_timings_for_failed_connect(flow)
181214
self.populate_server_ip_address(flow, original_error)
215+
self.populate_connect_and_ssl_timings(flow)
182216

183217
current_time_nanos = self.now_time_nanos()
184218

@@ -210,7 +244,7 @@ def calculate_total_elapsed_time(self, flow):
210244
return self.nano_to_ms(result)
211245

212246
def get_har_entry(self, flow):
213-
return flow.metadata['har_entry']
247+
return flow.request.har_entry
214248

215249
def get_http_connect_timing(self):
216250
if self.http_connect_timing is None:
@@ -231,6 +265,10 @@ def get_original_exception(flow_error):
231265
def now_time_nanos():
232266
return int(round(time.time() * 1000000000))
233267

268+
@staticmethod
269+
def timestamp_to_nanos(timestamp):
270+
return int(round(timestamp * 1000000000))
271+
234272
@staticmethod
235273
def nano_to_ms(time_nano):
236274
return int(time_nano / 1000000)

browserup-proxy-core/src/main/resources/mitmproxy/init_flow.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ def __init__(self):
4444
def get_resource(self):
4545
return InitFlowResource(self)
4646

47-
def http_connect(self, flow):
48-
if 'har_entry' not in flow.metadata:
49-
self.init_har_entry(flow)
47+
# def http_connect(self, flow):
48+
# if not hasattr(flow.request, 'har_entry'):
49+
# self.init_har_entry(flow)
5050

5151
def request(self, flow):
52-
if 'har_entry' not in flow.metadata:
52+
if not hasattr(flow.request, 'har_entry'):
5353
self.init_har_entry(flow)
5454

5555
def init_har_entry(self, flow):
56-
flow.metadata['har_entry'] = self.har_dump_addon.generate_har_entry()
57-
self.har_dump_addon.append_har_entry(flow.metadata['har_entry'])
56+
ctx.log.debug("Initializing har entry for flow request: {}".format(str(flow.request)))
57+
setattr(flow.request, 'har_entry', self.har_dump_addon.generate_har_entry())
58+
self.har_dump_addon.append_har_entry(flow.request.har_entry)
5859

5960
addons = [
6061
InitFlowAddOn()

browserup-proxy-core/src/test/groovy/com/browserup/bup/mitmproxy/NewHarTest.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class NewHarTest extends MockServerTest {
8585
proxy.setDnsResolvingDelayMs(1000)
8686

8787
proxy.start()
88+
8889
int proxyPort = proxy.getPort()
8990

9091
proxy.newHar()

0 commit comments

Comments
 (0)