Пример #1
0
def generate_iptables(config: TypedConfigParser) -> int:
    """
    Main runner to generate Iptables rules

    :param config: the configuration used to define the rules
    :raise subprocess.CalledProcessError on unexpected error
    :return: 0 on success, -X on error
    """
    if config.has_section("global"):
        try:
            executors.setup_global_begin(config["global"])
        except subprocess.CalledProcessError as exc:
            if exc.returncode == 127:
                print("iptables was not found in your path. This may be caused if you are not running it as root")
                return -1
            else:
                raise

    for section in config.sections():
        if section in ["global", "ssh_knocking", "logging"]:
            continue

        try:
            print(section)
            executors.handle_service(config[section])
        except subprocess.CalledProcessError:
            return -10

    if config.has_section("global"):
        try:
            executors.setup_global_end(config["global"])
        except subprocess.CalledProcessError:
            return -15
Пример #2
0
def generate_iptables(config: TypedConfigParser) -> int:
    """
    Main runner to generate Iptables rules

    :param config: the configuration used to define the rules
    :raise subprocess.CalledProcessError on unexpected error
    :return: 0 on success, -X on error
    """
    if config.has_section("global"):
        try:
            executors.setup_global_begin(config["global"])
        except subprocess.CalledProcessError as exc:
            if exc.returncode == 127:
                print(
                    "iptables was not found in your path. This may be caused if you are not running it as root"
                )
                return -1
            else:
                raise

    for section in config.sections():
        if section in ["global", "ssh_knocking", "logging"]:
            continue

        try:
            print(section)
            executors.handle_service(config[section])
        except subprocess.CalledProcessError:
            return -10

    if config.has_section("global"):
        try:
            executors.setup_global_end(config["global"])
        except subprocess.CalledProcessError:
            return -15
Пример #3
0
def run():
    """
    Parses the configuration, and run the utility

    :return: 0 on success, -X on error
    """
    arguments = parse_args()
    config = TypedConfigParser()
    config.read(arguments.conf)

    if arguments.new_config:
        conf_generator.generate_sample_conf()
        return

    if arguments.dry_run:
        Iptables.execute = lambda s, x: print("Iptables", x)
        Ip6tables.execute = lambda s, x: print("Ip6tables", x)

    try:
        return generate_iptables(config)
    except Exception as exc:
        print("ERROR :", exc)
        return -1
Пример #4
0
def run():
    """
    Parses the configuration, and run the utility

    :return: 0 on success, -X on error
    """
    arguments = parse_args()
    config = TypedConfigParser()
    config.read(arguments.conf)

    if arguments.new_config:
        conf_generator.generate_sample_conf()
        return

    if arguments.dry_run:
        Iptables.execute = lambda s, x: print("Iptables", x)
        Ip6tables.execute = lambda s, x: print("Ip6tables", x)

    try:
        return generate_iptables(config)
    except Exception as exc:
        print("ERROR :", exc)
        return -1