Version 0.2.11
This commit is contained in:
parent
9d274de709
commit
992edab315
17 changed files with 551 additions and 84 deletions
|
@ -3,7 +3,7 @@ import pappyproxy
|
|||
import shlex
|
||||
|
||||
from pappyproxy.plugin import active_intercepting_macros, add_intercepting_macro, remove_intercepting_macro
|
||||
from pappyproxy.macros import load_macros, macro_from_requests, gen_imacro
|
||||
from pappyproxy.macros import load_macros, macro_from_requests, MacroTemplate
|
||||
from pappyproxy.util import PappyException, load_reqlist, autocomplete_startswith
|
||||
from twisted.internet import defer
|
||||
|
||||
|
@ -12,6 +12,25 @@ loaded_int_macros = []
|
|||
macro_dict = {}
|
||||
int_macro_dict = {}
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def gen_macro_helper(line, template=None):
|
||||
args = shlex.split(line)
|
||||
if template is None:
|
||||
fname = args[0]
|
||||
template_name = args[1]
|
||||
argstart = 2
|
||||
else:
|
||||
fname = args[0]
|
||||
template_name = template
|
||||
argstart = 1
|
||||
if template_name not in MacroTemplate.template_list():
|
||||
raise PappyException('%s is not a valid template name' % template_name)
|
||||
script_str = yield MacroTemplate.fill_template_args(template_name, args[argstart:])
|
||||
fname = MacroTemplate.template_filename(template_name, fname)
|
||||
with open(fname, 'wc') as f:
|
||||
f.write(script_str)
|
||||
print 'Wrote script to %s' % fname
|
||||
|
||||
def load_macros_cmd(line):
|
||||
"""
|
||||
Load macros from a directory. By default loads macros in the current directory.
|
||||
|
@ -193,34 +212,37 @@ def generate_macro(line):
|
|||
Generate a macro script with request objects
|
||||
Usage: generate_macro <name> [reqs]
|
||||
"""
|
||||
if line == '':
|
||||
raise PappyException('Macro name is required')
|
||||
args = shlex.split(line)
|
||||
name = args[0]
|
||||
if len(args) > 1:
|
||||
reqs = yield load_reqlist(args[1])
|
||||
else:
|
||||
reqs = []
|
||||
script_str = macro_from_requests(reqs)
|
||||
fname = 'macro_%s.py' % name
|
||||
with open(fname, 'wc') as f:
|
||||
f.write(script_str)
|
||||
print 'Wrote script to %s' % fname
|
||||
yield gen_macro_helper(line, template='macro')
|
||||
|
||||
@crochet.wait_for(timeout=None)
|
||||
@defer.inlineCallbacks
|
||||
def generate_int_macro(line):
|
||||
"""
|
||||
Generate an intercepting macro script
|
||||
Usage: generate_int_macro <name>
|
||||
"""
|
||||
yield gen_macro_helper(line, template='intmacro')
|
||||
|
||||
@crochet.wait_for(timeout=None)
|
||||
@defer.inlineCallbacks
|
||||
def generate_template_macro(line):
|
||||
"""
|
||||
Generate a macro from a built in template
|
||||
Usage: generate_template_macro <fname> <template> [args]
|
||||
"""
|
||||
if line == '':
|
||||
raise PappyException('Macro name is required')
|
||||
args = shlex.split(line)
|
||||
name = args[0]
|
||||
script_str = gen_imacro()
|
||||
fname = 'int_%s.py' % name
|
||||
with open(fname, 'wc') as f:
|
||||
f.write(script_str)
|
||||
print 'Wrote script to %s' % fname
|
||||
print 'Usage: gtma <fname> <template> [args]'
|
||||
print 'Macro templates:'
|
||||
|
||||
templates = MacroTemplate.template_list()
|
||||
templates.sort()
|
||||
for t in templates:
|
||||
if MacroTemplate.template_argstring(t):
|
||||
print '"%s %s" - %s' % (t, MacroTemplate.template_argstring(t), MacroTemplate.template_description(t))
|
||||
else:
|
||||
print '"%s" - %s' % (t, MacroTemplate.template_description(t))
|
||||
else:
|
||||
yield gen_macro_helper(line)
|
||||
|
||||
@crochet.wait_for(timeout=None)
|
||||
@defer.inlineCallbacks
|
||||
|
@ -241,6 +263,7 @@ def load_cmds(cmd):
|
|||
'rpy': (rpy, None),
|
||||
'generate_int_macro': (generate_int_macro, None),
|
||||
'generate_macro': (generate_macro, None),
|
||||
'generate_template_macro': (generate_template_macro, None),
|
||||
'list_int_macros': (list_int_macros, None),
|
||||
'stop_int_macro': (stop_int_macro, complete_stop_int_macro),
|
||||
'run_int_macro': (run_int_macro, complete_run_int_macro),
|
||||
|
@ -251,6 +274,7 @@ def load_cmds(cmd):
|
|||
#('rpy', ''),
|
||||
('generate_int_macro', 'gima'),
|
||||
('generate_macro', 'gma'),
|
||||
('generate_template_macro', 'gtma'),
|
||||
('list_int_macros', 'lsim'),
|
||||
('stop_int_macro', 'sim'),
|
||||
('run_int_macro', 'rim'),
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import argparse
|
||||
import crochet
|
||||
import pappyproxy
|
||||
import shlex
|
||||
|
@ -7,8 +8,10 @@ from pappyproxy.colors import Colors, Styles, path_formatter, host_color, scode_
|
|||
from pappyproxy.util import PappyException, remove_color, confirm, load_reqlist, Capturing
|
||||
from pappyproxy.macros import InterceptMacro
|
||||
from pappyproxy.requestcache import RequestCache
|
||||
from pappyproxy.session import Session
|
||||
from pappyproxy.pappy import session
|
||||
from pappyproxy.plugin import add_intercepting_macro, remove_intercepting_macro
|
||||
from pappyproxy.plugin import add_intercepting_macro, remove_intercepting_macro, add_to_history
|
||||
from pappyproxy.http import async_submit_requests, Request
|
||||
from twisted.internet import defer
|
||||
from twisted.enterprise import adbapi
|
||||
|
||||
|
@ -190,6 +193,77 @@ def run_without_color(line):
|
|||
def version(line):
|
||||
import pappyproxy
|
||||
print pappyproxy.__version__
|
||||
|
||||
@crochet.wait_for(timeout=180.0)
|
||||
@defer.inlineCallbacks
|
||||
def submit(line):
|
||||
"""
|
||||
Resubmit some requests, optionally with modified headers and cookies.
|
||||
|
||||
Usage: submit reqids [-h] [-m] [-u] [-p] [-c [COOKIES [COOKIES ...]]] [-d [HEADERS [HEADERS ...]]]
|
||||
"""
|
||||
|
||||
parser = argparse.ArgumentParser(prog="submit", usage=submit.__doc__)
|
||||
parser.add_argument('reqids')
|
||||
parser.add_argument('-m', '--inmem', action='store_true', help='Store resubmitted requests in memory without storing them in the data file')
|
||||
parser.add_argument('-u', '--unique', action='store_true', help='Only resubmit one request per endpoint (different URL parameters are different endpoints)')
|
||||
parser.add_argument('-p', '--uniquepath', action='store_true', help='Only resubmit one request per endpoint (ignoring URL parameters)')
|
||||
parser.add_argument('-c', '--cookies', nargs='*', help='Apply a cookie to requests before submitting')
|
||||
parser.add_argument('-d', '--headers', nargs='*', help='Apply a header to requests before submitting')
|
||||
args = parser.parse_args(shlex.split(line))
|
||||
|
||||
headers = {}
|
||||
cookies = {}
|
||||
|
||||
if args.headers:
|
||||
for h in args.headers:
|
||||
k, v = h.split('=', 1)
|
||||
headers[k] = v
|
||||
|
||||
if args.cookies:
|
||||
for c in args.cookies:
|
||||
k, v = c.split('=', 1)
|
||||
cookies[k] = v
|
||||
|
||||
if args.unique and args.uniquepath:
|
||||
raise PappyException('Both -u and -p cannot be given as arguments')
|
||||
|
||||
newsession = Session(cookie_vals=cookies, header_vals=headers)
|
||||
|
||||
reqs = yield load_reqlist(args.reqids)
|
||||
|
||||
if args.unique or args.uniquepath:
|
||||
endpoints = set()
|
||||
new_reqs = []
|
||||
for r in reqs:
|
||||
if args.unique:
|
||||
s = r.url
|
||||
else:
|
||||
s = r.path
|
||||
|
||||
if not s in endpoints:
|
||||
new_reqs.append(r.copy())
|
||||
endpoints.add(s)
|
||||
reqs = new_reqs
|
||||
else:
|
||||
reqs = [r.copy() for r in reqs]
|
||||
|
||||
for req in reqs:
|
||||
newsession.apply_req(req)
|
||||
|
||||
conf_message = "You're about to submit %d requests, continue?" % len(reqs)
|
||||
if not confirm(conf_message):
|
||||
defer.returnValue(None)
|
||||
|
||||
for r in reqs:
|
||||
r.tags.add('resubmitted')
|
||||
|
||||
if args.inmem:
|
||||
yield async_submit_requests(reqs)
|
||||
for req in reqs:
|
||||
add_to_history(req)
|
||||
else:
|
||||
yield async_submit_requests(reqs, save=True)
|
||||
|
||||
def load_cmds(cmd):
|
||||
cmd.set_cmds({
|
||||
|
@ -202,6 +276,7 @@ def load_cmds(cmd):
|
|||
'nocolor': (run_without_color, None),
|
||||
'watch': (watch_proxy, None),
|
||||
'version': (version, None),
|
||||
'submit': (submit, None)
|
||||
})
|
||||
cmd.add_aliases([
|
||||
#('rpy', ''),
|
||||
|
|
|
@ -14,6 +14,7 @@ from pappyproxy.plugin import async_main_context_ids
|
|||
from pappyproxy.colors import Colors, Styles, verb_color, scode_color, path_formatter, host_color
|
||||
from pygments.formatters import TerminalFormatter
|
||||
from pygments.lexers.data import JsonLexer
|
||||
from pygments.lexers.html import XmlLexer
|
||||
|
||||
###################
|
||||
## Helper functions
|
||||
|
@ -103,6 +104,8 @@ def guess_pretty_print_fmt(msg):
|
|||
return 'json'
|
||||
elif 'www-form' in msg.headers['content-type']:
|
||||
return 'form'
|
||||
elif 'application/xml' in msg.headers['content-type']:
|
||||
return 'xml'
|
||||
return 'text'
|
||||
|
||||
def pretty_print_body(fmt, body):
|
||||
|
@ -121,6 +124,10 @@ def pretty_print_body(fmt, body):
|
|||
print s
|
||||
elif fmt.lower() == 'text':
|
||||
print body
|
||||
elif fmt.lower() == 'xml':
|
||||
import xml.dom.minidom
|
||||
xml = xml.dom.minidom.parseString(body)
|
||||
print pygments.highlight(xml.toprettyxml(), XmlLexer(), TerminalFormatter())
|
||||
else:
|
||||
raise PappyException('"%s" is not a valid format' % fmt)
|
||||
except PappyException as e:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue