File size: 3,646 Bytes
f6e0e0b
 
 
c04e9fa
 
 
 
b4f6dbd
 
 
c04e9fa
b4f6dbd
 
 
 
 
c04e9fa
 
b4f6dbd
 
 
 
 
 
c04e9fa
b4f6dbd
 
 
 
c04e9fa
 
 
 
 
 
f6e0e0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c04e9fa
f6e0e0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c04e9fa
f6e0e0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c04e9fa
f6e0e0b
 
 
 
 
c04e9fa
f6e0e0b
 
 
c04e9fa
f6e0e0b
c04e9fa
 
 
 
 
f6e0e0b
 
b4f6dbd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import subprocess

# 1. 安装 frpc
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)

        # 解压下载的 tar.gz 文件
        subprocess.run(["tar", "-xvzf", frpc_tar_gz, "-C", local_path], check=True)
        print("frpc 解压完成。")

        # 移除 tar.gz 文件
        os.remove(frpc_tar_gz)
        print("已删除 tar.gz 文件。")

        # 移动 frpc 到 /root 目录
        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)

# 2. 创建 frpc.toml 配置文件
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
    """

    # 写入 frpc.toml 文件
    with open(toml_path, 'w') as file:
        file.write(toml_content)

    print(f"frpc.toml 配置文件已创建:{toml_path}")

# 3. 创建启动脚本
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}")

# 4. 创建 systemd 服务文件
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}")

# 5. 启动并启用服务
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()          # 安装 frpc
    create_frpc_toml()      # 创建 frpc.toml 配置文件
    create_frp_service_script()  # 创建启动脚本
    create_systemd_service()     # 创建 systemd 服务
    enable_and_start_service()   # 启动并启用服务

if __name__ == "__main__":
    main()