frps debian服务端一键脚本
1#!/bin/bash
2
3# ================= 配置变量 =================
4FRP_VERSION="0.58.0"
5INSTALL_PATH="/usr/local/frp"
6BIN_PATH="/usr/local/frp/frps"
7CONFIG_FILE="/usr/local/frp/frps.toml"
8SERVICE_FILE="/etc/systemd/system/frps.service"
9# ===========================================
10
11# 颜色定义
12RED='\033[0;31m'
13GREEN='\033[0;32m'
14YELLOW='\033[0;33m'
15CYAN='\033[0;36m'
16NC='\033[0m'
17
18# --- 1. 检查重复安装 ---
19if [ -f "$BIN_PATH" ]; then
20 echo -e "${YELLOW}检测到 frps 已安装。${NC}"
21 read -p "是否强制覆盖重装?这将重置端口和密码 (y/n): " choice
22 if [[ "$choice" != "y" && "$choice" != "Y" ]]; then
23 echo "已取消安装。"
24 if [ -f "$CONFIG_FILE" ]; then
25 echo -e "\n${CYAN}--- 当前配置信息 ---${NC}"
26 cat $CONFIG_FILE
27 fi
28 exit 0
29 fi
30 systemctl stop frps 2>/dev/null
31 rm -rf $INSTALL_PATH
32fi
33
34# --- 2. 询问下载代理 (新增功能) ---
35echo -e "\n${CYAN}>>> 下载设置${NC}"
36read -p "是否使用 GitHub 代理加速下载? (y/n, 默认 y): " use_proxy
37use_proxy=${use_proxy:-y} # 默认为 y
38
39PROXY_PREFIX=""
40if [[ "$use_proxy" == "y" || "$use_proxy" == "Y" ]]; then
41 read -p "请输入代理前缀 (回车默认使用 https://ghfast.top/ ): " input_proxy
42 if [ -z "$input_proxy" ]; then
43 PROXY_PREFIX="https://ghfast.top/"
44 else
45 PROXY_PREFIX="$input_proxy"
46 fi
47 # 确保前缀以 / 结尾
48 [[ "${PROXY_PREFIX}" != */ ]] && PROXY_PREFIX="${PROXY_PREFIX}/"
49 echo -e "${GREEN}已启用代理: ${PROXY_PREFIX}${NC}"
50else
51 echo -e "${YELLOW}不使用代理,直接连接 GitHub。${NC}"
52fi
53
54# --- 3. 准备环境 ---
55echo -e "${GREEN}正在安装依赖...${NC}"
56apt-get update -qq && apt-get install -y wget tar openssl -qq
57
58# --- 4. 生成随机配置 ---
59RAND_PORT=$(shuf -i 10000-60000 -n 1)
60RAND_TOKEN=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9')
61
62# --- 5. 下载 ---
63mkdir -p $INSTALL_PATH
64GITHUB_URL="https://github.com/fatedier/frp/releases/download/v${FRP_VERSION}/frp_${FRP_VERSION}_linux_amd64.tar.gz"
65FULL_URL="${PROXY_PREFIX}${GITHUB_URL}"
66
67echo -e "${GREEN}正在下载 frp v${FRP_VERSION}...${NC}"
68echo "下载地址: $FULL_URL"
69wget -O /tmp/frp.tar.gz "$FULL_URL" --no-check-certificate -q --show-progress
70
71if [ $? -ne 0 ]; then
72 echo -e "${RED}下载失败!请检查网络或更换代理前缀。${NC}"
73 exit 1
74fi
75
76# --- 6. 解压安装 ---
77tar -zxvf /tmp/frp.tar.gz -C /tmp > /dev/null
78# 移动二进制文件 (注意:解压后的目录名包含版本号)
79mv /tmp/frp_${FRP_VERSION}_linux_amd64/frps $BIN_PATH
80chmod +x $BIN_PATH
81
82# --- 7. 写入配置 (TOML) ---
83cat > $CONFIG_FILE <<EOF
84bindPort = $RAND_PORT
85auth.method = "token"
86auth.token = "$RAND_TOKEN"
87EOF
88
89# --- 8. 配置 Systemd ---
90cat > $SERVICE_FILE <<EOF
91[Unit]
92Description=Frp Server Service
93After=network.target
94
95[Service]
96Type=simple
97User=root
98Restart=on-failure
99RestartSec=5s
100ExecStart=$BIN_PATH -c $CONFIG_FILE
101
102[Install]
103WantedBy=multi-user.target
104EOF
105
106# --- 9. 启动服务 ---
107systemctl daemon-reload
108systemctl enable frps > /dev/null 2>&1
109systemctl start frps
110
111# --- 10. 输出信息 ---
112PUBLIC_IP=$(curl -s ifconfig.me || echo "无法获取公网IP")
113
114echo -e "\n${GREEN}================ 安装成功 =================${NC}"
115echo -e "请在 Windows 客户端脚本中输入以下信息:"
116echo -e "-------------------------------------------"
117echo -e "服务器 IP : ${CYAN}${PUBLIC_IP}${NC}"
118echo -e "服务器端口 : ${CYAN}${RAND_PORT}${NC}"
119echo -e "连接密码 : ${CYAN}${RAND_TOKEN}${NC}"
120echo -e "-------------------------------------------"
121echo -e "请务必在防火墙放行端口: ${YELLOW}${RAND_PORT}${NC}"
122echo -e "${GREEN}===========================================${NC}"
123
124# 清理
125rm -rf /tmp/frp.tar.gz /tmp/frp_${FRP_VERSION}_linux_amd64
Windows客户端一键脚本
保存后缀.ps1 编码 UTF-8 with BOM
执行 powershell -ExecutionPolicy Bypass -File .\frpc.ps1
1# ================= 配置 =================
2$FrpVersion = "0.58.0"
3$InstallPath = "C:\frp"
4# =======================================
5
6# 检查管理员权限
7if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
8 Write-Host "请右键选择 [以管理员身份运行] !" -ForegroundColor Red
9 Start-Sleep -Seconds 3
10 Exit
11}
12
13# --- 1. 检查重复 ---
14if (Test-Path "$InstallPath\frpc.exe") {
15 Write-Host "检测到已安装。" -ForegroundColor Yellow
16 $choice = Read-Host "是否重装并重新配置?(y/n)"
17 if ($choice -ne 'y') { Exit }
18
19 Unregister-ScheduledTask -TaskName "FrpClientAutoStart" -Confirm:$false -ErrorAction SilentlyContinue
20 Stop-Process -Name "frpc" -ErrorAction SilentlyContinue
21 Remove-Item -Path $InstallPath -Recurse -Force
22}
23
24# --- 2. 收集信息 ---
25Clear-Host
26Write-Host "=== FRP 客户端安装向导 ===" -ForegroundColor Cyan
27$ServerIP = Read-Host "请输入服务器 IP"
28$ServerPort = Read-Host "请输入服务器端口 (Server Port)"
29$Token = Read-Host "请输入连接密码 (Token)"
30
31if (-not $ServerIP -or -not $ServerPort -or -not $Token) {
32 Write-Host "错误:所有信息都必须填写。" -ForegroundColor Red; Start-Sleep 3; Exit
33}
34
35# --- 3. 下载 (Windows也可以加代理,但通常本地网络环境复杂,这里直连或用户自行挂梯子) ---
36# 如果需要 Windows 也加代理,可以修改下面的 Url
37$DownloadUrl = "https://github.com/fatedier/frp/releases/download/v${FrpVersion}/frp_${FrpVersion}_windows_amd64.zip"
38$ZipPath = "$env:TEMP\frp.zip"
39
40New-Item -ItemType Directory -Force -Path $InstallPath | Out-Null
41
42Write-Host "正在下载..." -ForegroundColor Green
43try {
44 Invoke-WebRequest -Uri $DownloadUrl -OutFile $ZipPath
45} catch {
46 Write-Host "下载失败,请检查网络。" -ForegroundColor Red; Exit
47}
48
49Write-Host "正在安装..."
50Expand-Archive -Path $ZipPath -DestinationPath "$env:TEMP\frp_temp" -Force
51Copy-Item -Path "$env:TEMP\frp_temp\frp_${FrpVersion}_windows_amd64\*" -Destination $InstallPath -Recurse -Force
52
53# --- 4. 生成配置 ---
54$RemoteRDP = 6000 + (Get-Random -Minimum 1 -Maximum 1000)
55$Config = @"
56serverAddr = "$ServerIP"
57serverPort = $ServerPort
58auth.method = "token"
59auth.token = "$Token"
60
61[[proxies]]
62name = "rdp_$env:COMPUTERNAME"
63type = "tcp"
64localIP = "127.0.0.1"
65localPort = 3389
66remotePort = $RemoteRDP
67"@
68
69Set-Content -Path "$InstallPath\frpc.toml" -Value $Config
70
71# --- 5. 开机自启 ---
72$Action = New-ScheduledTaskAction -Execute "$InstallPath\frpc.exe" -Argument "-c $InstallPath\frpc.toml"
73$Trigger = New-ScheduledTaskTrigger -AtStartup
74$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
75$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit 0
76
77Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings -TaskName "FrpClientAutoStart" -Force | Out-Null
78Start-ScheduledTask -TaskName "FrpClientAutoStart"
79
80# --- 6. 完成 ---
81Write-Host "`n安装成功!" -ForegroundColor Green
82Write-Host "远程桌面连接地址: $ServerIP : $RemoteRDP" -ForegroundColor Cyan
83Write-Host "请确保服务器防火墙已放行上述端口。"
84Read-Host "按回车退出..."