use flowfilter.match

This commit is contained in:
Thomas Kriechbaumer 2016-10-01 12:44:17 +02:00
parent 5f8b8bf35e
commit bb60b76af4
8 changed files with 24 additions and 30 deletions

View File

@ -1,15 +1,16 @@
# This scripts demonstrates how to use mitmproxy's filter pattern in scripts. # This scripts demonstrates how to use mitmproxy's filter pattern in scripts.
# Usage: mitmdump -s "filter.py FILTER" # Usage: mitmdump -s "flowfilter.py FILTER"
import sys import sys
from mitmproxy import filter from mitmproxy import flowfilter
class Filter: class Filter:
def __init__(self, spec): def __init__(self, spec):
self.filter = filter.parse(spec) self.filter = flowfilter.parse(spec)
def response(self, flow): def response(self, flow):
if flow.match(self.filter): if flowfilter.match(flow, self.filter):
print("Flow matches filter:") print("Flow matches filter:")
print(flow) print(flow)

View File

@ -220,7 +220,7 @@ class Dumper(object):
return False return False
if not self.filter: if not self.filter:
return True return True
elif f.match(self.filter): elif flowfilter.match(f, self.filter):
return True return True
return False return False

View File

@ -22,6 +22,6 @@ class StickyAuth:
host = flow.request.host host = flow.request.host
if "authorization" in flow.request.headers: if "authorization" in flow.request.headers:
self.hosts[host] = flow.request.headers["authorization"] self.hosts[host] = flow.request.headers["authorization"]
elif flow.match(self.flt): elif flowfilter.match(flow, self.flt):
if host in self.hosts: if host in self.hosts:
flow.request.headers["authorization"] = self.hosts[host] flow.request.headers["authorization"] = self.hosts[host]

View File

@ -64,7 +64,7 @@ class StickyCookie:
def request(self, flow): def request(self, flow):
if self.flt: if self.flt:
l = [] l = []
if flow.match(self.flt): if flowfilter.match(flow, self.flt):
for domain, port, path in self.jar.keys(): for domain, port, path in self.jar.keys():
match = [ match = [
domain_match(flow.request.host, domain), domain_match(flow.request.host, domain),

View File

@ -22,6 +22,7 @@ from mitmproxy import contentviews
from mitmproxy import controller from mitmproxy import controller
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import flowfilter
from mitmproxy import utils from mitmproxy import utils
import mitmproxy.options import mitmproxy.options
from mitmproxy.console import flowlist from mitmproxy.console import flowlist
@ -134,9 +135,9 @@ class ConsoleState(flow.State):
fprev, _ = self.get_from_pos(fidx - dist) fprev, _ = self.get_from_pos(fidx - dist)
fnext, _ = self.get_from_pos(fidx + dist) fnext, _ = self.get_from_pos(fidx + dist)
if fprev and fprev.match(filt): if fprev and flowfilter.match(fprev, filt):
return fprev return fprev
elif fnext and fnext.match(filt): elif fnext and flowfilter.match(fnext, filt):
return fnext return fnext
dist += 1 dist += 1
@ -669,7 +670,7 @@ class ConsoleMaster(flow.FlowMaster):
def process_flow(self, f): def process_flow(self, f):
should_intercept = any( should_intercept = any(
[ [
self.state.intercept and f.match(self.state.intercept) and not f.request.is_replay, self.state.intercept and flowfilter.match(f, self.state.intercept) and not f.request.is_replay,
f.intercepted, f.intercepted,
] ]
) )

View File

@ -3,6 +3,7 @@ from __future__ import absolute_import, print_function, division
import os import os
from mitmproxy import exceptions from mitmproxy import exceptions
from mitmproxy import flowfilter
from mitmproxy import models from mitmproxy import models
from mitmproxy.contrib import tnetstring from mitmproxy.contrib import tnetstring
from mitmproxy.flow import io_compat from mitmproxy.flow import io_compat
@ -60,7 +61,7 @@ class FilteredFlowWriter:
self.filt = filt self.filt = filt
def add(self, f): def add(self, f):
if self.filt and not f.match(self.filt): if self.filt and not flowfilter.match(f, self.filt):
return return
d = f.get_state() d = f.get_state()
tnetstring.dump(d, self.fo) tnetstring.dump(d, self.fo)

View File

@ -8,8 +8,6 @@ from mitmproxy import stateobject
from mitmproxy.models.connections import ClientConnection from mitmproxy.models.connections import ClientConnection
from mitmproxy.models.connections import ServerConnection from mitmproxy.models.connections import ServerConnection
import six
from netlib import version from netlib import version
from typing import Optional # noqa from typing import Optional # noqa
@ -188,10 +186,3 @@ class Flow(stateobject.StateObject):
self.reply.ack() self.reply.ack()
self.reply.commit() self.reply.commit()
master.handle_accept_intercept(self) master.handle_accept_intercept(self)
def match(self, f):
import warnings
warnings.warn(".match() is deprecated, please use flowfilter.match(some_flow, some_filter) instead.", DeprecationWarning)
from mitmproxy import flowfilter
return flowfilter.match(self, f)

View File

@ -68,14 +68,14 @@ class TestHTTPFlow(object):
def test_match(self): def test_match(self):
f = tutils.tflow(resp=True) f = tutils.tflow(resp=True)
assert not f.match("~b test") assert not flowfilter.match(f, "~b test")
assert f.match(None) assert flowfilter.match(f, None)
assert not f.match("~b test") assert not flowfilter.match(f, "~b test")
f = tutils.tflow(err=True) f = tutils.tflow(err=True)
assert f.match("~e") assert flowfilter.match(f, "~e")
tutils.raises(ValueError, f.match, "~") tutils.raises(ValueError, flowfilter.match, f, "~")
def test_backup(self): def test_backup(self):
f = tutils.tflow() f = tutils.tflow()
@ -195,14 +195,14 @@ class TestTCPFlow:
def test_match(self): def test_match(self):
f = tutils.ttcpflow() f = tutils.ttcpflow()
assert not f.match("~b nonexistent") assert not flowfilter.match(f, "~b nonexistent")
assert f.match(None) assert flowfilter.match(f, None)
assert not f.match("~b nonexistent") assert not flowfilter.match(f, "~b nonexistent")
f = tutils.ttcpflow(err=True) f = tutils.ttcpflow(err=True)
assert f.match("~e") assert flowfilter.match(f, "~e")
tutils.raises(ValueError, f.match, "~") tutils.raises(ValueError, flowfilter.match, f, "~")
class TestState: class TestState: