diff --git a/run.sh b/run.sh index b4a0043a..b7466012 100644 --- a/run.sh +++ b/run.sh @@ -2,11 +2,11 @@ set -euo pipefail -# CyberStrikeAI 一键部署启动脚本 +# CyberStrikeAI one-click deploy and start script ROOT_DIR="$(cd "$(dirname "$0")" && pwd)" cd "$ROOT_DIR" -# 颜色定义 +# Color definitions RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' @@ -14,31 +14,31 @@ BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color -# 打印带颜色的消息 +# Print colored messages info() { echo -e "${BLUE}ℹ️ $1${NC}"; } success() { echo -e "${GREEN}✅ $1${NC}"; } warning() { echo -e "${YELLOW}⚠️ $1${NC}"; } error() { echo -e "${RED}❌ $1${NC}"; } note() { echo -e "${CYAN}ℹ️ $1${NC}"; } -# 临时源配置(仅在此脚本中生效) +# Temporary mirror/proxy settings (only effective in this script) PIP_INDEX_URL="${PIP_INDEX_URL:-https://pypi.tuna.tsinghua.edu.cn/simple}" GOPROXY="${GOPROXY:-https://goproxy.cn,direct}" -# 保存原始环境变量(用于恢复) +# Save original env vars (for restoration) ORIGINAL_PIP_INDEX_URL="${PIP_INDEX_URL:-}" ORIGINAL_GOPROXY="${GOPROXY:-}" -# 进度显示函数 +# Progress display helper show_progress() { local pid=$1 local message=$2 local i=0 local dots="" - # 检查进程是否存在 + # Check if the process exists if ! kill -0 "$pid" 2>/dev/null; then - # 进程已经结束,立即返回 + # Process already finished; return immediately return 0 fi @@ -53,7 +53,7 @@ show_progress() { printf "\r${BLUE}⏳ %s%s${NC}" "$message" "$dots" sleep 0.5 - # 再次检查进程是否还存在 + # Re-check whether the process is still running if ! kill -0 "$pid" 2>/dev/null; then break fi @@ -63,21 +63,21 @@ show_progress() { echo "" echo "==========================================" -echo " CyberStrikeAI 一键部署启动脚本" -echo " (默认 HTTPS 自签证书;纯 HTTP 请用: $0 --http)" +echo " CyberStrikeAI Deploy & Start Script" +echo " (HTTPS with self-signed cert by default; plain HTTP: $0 --http)" echo "==========================================" echo "" -# 显示临时源配置信息 +# Show temporary mirror/proxy info echo "" -warning "⚠️ 注意:此脚本将使用临时镜像源加速下载" +warning "Note: this script uses temporary mirrors to speed up downloads" echo "" -info "Python pip 临时镜像源:" +info "Python pip temporary mirror:" echo " ${PIP_INDEX_URL}" -info "Go Proxy 临时镜像源:" +info "Go temporary proxy:" echo " ${GOPROXY}" echo "" -note "这些设置仅在脚本运行期间生效,不会修改系统配置" +note "These settings apply only while this script runs and do not change system config" echo "" sleep 1 @@ -86,19 +86,19 @@ VENV_DIR="$ROOT_DIR/venv" REQUIREMENTS_FILE="$ROOT_DIR/requirements.txt" BINARY_NAME="cyberstrike-ai" -# 检查配置文件 +# Check config file if [ ! -f "$CONFIG_FILE" ]; then - error "配置文件 config.yaml 不存在" - info "请确保在项目根目录运行此脚本" + error "Config file config.yaml not found" + info "Make sure you run this script from the project root" exit 1 fi -# 检查并安装 Python 环境 +# Check Python environment check_python() { if ! command -v python3 >/dev/null 2>&1; then - error "未找到 python3" + error "python3 not found" echo "" - info "请先安装 Python 3.10 或更高版本:" + info "Install Python 3.10 or later first:" echo " macOS: brew install python3" echo " Ubuntu: sudo apt-get install python3 python3-venv" echo " CentOS: sudo yum install python3 python3-pip" @@ -110,23 +110,23 @@ check_python() { PYTHON_MINOR=$(echo "$PYTHON_VERSION" | cut -d. -f2) if [ "$PYTHON_MAJOR" -lt 3 ] || ([ "$PYTHON_MAJOR" -eq 3 ] && [ "$PYTHON_MINOR" -lt 10 ]); then - error "Python 版本过低: $PYTHON_VERSION (需要 3.10+)" + error "Python version too old: $PYTHON_VERSION (requires 3.10+)" exit 1 fi - success "Python 环境检查通过: $PYTHON_VERSION" + success "Python check passed: $PYTHON_VERSION" } -# 检查并安装 Go 环境 +# Check Go environment check_go() { if ! command -v go >/dev/null 2>&1; then - error "未找到 Go" + error "Go not found" echo "" - info "请先安装 Go 1.21 或更高版本:" + info "Install Go 1.21 or later first:" echo " macOS: brew install go" echo " Ubuntu: sudo apt-get install golang-go" echo " CentOS: sudo yum install golang" - echo " 或访问: https://go.dev/dl/" + echo " Or visit: https://go.dev/dl/" exit 1 fi @@ -135,63 +135,63 @@ check_go() { GO_MINOR=$(echo "$GO_VERSION" | cut -d. -f2) if [ "$GO_MAJOR" -lt 1 ] || ([ "$GO_MAJOR" -eq 1 ] && [ "$GO_MINOR" -lt 21 ]); then - error "Go 版本过低: $GO_VERSION (需要 1.21+)" + error "Go version too old: $GO_VERSION (requires 1.21+)" exit 1 fi - success "Go 环境检查通过: $(go version)" + success "Go check passed: $(go version)" } -# 设置 Python 虚拟环境 +# Set up Python virtual environment setup_python_env() { if [ ! -d "$VENV_DIR" ]; then - info "创建 Python 虚拟环境..." + info "Creating Python virtual environment..." python3 -m venv "$VENV_DIR" - success "虚拟环境创建完成" + success "Virtual environment created" else - info "Python 虚拟环境已存在" + info "Python virtual environment already exists" fi - info "激活虚拟环境..." + info "Activating virtual environment..." # shellcheck disable=SC1091 source "$VENV_DIR/bin/activate" if [ -f "$REQUIREMENTS_FILE" ]; then echo "" note "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" - note "⚠️ 使用临时 pip 镜像源(仅本次脚本运行有效)" - note " 镜像地址: ${PIP_INDEX_URL}" - note " 如需永久配置,请设置环境变量 PIP_INDEX_URL" + note "Using temporary pip mirror (this script run only)" + note " Mirror URL: ${PIP_INDEX_URL}" + note " For a permanent setting, set the PIP_INDEX_URL env var" note "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" - info "升级 pip..." + info "Upgrading pip..." pip install --index-url "$PIP_INDEX_URL" --upgrade pip >/dev/null 2>&1 || true - info "安装 Python 依赖包..." + info "Installing Python dependencies..." echo "" - # 尝试安装依赖,捕获错误输出并显示进度 + # Install deps in background; capture errors and show progress PIP_LOG=$(mktemp) ( - set +e # 在子shell中禁用错误退出 + set +e # disable errexit in subshell pip install --index-url "$PIP_INDEX_URL" -r "$REQUIREMENTS_FILE" >"$PIP_LOG" 2>&1 echo $? > "${PIP_LOG}.exit" ) & PIP_PID=$! - # 等待一小段时间,确保进程启动 + # Brief pause so the process can start sleep 0.1 - # 显示进度(如果进程还在运行) + # Show progress while still running if kill -0 "$PIP_PID" 2>/dev/null; then - show_progress "$PIP_PID" "正在安装依赖包" + show_progress "$PIP_PID" "Installing dependencies" else - # 进程已经结束,等待一下确保退出码文件已写入 + # Process already finished; wait for exit code file sleep 0.2 fi - # 等待进程完成,忽略 wait 的退出码 + # Wait for completion; ignore wait exit code wait "$PIP_PID" 2>/dev/null || true PIP_EXIT_CODE=0 @@ -199,74 +199,74 @@ setup_python_env() { PIP_EXIT_CODE=$(cat "${PIP_LOG}.exit" 2>/dev/null || echo "1") rm -f "${PIP_LOG}.exit" 2>/dev/null || true else - # 如果没有退出码文件,检查日志中是否有错误 + # No exit code file; check log for errors if [ -f "$PIP_LOG" ] && grep -q -i "error\|failed\|exception" "$PIP_LOG" 2>/dev/null; then PIP_EXIT_CODE=1 fi fi if [ $PIP_EXIT_CODE -eq 0 ]; then - success "Python 依赖安装完成" + success "Python dependencies installed" else - # 检查是否是 angr 安装失败(需要 Rust) + # Check for angr install failure (needs Rust) if grep -q "angr" "$PIP_LOG" && grep -q "Rust compiler\|can't find Rust" "$PIP_LOG"; then - warning "angr 安装失败(需要 Rust 编译器)" + warning "angr install failed (Rust compiler required)" echo "" - info "angr 是可选依赖,主要用于二进制分析工具" - info "如果需要使用 angr,请先安装 Rust:" + info "angr is optional and mainly used for binary analysis tools" + info "To use angr, install Rust first:" echo " macOS: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh" echo " Ubuntu: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh" - echo " 或访问: https://rustup.rs/" + echo " Or visit: https://rustup.rs/" echo "" - info "其他依赖已安装,可以继续使用(部分工具可能不可用)" + info "Other dependencies are installed; you can continue (some tools may be unavailable)" else - warning "部分 Python 依赖安装失败,但可以继续尝试运行" - warning "如果遇到问题,请检查错误信息并手动安装缺失的依赖" - # 显示最后几行错误信息 + warning "Some Python dependencies failed to install, but continuing" + warning "If you hit issues, check the errors and install missing packages manually" + # Show last lines of error output echo "" - info "错误详情(最后 10 行):" + info "Error details (last 10 lines):" tail -n 10 "$PIP_LOG" | sed 's/^/ /' echo "" fi fi rm -f "$PIP_LOG" else - warning "未找到 requirements.txt,跳过 Python 依赖安装" + warning "requirements.txt not found; skipping Python dependency install" fi } -# 构建 Go 项目 +# Build Go project build_go_project() { echo "" note "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" - note "⚠️ 使用临时 Go Proxy(仅本次脚本运行有效)" - note " Proxy 地址: ${GOPROXY}" - note " 如需永久配置,请设置环境变量 GOPROXY" + note "Using temporary Go proxy (this script run only)" + note " Proxy URL: ${GOPROXY}" + note " For a permanent setting, set the GOPROXY env var" note "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "" - info "下载 Go 依赖..." + info "Downloading Go dependencies..." GO_DOWNLOAD_LOG=$(mktemp) ( - set +e # 在子shell中禁用错误退出 + set +e # disable errexit in subshell export GOPROXY="$GOPROXY" go mod download >"$GO_DOWNLOAD_LOG" 2>&1 echo $? > "${GO_DOWNLOAD_LOG}.exit" ) & GO_DOWNLOAD_PID=$! - # 等待一小段时间,确保进程启动 + # Brief pause so the process can start sleep 0.1 - # 显示进度(如果进程还在运行) + # Show progress while still running if kill -0 "$GO_DOWNLOAD_PID" 2>/dev/null; then - show_progress "$GO_DOWNLOAD_PID" "正在下载 Go 依赖" + show_progress "$GO_DOWNLOAD_PID" "Downloading Go dependencies" else - # 进程已经结束,等待一下确保退出码文件已写入 + # Process already finished; wait for exit code file sleep 0.2 fi - # 等待进程完成,忽略 wait 的退出码 + # Wait for completion; ignore wait exit code wait "$GO_DOWNLOAD_PID" 2>/dev/null || true GO_DOWNLOAD_EXIT_CODE=0 @@ -274,7 +274,7 @@ build_go_project() { GO_DOWNLOAD_EXIT_CODE=$(cat "${GO_DOWNLOAD_LOG}.exit" 2>/dev/null || echo "1") rm -f "${GO_DOWNLOAD_LOG}.exit" 2>/dev/null || true else - # 如果没有退出码文件,检查日志中是否有错误 + # No exit code file; check log for errors if [ -f "$GO_DOWNLOAD_LOG" ] && grep -q -i "error\|failed" "$GO_DOWNLOAD_LOG" 2>/dev/null; then GO_DOWNLOAD_EXIT_CODE=1 fi @@ -282,33 +282,33 @@ build_go_project() { rm -f "$GO_DOWNLOAD_LOG" 2>/dev/null || true if [ $GO_DOWNLOAD_EXIT_CODE -ne 0 ]; then - error "Go 依赖下载失败" + error "Go dependency download failed" exit 1 fi - success "Go 依赖下载完成" + success "Go dependencies downloaded" - info "构建项目..." + info "Building project..." GO_BUILD_LOG=$(mktemp) ( - set +e # 在子shell中禁用错误退出 + set +e # disable errexit in subshell export GOPROXY="$GOPROXY" go build -o "$BINARY_NAME" cmd/server/main.go >"$GO_BUILD_LOG" 2>&1 echo $? > "${GO_BUILD_LOG}.exit" ) & GO_BUILD_PID=$! - # 等待一小段时间,确保进程启动 + # Brief pause so the process can start sleep 0.1 - # 显示进度(如果进程还在运行) + # Show progress while still running if kill -0 "$GO_BUILD_PID" 2>/dev/null; then - show_progress "$GO_BUILD_PID" "正在构建项目" + show_progress "$GO_BUILD_PID" "Building project" else - # 进程已经结束,等待一下确保退出码文件已写入 + # Process already finished; wait for exit code file sleep 0.2 fi - # 等待进程完成,忽略 wait 的退出码 + # Wait for completion; ignore wait exit code wait "$GO_BUILD_PID" 2>/dev/null || true GO_BUILD_EXIT_CODE=0 @@ -316,20 +316,20 @@ build_go_project() { GO_BUILD_EXIT_CODE=$(cat "${GO_BUILD_LOG}.exit" 2>/dev/null || echo "1") rm -f "${GO_BUILD_LOG}.exit" 2>/dev/null || true else - # 如果没有退出码文件,检查日志中是否有错误 + # No exit code file; check log for errors if [ -f "$GO_BUILD_LOG" ] && grep -q -i "error\|failed" "$GO_BUILD_LOG" 2>/dev/null; then GO_BUILD_EXIT_CODE=1 fi fi if [ $GO_BUILD_EXIT_CODE -eq 0 ]; then - success "项目构建完成: $BINARY_NAME" + success "Build complete: $BINARY_NAME" rm -f "$GO_BUILD_LOG" else - error "项目构建失败" - # 显示构建错误 + error "Build failed" + # Show build errors echo "" - info "构建错误详情:" + info "Build error details:" cat "$GO_BUILD_LOG" | sed 's/^/ /' echo "" rm -f "$GO_BUILD_LOG" @@ -337,24 +337,24 @@ build_go_project() { fi } -# 检查是否需要重新构建 +# Check whether a rebuild is needed need_rebuild() { if [ ! -f "$BINARY_NAME" ]; then - return 0 # 需要构建 + return 0 # needs build fi - # 检查源代码是否有更新 + # Check if source changed since last build if [ "$BINARY_NAME" -ot cmd/server/main.go ] || \ [ "$BINARY_NAME" -ot go.mod ] || \ find internal cmd -name "*.go" -newer "$BINARY_NAME" 2>/dev/null | grep -q .; then - return 0 # 需要重新构建 + return 0 # needs rebuild fi - return 1 # 不需要构建 + return 1 # no rebuild needed } -# 主流程 -# 默认启动主站 HTTPS(--https 传给二进制);传 --http 则走明文 HTTP。 +# Main flow +# Default: HTTPS (--https passed to binary); --http uses plain HTTP. main() { USE_HTTPS=1 FORWARD_ARGS=() @@ -366,39 +366,39 @@ main() { FORWARD_ARGS+=("$arg") done - # 环境检查 - info "检查运行环境..." + # Environment checks + info "Checking runtime environment..." check_python check_go echo "" - # 设置 Python 环境 - info "设置 Python 环境..." + # Python setup + info "Setting up Python environment..." setup_python_env echo "" - # 构建 Go 项目 + # Go build if need_rebuild; then - info "准备构建项目..." + info "Preparing to build project..." build_go_project else - success "可执行文件已是最新,跳过构建" + success "Binary is up to date; skipping build" fi echo "" - # 启动服务器 - success "所有准备工作完成!" + # Start server + success "All setup complete!" echo "" if [ "$USE_HTTPS" -eq 1 ]; then - info "启动 CyberStrikeAI 服务器(HTTPS + HTTP/2,自签证书)..." - note "纯 HTTP 启动请使用: $0 --http" + info "Starting CyberStrikeAI server (HTTPS + HTTP/2, self-signed cert)..." + note "For plain HTTP, use: $0 --http" else - info "启动 CyberStrikeAI 服务器(HTTP)..." + info "Starting CyberStrikeAI server (HTTP)..." fi echo "==========================================" echo "" - # 始终传入项目根目录下的 config.yaml,避免 cwd 不在项目根时找不到配置;额外参数仍可追加(如再次 -config 覆盖,以 Go flag 后写为准)。 + # Always pass config.yaml from project root so cwd does not matter; extra args still apply (e.g. -config override; last Go flag wins). if [ "$USE_HTTPS" -eq 1 ]; then if [ "${#FORWARD_ARGS[@]}" -gt 0 ]; then exec "./$BINARY_NAME" -config "$CONFIG_FILE" --https "${FORWARD_ARGS[@]}" @@ -414,5 +414,5 @@ main() { fi } -# 执行主流程(支持参数,如: ./run.sh --http) +# Run main (supports args, e.g. ./run.sh --http) main "$@"