Files
CNBUGS AI 8ad4c3576d Fix DHCP client unable to get IP and config not persisting
- Fixed verifyAssignment being too strict for new clients
- Fixed parseRequestedIP string conversion bug
- Fixed response sent to 0.0.0.0 instead of broadcast address
- Added SO_BROADCAST support for UDP socket
- Fixed session persistence after page refresh (localStorage)
- Added in-memory session store for auth middleware
- Added config reloader so DHCP server picks up web UI changes dynamically
2026-04-24 16:03:54 +08:00

170 regels
4.0 KiB
Bash
Executable File

#!/bin/bash
# ========================================
# DHCP & DNS 管理器 - Linux 安装脚本(修复版)
# ========================================
set -e
INSTALL_DIR="/opt/dhcp-dns-manager"
SERVICE_NAME="dhcp-dns-manager"
echo "========================================"
echo " DHCP & DNS 管理器 - Linux 安装脚本"
echo "========================================"
echo ""
# 检查是否以 root 运行
if [ "$EUID" -ne 0 ]; then
echo "❌ 请使用 sudo 运行此脚本"
echo " sudo ./install.sh"
exit 1
fi
# 检测系统
if [ -f /etc/debian_version ]; then
OS="debian"
echo "✓ 检测到 Debian/Ubuntu 系统"
elif [ -f /etc/redhat-release ]; then
OS="redhat"
echo "✓ 检测到 RHEL/CentOS 系统"
else
OS="unknown"
echo "⚠ 未识别的 Linux 发行版,尝试通用安装"
fi
# 安装依赖
echo ""
echo "📦 安装依赖..."
if [ "$OS" = "debian" ]; then
apt update
apt install -y curl wget git build-essential
elif [ "$OS" = "redhat" ]; then
yum install -y curl wget git gcc make
fi
# 检查 Go 环境
if ! command -v go &> /dev/null; then
echo ""
echo "📦 安装 Go 环境..."
GO_VERSION="1.21.0"
wget -q https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz
tar -C /usr/local -xzf go${GO_VERSION}.linux-amd64.tar.gz
rm go${GO_VERSION}.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
export PATH=$PATH:/usr/local/go/bin
echo "✓ Go 已安装"
else
echo "✓ Go 环境已存在: $(go version)"
fi
# 创建安装目录
echo ""
echo "📁 创建安装目录..."
mkdir -p $INSTALL_DIR
# 复制文件
echo ""
echo "📋 复制项目文件..."
cp -r ./* $INSTALL_DIR/
# 编译程序
echo ""
echo "🔨 编译程序..."
cd $INSTALL_DIR
# 整理并下载依赖
echo "整理依赖..."
go mod tidy
# 下载依赖
echo "下载 Go 依赖..."
go mod download
# 编译
echo "编译程序..."
CGO_ENABLED=1 go build -o dhcp-dns-manager ./cmd
# 创建数据目录
mkdir -p $INSTALL_DIR/data
chown -R root:root $INSTALL_DIR
# 创建 systemd 服务
echo ""
echo "⚙️ 创建 systemd 服务..."
cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
[Unit]
Description=DHCP & DNS Manager Service
After=network.target
Documentation=https://github.com/your-repo/dhcp-dns-manager
[Service]
Type=simple
User=root
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/dhcp-dns-manager -config $INSTALL_DIR/configs/config.json
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=dhcp-dns-manager
# 安全设置
NoNewPrivileges=false
ProtectSystem=false
ProtectHome=false
[Install]
WantedBy=multi-user.target
EOF
# 重载 systemd
systemctl daemon-reload
# 配置防火墙
echo ""
echo "🔥 配置防火墙..."
if command -v ufw &> /dev/null; then
echo "配置 UFW 防火墙..."
ufw allow 53/udp comment "DNS"
ufw allow 67/udp comment "DHCP"
ufw allow 8080/tcp comment "Web UI"
elif command -v firewall-cmd &> /dev/null; then
echo "配置 Firewalld..."
firewall-cmd --permanent --add-port=53/udp
firewall-cmd --permanent --add-port=67/udp
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
else
echo "⚠ 未检测到防火墙工具,请手动配置"
fi
# 启动服务
echo ""
echo "🚀 启动服务..."
systemctl enable $SERVICE_NAME
systemctl start $SERVICE_NAME
# 检查状态
sleep 2
echo ""
echo "========================================"
echo " ✅ 安装完成!"
echo "========================================"
echo ""
echo "服务状态:$(systemctl is-active $SERVICE_NAME)"
echo ""
echo "📱 Web 界面:http://$(hostname -I | awk '{print $1}'):8080"
echo "👤 默认账号:admin / admin"
echo ""
echo "常用命令:"
echo " 查看状态:systemctl status $SERVICE_NAME"
echo " 查看日志:journalctl -u $SERVICE_NAME -f"
echo " 重启服务:systemctl restart $SERVICE_NAME"
echo " 停止服务:systemctl stop $SERVICE_NAME"
echo ""
echo "⚠️ 首次使用请修改默认密码!"
echo ""