<s> import glob <EOL> import imp <EOL> import os <EOL> import random <EOL> import re <EOL> import stat <EOL> from jinja2 import Environment , FileSystemLoader <EOL> from pappyproxy . pappy import session <EOL> from pappyproxy . util import PappyException , load_reqlist <EOL> from twisted . internet import defer <EOL> @ defer . inlineCallbacks <EOL> def gen_template_args_macro ( args ) : <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> reqids = args [ <NUM_LIT:0> ] <EOL> reqs = yield load_reqlist ( reqids ) <EOL> else : <EOL> reqs = [ ] <EOL> defer . returnValue ( macro_from_requests ( reqs ) ) <EOL> def gen_template_generator_noargs ( name ) : <EOL> def f ( args ) : <EOL> subs = { } <EOL> subs [ '<STR_LIT>' ] = '<STR_LIT>' % random . randint ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> subs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return MacroTemplate . fill_template ( name , subs ) <EOL> return f <EOL> class Macro ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename = '<STR_LIT>' ) : <EOL> self . name = '<STR_LIT>' <EOL> self . short_name = None <EOL> self . file_name = '<STR_LIT>' <EOL> self . filename = filename or '<STR_LIT>' <EOL> self . source = None <EOL> if self . filename : <EOL> self . load ( ) <EOL> def __repr__ ( self ) : <EOL> s = self . name <EOL> names = [ ] <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> if self . source . SHORT_NAME : <EOL> names . append ( self . source . SHORT_NAME ) <EOL> names . append ( self . file_name ) <EOL> s += '<STR_LIT>' % ( '<STR_LIT:/>' . join ( names ) ) <EOL> return "<STR_LIT>" % s <EOL> def load ( self ) : <EOL> if self . filename : <EOL> match = re . findall ( '<STR_LIT>' , self . filename ) <EOL> self . file_name = match [ <NUM_LIT:0> ] <EOL> st = os . stat ( self . filename ) <EOL> if ( st . st_mode & stat . S_IWOTH ) : <EOL> raise PappyException ( "<STR_LIT>" % self . filename ) <EOL> module_name = os . path . basename ( os . path . splitext ( self . filename ) [ <NUM_LIT:0> ] ) <EOL> self . source = imp . load_source ( '<STR_LIT:%s>' % module_name , self . filename ) <EOL> if not hasattr ( self . source , '<STR_LIT>' ) : <EOL> raise PappyException ( '<STR_LIT>' % self . filename ) <EOL> self . name = self . source . MACRO_NAME <EOL> if self . name == '<STR_LIT>' : <EOL> raise PappyException ( '<STR_LIT>' % self . filename ) <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> self . short_name = self . source . SHORT_NAME <EOL> else : <EOL> self . short_name = None <EOL> else : <EOL> self . source = None <EOL> def execute ( self , args ) : <EOL> if self . source : <EOL> self . source . run_macro ( args ) <EOL> class InterceptMacro ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . name = '<STR_LIT>' <EOL> self . short_name = None <EOL> self . intercept_requests = False <EOL> self . intercept_responses = False <EOL> self . async_req = False <EOL> self . async_rsp = False <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def init ( self , args ) : <EOL> pass <EOL> def mangle_request ( self , request ) : <EOL> return request <EOL> def mangle_response ( self , request ) : <EOL> return request . response <EOL> @ defer . inlineCallbacks <EOL> def async_mangle_request ( self , request ) : <EOL> defer . returnValue ( request ) <EOL> @ defer . inlineCallbacks <EOL> def async_mangle_response ( self , request ) : <EOL> defer . returnValue ( request . response ) <EOL> class FileInterceptMacro ( InterceptMacro ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename = '<STR_LIT>' ) : <EOL> InterceptMacro . __init__ ( self ) <EOL> self . file_name = '<STR_LIT>' <EOL> self . filename = filename or '<STR_LIT>' <EOL> self . source = None <EOL> if self . filename : <EOL> self . load ( ) <EOL> def __repr__ ( self ) : <EOL> s = self . name <EOL> names = [ ] <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> if self . source . SHORT_NAME : <EOL> names . append ( self . source . SHORT_NAME ) <EOL> names . append ( self . file_name ) <EOL> s += '<STR_LIT>' % ( '<STR_LIT:/>' . join ( names ) ) <EOL> return "<STR_LIT>" % s <EOL> def load ( self ) : <EOL> if self . filename : <EOL> match = re . findall ( '<STR_LIT>' , self . filename ) <EOL> if len ( match ) > <NUM_LIT:0> : <EOL> self . file_name = match [ <NUM_LIT:0> ] <EOL> else : <EOL> self . file_name = self . filename <EOL> st = os . stat ( self . filename ) <EOL> if ( st . st_mode & stat . S_IWOTH ) : <EOL> raise PappyException ( "<STR_LIT>" % self . filename ) <EOL> module_name = os . path . basename ( os . path . splitext ( self . filename ) [ <NUM_LIT:0> ] ) <EOL> self . source = imp . load_source ( '<STR_LIT:%s>' % module_name , self . filename ) <EOL> self . name = self . source . MACRO_NAME <EOL> if self . name == '<STR_LIT>' : <EOL> raise PappyException ( '<STR_LIT>' % self . filename ) <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> self . short_name = self . source . SHORT_NAME <EOL> else : <EOL> self . short_name = None <EOL> if hasattr ( self . source , '<STR_LIT>' ) and hasattr ( self . source , '<STR_LIT>' ) : <EOL> raise PappyException ( '<STR_LIT>' % self . filename ) <EOL> if hasattr ( self . source , '<STR_LIT>' ) and hasattr ( self . source , '<STR_LIT>' ) : <EOL> raise PappyException ( '<STR_LIT>' % self . filename ) <EOL> else : <EOL> self . source = None <EOL> if self . source and hasattr ( self . source , '<STR_LIT>' ) : <EOL> self . intercept_requests = True <EOL> self . async_req = False <EOL> elif self . source and hasattr ( self . source , '<STR_LIT>' ) : <EOL> self . intercept_requests = True <EOL> self . async_req = True <EOL> else : <EOL> self . intercept_requests = True <EOL> if self . source and hasattr ( self . source , '<STR_LIT>' ) : <EOL> self . intercept_responses = True <EOL> self . async_rsp = False <EOL> elif self . source and hasattr ( self . source , '<STR_LIT>' ) : <EOL> self . intercept_responses = True <EOL> self . async_rsp = True <EOL> else : <EOL> self . intercept_responses = False <EOL> def init ( self , args ) : <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> self . source . init ( args ) <EOL> def mangle_request ( self , request ) : <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> req = self . source . mangle_request ( request ) <EOL> return req <EOL> return request <EOL> def mangle_response ( self , request ) : <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> rsp = self . source . mangle_response ( request ) <EOL> return rsp <EOL> return request . response <EOL> @ defer . inlineCallbacks <EOL> def async_mangle_request ( self , request ) : <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> req = yield self . source . async_mangle_request ( request ) <EOL> defer . returnValue ( req ) <EOL> defer . returnValue ( request ) <EOL> @ defer . inlineCallbacks <EOL> def async_mangle_response ( self , request ) : <EOL> if hasattr ( self . source , '<STR_LIT>' ) : <EOL> rsp = yield self . source . async_mangle_response ( request ) <EOL> defer . returnValue ( rsp ) <EOL> defer . returnValue ( request . response ) <EOL> class MacroTemplate ( object ) : <EOL> _template_data = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> gen_template_args_macro ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> gen_template_generator_noargs ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> gen_template_generator_noargs ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> gen_template_generator_noargs ( '<STR_LIT>' ) ) , <EOL> } <EOL> @ classmethod <EOL> def fill_template ( cls , template , subs ) : <EOL> loader = FileSystemLoader ( session . config . pappy_dir + '<STR_LIT>' ) <EOL> env = Environment ( loader = loader ) <EOL> template = env . get_template ( cls . _template_data [ template ] [ <NUM_LIT:0> ] ) <EOL> return template . render ( zip = zip , ** subs ) <EOL> @ classmethod <EOL> @ defer . inlineCallbacks <EOL> def fill_template_args ( cls , template , args = [ ] ) : <EOL> ret = cls . _template_data [ template ] [ <NUM_LIT:4> ] ( args ) <EOL> if isinstance ( ret , defer . Deferred ) : <EOL> ret = yield ret <EOL> defer . returnValue ( ret ) <EOL> @ classmethod <EOL> def template_filename ( cls , template , fname ) : <EOL> return cls . _template_data [ template ] [ <NUM_LIT:3> ] . format ( fname = fname ) <EOL> @ classmethod <EOL> def template_list ( cls ) : <EOL> return [ k for k , v in cls . _template_data . iteritems ( ) ] <EOL> @ classmethod <EOL> def template_description ( cls , template ) : <EOL> return cls . _template_data [ template ] [ <NUM_LIT:1> ] <EOL> @ classmethod <EOL> def template_argstring ( cls , template ) : <EOL> return cls . _template_data [ template ] [ <NUM_LIT:2> ] <EOL> def load_macros ( loc ) : <EOL> """<STR_LIT>""" <EOL> macro_files = glob . glob ( loc + "<STR_LIT>" ) <EOL> macro_objs = [ ] <EOL> for f in macro_files : <EOL> try : <EOL> macro_objs . append ( Macro ( f ) ) <EOL> except PappyException as e : <EOL> print str ( e ) <EOL> int_macro_files = glob . glob ( loc + "<STR_LIT>" ) <EOL> int_macro_objs = [ ] <EOL> for f in int_macro_files : <EOL> try : <EOL> int_macro_objs . append ( FileInterceptMacro ( f ) ) <EOL> except PappyException as e : <EOL> print str ( e ) <EOL> return ( macro_objs , int_macro_objs ) <EOL> def req_obj_def ( req ) : <EOL> lines = req . full_request . splitlines ( True ) <EOL> esclines = [ line . encode ( '<STR_LIT>' ) for line in lines ] <EOL> params = [ ] <EOL> if req . is_ssl : <EOL> params . append ( '<STR_LIT>' ) <EOL> if req . port != <NUM_LIT> : <EOL> params . append ( '<STR_LIT>' % req . port ) <EOL> else : <EOL> if req . port != <NUM_LIT> : <EOL> params . append ( '<STR_LIT>' % req . port ) <EOL> if '<STR_LIT:host>' in req . headers and req . host != req . headers [ '<STR_LIT:host>' ] : <EOL> params . append ( '<STR_LIT>' % req . host ) <EOL> if params : <EOL> req_params = '<STR_LIT:U+002CU+0020>' + '<STR_LIT:U+002CU+0020>' . join ( params ) <EOL> else : <EOL> req_params = '<STR_LIT>' <EOL> ret = '<STR_LIT>' <EOL> for line in esclines : <EOL> ret += "<STR_LIT>" % line <EOL> ret += '<STR_LIT:)>' <EOL> ret += req_params <EOL> ret += '<STR_LIT:)>' <EOL> return ret <EOL> def macro_from_requests ( reqs , short_name = '<STR_LIT>' , long_name = '<STR_LIT>' ) : <EOL> subs = { } <EOL> if long_name : <EOL> subs [ '<STR_LIT>' ] = long_name <EOL> else : <EOL> random . seed ( ) <EOL> subs [ '<STR_LIT>' ] = '<STR_LIT>' % random . randint ( <NUM_LIT:1> , <NUM_LIT> ) <EOL> subs [ '<STR_LIT>' ] = short_name <EOL> req_lines = [ ] <EOL> req_params = [ ] <EOL> for req in reqs : <EOL> lines = req . full_request . splitlines ( True ) <EOL> esclines = [ line . encode ( '<STR_LIT>' ) for line in lines ] <EOL> req_lines . append ( esclines ) <EOL> params = [ ] <EOL> if req . is_ssl : <EOL> params . append ( '<STR_LIT>' ) <EOL> if req . port != <NUM_LIT> : <EOL> params . append ( '<STR_LIT>' % req . port ) <EOL> else : <EOL> if req . port != <NUM_LIT> : <EOL> params . append ( '<STR_LIT>' % req . port ) <EOL> if params : <EOL> req_params . append ( '<STR_LIT:U+002CU+0020>' + '<STR_LIT:U+002CU+0020>' . join ( params ) ) <EOL> else : <EOL> req_params . append ( '<STR_LIT>' ) <EOL> subs [ '<STR_LIT>' ] = req_lines <EOL> subs [ '<STR_LIT>' ] = req_params <EOL> return MacroTemplate . fill_template ( '<STR_LIT>' , subs ) <EOL> @ defer . inlineCallbacks <EOL> def mangle_request ( request , intmacros ) : <EOL> """<STR_LIT>""" <EOL> if not intmacros : <EOL> defer . returnValue ( ( request , False ) ) <EOL> cur_req = request . copy ( ) <EOL> for k , macro in intmacros . iteritems ( ) : <EOL> if macro . intercept_requests : <EOL> if macro . async_req : <EOL> cur_req = yield macro . async_mangle_request ( cur_req . copy ( ) ) <EOL> else : <EOL> cur_req = macro . mangle_request ( cur_req . copy ( ) ) <EOL> if cur_req is None : <EOL> defer . returnValue ( ( None , True ) ) <EOL> mangled = False <EOL> if not cur_req == request or not cur_req . host == request . host or not cur_req . port == request . port : <EOL> cur_req . unmangled = request <EOL> cur_req . unmangled . is_unmangled_version = True <EOL> if request . response : <EOL> cur_req . response = request . response <EOL> request . response = None <EOL> mangled = True <EOL> else : <EOL> cur_req = request <EOL> defer . returnValue ( ( cur_req , mangled ) ) <EOL> @ defer . inlineCallbacks <EOL> def mangle_response ( request , intmacros ) : <EOL> """<STR_LIT>""" <EOL> if not intmacros : <EOL> defer . returnValue ( False ) <EOL> old_rsp = request . response <EOL> request . response = request . response . copy ( ) <EOL> for k , macro in intmacros . iteritems ( ) : <EOL> if macro . intercept_responses : <EOL> if macro . async_rsp : <EOL> request . response = yield macro . async_mangle_response ( request ) <EOL> else : <EOL> request . response = macro . mangle_response ( request ) <EOL> if request . response is None : <EOL> defer . returnValue ( True ) <EOL> mangled = False <EOL> if not old_rsp == request . response : <EOL> request . response . rspid = old_rsp <EOL> old_rsp . rspid = None <EOL> request . response . unmangled = old_rsp <EOL> request . response . unmangled . is_unmangled_version = True <EOL> mangled = True <EOL> else : <EOL> request . response = old_rsp <EOL> defer . returnValue ( mangled ) </s>
