コード例 #1
0
def iptables_runner(
    port_id: int,
    server_id: int,
    local_port: int,
    remote_address: str,
    remote_port: int = None,
    forward_type: str = None,
    update_status: bool = False,
):
    try:
        if not is_ip(remote_address):
            remote_ip = dns_query(remote_address)
        else:
            remote_ip = remote_address
        with db_session() as db:
            port = get_port(db, server_id, port_id)
            if not forward_type:
                args = f" delete {local_port}"
            elif remote_port:
                port.forward_rule.config["remote_ip"] = remote_ip
                db.add(port.forward_rule)
                db.commit()
                args = (
                    f" -t={forward_type}"
                    f" {'-v=6' if is_ipv6(remote_ip) else '-v=4'}"
                    f" forward {local_port} {remote_ip} {remote_port}"
                )
            else:
                args = f" list {local_port}"
            server = get_server_with_ports_usage(db, server_id)

        extravars = {
            "host": server.ansible_name,
            "local_port": local_port,
            "iptables_args": args,
        }

        run(
            server=server,
            playbook="iptables.yml",
            extravars=extravars,
            status_handler=lambda s, **k: status_handler(
                port_id, s, update_status
            ),
            finished_callback=iptables_finished_handler(
                server.id, port_id, True
            )
            if update_status
            else lambda r: None,
        )
    except Exception:
        traceback.print_exc()
        with db_session() as db:
            port = get_port(db, server_id, port_id)
            port.forward_rule.status = "failed"
            port.forward_rule.config["error"] = traceback.format_exc()
            print(port.forward_rule.__dict__)
            db.add(port.forward_rule)
            db.commit()
コード例 #2
0
ファイル: traffic.py プロジェクト: fishg/backend
def traffic_server_runner(server_id: Server):
    with db_session() as db:
        server = get_server_with_ports_usage(db, server_id)
    run(
        server=server,
        playbook="traffic.yml",
        finished_callback=iptables_finished_handler(server.id),
    )
コード例 #3
0
ファイル: app.py プロジェクト: fishg/backend
def app_runner(
    port_id: int,
    server_id: int,
    port_num: int,
    app_name: str,
    app_command: str = None,
    app_config: t.Dict = None,
    app_version_arg: str = "-v",
    traffic_meter: bool = True,
    app_role_name: str = "app",
    app_download_role_name: str = None,
    app_sync_role_name: str = "app_sync",
    app_get_role_name: str = "app_get",
    remote_ip: str = "ANYWHERE",
    ident: str = None,
    update_status: bool = False,
):
    with db_session() as db:
        server = get_server_with_ports_usage(db, server_id)
    extravars = {
        "host": server.ansible_name,
        "local_port": port_num,
        "remote_ip": remote_ip,
        "app_name": app_name,
        "app_command": app_command,
        "app_version_arg": app_version_arg,
        "traffic_meter": traffic_meter,
        "app_download_role_name": app_download_role_name
        if app_download_role_name is not None
        else f"{app_name}_download",
        "app_role_name": app_role_name,
        "app_sync_role_name": app_sync_role_name,
        "app_get_role_name": app_get_role_name,
        "update_status": update_status,
        "update_app": update_status and not server.config.get(app_name),
    }
    if app_config is not None:
        with open(
            f"ansible/project/roles/app/files/{app_name}-{port_id}", "w"
        ) as f:
            f.write(app_config)
        extravars["app_config"] = f"{app_name}-{port_id}"

    run(
        server=server,
        playbook="app.yml",
        extravars=extravars,
        ident=ident,
        status_handler=lambda s, **k: status_handler(port_id, s, update_status),
        finished_callback=iptables_finished_handler(server.id, port_id, True)
        if update_status
        else lambda r: None,
    )
コード例 #4
0
ファイル: clean.py プロジェクト: cmz0228/backend
def clean_port_runner(
    server_id: int, port_num: int, update_traffic: bool = True
):
    with db_session() as db:
        server = get_server_with_ports_usage(db, server_id)
    run(
        server=server,
        playbook="clean_port.yml",
        extravars={"local_port": port_num},
        finished_callback=iptables_finished_handler(
            server, accumulate=True, update_traffic_bool=update_traffic
        ),
    )
コード例 #5
0
ファイル: app.py プロジェクト: fishg/backend
def rule_runner(rule_id: int):
    try:
        with db_session() as db:
            rule = get_forward_rule_by_id(db, rule_id)
            port, port_id, port_num, server_id = (
                rule.port,
                rule.port.id,
                rule.port.num,
                rule.port.server.id,
            )
            ident = uuid4()
            app_configs = []
            if rule.config.get("reverse_proxy"):
                reverse_proxy_port = get_port_by_id(
                    db, rule.config.get("reverse_proxy")
                )
                app_configs.append(
                    AppConfig.configs[
                        reverse_proxy_port.forward_rule.method
                    ].apply(db, reverse_proxy_port)
                )
            app_configs.append(
                AppConfig.configs[rule.method].apply(db, rule.port)
            )
            db.refresh(rule)
            server = get_server_with_ports_usage(db, server_id)

        for config in app_configs:
            runner = run(
                server,
                config.playbook,
                extravars=config.extravars,
                ident=ident,
                status_handler=lambda s, **k: status_handler(port_id, s, True),
                finished_callback=iptables_finished_handler(
                    server.id, port_id, True
                ),
            )
            if runner.status != "successful":
                break
    except Exception:
        with db_session() as db:
            rule.status = "failed"
            rule.config["error"] = traceback.format_exc()
            db.add(rule)
            db.commit()
コード例 #6
0
ファイル: clean.py プロジェクト: fishg/backend
@huey.task(priority=4)
def clean_port_runner(server_id: int, port: Port, update_traffic: bool = True):
    with db_session() as db:
        if db_forward_rule := get_forward_rule(db, server_id, port.id):
            db.delete(db_forward_rule)
            db.commit()
        server = get_server_with_ports_usage(db, server_id)
        if db_forward_rule := get_forward_rule(db, server_id, port.id):
            db.delete(db_forward_rule)
            db.commit()
    run(
        server=server,
        playbook="clean_port.yml",
        extravars={"local_port": port.num},
        finished_callback=iptables_finished_handler(
            server.id, accumulate=True, update_traffic_bool=update_traffic),
    )


@huey.periodic_task(crontab(minute="*"), priority=4)
def clean_expired_port_runner():
    with db_session() as db:
        db_expire_rules = get_all_expire_rules(db)
    for db_rule in db_expire_rules:
        if time.time() > db_rule.config.get("expire_time", float("inf")):
            clean_port_runner(
                db_rule.port.server.id,
                db_rule.port,
                update_traffic=True,
            )