port/port.sh
2026-02-21 08:44:18 -05:00

249 lines
7.3 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

cat > /root/port-forward.sh << 'EOF'
#!/bin/bash
# 端口转发管理脚本
# 使用 socat 实现端口转发
SCRIPT_NAME="Port Forward Manager"
SERVICE_PREFIX="port-forward"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 检查是否安装 socat
check_socat() {
if ! command -v socat &> /dev/null; then
echo -e "${YELLOW}socat 未安装,正在安装...${NC}"
apt update && apt install socat -y
if [ $? -eq 0 ]; then
echo -e "${GREEN}socat 安装成功!${NC}"
else
echo -e "${RED}socat 安装失败,请手动安装${NC}"
exit 1
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
echo -e "${RED}错误: 输入不能为空${NC}"
return 1
fi
# 创建 systemd 服务
cat > /etc/systemd/system/${SERVICE_PREFIX}-${local_port}.service << SERVICEEOF
[Unit]
Description=Port Forward ${local_port} to ${target_ip}:${target_port}
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/socat TCP4-LISTEN:${local_port},reuseaddr,fork TCP4:${target_ip}:${target_port}
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
SERVICEEOF
# 启动服务
systemctl daemon-reload
systemctl enable ${SERVICE_PREFIX}-${local_port}
systemctl start ${SERVICE_PREFIX}-${local_port}
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ 端口转发添加成功!${NC}"
echo -e "本机端口: ${local_port} -> 目标: ${target_ip}:${target_port}"
else
echo -e "${RED}✗ 端口转发添加失败${NC}"
fi
}
# 批量添加端口转发
add_batch_forward() {
echo -e "${GREEN}=== 批量添加端口转发 ===${NC}"
read -p "请输入本机起始端口: " start_port
read -p "请输入本机结束端口: " end_port
read -p "请输入目标IP地址: " target_ip
read -p "请输入目标端口: " target_port
# 验证输入
if [[ -z "$start_port" ]] || [[ -z "$end_port" ]] || [[ -z "$target_ip" ]] || [[ -z "$target_port" ]]; then
echo -e "${RED}错误: 输入不能为空${NC}"
return 1
fi
echo -e "${YELLOW}即将创建 $((end_port - start_port + 1)) 个端口转发...${NC}"
read -p "确认继续? (y/n): " confirm
if [[ "$confirm" != "y" ]]; then
echo "操作已取消"
return
fi
for port in $(seq $start_port $end_port); do
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
[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
systemctl daemon-reload
echo -e "${GREEN}批量端口转发添加完成!${NC}"
}
# 查看所有转发
list_forwards() {
echo -e "${GREEN}=== 当前端口转发列表 ===${NC}"
echo ""
services=$(systemctl list-units --all --type=service --no-pager | grep ${SERVICE_PREFIX} | awk '{print $1}')
if [[ -z "$services" ]]; then
echo -e "${YELLOW}暂无端口转发${NC}"
return
fi
printf "%-15s %-10s %-30s\n" "本机端口" "状态" "转发目标"
echo "--------------------------------------------------------"
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
status_color="${GREEN}运行中${NC}"
else
status_color="${RED}已停止${NC}"
fi
# 从服务文件中提取目标信息
target=$(grep ExecStart /etc/systemd/system/$service | grep -oP 'TCP4:\K[^ ]+')
printf "%-15s %-20b %-30s\n" "$port" "$status_color" "$target"
done
}
# 删除端口转发
delete_forward() {
echo -e "${GREEN}=== 删除端口转发 ===${NC}"
read -p "请输入要删除的本机端口: " port
if [[ -z "$port" ]]; then
echo -e "${RED}错误: 端口不能为空${NC}"
return 1
fi
service_name="${SERVICE_PREFIX}-${port}.service"
if [[ ! -f "/etc/systemd/system/$service_name" ]]; then
echo -e "${RED}错误: 端口 ${port} 的转发不存在${NC}"
return 1
fi
systemctl stop $service_name
systemctl disable $service_name
rm -f /etc/systemd/system/$service_name
systemctl daemon-reload
echo -e "${GREEN}✓ 端口 ${port} 的转发已删除${NC}"
}
# 批量删除端口转发
delete_batch_forward() {
echo -e "${GREEN}=== 批量删除端口转发 ===${NC}"
read -p "请输入起始端口: " start_port
read -p "请输入结束端口: " end_port
echo -e "${YELLOW}即将删除端口 ${start_port}-${end_port} 的转发...${NC}"
read -p "确认继续? (y/n): " confirm
if [[ "$confirm" != "y" ]]; then
echo "操作已取消"
return
fi
for port in $(seq $start_port $end_port); do
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
systemctl daemon-reload
echo -e "${GREEN}批量删除完成!${NC}"
}
# 主菜单
main_menu() {
while true; do
echo ""
echo -e "${GREEN}================================${NC}"
echo -e "${GREEN} 端口转发管理脚本${NC}"
echo -e "${GREEN}================================${NC}"
echo "1. 添加单个端口转发"
echo "2. 批量添加端口转发"
echo "3. 查看所有转发"
echo "4. 删除单个端口转发"
echo "5. 批量删除端口转发"
echo "6. 退出"
echo -e "${GREEN}================================${NC}"
read -p "请选择操作 [1-6]: " choice
case $choice in
1) add_single_forward ;;
2) add_batch_forward ;;
3) list_forwards ;;
4) delete_forward ;;
5) delete_batch_forward ;;
6) echo "退出脚本"; exit 0 ;;
*) echo -e "${RED}无效选择,请重新输入${NC}" ;;
esac
done
}
# 检查是否为 root
if [[ $EUID -ne 0 ]]; then
echo -e "${RED}此脚本必须以 root 权限运行${NC}"
exit 1
fi
# 检查并安装 socat
check_socat
# 启动主菜单
main_menu
EOF
# 设置执行权限
chmod +x /root/port-forward.sh
echo -e "\033[0;32m脚本已创建成功\033[0m"
echo "运行命令: bash /root/port-forward.sh"