<s> r'''<STR_LIT>''' <EOL> import logging <EOL> import subprocess <EOL> from nogotofail . mitm . util . ip import get_interface_addresses <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def run_command ( command , ignore_failed = False ) : <EOL> try : <EOL> _ = subprocess . check_output ( <EOL> command , stderr = subprocess . STDOUT , shell = True ) <EOL> except Exception as e : <EOL> if not ignore_failed : <EOL> raise e <EOL> def run_iptables_cmd ( command , ipv4 = True , ipv6 = False , ignore_failed = False ) : <EOL> ipv4_bin = "<STR_LIT>" <EOL> ipv6_bin = "<STR_LIT>" <EOL> if ipv4 : <EOL> run_command ( ipv4_bin + "<STR_LIT:U+0020>" + command , ignore_failed ) <EOL> if ipv6 : <EOL> run_command ( ipv6_bin + "<STR_LIT:U+0020>" + command , ignore_failed ) <EOL> def run_ip_cmd ( command , ipv4 = True , ipv6 = False , ignore_failed = True ) : <EOL> ipv4_bin = "<STR_LIT>" <EOL> ipv6_bin = "<STR_LIT>" <EOL> if ipv4 : <EOL> run_command ( ipv4_bin + "<STR_LIT:U+0020>" + command , ignore_failed ) <EOL> if ipv6 : <EOL> run_command ( ipv6_bin + "<STR_LIT:U+0020>" + command , ignore_failed ) <EOL> def add_local_bypass ( table , chain , ipv6 ) : <EOL> """<STR_LIT>""" <EOL> run_iptables_cmd ( <EOL> "<STR_LIT>" % ( table , chain ) , ipv6 = ipv6 ) <EOL> v4_addrs , v6_addrs = get_interface_addresses ( ) <EOL> for addr in v4_addrs : <EOL> run_iptables_cmd ( <EOL> "<STR_LIT>" % ( table , chain , addr ) , <EOL> ipv6 = False ) <EOL> for addr in v6_addrs : <EOL> run_iptables_cmd ( <EOL> "<STR_LIT>" % ( table , chain , addr ) , <EOL> ipv4 = False , ipv6 = ipv6 ) <EOL> TPROXY_CHAIN = "<STR_LIT>" <EOL> def disable_tproxy_rules ( ipv6 = False , mark = <NUM_LIT:100> ) : <EOL> run_ip_cmd ( "<STR_LIT>" % ( mark , mark ) , <EOL> ipv6 = ipv6 , ignore_failed = True ) <EOL> run_iptables_cmd ( "<STR_LIT>" % TPROXY_CHAIN , <EOL> ipv6 = ipv6 , ignore_failed = True ) <EOL> run_iptables_cmd ( "<STR_LIT>" % TPROXY_CHAIN , ipv6 = ipv6 , ignore_failed = True ) <EOL> run_iptables_cmd ( "<STR_LIT>" % TPROXY_CHAIN , ipv6 = ipv6 , ignore_failed = True ) <EOL> def enable_tproxy_rules ( port , ipv6 = False , mark = <NUM_LIT:100> ) : <EOL> disable_tproxy_rules ( ipv6 , mark ) <EOL> try : <EOL> run_ip_cmd ( "<STR_LIT>" % ( mark , mark ) , ipv6 = ipv6 ) <EOL> run_ip_cmd ( "<STR_LIT>" % mark , ipv6 = ipv6 ) <EOL> run_iptables_cmd ( "<STR_LIT>" % TPROXY_CHAIN , ipv6 = ipv6 ) <EOL> add_local_bypass ( "<STR_LIT>" , TPROXY_CHAIN , ipv6 ) <EOL> run_iptables_cmd ( <EOL> "<STR_LIT>" % TPROXY_CHAIN , ipv6 = ipv6 ) <EOL> run_iptables_cmd ( <EOL> "<STR_LIT>" % <EOL> ( TPROXY_CHAIN , mark , port ) , ipv6 = ipv6 ) <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> disable_tproxy_rules ( ipv6 = ipv6 , mark = mark ) <EOL> REDIRECT_CHAIN = "<STR_LIT>" <EOL> def disable_redirect_rules ( ipv6 = False ) : <EOL> run_iptables_cmd ( "<STR_LIT>" % REDIRECT_CHAIN , <EOL> ipv6 = ipv6 , ignore_failed = True ) <EOL> run_iptables_cmd ( "<STR_LIT>" % REDIRECT_CHAIN , ipv6 = ipv6 , ignore_failed = True ) <EOL> run_iptables_cmd ( "<STR_LIT>" % REDIRECT_CHAIN , ipv6 = ipv6 , ignore_failed = True ) <EOL> def enable_redirect_rules ( port , ipv6 = False ) : <EOL> disable_redirect_rules ( ipv6 = ipv6 ) <EOL> try : <EOL> run_iptables_cmd ( "<STR_LIT>" % REDIRECT_CHAIN , ipv6 = ipv6 ) <EOL> add_local_bypass ( "<STR_LIT>" , REDIRECT_CHAIN , ipv6 ) <EOL> run_iptables_cmd ( <EOL> "<STR_LIT>" % REDIRECT_CHAIN , ipv6 = ipv6 ) <EOL> run_iptables_cmd ( <EOL> "<STR_LIT>" % ( REDIRECT_CHAIN , port ) , <EOL> ipv6 = ipv6 ) <EOL> except Exception : <EOL> logger . exception ( "<STR_LIT>" ) <EOL> disable_redirect_rules ( ipv6 = ipv6 ) </s>
