| import os |
| import subprocess |
|
|
| |
| def install_frpc(): |
| print("正在安装 frpc...") |
|
|
| |
| local_path = "/root/modelscope" |
| frpc_tar_gz = os.path.join(local_path, "frp_0.61.0_linux_amd64.tar.gz") |
|
|
| try: |
| |
| if not os.path.exists(frpc_tar_gz): |
| print(f"错误:文件 {frpc_tar_gz} 不存在。") |
| exit(1) |
|
|
| |
| subprocess.run(["tar", "-xvzf", frpc_tar_gz, "-C", local_path], check=True) |
| print("frpc 解压完成。") |
|
|
| |
| os.remove(frpc_tar_gz) |
| print("已删除 tar.gz 文件。") |
|
|
| |
| frpc_extracted_dir = os.path.join(local_path, "frp_0.61.0_linux_amd64") |
| subprocess.run(["mv", os.path.join(frpc_extracted_dir, "frpc"), "/root/"], check=True) |
| print("frpc 已成功移动到 /root 目录。") |
|
|
| except subprocess.CalledProcessError as e: |
| print(f"安装 frpc 失败: {e}") |
| exit(1) |
|
|
| |
| def create_frpc_toml(): |
| toml_path = "/root/frpc.toml" |
| toml_content = """ |
| [common] |
| server_addr = 182.43.67.222 |
| server_port = 7000 |
| [jl] |
| type = tcp |
| local_ip = 127.0.0.1 |
| local_port = 8888 |
| remote_port = 8888 |
| [comfyui] |
| type = tcp |
| local_ip = 127.0.0.1 |
| local_port = 8188 |
| remote_port = 8188 |
| """ |
|
|
| |
| with open(toml_path, 'w') as file: |
| file.write(toml_content) |
|
|
| print(f"frpc.toml 配置文件已创建:{toml_path}") |
|
|
| |
| def create_frp_service_script(): |
| script_path = "/root/frp_service.sh" |
| script_content = '''#!/bin/bash |
| # 设置 frp 的路径和配置文件路径 |
| FRP_PATH="/root/frpc" |
| FRP_CONFIG="/root/frpc.toml" # 使用新的 TOML 配置文件 |
| # 启动 frp 并保持运行 |
| while true; do |
| # 启动 frp |
| $FRP_PATH -c $FRP_CONFIG |
| # 如果 frp 崩溃或关闭,则重新启动 |
| echo "frp 连接已断开,正在重新启动..." |
| sleep 1 # 等待 1 秒再重启 |
| done |
| ''' |
|
|
| |
| with open(script_path, 'w') as file: |
| file.write(script_content) |
|
|
| |
| os.chmod(script_path, 0o755) |
| print(f"脚本已创建并设置为可执行:{script_path}") |
|
|
| |
| def create_systemd_service(): |
| service_path = "/etc/systemd/system/frp.service" |
| service_content = '''[Unit] |
| Description=frp connection service |
| After=network.target |
| [Service] |
| ExecStart=/root/frp_service.sh |
| Restart=always |
| User=root |
| WorkingDirectory=/root |
| StandardOutput=journal |
| StandardError=journal |
| SyslogIdentifier=frp |
| [Install] |
| WantedBy=multi-user.target |
| ''' |
|
|
| |
| with open(service_path, 'w') as file: |
| file.write(service_content) |
|
|
| print(f"systemd 服务文件已创建:{service_path}") |
|
|
| |
| def enable_and_start_service(): |
| try: |
| subprocess.run(["sudo", "systemctl", "daemon-reload"], check=True) |
| subprocess.run(["sudo", "systemctl", "enable", "frp.service"], check=True) |
| subprocess.run(["sudo", "systemctl", "start", "frp.service"], check=True) |
| print("frp 服务已启用并启动。") |
| except subprocess.CalledProcessError as e: |
| print(f"服务启动失败: {e}") |
|
|
| |
| def main(): |
| install_frpc() |
| create_frpc_toml() |
| create_frp_service_script() |
| create_systemd_service() |
| enable_and_start_service() |
|
|
| if __name__ == "__main__": |
| main() |
|
|