| 45 | self.flow = None |
| 46 | |
| 47 | def __call__(self, data): |
| 48 | # End of stream? |
| 49 | if len(data) == 0: |
| 50 | self.done() |
| 51 | return data |
| 52 | |
| 53 | # Just in case the option changes while a stream is in flight |
| 54 | if not ctx.options.save_streamed_data: |
| 55 | return data |
| 56 | |
| 57 | # This is a safeguard but should not be needed |
| 58 | if not self.flow or not self.flow.request: |
| 59 | return data |
| 60 | |
| 61 | if not self.fh: |
| 62 | self.path = datetime.fromtimestamp( |
| 63 | self.flow.request.timestamp_start |
| 64 | ).strftime(ctx.options.save_streamed_data) |
| 65 | self.path = self.path.replace("%+T", str(self.flow.request.timestamp_start)) |
| 66 | self.path = self.path.replace("%+I", str(self.flow.client_conn.id)) |
| 67 | self.path = self.path.replace("%+D", self.direction) |
| 68 | self.path = self.path.replace("%+C", self.flow.client_conn.address[0]) |
| 69 | self.path = os.path.expanduser(self.path) |
| 70 | |
| 71 | parent = Path(self.path).parent |
| 72 | try: |
| 73 | if not parent.exists(): |
| 74 | parent.mkdir(parents=True, exist_ok=True) |
| 75 | except OSError: |
| 76 | logging.error(f"{self.TAG}Failed to create directory: {parent}") |
| 77 | |
| 78 | try: |
| 79 | self.fh = open(self.path, "wb", buffering=0) |
| 80 | except OSError: |
| 81 | logging.error(f"{self.TAG}Failed to open for writing: {self.path}") |
| 82 | |
| 83 | if self.fh: |
| 84 | try: |
| 85 | self.fh.write(data) |
| 86 | except OSError: |
| 87 | logging.error(f"{self.TAG}Failed to write to: {self.path}") |
| 88 | |
| 89 | return data |
| 90 | |
| 91 | |
| 92 | def load(loader): |