Update frp.py
Browse files
frp.py
CHANGED
|
@@ -1,108 +1,110 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
|
| 4 |
-
def create_frpc_toml():
|
| 5 |
-
# frpc.toml 文件路径
|
| 6 |
-
toml_path = "/root/frpc.toml"
|
| 7 |
-
|
| 8 |
-
# frpc.toml 配置内容
|
| 9 |
-
toml_content = """
|
| 10 |
-
[common]
|
| 11 |
-
server_addr = 182.43.67.222
|
| 12 |
-
server_port = 7000
|
| 13 |
-
|
| 14 |
-
[jl]
|
| 15 |
-
type = tcp
|
| 16 |
-
local_ip = 127.0.0.1
|
| 17 |
-
local_port = 8888
|
| 18 |
-
remote_port = 8888
|
| 19 |
-
|
| 20 |
-
[comfyui]
|
| 21 |
-
type = tcp
|
| 22 |
-
local_ip = 127.0.0.1
|
| 23 |
-
local_port = 8188
|
| 24 |
-
remote_port = 8188
|
| 25 |
-
"""
|
| 26 |
-
|
| 27 |
-
# 写入 frpc.toml 文件
|
| 28 |
-
with open(toml_path, 'w') as file:
|
| 29 |
-
file.write(toml_content)
|
| 30 |
-
|
| 31 |
-
print(f"frpc.toml 配置文件已创建:{toml_path}")
|
| 32 |
-
|
| 33 |
-
def create_frp_service_script():
|
| 34 |
-
# 脚本路径
|
| 35 |
-
script_path = "/root/frp_service.sh"
|
| 36 |
-
|
| 37 |
-
# 创建并写入启动脚本
|
| 38 |
-
script_content = '''#!/bin/bash
|
| 39 |
-
|
| 40 |
-
# 设置 frp 的路径和配置文件路径
|
| 41 |
-
FRP_PATH="/root/frpc"
|
| 42 |
-
FRP_CONFIG="/root/frpc.toml" # 使用新的 TOML 配置文件
|
| 43 |
-
|
| 44 |
-
# 启动 frp 并保持运行
|
| 45 |
-
while true; do
|
| 46 |
-
# 启动 frp
|
| 47 |
-
$FRP_PATH -c $FRP_CONFIG
|
| 48 |
-
|
| 49 |
-
# 如果 frp 崩溃或关闭,则重新启动
|
| 50 |
-
echo "frp 连接已断开,正在重新启动..."
|
| 51 |
-
sleep 1 # 等待 1 秒再重启
|
| 52 |
-
done
|
| 53 |
-
'''
|
| 54 |
-
|
| 55 |
-
# 写入脚本文件
|
| 56 |
-
with open(script_path, 'w') as file:
|
| 57 |
-
file.write(script_content)
|
| 58 |
-
|
| 59 |
-
# 确保脚本具有执行权限
|
| 60 |
-
os.chmod(script_path, 0o755)
|
| 61 |
-
print(f"脚本已创建并设置为可执行:{script_path}")
|
| 62 |
-
|
| 63 |
-
def create_systemd_service():
|
| 64 |
-
# systemd 服务文件路径
|
| 65 |
-
service_path = "/etc/systemd/system/frp.service"
|
| 66 |
-
|
| 67 |
-
# 创建并写入 systemd 服务文件
|
| 68 |
-
service_content = '''[Unit]
|
| 69 |
-
Description=frp connection service
|
| 70 |
-
After=network.target
|
| 71 |
-
|
| 72 |
-
[Service]
|
| 73 |
-
ExecStart=/root/frp_service.sh
|
| 74 |
-
Restart=always
|
| 75 |
-
User=root
|
| 76 |
-
WorkingDirectory=/root
|
| 77 |
-
StandardOutput=journal
|
| 78 |
-
StandardError=journal
|
| 79 |
-
SyslogIdentifier=frp
|
| 80 |
-
|
| 81 |
-
[Install]
|
| 82 |
-
WantedBy=multi-user.target
|
| 83 |
-
'''
|
| 84 |
-
|
| 85 |
-
# 写入服务文件
|
| 86 |
-
with open(service_path, 'w') as file:
|
| 87 |
-
file.write(service_content)
|
| 88 |
-
|
| 89 |
-
print(f"systemd 服务文件已创建:{service_path}")
|
| 90 |
-
|
| 91 |
-
def enable_and_start_service():
|
| 92 |
-
# 启用并启动服务
|
| 93 |
-
try:
|
| 94 |
-
|
| 95 |
-
subprocess.run(["sudo", "systemctl", "
|
| 96 |
-
subprocess.run(["sudo", "systemctl", "
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
def create_frpc_toml():
|
| 5 |
+
# frpc.toml 文件路径
|
| 6 |
+
toml_path = "/root/frpc.toml"
|
| 7 |
+
|
| 8 |
+
# frpc.toml 配置内容
|
| 9 |
+
toml_content = """
|
| 10 |
+
[common]
|
| 11 |
+
server_addr = 182.43.67.222
|
| 12 |
+
server_port = 7000
|
| 13 |
+
|
| 14 |
+
[jl]
|
| 15 |
+
type = tcp
|
| 16 |
+
local_ip = 127.0.0.1
|
| 17 |
+
local_port = 8888
|
| 18 |
+
remote_port = 8888
|
| 19 |
+
|
| 20 |
+
[comfyui]
|
| 21 |
+
type = tcp
|
| 22 |
+
local_ip = 127.0.0.1
|
| 23 |
+
local_port = 8188
|
| 24 |
+
remote_port = 8188
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# 写入 frpc.toml 文件
|
| 28 |
+
with open(toml_path, 'w') as file:
|
| 29 |
+
file.write(toml_content)
|
| 30 |
+
|
| 31 |
+
print(f"frpc.toml 配置文件已创建:{toml_path}")
|
| 32 |
+
|
| 33 |
+
def create_frp_service_script():
|
| 34 |
+
# 脚本路径
|
| 35 |
+
script_path = "/root/frp_service.sh"
|
| 36 |
+
|
| 37 |
+
# 创建并写入启动脚本
|
| 38 |
+
script_content = '''#!/bin/bash
|
| 39 |
+
|
| 40 |
+
# 设置 frp 的路径和配置文件路径
|
| 41 |
+
FRP_PATH="/root/frpc"
|
| 42 |
+
FRP_CONFIG="/root/frpc.toml" # 使用新的 TOML 配置文件
|
| 43 |
+
|
| 44 |
+
# 启动 frp 并保持运行
|
| 45 |
+
while true; do
|
| 46 |
+
# 启动 frp
|
| 47 |
+
$FRP_PATH -c $FRP_CONFIG
|
| 48 |
+
|
| 49 |
+
# 如果 frp 崩溃或关闭,则重新启动
|
| 50 |
+
echo "frp 连接已断开,正在重新启动..."
|
| 51 |
+
sleep 1 # 等待 1 秒再重启
|
| 52 |
+
done
|
| 53 |
+
'''
|
| 54 |
+
|
| 55 |
+
# 写入脚本文件
|
| 56 |
+
with open(script_path, 'w') as file:
|
| 57 |
+
file.write(script_content)
|
| 58 |
+
|
| 59 |
+
# 确保脚本具有执行权限
|
| 60 |
+
os.chmod(script_path, 0o755)
|
| 61 |
+
print(f"脚本已创建并设置为可执行:{script_path}")
|
| 62 |
+
|
| 63 |
+
def create_systemd_service():
|
| 64 |
+
# systemd 服务文件路径
|
| 65 |
+
service_path = "/etc/systemd/system/frp.service"
|
| 66 |
+
|
| 67 |
+
# 创建并写入 systemd 服务文件
|
| 68 |
+
service_content = '''[Unit]
|
| 69 |
+
Description=frp connection service
|
| 70 |
+
After=network.target
|
| 71 |
+
|
| 72 |
+
[Service]
|
| 73 |
+
ExecStart=/root/frp_service.sh
|
| 74 |
+
Restart=always
|
| 75 |
+
User=root
|
| 76 |
+
WorkingDirectory=/root
|
| 77 |
+
StandardOutput=journal
|
| 78 |
+
StandardError=journal
|
| 79 |
+
SyslogIdentifier=frp
|
| 80 |
+
|
| 81 |
+
[Install]
|
| 82 |
+
WantedBy=multi-user.target
|
| 83 |
+
'''
|
| 84 |
+
|
| 85 |
+
# 写入服务文件
|
| 86 |
+
with open(service_path, 'w') as file:
|
| 87 |
+
file.write(service_content)
|
| 88 |
+
|
| 89 |
+
print(f"systemd 服务文件已创建:{service_path}")
|
| 90 |
+
|
| 91 |
+
def enable_and_start_service():
|
| 92 |
+
# 启用并启动服务
|
| 93 |
+
try:
|
| 94 |
+
# 使用 sudo 来执行 systemctl 命令
|
| 95 |
+
subprocess.run(["sudo", "systemctl", "daemon-reload"], check=True)
|
| 96 |
+
subprocess.run(["sudo", "systemctl", "enable", "frp.service"], check=True)
|
| 97 |
+
subprocess.run(["sudo", "systemctl", "start", "frp.service"], check=True)
|
| 98 |
+
print("服务已启用并启动。")
|
| 99 |
+
except subprocess.CalledProcessError as e:
|
| 100 |
+
print(f"服务启动失败: {e}")
|
| 101 |
+
|
| 102 |
+
def main():
|
| 103 |
+
create_frpc_toml()
|
| 104 |
+
create_frp_service_script()
|
| 105 |
+
create_systemd_service()
|
| 106 |
+
enable_and_start_service()
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
main()
|
| 110 |
+
|