Example #1
0
def flush_tables(ipv6=False):
    """Flush all tables and apply the default policy ACCEPT to standard tables"""
    printer = Printer()
    with printer.Do("Flushing all tables: ipv6={!r}".format(ipv6)):
        iptc.easy.flush_all(ipv6=ipv6)

    with printer.Do("Setting ACCEPT policy in all chains"):
        policy = iptc.Policy("ACCEPT")
        for table_s in iptc.easy.get_tables(ipv6):
            for chain_s in iptc.easy.get_chains(table_s, ipv6):
                iptc.easy.set_policy(table_s,
                                     chain_s,
                                     policy=policy,
                                     ipv6=ipv6)
Example #2
0
def apply_config(config_file, server=None, protocol=None):
    """Apply a configuration to ip[6]tables (depends on the file name)

    :raises: IptablesError if an invalid rule is present. This leaves the
             table intact with rules applied so far.
    """
    table = init_table_from_file_name(config_file)
    table_s = table.name
    is_ipv6 = is_table_v6(table)

    printer = Printer()
    with printer.Do("Flushing table '{}', ipv6={!r}".format(table_s, is_ipv6)):
        iptc.easy.flush_table(table_s, ipv6=is_ipv6)
        policy = iptc.Policy("ACCEPT")
        for chain_s in iptc.easy.get_chains(table_s, ipv6=is_ipv6):
            iptc.easy.set_policy(table_s, chain_s, policy=policy, ipv6=is_ipv6)

    config_base = os.path.basename(config_file)
    with printer.Do("Applying '{}'".format(config_base)):
        config_d = read_config(config_file, server, protocol)
        _apply_config(table_s, config_d, ipv6=is_ipv6)

    return True
Example #3
0
def unzip():
    """Unzip the configuration files"""

    printer = Printer()
    zip_dir = resources.get_zip_dir(create=True)

    with printer.Do("Deleting old configuration files"):
        for ovpn_dir in ("ovpn_udp", "ovpn_tcp"):
            remove_dir = "{}/{}".format(zip_dir, ovpn_dir)
            if os.path.exists(remove_dir):
                rmtree(remove_dir, ignore_errors=True)

    for key, archive in __ARCHIVES.items():
        zip_file = resources.get_zip_file(archive)
        with ZipFile(zip_file, "r") as zip_stream:
            name_list = zip_stream.namelist()

            with printer.incremental_bar(
                "Unzipping {} configurations".format(key), max=len(name_list)
            ) as incremental_bar:
                for file_name in name_list:
                    zip_stream.extract(file_name, zip_dir)
                    incremental_bar.next()