-
Notifications
You must be signed in to change notification settings - Fork 0
Fix 2 issues #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mikekap
wants to merge
5
commits into
master
Choose a base branch
from
fix-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fix 2 issues #3
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,6 +27,8 @@ | |||||||||||
|
|
||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||
|
|
||||||||||||
| MAX_BODY_SIZE = 10 * 1024 * 1024 | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def check_tshark_version(): | ||||||||||||
| """Check tshark version and log warning if <= 4.4.10.""" | ||||||||||||
|
|
@@ -107,7 +109,7 @@ class HttpResponse: | |||||||||||
| body: bytes = b"" | ||||||||||||
| compressionSaved: int = 0 | ||||||||||||
|
|
||||||||||||
| def to_har_response(self): | ||||||||||||
| def to_har_response(self, max_body_size=MAX_BODY_SIZE): | ||||||||||||
| """Convert this HTTP response to HAR format.""" | ||||||||||||
| return { | ||||||||||||
| "status": self.status, | ||||||||||||
|
|
@@ -124,6 +126,7 @@ def to_har_response(self): | |||||||||||
| **content_to_json( | ||||||||||||
| first(self.headers.get("content-type", [])), | ||||||||||||
| self.body, | ||||||||||||
| max_body_size=max_body_size, | ||||||||||||
| ), | ||||||||||||
| }, | ||||||||||||
| } | ||||||||||||
|
|
@@ -170,14 +173,14 @@ def __str__(self): | |||||||||||
| s += ")" | ||||||||||||
| return s | ||||||||||||
|
|
||||||||||||
| def to_har_entry(self, cid): | ||||||||||||
| def to_har_entry(self, cid, max_body_size=MAX_BODY_SIZE): | ||||||||||||
| """Convert this HTTP session to a HAR entry.""" | ||||||||||||
| return { | ||||||||||||
| "startedDateTime": unix_ts_to8601(self.request.startTimestamp), | ||||||||||||
| "time": (self.maxPacketTs - self.request.startTimestamp) * 1000.0, | ||||||||||||
| "serverIPAddress": self.remoteAddress.rsplit(":", 1)[0], | ||||||||||||
| "request": self.request.to_har_request(), | ||||||||||||
| "response": self.response.to_har_response(), | ||||||||||||
| "response": self.response.to_har_response(max_body_size=max_body_size), | ||||||||||||
| "_resourceType": "websocket" if self.websocketMessages else None, | ||||||||||||
| "_webSocketMessages": ( | ||||||||||||
| [m.to_har_message() for m in self.websocketMessages] | ||||||||||||
|
|
@@ -229,8 +232,19 @@ def to_har_timings(self): | |||||||||||
| type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]), | ||||||||||||
| help="Set the logging level.", | ||||||||||||
| ) | ||||||||||||
| @click.option( | ||||||||||||
| "--max-body-size", | ||||||||||||
| default=MAX_BODY_SIZE, | ||||||||||||
| type=int, | ||||||||||||
| help="Maximum response body size to include in HAR (in bytes).", | ||||||||||||
| ) | ||||||||||||
| def main( | ||||||||||||
| pcap_file: Path, output: str = None, pretty=False, log_level="INFO", check="warning" | ||||||||||||
| pcap_file: Path, | ||||||||||||
| output: str = None, | ||||||||||||
| pretty=False, | ||||||||||||
| log_level="INFO", | ||||||||||||
| check="warning", | ||||||||||||
| max_body_size=MAX_BODY_SIZE, | ||||||||||||
| ): | ||||||||||||
| """Convert PCAP file to HAR format""" | ||||||||||||
|
|
||||||||||||
|
|
@@ -253,7 +267,12 @@ def main( | |||||||||||
| if not run_consistency_checks(conv_details, fatal=check == "error"): | ||||||||||||
| sys.exit(-1) | ||||||||||||
|
|
||||||||||||
| js = to_har_json(conv_details, comment=f"From {pcap_file}", fatal=check == "error") | ||||||||||||
| js = to_har_json( | ||||||||||||
| conv_details, | ||||||||||||
| comment=f"From {pcap_file}", | ||||||||||||
| fatal=check == "error", | ||||||||||||
| max_body_size=max_body_size, | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| logger.info(f"Writing {len(conv_details)} conversations to {output_path}") | ||||||||||||
| with click.open_file(output_path, "w") as fp: | ||||||||||||
|
|
@@ -278,9 +297,10 @@ def log_fn(*args, **kwargs): | |||||||||||
| if content_length and int(content_length[0]) > 0 and not conv.request.body: | ||||||||||||
| log_fn(f"{conv!s}: Missing request body") | ||||||||||||
|
|
||||||||||||
| content_length = conv.response.headers.get("content-length") | ||||||||||||
| if content_length and int(content_length[0]) > 0 and not conv.response.body: | ||||||||||||
| log_fn(f"{conv!s}: Missing response body") | ||||||||||||
| if conv.request.method != "HEAD": | ||||||||||||
| content_length = conv.response.headers.get("content-length") | ||||||||||||
| if content_length and int(content_length[0]) > 0 and not conv.response.body: | ||||||||||||
| log_fn(f"{conv!s}: Missing response body") | ||||||||||||
|
|
||||||||||||
| content_type = conv.response.headers.get("content-type") | ||||||||||||
| if ( | ||||||||||||
|
|
@@ -308,6 +328,8 @@ def read_pcap_file(pcap_file): | |||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| conv_details = defaultdict(HttpSession) | ||||||||||||
| http1_sequence_counters = defaultdict(int) | ||||||||||||
| http1_last_request_direction = {} | ||||||||||||
|
|
||||||||||||
| def unnest(packet): | ||||||||||||
| return ((layer, packet) for layer in packet.layers) | ||||||||||||
|
|
@@ -329,11 +351,21 @@ def unnest(packet): | |||||||||||
| port = packet.tcp.dstport | ||||||||||||
| http_version = "HTTP/2" | ||||||||||||
| elif layer.layer_name == "http": | ||||||||||||
| full_stream_id = ("1", packet.tcp.stream) | ||||||||||||
| tcp_stream = packet.tcp.stream | ||||||||||||
|
|
||||||||||||
| if layer.get_field("request_line"): | ||||||||||||
| if conv_details[ | ||||||||||||
| (1, tcp_stream, http1_sequence_counters[tcp_stream]) | ||||||||||||
| ].request.url: | ||||||||||||
| # This is a new request on the same connection, increment sequence | ||||||||||||
| http1_sequence_counters[tcp_stream] += 1 | ||||||||||||
|
|
||||||||||||
| full_stream_id = ("1", tcp_stream, http1_sequence_counters[tcp_stream]) | ||||||||||||
|
Comment on lines
+362
to
+363
|
||||||||||||
| full_stream_id = ("1", tcp_stream, http1_sequence_counters[tcp_stream]) | |
| full_stream_id = current_session_id | |
| else: | |
| full_stream_id = ("1", tcp_stream, http1_sequence_counters[tcp_stream]) |
This file contains hidden or 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 hidden or 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 hidden or 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
Binary file not shown.
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The indentation change moves the response body check inside the HEAD method guard, but the request body check above (lines 296-298) is not similarly guarded. Consider documenting why HEAD responses specifically need this special handling, or extract this logic into a helper function for clarity.