예제 #1
0

class MemBool:
    """
        Truth-checking with a memory, for use in chained if statements.
    """
    def __init__(self) -> None:
        self.v: typing.Optional[bool] = None

    def __call__(self, v: bool) -> bool:
        self.v = v
        return bool(v)


# FIXME: change this name
data = mdata.Data(__name__)


def daemonize(stdin='/dev/null',
              stdout='/dev/null',
              stderr='/dev/null'):  # pragma: no cover
    try:
        pid = os.fork()
        if pid > 0:
            sys.exit(0)
    except OSError as e:
        sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
        sys.exit(1)
    os.chdir("/")
    os.umask(0)
    os.setsid()
예제 #2
0
from mitmproxy import certs
from mitmproxy import exceptions
from mitmproxy import http
from mitmproxy import options
from mitmproxy.addons import script
from mitmproxy.net import socks
from mitmproxy.net import tcp
from mitmproxy.net.http import http1
from mitmproxy.proxy.config import HostMatcher
from mitmproxy.utils import data
from pathod import pathoc
from pathod import pathod
from .. import tservers
from ...conftest import skip_appveyor

cdata = data.Data(__name__)


class CommonMixin:
    def test_large(self):
        assert len(self.pathod("200:b@50k").content) == 1024 * 50

    def test_http(self):
        f = self.pathod("304")
        assert f.status_code == 304

        # In Upstream mode with SSL, we may already have a previous CONNECT
        # request.
        l = self.master.state.flows[-1]
        assert l.client_conn.address
        assert "host" in l.request.headers
예제 #3
0
class RaisesContext:
    def __init__(self, expected_exception):
        self.expected_exception = expected_exception

    def __enter__(self):
        return

    def __exit__(self, exc_type, exc_val, exc_tb):
        if not exc_type:
            raise AssertionError("No exception raised.")
        else:
            _check_exception(self.expected_exception, exc_val, exc_tb)
        return True


test_data = data.Data(__name__).push("../../test/")


def treq(**kwargs):
    """
    Returns:
        mitmproxy.net.http.Request
    """
    default = dict(
        first_line_format="relative",
        method=b"GET",
        scheme=b"http",
        host=b"address",
        port=22,
        path=b"/path",
        http_version=b"HTTP/1.1",
예제 #4
0
import shlex

from mitmproxy import options
from mitmproxy import contentviews
from mitmproxy import proxy
from mitmproxy.addons import script
from mitmproxy.utils import data
from mitmproxy import master

from mitmproxy.test import tutils as netutils
from mitmproxy.net.http import Headers
from mitmproxy.net.http import cookies

from . import tutils, mastertest

example_dir = data.Data(__name__).push("../../examples")


class ScriptError(Exception):
    pass


class RaiseMaster(master.Master):
    def add_log(self, e, level):
        if level in ("warn", "error"):
            raise ScriptError(e)


def tscript(cmd, args=""):
    o = options.Options()
    cmd = example_dir.path(cmd) + " " + args
예제 #5
0
def tdata():
    return data.Data(__name__)
예제 #6
0
def tmpdir(*args, **kwargs):
    temp_workdir = tempfile.mkdtemp(*args, **kwargs)
    with chdir(temp_workdir):
        yield temp_workdir
    shutil.rmtree(temp_workdir)


class MockParser(argparse.ArgumentParser):

    """
    argparse.ArgumentParser sys.exits() by default.
    Make it more testable by throwing an exception instead.
    """

    def error(self, message):
        raise Exception(message)


raises = mitmproxy.test.tutils.raises


@contextmanager
def capture_stderr(command, *args, **kwargs):
    out, sys.stderr = sys.stderr, io.StringIO()
    command(*args, **kwargs)
    yield sys.stderr.getvalue()
    sys.stderr = out


test_data = data.Data(__name__)
예제 #7
0
파일: tutils.py 프로젝트: xorrbit/mitmproxy
class RaisesContext:
    def __init__(self, expected_exception):
        self.expected_exception = expected_exception

    def __enter__(self):
        return

    def __exit__(self, exc_type, exc_val, exc_tb):
        if not exc_type:
            raise AssertionError("No exception raised.")
        else:
            _check_exception(self.expected_exception, exc_val, exc_tb)
        return True


test_data = data.Data(__name__).push("../../test/mitmproxy/net")


def treq(**kwargs):
    """
    Returns:
        mitmproxy.net.http.Request
    """
    default = dict(first_line_format="relative",
                   method=b"GET",
                   scheme=b"http",
                   host=b"address",
                   port=22,
                   path=b"/path",
                   http_version=b"HTTP/1.1",
                   headers=http.Headers(