Compare commits
No commits in common. "master" and "main" have entirely different histories.
530
port.sh
530
port.sh
@ -1,25 +1,8 @@
|
||||
#!/bin/sh
|
||||
# 安装脚本 - POSIX sh 兼容
|
||||
# Alpine 下自动安装 bash 和 openrc
|
||||
if [ -f /etc/alpine-release ]; then
|
||||
if ! command -v bash > /dev/null 2>&1; then
|
||||
echo "Alpine 系统检测到,正在安装 bash..."
|
||||
apk add --no-cache bash
|
||||
fi
|
||||
if ! command -v rc-service > /dev/null 2>&1; then
|
||||
echo "正在安装 OpenRC..."
|
||||
apk add --no-cache openrc
|
||||
mkdir -p /run/openrc
|
||||
touch /run/openrc/softlevel
|
||||
fi
|
||||
fi
|
||||
|
||||
cat > /root/port-forward.sh << 'EOF'
|
||||
#!/bin/bash
|
||||
|
||||
# 端口转发管理脚本
|
||||
# 使用 socat 实现端口转发(含自动重启监控)
|
||||
# 支持 systemd (Debian/Ubuntu/CentOS) 和 OpenRC (Alpine)
|
||||
|
||||
SERVICE_PREFIX="port-forward"
|
||||
WATCHDOG_SERVICE="port-forward-watchdog"
|
||||
@ -31,67 +14,10 @@ YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
# ============================================
|
||||
# 系统检测
|
||||
# ============================================
|
||||
|
||||
detect_system() {
|
||||
if [ -f /etc/alpine-release ]; then
|
||||
OS="alpine"
|
||||
elif [ -f /etc/debian_version ]; then
|
||||
OS="debian"
|
||||
else
|
||||
OS="unknown"
|
||||
fi
|
||||
|
||||
# Alpine 优先走 OpenRC,不依赖 systemctl 检测
|
||||
if [ "$OS" = "alpine" ]; then
|
||||
if command -v rc-service > /dev/null 2>&1; then
|
||||
INIT_SYS="openrc"
|
||||
else
|
||||
echo -e "${YELLOW}OpenRC 未安装,正在安装...${NC}"
|
||||
apk add --no-cache openrc
|
||||
mkdir -p /run/openrc
|
||||
touch /run/openrc/softlevel
|
||||
if command -v rc-service > /dev/null 2>&1; then
|
||||
INIT_SYS="openrc"
|
||||
else
|
||||
echo -e "${RED}错误: OpenRC 安装失败${NC}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
elif command -v systemctl > /dev/null 2>&1 && [ -d /etc/systemd/system ]; then
|
||||
INIT_SYS="systemd"
|
||||
elif command -v rc-service > /dev/null 2>&1; then
|
||||
INIT_SYS="openrc"
|
||||
else
|
||||
echo -e "${RED}错误: 不支持的初始化系统(需要 systemd 或 OpenRC)${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo -e "${BLUE}系统: ${OS} | 初始化系统: ${INIT_SYS}${NC}"
|
||||
}
|
||||
|
||||
# ============================================
|
||||
# 包管理
|
||||
# ============================================
|
||||
|
||||
check_deps() {
|
||||
# Alpine 下确保 bash 已安装
|
||||
if [ "$OS" = "alpine" ]; then
|
||||
if ! command -v bash > /dev/null 2>&1; then
|
||||
echo -e "${YELLOW}bash 未安装,正在安装...${NC}"
|
||||
apk add --no-cache bash
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! command -v socat > /dev/null 2>&1; then
|
||||
check_socat() {
|
||||
if ! command -v socat &> /dev/null; then
|
||||
echo -e "${YELLOW}socat 未安装,正在安装...${NC}"
|
||||
if [ "$OS" = "alpine" ]; then
|
||||
apk add --no-cache socat
|
||||
else
|
||||
apt update && apt install socat -y
|
||||
fi
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}socat 安装成功!${NC}"
|
||||
else
|
||||
@ -101,18 +27,17 @@ check_deps() {
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================
|
||||
# 服务管理抽象层
|
||||
# ============================================
|
||||
add_single_forward() {
|
||||
echo -e "${GREEN}=== 添加单个端口转发 ===${NC}"
|
||||
read -p "请输入本机监听端口: " local_port
|
||||
read -p "请输入目标IP地址: " target_ip
|
||||
read -p "请输入目标端口: " target_port
|
||||
|
||||
svc_create_forward() {
|
||||
local svc_name=$1
|
||||
local local_port=$2
|
||||
local target_ip=$3
|
||||
local target_port=$4
|
||||
if [[ -z "$local_port" ]] || [[ -z "$target_ip" ]] || [[ -z "$target_port" ]]; then
|
||||
echo -e "${RED}错误: 输入不能为空${NC}"; return 1
|
||||
fi
|
||||
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
cat > /etc/systemd/system/${svc_name}.service << SERVICEEOF
|
||||
cat > /etc/systemd/system/${SERVICE_PREFIX}-${local_port}.service << SERVICEEOF
|
||||
[Unit]
|
||||
Description=Port Forward ${local_port} to ${target_ip}:${target_port}
|
||||
After=network.target
|
||||
@ -127,145 +52,12 @@ StartLimitInterval=0
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
SERVICEEOF
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
cat > /etc/init.d/${svc_name} << SERVICEEOF
|
||||
#!/sbin/openrc-run
|
||||
|
||||
name="${svc_name}"
|
||||
description="Port Forward ${local_port} to ${target_ip}:${target_port}"
|
||||
command="/usr/bin/socat"
|
||||
command_args="TCP4-LISTEN:${local_port},reuseaddr,fork TCP4:${target_ip}:${target_port}"
|
||||
command_background=true
|
||||
pidfile="/run/${svc_name}.pid"
|
||||
|
||||
depend() {
|
||||
need net
|
||||
}
|
||||
SERVICEEOF
|
||||
chmod +x /etc/init.d/${svc_name}
|
||||
fi
|
||||
}
|
||||
|
||||
svc_enable() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl enable ${svc_name} > /dev/null 2>&1
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
rc-update add ${svc_name} default > /dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
svc_disable() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl disable ${svc_name} > /dev/null 2>&1
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
rc-update del ${svc_name} default > /dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
svc_start() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl start ${svc_name}
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
rc-service ${svc_name} start > /dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
svc_stop() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl stop ${svc_name} > /dev/null 2>&1
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
rc-service ${svc_name} stop > /dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
svc_restart() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl restart ${svc_name}
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
rc-service ${svc_name} restart > /dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
svc_is_active() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl is-active ${svc_name} 2>/dev/null
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
if rc-service ${svc_name} status 2>/dev/null | grep -q "started"; then
|
||||
echo "active"
|
||||
else
|
||||
echo "inactive"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
svc_exists() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
[ -f "/etc/systemd/system/${svc_name}.service" ]
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
[ -f "/etc/init.d/${svc_name}" ]
|
||||
fi
|
||||
}
|
||||
|
||||
svc_delete() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
rm -f /etc/systemd/system/${svc_name}.service
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
rm -f /etc/init.d/${svc_name}
|
||||
fi
|
||||
}
|
||||
|
||||
svc_daemon_reload() {
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl daemon-reload
|
||||
fi
|
||||
}
|
||||
systemctl enable ${SERVICE_PREFIX}-${local_port}
|
||||
systemctl start ${SERVICE_PREFIX}-${local_port}
|
||||
|
||||
svc_list_forward_names() {
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl list-units --all --type=service --no-pager | grep "${SERVICE_PREFIX}" | grep -v watchdog | grep -v daily | awk '{print $1}' | sed 's/.service$//'
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
ls /etc/init.d/${SERVICE_PREFIX}-* 2>/dev/null | grep -v watchdog | xargs -I{} basename {} 2>/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
svc_get_target() {
|
||||
local svc_name=$1
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
grep ExecStart /etc/systemd/system/${svc_name}.service 2>/dev/null | sed -n 's/.*TCP4:\([^ ]*\).*/\1/p'
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
grep command_args /etc/init.d/${svc_name} 2>/dev/null | sed -n 's/.*TCP4:\([^ "]*\).*/\1/p'
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================
|
||||
# 端口转发管理
|
||||
# ============================================
|
||||
|
||||
add_single_forward() {
|
||||
echo -e "${GREEN}=== 添加单个端口转发 ===${NC}"
|
||||
read -p "请输入本机监听端口: " local_port
|
||||
read -p "请输入目标IP地址: " target_ip
|
||||
read -p "请输入目标端口: " target_port
|
||||
|
||||
if [[ -z "$local_port" ]] || [[ -z "$target_ip" ]] || [[ -z "$target_port" ]]; then
|
||||
echo -e "${RED}错误: 输入不能为空${NC}"; return 1
|
||||
fi
|
||||
|
||||
local svc_name="${SERVICE_PREFIX}-${local_port}"
|
||||
svc_create_forward "$svc_name" "$local_port" "$target_ip" "$target_port"
|
||||
svc_daemon_reload
|
||||
svc_enable "$svc_name"
|
||||
svc_start "$svc_name"
|
||||
|
||||
if [ "$(svc_is_active $svc_name)" = "active" ]; then
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✓ 端口转发添加成功!${NC}"
|
||||
echo -e "本机端口: ${local_port} -> 目标: ${target_ip}:${target_port}"
|
||||
else
|
||||
@ -289,14 +81,28 @@ add_batch_forward() {
|
||||
if [[ "$confirm" != "y" ]]; then echo "操作已取消"; return; fi
|
||||
|
||||
for port in $(seq $start_port $end_port); do
|
||||
local svc_name="${SERVICE_PREFIX}-${port}"
|
||||
svc_create_forward "$svc_name" "$port" "$target_ip" "$target_port"
|
||||
svc_enable "$svc_name"
|
||||
svc_start "$svc_name"
|
||||
cat > /etc/systemd/system/${SERVICE_PREFIX}-${port}.service << SERVICEEOF
|
||||
[Unit]
|
||||
Description=Port Forward ${port} to ${target_ip}:${target_port}
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=/usr/bin/socat TCP4-LISTEN:${port},reuseaddr,fork TCP4:${target_ip}:${target_port}
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
StartLimitInterval=0
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
SERVICEEOF
|
||||
|
||||
systemctl enable ${SERVICE_PREFIX}-${port} > /dev/null 2>&1
|
||||
systemctl start ${SERVICE_PREFIX}-${port}
|
||||
echo -e "${GREEN}✓${NC} 端口 ${port} 转发已创建"
|
||||
done
|
||||
|
||||
svc_daemon_reload
|
||||
systemctl daemon-reload
|
||||
echo -e "${GREEN}批量端口转发添加完成!${NC}"
|
||||
}
|
||||
|
||||
@ -304,8 +110,7 @@ list_forwards() {
|
||||
echo -e "${GREEN}=== 当前端口转发列表 ===${NC}"
|
||||
echo ""
|
||||
|
||||
local services
|
||||
services=$(svc_list_forward_names)
|
||||
services=$(systemctl list-units --all --type=service --no-pager | grep ${SERVICE_PREFIX} | grep -v watchdog | awk '{print $1}')
|
||||
|
||||
if [[ -z "$services" ]]; then
|
||||
echo -e "${YELLOW}暂无端口转发${NC}"
|
||||
@ -313,62 +118,53 @@ list_forwards() {
|
||||
printf "%-15s %-12s %-30s\n" "本机端口" "状态" "转发目标"
|
||||
echo "--------------------------------------------------------"
|
||||
|
||||
for svc_name in $services; do
|
||||
local port=$(echo $svc_name | sed "s/${SERVICE_PREFIX}-//g")
|
||||
local status=$(svc_is_active $svc_name)
|
||||
for service in $services; do
|
||||
port=$(echo $service | sed "s/${SERVICE_PREFIX}-//g" | sed 's/.service//g')
|
||||
status=$(systemctl is-active $service)
|
||||
|
||||
if [[ "$status" == "active" ]]; then
|
||||
local status_color="${GREEN}运行中${NC}"
|
||||
status_color="${GREEN}运行中${NC}"
|
||||
else
|
||||
local status_color="${RED}已停止${NC}"
|
||||
status_color="${RED}已停止${NC}"
|
||||
fi
|
||||
|
||||
local target=$(svc_get_target $svc_name)
|
||||
target=$(grep ExecStart /etc/systemd/system/$service 2>/dev/null | grep -oP 'TCP4:\K[^ ]+')
|
||||
printf "%-15s %-20b %-30s\n" "$port" "$status_color" "$target"
|
||||
done
|
||||
fi
|
||||
|
||||
echo ""
|
||||
local watchdog_status=$(svc_is_active ${WATCHDOG_SERVICE})
|
||||
watchdog_status=$(systemctl is-active ${WATCHDOG_SERVICE} 2>/dev/null)
|
||||
if [[ "$watchdog_status" == "active" ]]; then
|
||||
echo -e "🛡️ Watchdog: ${GREEN}运行中(自动重启已开启)${NC}"
|
||||
else
|
||||
echo -e "🛡️ Watchdog: ${RED}未运行${NC}(菜单 8 可启动)"
|
||||
fi
|
||||
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
local timer_status=$(systemctl is-active port-forward-daily.timer 2>/dev/null)
|
||||
timer_status=$(systemctl is-active port-forward-daily.timer 2>/dev/null)
|
||||
if [[ "$timer_status" == "active" ]]; then
|
||||
local next=$(systemctl list-timers port-forward-daily.timer --no-pager 2>/dev/null | awk 'NR==2{print $1, $2}')
|
||||
next=$(systemctl list-timers port-forward-daily.timer --no-pager 2>/dev/null | awk 'NR==2{print $1, $2}')
|
||||
echo -e "⏰ 定时重启: ${GREEN}已开启${NC},下次: ${YELLOW}${next}${NC}"
|
||||
else
|
||||
echo -e "⏰ 定时重启: ${RED}未开启${NC}(菜单 9 可设置)"
|
||||
fi
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
if crontab -l 2>/dev/null | grep -q "port-forward-daily-restart"; then
|
||||
local cron_time=$(crontab -l 2>/dev/null | grep "port-forward-daily-restart" | awk '{printf "%02d:%02d", $2, $1}')
|
||||
echo -e "⏰ 定时重启: ${GREEN}已开启${NC},时间: ${YELLOW}每天 ${cron_time}${NC}"
|
||||
else
|
||||
echo -e "⏰ 定时重启: ${RED}未开启${NC}(菜单 9 可设置)"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
restart_single_forward() {
|
||||
echo -e "${GREEN}=== 重启单个端口转发 ===${NC}"
|
||||
read -p "请输入要重启的本机端口: " port
|
||||
|
||||
local svc_name="${SERVICE_PREFIX}-${port}"
|
||||
if ! svc_exists "$svc_name"; then
|
||||
service_name="${SERVICE_PREFIX}-${port}.service"
|
||||
if [[ ! -f "/etc/systemd/system/$service_name" ]]; then
|
||||
echo -e "${RED}错误: 端口 ${port} 的转发不存在${NC}"; return 1
|
||||
fi
|
||||
|
||||
svc_restart "$svc_name"
|
||||
systemctl restart $service_name
|
||||
sleep 1
|
||||
if [ "$(svc_is_active $svc_name)" = "active" ]; then
|
||||
if [[ "$(systemctl is-active $service_name)" == "active" ]]; then
|
||||
echo -e "${GREEN}✓ 端口 ${port} 重启成功!${NC}"
|
||||
else
|
||||
echo -e "${RED}✗ 重启失败${NC}"
|
||||
echo -e "${RED}✗ 重启失败,查看日志: journalctl -u $service_name${NC}"
|
||||
fi
|
||||
}
|
||||
|
||||
@ -377,19 +173,18 @@ restart_all_forwards() {
|
||||
read -p "确认重启所有转发? (y/n): " confirm
|
||||
if [[ "$confirm" != "y" ]]; then echo "操作已取消"; return; fi
|
||||
|
||||
local services
|
||||
services=$(svc_list_forward_names)
|
||||
services=$(systemctl list-units --all --type=service --no-pager | grep ${SERVICE_PREFIX} | grep -v watchdog | awk '{print $1}')
|
||||
|
||||
if [[ -z "$services" ]]; then
|
||||
echo -e "${YELLOW}暂无端口转发${NC}"; return
|
||||
fi
|
||||
|
||||
local failed=0 success=0
|
||||
for svc_name in $services; do
|
||||
local port=$(echo $svc_name | sed "s/${SERVICE_PREFIX}-//g")
|
||||
svc_restart "$svc_name"
|
||||
failed=0; success=0
|
||||
for service in $services; do
|
||||
port=$(echo $service | sed "s/${SERVICE_PREFIX}-//g" | sed 's/.service//g')
|
||||
systemctl restart $service
|
||||
sleep 0.5
|
||||
if [ "$(svc_is_active $svc_name)" = "active" ]; then
|
||||
if [[ "$(systemctl is-active $service)" == "active" ]]; then
|
||||
echo -e "${GREEN}✓${NC} 端口 ${port} 重启成功"; ((success++))
|
||||
else
|
||||
echo -e "${RED}✗${NC} 端口 ${port} 重启失败"; ((failed++))
|
||||
@ -411,63 +206,26 @@ install_watchdog() {
|
||||
|
||||
cat > ${WATCHDOG_SCRIPT} << WATCHEOF
|
||||
#!/bin/bash
|
||||
# Port Forward Watchdog - 支持 systemd 和 OpenRC
|
||||
# Port Forward Watchdog
|
||||
LOG_FILE="/var/log/port-forward-watchdog.log"
|
||||
SERVICE_PREFIX="port-forward"
|
||||
|
||||
if command -v systemctl &> /dev/null && systemctl --version &> /dev/null 2>&1; then
|
||||
W_INIT="systemd"
|
||||
elif command -v rc-service &> /dev/null; then
|
||||
W_INIT="openrc"
|
||||
else
|
||||
echo "Unsupported init system" >> \$LOG_FILE
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log() {
|
||||
echo "\$(date '+%Y-%m-%d %H:%M:%S') \$1" | tee -a \$LOG_FILE
|
||||
}
|
||||
|
||||
w_list_services() {
|
||||
if [ "\$W_INIT" = "systemd" ]; then
|
||||
systemctl list-units --all --type=service --no-pager | grep \${SERVICE_PREFIX} | grep -v watchdog | grep -v daily | awk '{print \$1}' | sed 's/.service\$//'
|
||||
elif [ "\$W_INIT" = "openrc" ]; then
|
||||
ls /etc/init.d/\${SERVICE_PREFIX}-* 2>/dev/null | grep -v watchdog | xargs -I{} basename {} 2>/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
w_is_active() {
|
||||
if [ "\$W_INIT" = "systemd" ]; then
|
||||
systemctl is-active \$1 2>/dev/null
|
||||
elif [ "\$W_INIT" = "openrc" ]; then
|
||||
if rc-service \$1 status 2>/dev/null | grep -q "started"; then
|
||||
echo "active"
|
||||
else
|
||||
echo "inactive"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
w_restart() {
|
||||
if [ "\$W_INIT" = "systemd" ]; then
|
||||
systemctl restart \$1
|
||||
elif [ "\$W_INIT" = "openrc" ]; then
|
||||
rc-service \$1 restart > /dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
log "=== Watchdog 启动,检查间隔: ${interval}秒,初始化系统: \$W_INIT ==="
|
||||
log "=== Watchdog 启动,检查间隔: ${interval}秒 ==="
|
||||
|
||||
while true; do
|
||||
services=\$(w_list_services)
|
||||
services=\$(systemctl list-units --all --type=service --no-pager | grep \${SERVICE_PREFIX} | grep -v watchdog | awk '{print \$1}')
|
||||
|
||||
for service in \$services; do
|
||||
status=\$(w_is_active \$service)
|
||||
status=\$(systemctl is-active \$service)
|
||||
if [[ "\$status" != "active" ]]; then
|
||||
log "⚠ 检测到 \$service 异常(状态: \$status),正在重启..."
|
||||
w_restart \$service
|
||||
systemctl restart \$service
|
||||
sleep 2
|
||||
new_status=\$(w_is_active \$service)
|
||||
new_status=\$(systemctl is-active \$service)
|
||||
if [[ "\$new_status" == "active" ]]; then
|
||||
log "✓ \$service 重启成功"
|
||||
else
|
||||
@ -482,7 +240,6 @@ WATCHEOF
|
||||
|
||||
chmod +x ${WATCHDOG_SCRIPT}
|
||||
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
cat > /etc/systemd/system/${WATCHDOG_SERVICE}.service << SERVICEEOF
|
||||
[Unit]
|
||||
Description=Port Forward Watchdog - Auto Restart Monitor
|
||||
@ -498,29 +255,12 @@ StartLimitInterval=0
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
SERVICEEOF
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
cat > /etc/init.d/${WATCHDOG_SERVICE} << SERVICEEOF
|
||||
#!/sbin/openrc-run
|
||||
|
||||
name="${WATCHDOG_SERVICE}"
|
||||
description="Port Forward Watchdog - Auto Restart Monitor"
|
||||
command="/bin/bash"
|
||||
command_args="${WATCHDOG_SCRIPT}"
|
||||
command_background=true
|
||||
pidfile="/run/${WATCHDOG_SERVICE}.pid"
|
||||
systemctl daemon-reload
|
||||
systemctl enable ${WATCHDOG_SERVICE}
|
||||
systemctl start ${WATCHDOG_SERVICE}
|
||||
|
||||
depend() {
|
||||
need net
|
||||
}
|
||||
SERVICEEOF
|
||||
chmod +x /etc/init.d/${WATCHDOG_SERVICE}
|
||||
fi
|
||||
|
||||
svc_daemon_reload
|
||||
svc_enable "${WATCHDOG_SERVICE}"
|
||||
svc_start "${WATCHDOG_SERVICE}"
|
||||
|
||||
if [ "$(svc_is_active ${WATCHDOG_SERVICE})" = "active" ]; then
|
||||
if [[ "$(systemctl is-active ${WATCHDOG_SERVICE})" == "active" ]]; then
|
||||
echo -e "${GREEN}✓ Watchdog 安装成功!${NC}"
|
||||
echo -e " 检查间隔: ${interval} 秒"
|
||||
echo -e " 日志位置: /var/log/port-forward-watchdog.log"
|
||||
@ -534,11 +274,11 @@ uninstall_watchdog() {
|
||||
read -p "确认卸载 Watchdog? (y/n): " confirm
|
||||
if [[ "$confirm" != "y" ]]; then echo "操作已取消"; return; fi
|
||||
|
||||
svc_stop "${WATCHDOG_SERVICE}"
|
||||
svc_disable "${WATCHDOG_SERVICE}"
|
||||
svc_delete "${WATCHDOG_SERVICE}"
|
||||
systemctl stop ${WATCHDOG_SERVICE} 2>/dev/null
|
||||
systemctl disable ${WATCHDOG_SERVICE} 2>/dev/null
|
||||
rm -f /etc/systemd/system/${WATCHDOG_SERVICE}.service
|
||||
rm -f ${WATCHDOG_SCRIPT}
|
||||
svc_daemon_reload
|
||||
systemctl daemon-reload
|
||||
echo -e "${GREEN}✓ Watchdog 已卸载${NC}"
|
||||
}
|
||||
|
||||
@ -553,6 +293,7 @@ install_daily_restart() {
|
||||
read -p "请输入分钟(0-59,默认0分): " minute
|
||||
minute=${minute:-0}
|
||||
|
||||
# 验证输入
|
||||
if ! [[ "$hour" =~ ^[0-9]+$ ]] || [[ $hour -gt 23 ]]; then
|
||||
echo -e "${RED}错误: 小时必须为 0-23${NC}"; return 1
|
||||
fi
|
||||
@ -560,58 +301,24 @@ install_daily_restart() {
|
||||
echo -e "${RED}错误: 分钟必须为 0-59${NC}"; return 1
|
||||
fi
|
||||
|
||||
# 创建重启脚本(内嵌初始化系统检测)
|
||||
# 创建重启脚本
|
||||
cat > /root/port-forward-daily-restart.sh << DAILYEOF
|
||||
#!/bin/bash
|
||||
# 每日定时重启所有端口转发服务 - 支持 systemd 和 OpenRC
|
||||
# 每日定时重启所有端口转发服务
|
||||
LOG_FILE="/var/log/port-forward-daily.log"
|
||||
|
||||
if command -v systemctl &> /dev/null && systemctl --version &> /dev/null 2>&1; then
|
||||
D_INIT="systemd"
|
||||
elif command -v rc-service &> /dev/null; then
|
||||
D_INIT="openrc"
|
||||
fi
|
||||
|
||||
log() {
|
||||
echo "\$(date '+%Y-%m-%d %H:%M:%S') \$1" | tee -a \$LOG_FILE
|
||||
}
|
||||
|
||||
d_list_services() {
|
||||
if [ "\$D_INIT" = "systemd" ]; then
|
||||
systemctl list-units --all --type=service --no-pager | grep port-forward | grep -v watchdog | grep -v daily | awk '{print \$1}' | sed 's/.service\$//'
|
||||
elif [ "\$D_INIT" = "openrc" ]; then
|
||||
ls /etc/init.d/port-forward-* 2>/dev/null | grep -v watchdog | xargs -I{} basename {} 2>/dev/null
|
||||
fi
|
||||
}
|
||||
|
||||
d_is_active() {
|
||||
if [ "\$D_INIT" = "systemd" ]; then
|
||||
systemctl is-active \$1 2>/dev/null
|
||||
elif [ "\$D_INIT" = "openrc" ]; then
|
||||
if rc-service \$1 status 2>/dev/null | grep -q "started"; then
|
||||
echo "active"
|
||||
else
|
||||
echo "inactive"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
d_restart() {
|
||||
if [ "\$D_INIT" = "systemd" ]; then
|
||||
systemctl restart \$1
|
||||
elif [ "\$D_INIT" = "openrc" ]; then
|
||||
rc-service \$1 restart > /dev/null 2>&1
|
||||
fi
|
||||
}
|
||||
|
||||
log "=== 开始每日定时重启(\$D_INIT)==="
|
||||
services=\$(d_list_services)
|
||||
log "=== 开始每日定时重启 ==="
|
||||
services=\$(systemctl list-units --all --type=service --no-pager | grep port-forward | grep -v watchdog | grep -v daily | awk '{print \$1}')
|
||||
|
||||
success=0; failed=0
|
||||
for service in \$services; do
|
||||
d_restart \$service
|
||||
systemctl restart \$service
|
||||
sleep 1
|
||||
if [[ "\$(d_is_active \$service)" == "active" ]]; then
|
||||
if [[ "\$(systemctl is-active \$service)" == "active" ]]; then
|
||||
log "✓ \$service 重启成功"
|
||||
((success++))
|
||||
else
|
||||
@ -625,7 +332,7 @@ DAILYEOF
|
||||
|
||||
chmod +x /root/port-forward-daily-restart.sh
|
||||
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
# 创建 systemd service(执行单元)
|
||||
cat > /etc/systemd/system/port-forward-daily.service << SERVICEEOF
|
||||
[Unit]
|
||||
Description=Port Forward Daily Restart
|
||||
@ -636,6 +343,7 @@ Type=oneshot
|
||||
ExecStart=/bin/bash /root/port-forward-daily-restart.sh
|
||||
SERVICEEOF
|
||||
|
||||
# 创建 systemd timer(定时器)
|
||||
cat > /etc/systemd/system/port-forward-daily.timer << TIMEREOF
|
||||
[Unit]
|
||||
Description=Port Forward Daily Restart Timer
|
||||
@ -663,40 +371,18 @@ TIMEREOF
|
||||
else
|
||||
echo -e "${RED}✗ 定时器启动失败${NC}"
|
||||
fi
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
# Alpine 下使用 cron 实现定时任务
|
||||
if ! command -v crond &> /dev/null && ! pgrep crond > /dev/null 2>&1; then
|
||||
echo -e "${YELLOW}正在安装 cron 服务...${NC}"
|
||||
apk add --no-cache dcron
|
||||
rc-service dcron start > /dev/null 2>&1
|
||||
rc-update add dcron default > /dev/null 2>&1
|
||||
fi
|
||||
|
||||
# 移除旧条目并添加新条目
|
||||
crontab -l 2>/dev/null | grep -v "port-forward-daily-restart" | crontab - 2>/dev/null
|
||||
(crontab -l 2>/dev/null; echo "${minute} ${hour} * * * /bin/bash /root/port-forward-daily-restart.sh") | crontab -
|
||||
|
||||
echo -e "${GREEN}✓ 每日定时重启设置成功!${NC}"
|
||||
printf " 重启时间: 每天 %02d:%02d\n" $hour $minute
|
||||
echo -e " 日志位置: /var/log/port-forward-daily.log"
|
||||
fi
|
||||
}
|
||||
|
||||
uninstall_daily_restart() {
|
||||
read -p "确认取消每日定时重启? (y/n): " confirm
|
||||
if [[ "$confirm" != "y" ]]; then echo "操作已取消"; return; fi
|
||||
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
systemctl stop port-forward-daily.timer 2>/dev/null
|
||||
systemctl disable port-forward-daily.timer 2>/dev/null
|
||||
rm -f /etc/systemd/system/port-forward-daily.timer
|
||||
rm -f /etc/systemd/system/port-forward-daily.service
|
||||
systemctl daemon-reload
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
crontab -l 2>/dev/null | grep -v "port-forward-daily-restart" | crontab - 2>/dev/null
|
||||
fi
|
||||
|
||||
rm -f /root/port-forward-daily-restart.sh
|
||||
systemctl daemon-reload
|
||||
echo -e "${GREEN}✓ 每日定时重启已取消${NC}"
|
||||
}
|
||||
|
||||
@ -718,26 +404,14 @@ daily_restart_menu() {
|
||||
echo -e "${BLUE} ⏰ 每日定时重启管理${NC}"
|
||||
echo -e "${BLUE}================================${NC}"
|
||||
|
||||
if [ "$INIT_SYS" = "systemd" ]; then
|
||||
local timer_status=$(systemctl is-active port-forward-daily.timer 2>/dev/null)
|
||||
timer_status=$(systemctl is-active port-forward-daily.timer 2>/dev/null)
|
||||
if [[ "$timer_status" == "active" ]]; then
|
||||
echo -e "当前状态: ${GREEN}● 已开启${NC}"
|
||||
local next=$(systemctl list-timers port-forward-daily.timer --no-pager 2>/dev/null | awk 'NR==2{print $1, $2}')
|
||||
next=$(systemctl list-timers port-forward-daily.timer --no-pager 2>/dev/null | awk 'NR==2{print $1, $2}')
|
||||
echo -e "下次执行: ${YELLOW}${next}${NC}"
|
||||
else
|
||||
echo -e "当前状态: ${RED}● 未开启${NC}"
|
||||
fi
|
||||
elif [ "$INIT_SYS" = "openrc" ]; then
|
||||
if crontab -l 2>/dev/null | grep -q "port-forward-daily-restart"; then
|
||||
local cron_entry=$(crontab -l 2>/dev/null | grep "port-forward-daily-restart")
|
||||
local cron_min=$(echo "$cron_entry" | awk '{print $1}')
|
||||
local cron_hr=$(echo "$cron_entry" | awk '{print $2}')
|
||||
echo -e "当前状态: ${GREEN}● 已开启${NC}"
|
||||
printf "执行时间: ${YELLOW}每天 %02d:%02d${NC}\n" $cron_hr $cron_min
|
||||
else
|
||||
echo -e "当前状态: ${RED}● 未开启${NC}"
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "--------------------------------"
|
||||
echo "1. 设置每日定时重启"
|
||||
@ -781,7 +455,7 @@ watchdog_menu() {
|
||||
echo -e "${BLUE} 🛡️ Watchdog 自动重启管理${NC}"
|
||||
echo -e "${BLUE}================================${NC}"
|
||||
|
||||
local w_status=$(svc_is_active ${WATCHDOG_SERVICE})
|
||||
w_status=$(systemctl is-active ${WATCHDOG_SERVICE} 2>/dev/null)
|
||||
if [[ "$w_status" == "active" ]]; then
|
||||
echo -e "当前状态: ${GREEN}● 运行中${NC}"
|
||||
else
|
||||
@ -800,8 +474,8 @@ watchdog_menu() {
|
||||
|
||||
case $choice in
|
||||
1) install_watchdog ;;
|
||||
2) svc_stop "${WATCHDOG_SERVICE}" && echo -e "${GREEN}✓ Watchdog 已停止${NC}" ;;
|
||||
3) svc_start "${WATCHDOG_SERVICE}" && echo -e "${GREEN}✓ Watchdog 已启动${NC}" ;;
|
||||
2) systemctl stop ${WATCHDOG_SERVICE} && echo -e "${GREEN}✓ Watchdog 已停止${NC}" ;;
|
||||
3) systemctl start ${WATCHDOG_SERVICE} && echo -e "${GREEN}✓ Watchdog 已启动${NC}" ;;
|
||||
4) view_watchdog_log ;;
|
||||
5) uninstall_watchdog ;;
|
||||
6) return ;;
|
||||
@ -814,15 +488,15 @@ delete_forward() {
|
||||
echo -e "${GREEN}=== 删除端口转发 ===${NC}"
|
||||
read -p "请输入要删除的本机端口: " port
|
||||
|
||||
local svc_name="${SERVICE_PREFIX}-${port}"
|
||||
if ! svc_exists "$svc_name"; then
|
||||
service_name="${SERVICE_PREFIX}-${port}.service"
|
||||
if [[ ! -f "/etc/systemd/system/$service_name" ]]; then
|
||||
echo -e "${RED}错误: 端口 ${port} 的转发不存在${NC}"; return 1
|
||||
fi
|
||||
|
||||
svc_stop "$svc_name"
|
||||
svc_disable "$svc_name"
|
||||
svc_delete "$svc_name"
|
||||
svc_daemon_reload
|
||||
systemctl stop $service_name
|
||||
systemctl disable $service_name
|
||||
rm -f /etc/systemd/system/$service_name
|
||||
systemctl daemon-reload
|
||||
echo -e "${GREEN}✓ 端口 ${port} 的转发已删除${NC}"
|
||||
}
|
||||
|
||||
@ -836,16 +510,16 @@ delete_batch_forward() {
|
||||
if [[ "$confirm" != "y" ]]; then echo "操作已取消"; return; fi
|
||||
|
||||
for port in $(seq $start_port $end_port); do
|
||||
local svc_name="${SERVICE_PREFIX}-${port}"
|
||||
if svc_exists "$svc_name"; then
|
||||
svc_stop "$svc_name"
|
||||
svc_disable "$svc_name"
|
||||
svc_delete "$svc_name"
|
||||
service_name="${SERVICE_PREFIX}-${port}.service"
|
||||
if [[ -f "/etc/systemd/system/$service_name" ]]; then
|
||||
systemctl stop $service_name
|
||||
systemctl disable $service_name > /dev/null 2>&1
|
||||
rm -f /etc/systemd/system/$service_name
|
||||
echo -e "${GREEN}✓${NC} 端口 ${port} 的转发已删除"
|
||||
fi
|
||||
done
|
||||
|
||||
svc_daemon_reload
|
||||
systemctl daemon-reload
|
||||
echo -e "${GREEN}批量删除完成!${NC}"
|
||||
}
|
||||
|
||||
@ -854,7 +528,6 @@ main_menu() {
|
||||
echo ""
|
||||
echo -e "${GREEN}================================${NC}"
|
||||
echo -e "${GREEN} 端口转发管理脚本${NC}"
|
||||
echo -e "${GREEN} (${INIT_SYS} | ${OS})${NC}"
|
||||
echo -e "${GREEN}================================${NC}"
|
||||
echo "1. 添加单个端口转发"
|
||||
echo "2. 批量添加端口转发"
|
||||
@ -885,23 +558,12 @@ main_menu() {
|
||||
done
|
||||
}
|
||||
|
||||
# 确保使用 bash 执行
|
||||
if [ -z "$BASH_VERSION" ]; then
|
||||
if command -v bash > /dev/null 2>&1; then
|
||||
exec bash "$0" "$@"
|
||||
else
|
||||
echo "错误: 需要 bash 来运行此脚本"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ $EUID -ne 0 ]]; then
|
||||
echo -e "${RED}此脚本必须以 root 权限运行${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
detect_system
|
||||
check_deps
|
||||
check_socat
|
||||
main_menu
|
||||
EOF
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user