MCPcopy
hub / github.com/mitmproxy/mitmproxy / NextLayer

Class NextLayer

mitmproxy/addons/next_layer.py:78–428  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76
77
78class NextLayer:
79 ignore_hosts: Sequence[re.Pattern] = ()
80 allow_hosts: Sequence[re.Pattern] = ()
81 tcp_hosts: Sequence[re.Pattern] = ()
82 udp_hosts: Sequence[re.Pattern] = ()
83
84 def configure(self, updated):
85 if "tcp_hosts" in updated:
86 self.tcp_hosts = [
87 re.compile(x, re.IGNORECASE) for x in ctx.options.tcp_hosts
88 ]
89 if "udp_hosts" in updated:
90 self.udp_hosts = [
91 re.compile(x, re.IGNORECASE) for x in ctx.options.udp_hosts
92 ]
93 if "allow_hosts" in updated or "ignore_hosts" in updated:
94 self.ignore_hosts = [
95 re.compile(x, re.IGNORECASE) for x in ctx.options.ignore_hosts
96 ]
97 self.allow_hosts = [
98 re.compile(x, re.IGNORECASE) for x in ctx.options.allow_hosts
99 ]
100
101 def next_layer(self, nextlayer: layer.NextLayer):
102 if nextlayer.layer:
103 return # do not override something another addon has set.
104 try:
105 nextlayer.layer = self._next_layer(
106 nextlayer.context,
107 nextlayer.data_client(),
108 nextlayer.data_server(),
109 )
110 except NeedsMoreData:
111 logger.debug(
112 f"Deferring layer decision, not enough data: {nextlayer.data_client().hex()!r}"
113 )
114
115 def _next_layer(
116 self, context: Context, data_client: bytes, data_server: bytes
117 ) -> Layer | None:
118 assert context.layers
119
120 def s(*layers):
121 return stack_match(context, layers)
122
123 tcp_based = context.client.transport_protocol == "tcp"
124 udp_based = context.client.transport_protocol == "udp"
125
126 # 1) check for --ignore/--allow
127 if self._ignore_connection(context, data_client, data_server):
128 return (
129 layers.TCPLayer(context, ignore=not ctx.options.show_ignored_hosts)
130 if tcp_based
131 else layers.UDPLayer(context, ignore=not ctx.options.show_ignored_hosts)
132 )
133
134 # 2) Handle proxy modes with well-defined next protocol
135 # 2a) Reverse proxy: derive from spec

Callers 10

test_next_layerMethod · 0.90
test_next_layerFunction · 0.90
test_start_stopFunction · 0.90
test_injectFunction · 0.90
test_udpFunction · 0.90
test_regular_http3Function · 0.90

Calls

no outgoing calls

Tested by 10

test_next_layerMethod · 0.72
test_next_layerFunction · 0.72
test_start_stopFunction · 0.72
test_injectFunction · 0.72
test_udpFunction · 0.72
test_regular_http3Function · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…