Exemple #1
0
def test_common():
    parser = argparse.ArgumentParser()
    cmdline.common_options(parser)
    opts = parser.parse_args(args=[])

    assert cmdline.get_common_options(opts)

    opts.stickycookie_filt = "foo"
    opts.stickyauth_filt = "foo"
    v = cmdline.get_common_options(opts)
    assert v["stickycookie"] == "foo"
    assert v["stickyauth"] == "foo"

    opts.setheader = ["/foo/bar/voing"]
    v = cmdline.get_common_options(opts)
    assert v["setheaders"] == [("foo", "bar", "voing")]

    opts.setheader = ["//"]
    tutils.raises(
        "empty clause",
        cmdline.get_common_options,
        opts
    )
    opts.setheader = []

    opts.replace = ["/foo/bar/voing"]
    v = cmdline.get_common_options(opts)
    assert v["replacements"] == [("foo", "bar", "voing")]

    opts.replace = ["//"]
    tutils.raises(
        "empty clause",
        cmdline.get_common_options,
        opts
    )

    opts.replace = []
    opts.replace_file = [("/foo/bar/nonexistent")]
    tutils.raises(
        "could not read replace file",
        cmdline.get_common_options,
        opts
    )

    opts.replace_file = [("/~/bar/nonexistent")]
    tutils.raises(
        "filter pattern",
        cmdline.get_common_options,
        opts
    )

    p = tutils.test_data.path("data/replace")
    opts.replace_file = [("/foo/bar/%s" % p)]
    v = cmdline.get_common_options(opts)["replacements"]
    assert len(v) == 1
    assert v[0][2].strip() == "replacecontents"
Exemple #2
0
def mitmdump(args=None):  # pragma: no cover
    from . import dump

    version_check.check_pyopenssl_version()

    parser = cmdline.mitmdump()
    args = parser.parse_args(args)
    if args.quiet:
        args.flow_detail = 0

    try:
        dump_options = dump.Options(**cmdline.get_common_options(args))
        dump_options.flow_detail = args.flow_detail
        dump_options.keepserving = args.keepserving
        dump_options.filtstr = " ".join(args.args) if args.args else None
        proxy_config = process_options(parser, dump_options, args)
        server = get_server(dump_options.no_server, proxy_config)
        master = dump.DumpMaster(server, dump_options)

        def cleankill(*args, **kwargs):
            master.shutdown()

        signal.signal(signal.SIGTERM, cleankill)
        master.run()
    except (dump.DumpError, exceptions.OptionsError) as e:
        print("mitmdump: %s" % e, file=sys.stderr)
        sys.exit(1)
    except (KeyboardInterrupt, _thread.error):
        pass
    if master.has_errored:
        print("mitmdump: errors occurred during run", file=sys.stderr)
        sys.exit(1)
Exemple #3
0
def mitmproxy(args=None):  # pragma: no cover
    if os.name == "nt":
        print("Error: mitmproxy's console interface is not supported on Windows. "
              "You can run mitmdump or mitmweb instead.", file=sys.stderr)
        sys.exit(1)
    from . import console

    version_check.check_pyopenssl_version()
    assert_utf8_env()

    parser = cmdline.mitmproxy()
    args = parser.parse_args(args)

    try:
        console_options = console.master.Options(
            **cmdline.get_common_options(args)
        )
        console_options.palette = args.palette
        console_options.palette_transparent = args.palette_transparent
        console_options.eventlog = args.eventlog
        console_options.follow = args.follow
        console_options.intercept = args.intercept
        console_options.limit = args.limit
        console_options.no_mouse = args.no_mouse

        proxy_config = process_options(parser, console_options, args)
        server = get_server(console_options.no_server, proxy_config)
        m = console.master.ConsoleMaster(server, console_options)
    except exceptions.OptionsError as e:
        print("mitmproxy: %s" % e, file=sys.stderr)
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, _thread.error):
        pass
Exemple #4
0
def mitmweb(args=None):  # pragma: no cover
    from . import web

    version_check.check_pyopenssl_version()

    parser = cmdline.mitmweb()

    args = parser.parse_args(args)

    try:
        web_options = web.master.Options(**cmdline.get_common_options(args))
        web_options.intercept = args.intercept
        web_options.wdebug = args.wdebug
        web_options.wiface = args.wiface
        web_options.wport = args.wport
        web_options.wsingleuser = args.wsingleuser
        web_options.whtpasswd = args.whtpasswd
        web_options.process_web_options(parser)

        proxy_config = process_options(parser, web_options, args)
        server = get_server(web_options.no_server, proxy_config)
        m = web.master.WebMaster(server, web_options)
    except exceptions.OptionsError as e:
        print("mitmweb: %s" % e, file=sys.stderr)
        sys.exit(1)
    try:
        m.run()
    except (KeyboardInterrupt, _thread.error):
        pass
Exemple #5
0
def mitmdump(args=None):  # pragma: no cover
    from . import dump

    version_check.check_pyopenssl_version()

    parser = cmdline.mitmdump()
    options = parser.parse_args(args)
    if options.quiet:
        options.verbose = 0
        options.flow_detail = 0

    proxy_config = config.process_proxy_options(parser, options)
    dump_options = dump.Options(**cmdline.get_common_options(options))
    dump_options.flow_detail = options.flow_detail
    dump_options.keepserving = options.keepserving
    dump_options.filtstr = " ".join(options.args) if options.args else None

    server = get_server(dump_options.no_server, proxy_config)

    try:
        master = dump.DumpMaster(server, dump_options)

        def cleankill(*args, **kwargs):
            master.shutdown()

        signal.signal(signal.SIGTERM, cleankill)
        master.run()
    except dump.DumpError as e:
        print("mitmdump: %s" % e, file=sys.stderr)
        sys.exit(1)
    except (KeyboardInterrupt, _thread.error):
        pass
Exemple #6
0
def mitmweb(args=None):  # pragma: no cover
    from . import web

    version_check.check_pyopenssl_version()

    parser = cmdline.mitmweb()

    options = parser.parse_args(args)
    if options.quiet:
        options.verbose = 0

    proxy_config = config.process_proxy_options(parser, options)
    web_options = web.master.Options(**cmdline.get_common_options(options))
    web_options.intercept = options.intercept
    web_options.wdebug = options.wdebug
    web_options.wiface = options.wiface
    web_options.wport = options.wport
    web_options.wsingleuser = options.wsingleuser
    web_options.whtpasswd = options.whtpasswd
    web_options.process_web_options(parser)

    server = get_server(web_options.no_server, proxy_config)

    m = web.master.WebMaster(server, web_options)
    try:
        m.run()
    except (KeyboardInterrupt, _thread.error):
        pass
Exemple #7
0
 def p(self, *args):
     parser = tutils.MockParser()
     cmdline.common_options(parser)
     args = parser.parse_args(args=args)
     opts = cmdline.get_common_options(args)
     pconf = config.ProxyConfig(options.Options(**opts))
     return parser, pconf
Exemple #8
0
    def run(self):
        """
        start one proxy in background with specific port
        :param port:
        :param addon:
        :return:
        """
        args = ['-p', '%s' % self.port, '--stream', '100k']

        print('proxy arguments: %s' % ' '.join(args))

        def process_options(parser, options, args):
            from mitmproxy.proxy import server
            if args.sysinfo:
                print(debug.sysinfo())
            try:
                debug.register_info_dumpers()
            except Exception as e:
                print(str(e))
            pconf = config.ProxyConfig(options)
            if options.no_server:
                return server.DummyServer(pconf)
            else:
                try:
                    return server.ProxyServer(pconf)
                except exceptions.ServerException as v:
                    print(str(v))
                    return

        version_check.check_pyopenssl_version()

        parser = cmdline.mitmdump()
        args = parser.parse_args(args)
        # if args.quiet:
        args.flow_detail = 0

        self.master = None
        try:
            dump_options = dump.Options(**cmdline.get_common_options(args))
            dump_options.flow_detail = args.flow_detail
            dump_options.keepserving = args.keepserving
            dump_options.filtstr = " ".join(args.args) if args.args else None
            server = process_options(parser, dump_options, args)
            self.master = FileDumpMaster(server, dump_options)

            for add_on in self.addon:
                self.master.addons.add(add_on)

            def cleankill(*args, **kwargs):
                self.master.shutdown()

            # signal.signal(signal.SIGTERM, cleankill)
            global_config.PROXY = self.master
            self.master.run()
        except (dump.DumpError, exceptions.OptionsError) as e:
            print("mitmdump: %s" % e)
            return
        # except (KeyboardInterrupt, _thread.error):
        #     pass
        if self.master is None or self.master.has_errored:
            print("mitmdump: errors occurred during run")
            return