One-click Generation of Automatic frpc.sh Installation

One-click Generation of Automatic frpc.sh Installation
一键生成自动安装frpc.sh脚本
一、把当前frpc执行文件和deploy_frpc.sh放在同一位置

#!/bin/bash

# ============================================================
# frpc 配置生成与服务部署脚本 (Ubuntu 24.04 / frpc 0.68.1)
# 用法: sudo ./deploy_frpc.sh [install|uninstall]
#   install   - 安装/更新 frpc (默认)
#   uninstall - 完全卸载 frpc
# ============================================================

set -e

# -------------------- 颜色定义 --------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# -------------------- 预设连接信息(除用户名外)--------------------
SERVER_ADDR="discover.zhonjin.com"      # frps 服务器地址
SERVER_PORT=9443                        # frps 绑定端口
AUTH_TOKEN="Z8Rpj2uqTvJyqBDe"           # 认证 Token
CLIENT_ID="$(date +%Y%m%d%H%M%S%N | cut -c 1-20)"  # 自动生成唯一 ID

# -------------------- 固定安装目录 --------------------
INSTALL_DIR="/opt/openAI/frp"
FRPC_BIN="${INSTALL_DIR}/frpc"
CONFIG_FILE="${INSTALL_DIR}/frpc.toml"
LOG_FILE="${INSTALL_DIR}/frpc.log"
SERVICE_FILE="/etc/systemd/system/frpc.service"

# 脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LOCAL_FRPC="${SCRIPT_DIR}/frpc"

# -------------------- 辅助函数 --------------------
print_color() { echo -e "${1}${2}${NC}"; }
print_info() { print_color "${BLUE}" "[INFO] $1"; }
print_success() { print_color "${GREEN}" "[SUCCESS] $1"; }
print_warning() { print_color "${YELLOW}" "[WARNING] $1"; }
print_error() { print_color "${RED}" "[ERROR] $1"; }

check_root() {
    if [[ $EUID -ne 0 ]]; then
        print_error "请使用 root 权限运行此脚本 (sudo $0)"
        exit 1
    fi
}

# -------------------- 安装功能 --------------------
create_install_dir() {
    if [[ ! -d "${INSTALL_DIR}" ]]; then
        print_info "创建安装目录: ${INSTALL_DIR}"
        mkdir -p "${INSTALL_DIR}"
    else
        print_info "安装目录已存在: ${INSTALL_DIR}"
    fi
}

setup_frpc() {
    if [[ ! -f "${LOCAL_FRPC}" ]]; then
        print_error "脚本同目录下未找到 frpc 可执行文件: ${LOCAL_FRPC}"
        print_error "请将 frpc 二进制文件与脚本放在同一目录后重试。"
        exit 1
    fi

    print_info "复制 frpc 到 ${INSTALL_DIR}/"
    cp -f "${LOCAL_FRPC}" "${FRPC_BIN}"
    chmod +x "${FRPC_BIN}"
    print_success "frpc 已就绪: ${FRPC_BIN}"
}

generate_proxy_config() {
    clear
    echo "=========================================="
    echo "      代理配置生成 (SSH/HTTP/VNC)"
    echo "=========================================="
    echo ""
    print_info "连接信息已预设:"
    echo "  服务器地址 : ${SERVER_ADDR}"
    echo "  服务器端口 : ${SERVER_PORT}"
    echo "  Token      : ${AUTH_TOKEN}"
    echo ""

    read -p "请输入用户名 (用于代理名称前缀, 示例: PORT_WEB_PRJ): " USER_NAME
    while [[ -z "${USER_NAME}" ]]; do
        print_error "用户名不能为空"
        read -p "请输入用户名 (示例: PORT_WEB_PRJ): " USER_NAME
    done

    read -p "请输入起始远程端口号 (例如 40710): " BASE_PORT
    while ! [[ "${BASE_PORT}" =~ ^[0-9]+$ ]]; do
        print_error "端口号必须为数字"
        read -p "请输入起始远程端口号: " BASE_PORT
    done

    PORT_SSH=$BASE_PORT
    PORT_HTTP=$((BASE_PORT + 1))
    PORT_VNC=$((BASE_PORT + 2))

    PROXY_BLOCK=$(cat <<EOF
# ===== 自动生成的代理 (起始端口 ${BASE_PORT}) =====
# 生成时间: $(date)

[[proxies]]
name = "ssh_tcp_${PORT_SSH}_22"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = ${PORT_SSH}
transport.useEncryption = true
transport.useCompression = true

[[proxies]]
name = "http_tcp_${PORT_HTTP}_80"
type = "tcp"
localIP = "127.0.0.1"
localPort = 80
remotePort = ${PORT_HTTP}
transport.useEncryption = true
transport.useCompression = true

[[proxies]]
name = "vnc_tcp_${PORT_VNC}_5900"
type = "tcp"
localIP = "127.0.0.1"
localPort = 5900
remotePort = ${PORT_VNC}
transport.useEncryption = true
transport.useCompression = true
EOF
)
    print_success "代理配置块生成完毕"
}

generate_full_config() {
    print_info "生成配置文件: ${CONFIG_FILE}"

    cat > "${CONFIG_FILE}" <<EOF
# frpc TOML configuration
# 自动生成于 $(date)

# ---------- 全局配置 ----------
clientID = "${CLIENT_ID}"
user = "${USER_NAME}"

serverAddr = "${SERVER_ADDR}"
serverPort = ${SERVER_PORT}

loginFailExit = false

log.to = "${LOG_FILE}"
log.level = "info"
log.maxDays = 3
log.disablePrintColor = false

auth.method = "token"
auth.token = "${AUTH_TOKEN}"
auth.additionalScopes = ["NewWorkConns"]

transport.poolCount = 50
transport.protocol = "tcp"
transport.tls.enable = true
transport.tls.disableCustomTLSFirstByte = true
transport.heartbeatInterval = 30
transport.heartbeatTimeout = 90

udpPacketSize = 1500

# ---------- 代理列表 ----------
${PROXY_BLOCK}
EOF

    print_success "配置文件已生成"
}

create_systemd_service() {
    print_info "创建 systemd 服务..."

    cat > "${SERVICE_FILE}" <<EOF
[Unit]
Description=frp client service (frpc)
After=network.target syslog.target
Wants=network.target

[Service]
Type=simple
User=root
ExecStart=${FRPC_BIN} -c ${CONFIG_FILE}
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=frpc

NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=${INSTALL_DIR}

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    print_success "systemd 服务文件已创建"
}

start_service() {
    print_info "启动 frpc 服务..."
    systemctl stop frpc.service 2>/dev/null || true
    systemctl start frpc.service
    systemctl enable frpc.service &>/dev/null

    sleep 2
    if systemctl is-active --quiet frpc.service; then
        print_success "frpc 服务运行中"
    else
        print_warning "服务可能未正常启动,请检查日志: journalctl -u frpc -n 20"
    fi
}

show_summary() {
    echo ""
    echo "=========================================="
    echo "            部署完成"
    echo "=========================================="
    echo ""
    print_success "frpc 已配置完成"
    echo ""
    echo "安装目录 : ${INSTALL_DIR}"
    echo "可执行文件 : ${FRPC_BIN}"
    echo "配置文件 : ${CONFIG_FILE}"
    echo "日志文件 : ${LOG_FILE}"
    echo ""
    echo "用户名   : ${USER_NAME}"
    echo "生成的代理:"
    echo "  SSH  : 127.0.0.1:22   -> 远程端口 ${PORT_SSH}"
    echo "  HTTP : 127.0.0.1:80   -> 远程端口 ${PORT_HTTP}"
    echo "  VNC  : 127.0.0.1:5900 -> 远程端口 ${PORT_VNC}"
    echo ""
    echo "管理命令:"
    echo "  systemctl status frpc    # 查看状态"
    echo "  systemctl restart frpc   # 重启服务"
    echo "  journalctl -u frpc -f    # 实时日志"
    echo "  sudo $0 uninstall        # 卸载 frpc"
    echo ""
    systemctl status frpc.service --no-pager --lines=0
}

# -------------------- 卸载功能 --------------------
uninstall_frpc() {
    clear
    echo "=========================================="
    echo "           卸载 frpc"
    echo "=========================================="
    echo ""
    print_warning "即将停止并卸载 frpc,包括:"
    echo "  - 停止 frpc 服务"
    echo "  - 删除 systemd 服务文件"
    echo "  - 删除安装目录 ${INSTALL_DIR}"
    echo ""

    read -p "确认卸载? (输入 yes 继续): " CONFIRM
    if [[ "${CONFIRM}" != "yes" ]]; then
        print_info "已取消卸载"
        exit 0
    fi

    print_info "停止 frpc 服务..."
    systemctl stop frpc.service 2>/dev/null || true
    systemctl disable frpc.service 2>/dev/null || true

    if [[ -f "${SERVICE_FILE}" ]]; then
        print_info "删除服务文件: ${SERVICE_FILE}"
        rm -f "${SERVICE_FILE}"
        systemctl daemon-reload
    fi

    if [[ -d "${INSTALL_DIR}" ]]; then
        print_info "删除安装目录: ${INSTALL_DIR}"
        rm -rf "${INSTALL_DIR}"
    fi

    print_success "frpc 已完全卸载"
    echo ""
    echo "如需重新安装,请运行: sudo $0 install"
}

# -------------------- 帮助信息 --------------------
show_help() {
    echo "用法: sudo $0 [install|uninstall|help]"
    echo ""
    echo "命令:"
    echo "  install     安装/更新 frpc (默认)"
    echo "  uninstall   完全卸载 frpc"
    echo "  help        显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  sudo $0              # 执行安装"
    echo "  sudo $0 install      # 同上"
    echo "  sudo $0 uninstall    # 卸载"
}

# -------------------- 主流程 --------------------
main() {
    check_root

    local ACTION="${1:-install}"

    case "${ACTION}" in
        install)
            create_install_dir
            setup_frpc
            generate_proxy_config
            generate_full_config
            create_systemd_service
            start_service
            show_summary
            ;;
        uninstall)
            uninstall_frpc
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            print_error "未知命令: ${ACTION}"
            show_help
            exit 1
            ;;
    esac
}

main "$@"

 

二、

已为您增加卸载功能。脚本现在支持两种运行模式:
chmod a+x deploy_frpc.sh
• sudo ./deploy_frpc.sh install —— 安装/更新部署(默认)
• sudo ./deploy_frpc.sh uninstall —— 完全卸载 frpc 及相关配置

一、注意端口号,不可以重复
1、
chmod a+x deploy_frpc.sh
2、
sudo ./deploy_frpc.sh uninstall
sudo ./deploy_frpc.sh install

3、frpc 和deploy_frpc.sh放在同一目录
输入项目名:40760_git_me
端口号:40760

您可能还喜欢...

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注