Skip to content

Commit

Permalink
Basic web service and options
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Sep 14, 2014
1 parent adfaa1e commit 6812d30
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
8 changes: 4 additions & 4 deletions libmproxy/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,23 @@ def common_options(parser):
help="Override the HTTP request form sent upstream by the proxy"
)

group = parser.add_argument_group("Web App")
group = parser.add_argument_group("Onboarding App")
group.add_argument(
"-a",
action="store_false", dest="app", default=True,
help="Disable the mitmproxy web app."
help="Disable the mitmproxy onboarding app."
)
group.add_argument(
"--app-host",
action="store", dest="app_host", default=APP_HOST, metavar="host",
help="Domain to serve the app from. For transparent mode, use an IP when\
help="Domain to serve the onboarding app from. For transparent mode, use an IP when\
a DNS entry for the app domain is not present. Default: %s" % APP_HOST

)
group.add_argument(
"--app-port",
action="store", dest="app_port", default=APP_PORT, type=int, metavar="80",
help="Port to serve the app from."
help="Port to serve the onboarding app from."
)

group = parser.add_argument_group("Client Replay")
Expand Down
28 changes: 26 additions & 2 deletions libmproxy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,28 @@ def mitmweb_cmdline():
parser.add_argument(
'--version',
action='version',
version=version.NAMEVERSION
version="mitmweb" + " " + version.VERSION
)

group = parser.add_argument_group("Mitmweb")
group.add_argument(
"--wport",
action="store", type=int, dest="wport", default=8081,
metavar="PORT",
help="Mitmweb port."
)
group.add_argument(
"--wiface",
action="store", dest="wiface", default="127.0.0.1",
metavar="IFACE",
help="Mitmweb interface."
)
group.add_argument(
"--wdebug",
action="store_true", dest="wdebug",
help="Turn on mitmweb debugging"
)

cmdline.common_options(parser)
group = parser.add_argument_group(
"Filters",
Expand All @@ -189,6 +209,10 @@ def mitmweb_cmdline():

proxy_config = process_proxy_options(parser, options)
web_options = web.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
return web_options, proxy_config


Expand All @@ -197,7 +221,7 @@ def mitmweb(): # pragma: nocover

check_versions()
assert_utf8_env()
web_options, proxy_config = mitmproxy_cmdline()
web_options, proxy_config = mitmweb_cmdline()
server = get_server(web_options.no_server, proxy_config)

m = web.WebMaster(server, web_options)
Expand Down
12 changes: 12 additions & 0 deletions libmproxy/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tornado.ioloop
import tornado.httpserver
from .. import controller, utils, flow, script, proxy
import app


class Stop(Exception):
Expand Down Expand Up @@ -40,6 +41,10 @@ class Options(object):
"verbosity",
"wfile",
"nopop",

"wdebug",
"wport",
"wiface",
]

def __init__(self, **kwargs):
Expand All @@ -52,6 +57,7 @@ def __init__(self, **kwargs):

class WebMaster(flow.FlowMaster):
def __init__(self, server, options):
self.options = options
flow.FlowMaster.__init__(self, server, WebState())

def tick(self):
Expand All @@ -63,6 +69,12 @@ def run(self): # pragma: no cover
controller.Channel(self.masterq, self.should_exit)
)
iol = tornado.ioloop.IOLoop.instance()

http_server = tornado.httpserver.HTTPServer(
app.Application(self.options.wdebug)
)
http_server.listen(self.options.wport)

tornado.ioloop.PeriodicCallback(self.tick, 5).start()
try:
iol.start()
Expand Down
18 changes: 18 additions & 0 deletions libmproxy/web/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import os.path
import tornado.web


class Application(tornado.web.Application):
def __init__(self, debug):
handlers = [
]
settings = dict(
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=True,
cookie_secret="__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
debug=debug,
)
tornado.web.Application.__init__(self, handlers, **settings)

0 comments on commit 6812d30

Please sign in to comment.