<s> import urlparse , logging , os , sys , random <EOL> from twisted . web . http import Request <EOL> from twisted . web . http import HTTPChannel <EOL> from twisted . web . http import HTTPClient <EOL> from twisted . internet import ssl <EOL> from twisted . internet import defer <EOL> from twisted . internet import reactor <EOL> from twisted . internet . protocol import ClientFactory <EOL> from ServerConnectionFactory import ServerConnectionFactory <EOL> from ServerConnection import ServerConnection <EOL> from SSLServerConnection import SSLServerConnection <EOL> from URLMonitor import URLMonitor <EOL> from CookieCleaner import CookieCleaner <EOL> from DnsCache import DnsCache <EOL> class ClientRequest ( Request ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , channel , queued , reactor = reactor ) : <EOL> Request . __init__ ( self , channel , queued ) <EOL> self . reactor = reactor <EOL> self . urlMonitor = URLMonitor . getInstance ( ) <EOL> self . cookieCleaner = CookieCleaner . getInstance ( ) <EOL> self . dnsCache = DnsCache . getInstance ( ) <EOL> def cleanHeaders ( self ) : <EOL> headers = self . getAllHeaders ( ) . copy ( ) <EOL> if '<STR_LIT>' in headers : <EOL> del headers [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in headers : <EOL> del headers [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in headers : <EOL> del headers [ '<STR_LIT>' ] <EOL> return headers <EOL> def getPathFromUri ( self ) : <EOL> if ( self . uri . find ( "<STR_LIT>" ) == <NUM_LIT:0> ) : <EOL> index = self . uri . find ( '<STR_LIT:/>' , <NUM_LIT:7> ) <EOL> return self . uri [ index : ] <EOL> return self . uri <EOL> def getPathToLockIcon ( self ) : <EOL> if os . path . exists ( "<STR_LIT>" ) : return "<STR_LIT>" <EOL> scriptPath = os . path . abspath ( os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) ) <EOL> scriptPath = os . path . join ( scriptPath , "<STR_LIT>" ) <EOL> if os . path . exists ( scriptPath ) : return scriptPath <EOL> logging . warning ( "<STR_LIT>" % ( self . getClientIP ( ) ) ) <EOL> return "<STR_LIT>" <EOL> def handleHostResolvedSuccess ( self , address ) : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) , self . getHeader ( '<STR_LIT:host>' ) , address ) ) <EOL> host = self . getHeader ( "<STR_LIT:host>" ) <EOL> headers = self . cleanHeaders ( ) <EOL> client = self . getClientIP ( ) <EOL> path = self . getPathFromUri ( ) <EOL> self . content . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> postData = self . content . read ( ) <EOL> url = '<STR_LIT>' + host + path <EOL> self . dnsCache . cacheResolution ( host , address ) <EOL> if ( not self . cookieCleaner . isClean ( self . method , client , host , headers ) ) : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) ) ) <EOL> self . sendExpiredCookies ( host , path , self . cookieCleaner . getExpireHeaders ( self . method , client , <EOL> host , headers , path ) ) <EOL> elif ( self . urlMonitor . isSecureFavicon ( client , path ) ) : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) ) ) <EOL> self . sendSpoofedFaviconResponse ( ) <EOL> elif ( self . urlMonitor . isSecureLink ( client , url ) ) : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) ) ) <EOL> self . proxyViaSSL ( address , self . method , path , postData , headers , <EOL> self . urlMonitor . getSecurePort ( client , url ) ) <EOL> else : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) ) ) <EOL> self . proxyViaHTTP ( address , self . method , path , postData , headers ) <EOL> def handleHostResolvedError ( self , error ) : <EOL> logging . warning ( "<STR_LIT>" + str ( error ) % ( self . getClientIP ( ) ) ) <EOL> self . finish ( ) <EOL> def resolveHost ( self , host ) : <EOL> address = self . dnsCache . getCachedAddress ( host ) <EOL> if address != None : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) ) ) <EOL> return defer . succeed ( address ) <EOL> else : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) ) ) <EOL> return reactor . resolve ( host ) <EOL> def process ( self ) : <EOL> logging . debug ( "<STR_LIT>" % ( self . getClientIP ( ) , self . getHeader ( '<STR_LIT:host>' ) ) ) <EOL> host = self . getHeader ( '<STR_LIT:host>' ) <EOL> deferred = self . resolveHost ( host ) <EOL> deferred . addCallback ( self . handleHostResolvedSuccess ) <EOL> deferred . addErrback ( self . handleHostResolvedError ) <EOL> def proxyViaHTTP ( self , host , method , path , postData , headers ) : <EOL> connectionFactory = ServerConnectionFactory ( method , path , postData , headers , self ) <EOL> connectionFactory . protocol = ServerConnection <EOL> self . reactor . connectTCP ( host , <NUM_LIT> , connectionFactory ) <EOL> def proxyViaSSL ( self , host , method , path , postData , headers , port ) : <EOL> clientContextFactory = ssl . ClientContextFactory ( ) <EOL> connectionFactory = ServerConnectionFactory ( method , path , postData , headers , self ) <EOL> connectionFactory . protocol = SSLServerConnection <EOL> self . reactor . connectSSL ( host , port , connectionFactory , clientContextFactory ) <EOL> def sendExpiredCookies ( self , host , path , expireHeaders ) : <EOL> self . setResponseCode ( <NUM_LIT> , "<STR_LIT>" ) <EOL> self . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . setHeader ( "<STR_LIT>" , "<STR_LIT>" + host + path ) <EOL> for header in expireHeaders : <EOL> self . setHeader ( "<STR_LIT>" , header ) <EOL> self . finish ( ) <EOL> def sendSpoofedFaviconResponse ( self ) : <EOL> icoFile = open ( self . getPathToLockIcon ( ) ) <EOL> self . setResponseCode ( <NUM_LIT:200> , "<STR_LIT:OK>" ) <EOL> self . setHeader ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . write ( icoFile . read ( ) ) <EOL> icoFile . close ( ) <EOL> self . finish ( ) </s>
