<s> import quickproxy <EOL> import os <EOL> import fnmatch <EOL> import devdns <EOL> from tornado import ioloop <EOL> from copy import copy <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> routes = [ ] <EOL> def split_host ( host ) : <EOL> name = '<STR_LIT>' <EOL> port = None <EOL> parts = host . split ( '<STR_LIT::>' ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> name = parts [ <NUM_LIT:0> ] <EOL> try : <EOL> port = int ( parts [ - <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> name = parts [ - <NUM_LIT:1> ] <EOL> return name , port <EOL> def make_callbacks ( routes ) : <EOL> def req_callback ( req ) : <EOL> host = '<STR_LIT>' <EOL> port = None <EOL> for src , dst in routes : <EOL> if src [ <NUM_LIT:0> ] and fnmatch . fnmatch ( req . host , src [ <NUM_LIT:0> ] ) : <EOL> if src [ <NUM_LIT:1> ] and req . port != src [ <NUM_LIT:1> ] : <EOL> continue <EOL> host = dst [ <NUM_LIT:0> ] or '<STR_LIT:127.0.0.1>' <EOL> port = dst [ <NUM_LIT:1> ] or <NUM_LIT> <EOL> break <EOL> if not host : <EOL> body = '<STR_LIT>' % ( req . host , req . port ) <EOL> return quickproxy . ResponseObj ( body = body , headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } ) <EOL> else : <EOL> req . host , req . port = host , port <EOL> return req <EOL> def ssl_callback ( req ) : <EOL> req . port = <NUM_LIT> <EOL> req . protocol = '<STR_LIT:http>' <EOL> req . headers . update ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> return req_callback ( req ) <EOL> return req_callback , ssl_callback <EOL> def run ( port , routes , sslport = None , dnsport = <NUM_LIT> , tld = '<STR_LIT>' , ip = '<STR_LIT:127.0.0.1>' , verbose = False ) : <EOL> req_callback , ssl_callback = make_callbacks ( routes ) <EOL> dnsc = devdns . connect ( port = dnsport ) <EOL> print '<STR_LIT>' % ( tld , ip ) <EOL> mainloop = ioloop . IOLoop . instance ( ) <EOL> periodic = ioloop . PeriodicCallback ( lambda : devdns . get_data ( dnsc , tld , ip , verbose ) , <EOL> <NUM_LIT> , <EOL> io_loop = mainloop ) <EOL> periodic . start ( ) <EOL> all_methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> quickproxy . run_proxy ( port = port , <EOL> methods = all_methods , <EOL> req_callback = req_callback , <EOL> debug_level = <NUM_LIT:1> if verbose else <NUM_LIT:0> , <EOL> start_ioloop = False ) <EOL> if sslport : <EOL> quickproxy . run_proxy ( port = sslport , <EOL> test_ssl = True , <EOL> methods = all_methods , <EOL> req_callback = ssl_callback , <EOL> debug_level = <NUM_LIT:1> if verbose else <NUM_LIT:0> , <EOL> start_ioloop = False ) <EOL> try : <EOL> print ( "<STR_LIT>" % port ) <EOL> mainloop . start ( ) <EOL> except KeyboardInterrupt : <EOL> dnsc . close ( ) <EOL> def configure ( routes = None , filename = None , watch = False ) : <EOL> def load_routes ( ) : <EOL> newroutes = [ ] <EOL> f = open ( filename ) <EOL> for line in f : <EOL> parts = line . split ( ) <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> newroutes . append ( map ( split_host , parts ) ) <EOL> else : <EOL> if parts : <EOL> print "<STR_LIT>" % line <EOL> f . close ( ) <EOL> return newroutes <EOL> if filename : <EOL> routes = load_routes ( ) <EOL> elif routes : <EOL> routes = [ map ( split_host , route ) for route in routes ] <EOL> print "<STR_LIT>" % len ( routes ) <EOL> if watch and filename : <EOL> from watchdog . observers import Observer <EOL> from watchdog . events import PatternMatchingEventHandler <EOL> class AliasWatcher ( PatternMatchingEventHandler ) : <EOL> def on_modified ( self , event ) : <EOL> routes [ : ] = load_routes ( ) <EOL> filename = os . path . normpath ( os . path . abspath ( filename ) ) <EOL> event_handler = AliasWatcher ( patterns = [ copy ( filename ) ] ) <EOL> observer = Observer ( ) <EOL> if not os . path . splitext ( filename ) [ <NUM_LIT:1> ] : <EOL> filename = filename . rstrip ( '<STR_LIT:/>' ) + '<STR_LIT:/>' <EOL> folder = os . path . split ( filename ) [ <NUM_LIT:0> ] <EOL> observer . schedule ( event_handler , path = folder , recursive = True ) <EOL> observer . start ( ) <EOL> return routes </s>
