Skip to content

Commit

Permalink
add cache script
Browse files Browse the repository at this point in the history
  • Loading branch information
akerl committed Apr 4, 2024
1 parent c85e374 commit d070850
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions files/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from mitmproxy import http
from mitmproxy import ctx

import os
import hashlib

class FrameProxy:
path = "/opt/cache"

def response(self, flow: http.HTTPFlow) -> None:
path = os.path.join(self.path, self.url_to_file(flow.request.path)
if not os.path.isfile(path):
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
with open(path, "w") as cache_file:
file.write(flow.response.text)

def request(self, flow: http.HTTPFlow) -> None:
path = os.path.join(self.path, self.url_to_file(flow.request.path)
if os.path.isfile(path):
with open(path, 'r') as cache_file:
cache_content = cache_file.read()
flow.response = http.HTTPResponse.make(200, cache_content)

def url_to_file(self, path)
url = path.split('?')[0]
filename = hashlib.sha512(url).encode('utf-8').hexdigest()
return filename

addons = [FrameProxy()]

0 comments on commit d070850

Please sign in to comment.