Compare commits

..

No commits in common. "main" and "master" have entirely different histories.
main ... master

584
port.sh
View File

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