mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 10:16:27 +00:00
scripts: refactor some examples that keep global state
We now have a better way to do this.
This commit is contained in:
parent
b5416895f5
commit
dbafe9f87b
@ -1,18 +1,20 @@
|
|||||||
# This scripts demonstrates how to use mitmproxy's filter pattern in inline scripts.
|
# This scripts demonstrates how to use mitmproxy's filter pattern in scripts.
|
||||||
# Usage: mitmdump -s "filt.py FILTER"
|
# Usage: mitmdump -s "filt.py FILTER"
|
||||||
import sys
|
import sys
|
||||||
from mitmproxy import filt
|
from mitmproxy import filt
|
||||||
|
|
||||||
state = {}
|
|
||||||
|
class Filter:
|
||||||
|
def __init__(self, spec):
|
||||||
|
self.filter = filt.parse(spec)
|
||||||
|
|
||||||
|
def response(self, flow):
|
||||||
|
if flow.match(self.filter):
|
||||||
|
print("Flow matches filter:")
|
||||||
|
print(flow)
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
raise ValueError("Usage: -s 'filt.py FILTER'")
|
raise ValueError("Usage: -s 'filt.py FILTER'")
|
||||||
state["filter"] = filt.parse(sys.argv[1])
|
return Filter(sys.argv[1])
|
||||||
|
|
||||||
|
|
||||||
def response(flow):
|
|
||||||
if flow.match(state["filter"]):
|
|
||||||
print("Flow matches filter:")
|
|
||||||
print(flow)
|
|
||||||
|
@ -3,20 +3,21 @@ import sys
|
|||||||
|
|
||||||
from mitmproxy.flow import FlowWriter
|
from mitmproxy.flow import FlowWriter
|
||||||
|
|
||||||
state = {}
|
|
||||||
|
class Writer:
|
||||||
|
def __init__(self, path):
|
||||||
|
if path == "-":
|
||||||
|
f = sys.stdout
|
||||||
|
else:
|
||||||
|
f = open(path, "wb")
|
||||||
|
self.w = FlowWriter(f)
|
||||||
|
|
||||||
|
def response(self, flow):
|
||||||
|
if random.choice([True, False]):
|
||||||
|
self.w.add(flow)
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
raise ValueError('Usage: -s "flowriter.py filename"')
|
raise ValueError('Usage: -s "flowriter.py filename"')
|
||||||
|
return Writer(sys.argv[1])
|
||||||
if sys.argv[1] == "-":
|
|
||||||
f = sys.stdout
|
|
||||||
else:
|
|
||||||
f = open(sys.argv[1], "wb")
|
|
||||||
state["flow_writer"] = FlowWriter(f)
|
|
||||||
|
|
||||||
|
|
||||||
def response(flow):
|
|
||||||
if random.choice([True, False]):
|
|
||||||
state["flow_writer"].add(flow)
|
|
||||||
|
@ -3,26 +3,27 @@
|
|||||||
import sys
|
import sys
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
iframe_url = None
|
|
||||||
|
class Injector:
|
||||||
|
def __init__(self, iframe_url):
|
||||||
|
self.iframe_url = iframe_url
|
||||||
|
|
||||||
|
def response(self, flow):
|
||||||
|
if flow.request.host in self.iframe_url:
|
||||||
|
return
|
||||||
|
html = BeautifulSoup(flow.response.content, "lxml")
|
||||||
|
if html.body:
|
||||||
|
iframe = html.new_tag(
|
||||||
|
"iframe",
|
||||||
|
src=self.iframe_url,
|
||||||
|
frameborder=0,
|
||||||
|
height=0,
|
||||||
|
width=0)
|
||||||
|
html.body.insert(0, iframe)
|
||||||
|
flow.response.content = str(html).encode("utf8")
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
raise ValueError('Usage: -s "iframe_injector.py url"')
|
raise ValueError('Usage: -s "iframe_injector.py url"')
|
||||||
global iframe_url
|
return Injector(sys.argv[1])
|
||||||
iframe_url = sys.argv[1]
|
|
||||||
|
|
||||||
|
|
||||||
def response(flow):
|
|
||||||
if flow.request.host in iframe_url:
|
|
||||||
return
|
|
||||||
html = BeautifulSoup(flow.response.content, "lxml")
|
|
||||||
if html.body:
|
|
||||||
iframe = html.new_tag(
|
|
||||||
"iframe",
|
|
||||||
src=iframe_url,
|
|
||||||
frameborder=0,
|
|
||||||
height=0,
|
|
||||||
width=0)
|
|
||||||
html.body.insert(0, iframe)
|
|
||||||
flow.response.content = str(html).encode("utf8")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user