def watch_proxy(line): print 'Watching proxy... press ENTER to exit' macro = PrintStreamInterceptMacro() macro.intercept_requests = True macro.intercept_responses = True try: add_intercepting_macro('pappy_watch_proxy', macro) raw_input() finally: try: remove_intercepting_macro('pappy_watch_proxy') except PappyException: pass
def stop_int_macro(line): """ Stop a running intercepting macro Usage: stop_int_macro <macro name or macro short name> """ global int_macro_dict global loaded_int_macros if not line: raise PappyException('You must give an intercepting macro to run. You can give its short name, or the name in the filename.') if line not in int_macro_dict: raise PappyException('%s not a loaded intercepting macro' % line) macro = int_macro_dict[line] remove_intercepting_macro(macro.name) print '"%s" stopped' % macro.name
def intercept(line): """ Intercept requests and/or responses and edit them with before passing them along Usage: intercept <reqid> """ global edit_queue args = shlex.split(line) intercept_requests = False intercept_responses = False intercept_ws = True intercept_ws req_names = ('req', 'request', 'requests') rsp_names = ('rsp', 'response', 'responses') ws_names = ('ws', 'websocket') if any(a in req_names for a in args): intercept_requests = True if any(a in rsp_names for a in args): intercept_responses = True if any(a in req_names for a in args): intercept_ws = True if not args: intercept_requests = True intercepting = [] if intercept_requests: intercepting.append('Requests') if intercept_responses: intercepting.append('Responses') if intercept_ws: intercepting.append('Websocket Messages') if not intercept_requests and not intercept_responses and not intercept_ws: intercept_str = 'NOTHING' else: intercept_str = ', '.join(intercepting) mangle_macro = MangleInterceptMacro() mangle_macro.intercept_requests = intercept_requests mangle_macro.intercept_responses = intercept_responses mangle_macro.intercept_ws = intercept_ws add_intercepting_macro('pappy_intercept', mangle_macro) ## Interceptor loop stdscr = curses.initscr() curses.noecho() curses.cbreak() try: editnext = False stdscr.nodelay(True) while True: stdscr.addstr(0, 0, "Currently intercepting: %s" % intercept_str) stdscr.clrtoeol() stdscr.addstr(1, 0, "%d item(s) in queue." % len(edit_queue)) stdscr.clrtoeol() if editnext: stdscr.addstr( 2, 0, "Waiting for next item... Press 'q' to quit or 'b' to quit waiting" ) else: stdscr.addstr( 2, 0, "Press 'n' to edit the next item or 'q' to quit interceptor." ) stdscr.clrtoeol() c = stdscr.getch() if c == ord('q'): break elif c == ord('n'): editnext = True elif c == ord('b'): editnext = False if editnext and edit_queue: editnext = False (to_edit, deferred) = edit_queue.pop(0) editor = 'vi' if 'EDITOR' in os.environ: editor = os.environ['EDITOR'] additional_args = [] if editor == 'vim': # prevent adding additional newline additional_args.append('-b') subprocess.call([editor, to_edit] + additional_args) stdscr.clear() deferred.callback(None) finally: curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin() try: remove_intercepting_macro('pappy_intercept') except PappyException: pass # Send remaining requests along while len(edit_queue) > 0: (fname, deferred) = edit_queue.pop(0) deferred.callback(None)
def intercept(line): """ Intercept requests and/or responses and edit them with before passing them along Usage: intercept <reqid> """ global edit_queue args = shlex.split(line) intercept_requests = False intercept_responses = False intercept_ws = True intercept_ws req_names = ('req', 'request', 'requests') rsp_names = ('rsp', 'response', 'responses') ws_names = ('ws', 'websocket') if any(a in req_names for a in args): intercept_requests = True if any(a in rsp_names for a in args): intercept_responses = True if any(a in req_names for a in args): intercept_ws = True if not args: intercept_requests = True intercepting = [] if intercept_requests: intercepting.append('Requests') if intercept_responses: intercepting.append('Responses') if intercept_ws: intercepting.append('Websocket Messages') if not intercept_requests and not intercept_responses and not intercept_ws: intercept_str = 'NOTHING' else: intercept_str = ', '.join(intercepting) mangle_macro = MangleInterceptMacro() mangle_macro.intercept_requests = intercept_requests mangle_macro.intercept_responses = intercept_responses mangle_macro.intercept_ws = intercept_ws add_intercepting_macro('pappy_intercept', mangle_macro) ## Interceptor loop stdscr = curses.initscr() curses.noecho() curses.cbreak() try: editnext = False stdscr.nodelay(True) while True: stdscr.addstr(0, 0, "Currently intercepting: %s" % intercept_str) stdscr.clrtoeol() stdscr.addstr(1, 0, "%d item(s) in queue." % len(edit_queue)) stdscr.clrtoeol() if editnext: stdscr.addstr(2, 0, "Waiting for next item... Press 'q' to quit or 'b' to quit waiting") else: stdscr.addstr(2, 0, "Press 'n' to edit the next item or 'q' to quit interceptor.") stdscr.clrtoeol() c = stdscr.getch() if c == ord('q'): break elif c == ord('n'): editnext = True elif c == ord('b'): editnext = False if editnext and edit_queue: editnext = False (to_edit, deferred) = edit_queue.pop(0) editor = 'vi' if 'EDITOR' in os.environ: editor = os.environ['EDITOR'] additional_args = [] if editor == 'vim': # prevent adding additional newline additional_args.append('-b') subprocess.call([editor, to_edit] + additional_args) stdscr.clear() deferred.callback(None) finally: curses.nocbreak() stdscr.keypad(0) curses.echo() curses.endwin() try: remove_intercepting_macro('pappy_intercept') except PappyException: pass # Send remaining requests along while len(edit_queue) > 0: (fname, deferred) = edit_queue.pop(0) deferred.callback(None)