Bug 1612790 - Dump domain stats r=Bebe

This patch allows us to dump per-domain hit counts to add more insights in the
proxy usage.

Differential Revision: https://phabricator.services.mozilla.com/D61432

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Tarek Ziadé 2020-02-03 15:03:28 +00:00
Родитель 8da98936d8
Коммит 0360f3134e
2 изменённых файлов: 18 добавлений и 0 удалений

Просмотреть файл

@ -224,6 +224,7 @@ class Mitmproxy(Playback):
args = [ args = [
"-v", "-v",
"--set", "upstream_cert=false", "--set", "upstream_cert=false",
"--set", "upload_dir=" + self.upload_dir,
"--set", "websocket=false", "--set", "websocket=false",
"--set", "server_replay_files={}".format(",".join(recording_paths)), "--set", "server_replay_files={}".format(",".join(recording_paths)),
"--scripts", script, "--scripts", script,

Просмотреть файл

@ -10,6 +10,7 @@ import os
import json import json
import hashlib import hashlib
import urllib import urllib
from collections import defaultdict
import typing import typing
from urllib import parse from urllib import parse
@ -85,6 +86,7 @@ class AlternateServerPlayback:
ctx.master.addons.remove(ctx.master.addons.get("serverplayback")) ctx.master.addons.remove(ctx.master.addons.get("serverplayback"))
self.flowmap = {} self.flowmap = {}
self.configured = False self.configured = False
self.netlocs = defaultdict(int)
def load(self, loader): def load(self, loader):
loader.add_option( loader.add_option(
@ -93,6 +95,10 @@ class AlternateServerPlayback:
[], [],
"Replay server responses from a saved file.", "Replay server responses from a saved file.",
) )
loader.add_option(
"upload_dir", str, "",
"Upload directory",
)
def load_flows(self, flows): def load_flows(self, flows):
""" """
@ -171,8 +177,19 @@ class AlternateServerPlayback:
self.configured = True self.configured = True
self.load_files(ctx.options.server_replay_files) self.load_files(ctx.options.server_replay_files)
def done(self):
if not ctx.options.upload_dir:
return
netlocs = sorted(self.netlocs.items(), key=lambda item: -item[1])
path = os.path.join(ctx.options.upload_dir, "mitm_netlocs.log")
with open(path, "w") as f:
for url, count in netlocs:
f.write("%s: %d\n" % (url, count))
def request(self, f): def request(self, f):
if self.flowmap: if self.flowmap:
parsed_url = urllib.parse.urlparse(parse.unquote(f.request.url))
self.netlocs[parsed_url.netloc] += 1
rflow = self.next_flow(f) rflow = self.next_flow(f)
if rflow: if rflow:
response = rflow.response.copy() response = rflow.response.copy()