<s> from teleport import Teleport <EOL> import logging <EOL> from contextlib import contextmanager <EOL> from subprocess import check_call <EOL> def _run_commands ( commands ) : <EOL> for command in commands : <EOL> logging . debug ( "<STR_LIT>" , "<STR_LIT:U+0020>" . join ( command ) ) <EOL> check_call ( command ) <EOL> def allow_traffic_only_to ( address , dns_servers = None ) : <EOL> host = address . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] <EOL> logging . info ( "<STR_LIT>" , host ) <EOL> commands = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:state>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , host , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ] <EOL> if dns_servers is not None : <EOL> logging . info ( '<STR_LIT>' , dns_servers ) <EOL> for dns_server in dns_servers : <EOL> commands . append ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , dns_server , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> _run_commands ( commands ) <EOL> def reset_firewall ( ) : <EOL> logging . info ( "<STR_LIT>" ) <EOL> commands = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ] <EOL> _run_commands ( commands ) <EOL> @ contextmanager <EOL> def FirewallContext ( address , dns_servers = None ) : <EOL> allow_traffic_only_to ( address , dns_servers ) <EOL> try : <EOL> yield <EOL> finally : <EOL> reset_firewall ( ) <EOL> @ contextmanager <EOL> def Teleporter ( config , place , with_firewall = True , dns_servers = None ) : <EOL> t = Teleport ( config ) . goto ( place ) <EOL> try : <EOL> if with_firewall : <EOL> with FirewallContext ( t . get_peer_address ( ) , dns_servers = dns_servers ) : <EOL> yield t <EOL> else : <EOL> yield t <EOL> finally : <EOL> t . go_home ( ) </s>
