fix: add error message and tune the mitmproxy/har detection logic

pull/20/head
alufers 3 years ago
parent ab827a9bba
commit 065865ef36

@ -2,6 +2,7 @@ import sys
ANSI_RGB = "\033[38;2;{};{};{}m" ANSI_RGB = "\033[38;2;{};{};{}m"
ANSI_RGB_BG = "\033[48;2;{};{};{}m" ANSI_RGB_BG = "\033[48;2;{};{};{}m"
ANSI_RED = "\033[31m"
ANSI_RESET = "\033[0m" ANSI_RESET = "\033[0m"
RAINBOW_COLORS = [ RAINBOW_COLORS = [

@ -14,10 +14,10 @@ def har_archive_heuristic(file_path: str) -> int:
data = f.read(2048) data = f.read(2048)
# if file contains only ascii characters # if file contains only ascii characters
if data.decode('utf-8', 'ignore').isprintable() is True: if data.decode('utf-8', 'ignore').isprintable() is True:
val += 40 val += 25
# if first character is a '{' # if first character is a '{'
if data[0] == '{': if data[0] == '{':
val += 15 val += 23
# if it contains the word '"WebInspector"' # if it contains the word '"WebInspector"'
if b'"WebInspector"' in data: if b'"WebInspector"' in data:
val += 15 val += 15
@ -89,3 +89,5 @@ class HarCaptureReader:
if self.progress_callback: if self.progress_callback:
self.progress_callback(f.tell() / har_file_size) self.progress_callback(f.tell() / har_file_size)
yield HarFlowWrapper(entry) yield HarFlowWrapper(entry)
def name(self):
return 'har'

@ -2,6 +2,9 @@
""" """
Converts a mitmproxy dump file to a swagger schema. Converts a mitmproxy dump file to a swagger schema.
""" """
import os
import sys
import traceback
from mitmproxy.exceptions import FlowReadException from mitmproxy.exceptions import FlowReadException
import json import json
import argparse import argparse
@ -37,7 +40,12 @@ def progress_callback(progress):
def detect_input_format(file_path): def detect_input_format(file_path):
if har_archive_heuristic(file_path) > mitmproxy_dump_file_huristic(file_path): har_score = har_archive_heuristic(file_path)
mitmproxy_score = mitmproxy_dump_file_huristic(file_path)
if 'MITMPROXY2SWAGGER_DEBUG' in os.environ:
print('har score: ' + str(har_score))
print('mitmproxy score: ' + str(mitmproxy_score))
if har_score > mitmproxy_score:
return HarCaptureReader(file_path, progress_callback) return HarCaptureReader(file_path, progress_callback)
return MitmproxyCaptureReader(file_path, progress_callback) return MitmproxyCaptureReader(file_path, progress_callback)
@ -59,7 +67,7 @@ def main():
yaml = ruamel.yaml.YAML() yaml = ruamel.yaml.YAML()
caputre_reader = None caputre_reader = None
if args.format == 'flow': if args.format == 'flow' or args.format == 'mitmproxy':
caputre_reader = MitmproxyCaptureReader(args.input, progress_callback) caputre_reader = MitmproxyCaptureReader(args.input, progress_callback)
elif args.format == 'har': elif args.format == 'har':
caputre_reader = HarCaptureReader(args.input, progress_callback) caputre_reader = HarCaptureReader(args.input, progress_callback)
@ -204,6 +212,14 @@ def main():
except FlowReadException as e: except FlowReadException as e:
print(f"Flow file corrupted: {e}") print(f"Flow file corrupted: {e}")
except ValueError as e:
print(f"ValueError: {e}")
# print stack trace
traceback.print_exception(*sys.exc_info())
print(f"{console_util.ANSI_RED}Failed to parse the input file as '{caputre_reader.name()}'. ")
if not args.format:
print(f"It might happen that the input format as incorrectly detected. Please try using '--fromat flow' or '--format har' to specify the input format.{console_util.ANSI_RESET}")
sys.exit(1)
new_path_templates.sort() new_path_templates.sort()

@ -86,3 +86,5 @@ class MitmproxyCaptureReader:
yield MitmproxyFlowWrapper(f) yield MitmproxyFlowWrapper(f)
except FlowReadException as e: except FlowReadException as e:
print(f"Flow file corrupted: {e}") print(f"Flow file corrupted: {e}")
def name(self):
return "flow"

Loading…
Cancel
Save