<s> import sys <EOL> import socket <EOL> from random import randint <EOL> from zope . interface import implements <EOL> from twisted . internet import protocol , defer <EOL> from twisted . web . iweb import IBodyProducer <EOL> from scapy . config import conf <EOL> from ooni . errors import IfaceError <EOL> try : <EOL> from twisted . internet . endpoints import connectProtocol <EOL> except ImportError : <EOL> def connectProtocol ( endpoint , protocol ) : <EOL> class OneShotFactory ( protocol . Factory ) : <EOL> def buildProtocol ( self , addr ) : <EOL> return protocol <EOL> return endpoint . connect ( OneShotFactory ( ) ) <EOL> userAgents = ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> PLATFORMS = { '<STR_LIT>' : sys . platform . startswith ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : sys . platform . startswith ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : sys . platform . startswith ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : sys . platform . startswith ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : sys . platform . startswith ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : sys . platform . startswith ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : sys . platform . startswith ( "<STR_LIT:win32>" ) } <EOL> GOOGLE_HUMANS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class StringProducer ( object ) : <EOL> implements ( IBodyProducer ) <EOL> def __init__ ( self , body ) : <EOL> self . body = body <EOL> self . length = len ( body ) <EOL> def startProducing ( self , consumer ) : <EOL> consumer . write ( self . body ) <EOL> return defer . succeed ( None ) <EOL> def pauseProducing ( self ) : <EOL> pass <EOL> def stopProducing ( self ) : <EOL> pass <EOL> class BodyReceiver ( protocol . Protocol ) : <EOL> def __init__ ( self , finished , content_length = None , body_processor = None ) : <EOL> self . finished = finished <EOL> self . data = "<STR_LIT>" <EOL> self . bytes_remaining = content_length <EOL> self . body_processor = body_processor <EOL> def dataReceived ( self , b ) : <EOL> self . data += b <EOL> if self . bytes_remaining : <EOL> if self . bytes_remaining == <NUM_LIT:0> : <EOL> self . connectionLost ( None ) <EOL> else : <EOL> self . bytes_remaining -= len ( b ) <EOL> def connectionLost ( self , reason ) : <EOL> try : <EOL> if self . body_processor : <EOL> self . data = self . body_processor ( self . data ) <EOL> self . finished . callback ( self . data ) <EOL> except Exception as exc : <EOL> self . finished . errback ( exc ) <EOL> class Downloader ( protocol . Protocol ) : <EOL> def __init__ ( self , download_path , <EOL> finished , content_length = None ) : <EOL> self . finished = finished <EOL> self . bytes_remaining = content_length <EOL> self . fp = open ( download_path , '<STR_LIT>' ) <EOL> def dataReceived ( self , b ) : <EOL> self . fp . write ( b ) <EOL> if self . bytes_remaining : <EOL> if self . bytes_remaining == <NUM_LIT:0> : <EOL> self . connectionLost ( None ) <EOL> else : <EOL> self . bytes_remaining -= len ( b ) <EOL> def connectionLost ( self , reason ) : <EOL> self . fp . flush ( ) <EOL> self . fp . close ( ) <EOL> self . finished . callback ( None ) <EOL> class ConnectAndCloseProtocol ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . transport . loseConnection ( ) <EOL> def randomFreePort ( addr = "<STR_LIT:127.0.0.1>" ) : <EOL> """<STR_LIT>""" <EOL> free = False <EOL> while not free : <EOL> port = randint ( <NUM_LIT> , <NUM_LIT> ) <EOL> s = socket . socket ( ) <EOL> try : <EOL> s . bind ( ( addr , port ) ) <EOL> free = True <EOL> except : <EOL> pass <EOL> s . close ( ) <EOL> return port <EOL> def getDefaultIface ( ) : <EOL> """<STR_LIT>""" <EOL> iface = conf . route . route ( '<STR_LIT>' , verbose = <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> if len ( iface ) > <NUM_LIT:0> : <EOL> return iface <EOL> raise IfaceError <EOL> def getAddresses ( ) : <EOL> from scapy . all import get_if_addr , get_if_list <EOL> from ipaddr import IPAddress <EOL> addresses = set ( ) <EOL> for i in get_if_list ( ) : <EOL> try : <EOL> addresses . add ( get_if_addr ( i ) ) <EOL> except : <EOL> pass <EOL> if '<STR_LIT>' in addresses : <EOL> addresses . remove ( '<STR_LIT>' ) <EOL> return [ IPAddress ( addr ) for addr in addresses ] <EOL> def hasRawSocketPermission ( ) : <EOL> try : <EOL> socket . socket ( socket . AF_INET , socket . SOCK_RAW , socket . IPPROTO_RAW ) <EOL> return True <EOL> except socket . error : <EOL> return False </s>
