Update frp.py
Browse files
frp.py
CHANGED
|
@@ -1,11 +1,34 @@
|
|
| 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
|
|
@@ -30,11 +53,9 @@ remote_port = 8188
|
|
| 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 的路径和配置文件路径
|
|
@@ -60,11 +81,9 @@ done
|
|
| 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
|
|
@@ -88,23 +107,23 @@ WantedBy=multi-user.target
|
|
| 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 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
main()
|
| 110 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import subprocess
|
| 3 |
|
| 4 |
+
# 1. 安装 frpc
|
| 5 |
+
def install_frpc():
|
| 6 |
+
print("正在安装 frpc...")
|
| 7 |
+
|
| 8 |
+
# 下载 frpc 二进制文件(更新为 Hugging Face 镜像)
|
| 9 |
+
try:
|
| 10 |
+
frpc_url = "https://huggingface.co/HOPStudio/modelscope/resolve/main/frp_0.61.0_linux_amd64.tar.gz?download=true"
|
| 11 |
+
download_dir = "/root/frp"
|
| 12 |
+
os.makedirs(download_dir, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
# 使用 aria2 下载并解压 frpc
|
| 15 |
+
subprocess.run(["aria2c", "--dir", download_dir, frpc_url], check=True)
|
| 16 |
+
|
| 17 |
+
# 解压下载的 tar.gz 文件
|
| 18 |
+
subprocess.run(["tar", "-xvzf", os.path.join(download_dir, "frp_0.61.0_linux_amd64.tar.gz"), "-C", download_dir], check=True)
|
| 19 |
+
subprocess.run(["rm", os.path.join(download_dir, "frp_0.61.0_linux_amd64.tar.gz")], check=True)
|
| 20 |
+
|
| 21 |
+
# 移动 frpc 到合适的位置
|
| 22 |
+
subprocess.run(["mv", os.path.join(download_dir, "frp_0.61.0_linux_amd64", "frpc"), "/root/"], check=True)
|
| 23 |
+
|
| 24 |
+
print("frpc 安装完成。")
|
| 25 |
+
except subprocess.CalledProcessError as e:
|
| 26 |
+
print(f"安装 frpc 失败: {e}")
|
| 27 |
+
exit(1)
|
| 28 |
+
|
| 29 |
+
# 2. 创建 frpc.toml 配置文件
|
| 30 |
def create_frpc_toml():
|
|
|
|
| 31 |
toml_path = "/root/frpc.toml"
|
|
|
|
|
|
|
| 32 |
toml_content = """
|
| 33 |
[common]
|
| 34 |
server_addr = 182.43.67.222
|
|
|
|
| 53 |
|
| 54 |
print(f"frpc.toml 配置文件已创建:{toml_path}")
|
| 55 |
|
| 56 |
+
# 3. 创建启动脚本
|
| 57 |
def create_frp_service_script():
|
|
|
|
| 58 |
script_path = "/root/frp_service.sh"
|
|
|
|
|
|
|
| 59 |
script_content = '''#!/bin/bash
|
| 60 |
|
| 61 |
# 设置 frp 的路径和配置文件路径
|
|
|
|
| 81 |
os.chmod(script_path, 0o755)
|
| 82 |
print(f"脚本已创建并设置为可执行:{script_path}")
|
| 83 |
|
| 84 |
+
# 4. 创建 systemd 服务文件
|
| 85 |
def create_systemd_service():
|
|
|
|
| 86 |
service_path = "/etc/systemd/system/frp.service"
|
|
|
|
|
|
|
| 87 |
service_content = '''[Unit]
|
| 88 |
Description=frp connection service
|
| 89 |
After=network.target
|
|
|
|
| 107 |
|
| 108 |
print(f"systemd 服务文件已创建:{service_path}")
|
| 109 |
|
| 110 |
+
# 5. 启动并启用服务
|
| 111 |
def enable_and_start_service():
|
|
|
|
| 112 |
try:
|
|
|
|
| 113 |
subprocess.run(["sudo", "systemctl", "daemon-reload"], check=True)
|
| 114 |
subprocess.run(["sudo", "systemctl", "enable", "frp.service"], check=True)
|
| 115 |
subprocess.run(["sudo", "systemctl", "start", "frp.service"], check=True)
|
| 116 |
+
print("frp 服务已启用并启动。")
|
| 117 |
except subprocess.CalledProcessError as e:
|
| 118 |
print(f"服务启动失败: {e}")
|
| 119 |
|
| 120 |
+
# 主函数
|
| 121 |
def main():
|
| 122 |
+
install_frpc() # 安装 frpc
|
| 123 |
+
create_frpc_toml() # 创建 frpc.toml 配置文件
|
| 124 |
+
create_frp_service_script() # 创建启动脚本
|
| 125 |
+
create_systemd_service() # 创建 systemd 服务
|
| 126 |
+
enable_and_start_service() # 启动并启用服务
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
+
main()
|
|
|