MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / feed_payload

Method feed_payload

irc_dpi.py:273–335  ·  view source on GitHub ↗

Append `payload` to the per-flow buffer and parse complete lines. Returns the list of parsed `IRCMessage` objects produced by this call (zero or more).

(self, src_ip: str, dst_ip: str,
                     src_port: int, dst_port: int,
                     ts: float, payload: bytes)

Source from the content-addressed store, hash-verified

271 # ------------------------------------------------------------------ #
272
273 def feed_payload(self, src_ip: str, dst_ip: str,
274 src_port: int, dst_port: int,
275 ts: float, payload: bytes) -> List[IRCMessage]:
276 """Append `payload` to the per-flow buffer and parse complete lines.
277
278 Returns the list of parsed `IRCMessage` objects produced by this
279 call (zero or more).
280 """
281 if not payload:
282 return []
283
284 # Determine direction by which port matches a configured IRC port.
285 if dst_port in self.ports and src_port not in self.ports:
286 direction = 'c2s'
287 client_ip, server_ip, server_port = src_ip, dst_ip, dst_port
288 elif src_port in self.ports and dst_port not in self.ports:
289 direction = 's2c'
290 client_ip, server_ip, server_port = dst_ip, src_ip, src_port
291 else:
292 # Neither side looks like IRC — bail.
293 return []
294
295 flow_key = (src_ip, dst_ip, src_port, dst_port)
296 buf = self._buffers.setdefault(flow_key, bytearray())
297 if len(buf) + len(payload) > self.MAX_BUFFER_PER_FLOW:
298 # Garbage / binary protocol on this port — reset to avoid OOM.
299 buf.clear()
300 buf += payload
301
302 out: List[IRCMessage] = []
303 while True:
304 # IRC lines terminate with CRLF, but be lenient and accept LF.
305 idx = buf.find(b'\n')
306 if idx < 0:
307 break
308 line_bytes = bytes(buf[:idx]).rstrip(b'\r')
309 del buf[:idx + 1]
310 if not line_bytes:
311 continue
312 # Reject obviously-non-text frames quickly (likely TLS or junk).
313 if any(b < 9 for b in line_bytes[:8]):
314 # Binary control chars at the start -> not IRC
315 buf.clear()
316 break
317 try:
318 text = line_bytes.decode('utf-8', errors='replace')
319 except Exception:
320 continue
321 parsed = parse_irc_line(text)
322 if parsed is None:
323 continue
324 prefix, command, params = parsed
325 msg = IRCMessage(ts=ts, direction=direction, prefix=prefix,
326 command=command, params=params, raw=text)
327 sess = self._update_session(client_ip, server_ip, server_port,
328 ts, msg)
329 out.append(msg)
330 if self._on_event:

Callers 4

_handle_tshark_lineMethod · 0.95
_feedFunction · 0.80

Calls 7

_update_sessionMethod · 0.95
parse_irc_lineFunction · 0.85
IRCMessageClass · 0.85
appendMethod · 0.80
debugMethod · 0.80
clearMethod · 0.45
decodeMethod · 0.45

Tested by 3

_feedFunction · 0.64