-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
104 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,60 @@ | ||
import glob | ||
import os | ||
import json | ||
from mitmproxy import http | ||
from urllib.parse import ParseResult, urlparse | ||
from typing import BinaryIO | ||
from threading import Lock | ||
|
||
from mitmproxy import http | ||
from mitmproxy import io | ||
import query | ||
|
||
LOG_FILE_PREFIX = "/var/mitmproxy/requests/" | ||
MITM_FILE = os.path.join(LOG_FILE_PREFIX, "requests.mitm") | ||
TXT_FILE = os.path.join(LOG_FILE_PREFIX, "requests.txt") | ||
LOG_DIR = "/var/mitmproxy/requests/" | ||
|
||
|
||
class Writer: | ||
def __init__(self, path: str) -> None: | ||
self.f: BinaryIO = open(path, "ab") | ||
self.w = io.FlowWriter(self.f) | ||
self.req_nr = 1 | ||
for file in glob.glob(os.path.join(LOG_FILE_PREFIX, '*.http')): | ||
def __init__(self) -> None: | ||
self.w = io.FlowWriter() | ||
self.req_nr = 0 | ||
self.lock = Lock() # only for self.req_nr | ||
# clean requests on (re)start | ||
for file in glob.glob(os.path.join(LOG_DIR, '*.http')): | ||
os.remove(file) | ||
|
||
def response(self, flow: http.HTTPFlow) -> None: | ||
self.w.add(flow) | ||
|
||
def done(self): | ||
self.f.close() | ||
|
||
|
||
writer = Writer(MITM_FILE) | ||
|
||
|
||
def parse_json_body(index_name, method, body, ofile): | ||
try: | ||
json_body = json.loads(body) | ||
if 'query' in json_body: | ||
query.parsed_query_json(index_name, method, json_body['query']) | ||
query_body = json_body['query'] | ||
filter_only = ('bool' in query_body and 'filter' in query_body['bool']) | ||
for field in ['must', 'must_not', 'should']: | ||
if field in query_body: | ||
if len(query_body[field]) > 0: | ||
filter_only = False | ||
if filter_only: | ||
ofile.write(b"\n Query filter:\n") | ||
query_body = query_body['bool']['filter'] | ||
else: | ||
ofile.write(b"\n Query:\n") | ||
writer = Writer() | ||
|
||
ofile.write(json.dumps(query_body, indent=2).encode()) | ||
except: | ||
pass | ||
|
||
def record_request(flow: http.HTTPFlow) -> None: | ||
with writer.lock: | ||
writer.req_nr += 1 | ||
cur_req_nr = writer.req_nr | ||
|
||
def record_request(index_name, method, flow: http.HTTPFlow) -> None: | ||
with open(os.path.join(LOG_FILE_PREFIX, str(writer.req_nr) + '.http'), "ab") as ofile: | ||
writer.req_nr += 1 # TODO add atomic | ||
with open(os.path.join(LOG_DIR, str(cur_req_nr) + '.http'), "ab") as ofile: | ||
url = urlparse(flow.request.url) | ||
trimmed_url = ParseResult('', '', *url[2:]).geturl() | ||
print(trimmed_url) | ||
ofile.write((trimmed_url + "\n").encode()) | ||
trimmed_url_to_save = ParseResult('', '', *url[2:]).geturl() # save only e.g. /(index/)/_search | ||
ofile.write((trimmed_url_to_save + "\n").encode()) | ||
|
||
body = flow.request.content.decode('utf-8') | ||
body_json = json.loads(body) | ||
ofile.write(json.dumps(body_json, indent=4).encode()) | ||
# parse_json_body(index_name, method, body, ofile) | ||
|
||
writer.response(flow) | ||
|
||
|
||
def extract_index_name(parsed_url, method): | ||
result = parsed_url.path[:-len(method)] | ||
if result.startswith('/'): | ||
result = result[1:] | ||
|
||
result = result.replace('*', 'X') # For convience, replace wildcard with X | ||
|
||
if len(result) == 0: | ||
return 'default' | ||
return result | ||
|
||
|
||
def request(flow: http.HTTPFlow) -> None: | ||
parsed_url = urlparse(flow.request.url) | ||
url_path = parsed_url.path | ||
search_methods = ['/_search', '/_async_search', '/_terms_enum'] | ||
|
||
for method in search_methods: | ||
if url_path.endswith(method): | ||
index_name = extract_index_name(parsed_url, method) | ||
print(index_name) | ||
if len(index_name) > 0 and index_name[0] != ".": | ||
record_request(index_name, method, flow) | ||
# so far we skip requests with prefixes: . and /. | ||
# maybe that's to be changed | ||
if len(url_path) > 0 and url_path[0] == '.': | ||
break | ||
if len(url_path) > 1 and url_path[:2] == '/.': | ||
break | ||
record_request(flow) | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.