#!/bin/bash
# 加载公共函数库
source <(curl -sSL https://www.klquan.com/scripts/common.sh)

# 基础配置（每个脚本不一样）
server="frp"
home="/usr/local"
download_patch="https://github.com/fatedier/frp/releases"

# 1.获取软件安装包并选择版本
all_package=($(get_packages "$server" "$download_patch"))
package=$(select_version "$1" "${all_package[@]}")
show_usage

# 2.确认信息
countdown "$home" "$package"

# 3.安装过程
echo "【1】下载源码包"
curl -L -o "$home/$package" -# "$packages_url$package"
check

echo "【2】解压并安装"
cd "$home"
tar xf "$package" && rm -f "$package"
mv frp* frp 2>/dev/null
cd frp && chmod +x frpc
check

echo "【3】配置优化服务参数"
last_ip=$(hostname -I | awk '{print $1}' | awk -F '.' '{print $NF}')
cat > frpc.toml << EOF
serverAddr = "www.klquan.com"
serverPort = 7000
auth.method = "token"
auth.token = "a1234"
loginFailExit=false

[[proxies]]
name = "ssh-$last_ip"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 22$last_ip
EOF
check

echo "【4】配置systemd管理"
cat > /etc/systemd/system/frpc.service << EOF
[Unit]
Description=frpclient
After=network.target

[Service]
Type=simple
ExecStart=$home/frp/frpc -c $home/frp/frpc.toml
Restart=always

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable frpc >/dev/null 2>&1
systemctl start frpc
check
echo "    ····查看执行：systemctl status frpc"

echo "【完成】路径在$home/frp/下"
echo "    ····SSH映射地址: www.klquan.com:22$last_ip"
