Files
gitea f947a682b0 创作者工具:视频/音乐解析、视音频转格式、网盘直链
- 服务端新增 lib/creator.mjs:yt-dlp 解析、ffmpeg 边转边传、蓝奏/阿里/百度直链
- server.mjs 增加 /api/creator/{status,cfg,resolve,convert,download,netdisk},各操作扣 1 积分
- 前端新增 js/tools-creator.js 与「创作者」分组(5 个工具),配套样式
- install.sh 顺手装 ffmpeg / yt-dlp;README、INSTALL 补依赖与用法
2026-11-20 13:40:04 +08:00

288 lines
10 KiB
Bash
Executable File
Raw Permalink 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.
#!/usr/bin/env bash
# DevBox 一键安装 / 更新 / 卸载
#
# 仓库在自己搭的 Gitea 上(公开库,一条命令就行):
# curl -fsSL http://110.42.217.133:3000/gitea/devbox/raw/branch/main/install.sh | sudo bash
# 换端口 / 更新 / 卸载:在后面加 --port 8080、--update、--uninstall
# 需要 Node >= 22.5(自带 node:sqlite);没有会装一个便携版到 /opt/devbox-node
# 私有库才需要带上令牌:| sudo bash -s -- --token "$GITEA_TOKEN"
set -euo pipefail
REPO_URL="${DEVBOX_REPO:-http://110.42.217.133:3000/gitea/devbox.git}"
RAW_URL="${REPO_URL%.git}/raw/branch/main"
APP_DIR="${DEVBOX_DIR:-/opt/devbox}"
PORT="${DEVBOX_PORT:-8090}"
SERVICE="devbox"
NGINX_DOMAIN=""
MODE="install"
NO_SERVICE=0
GIT_USER="${GITEA_USER:-gitea}"
TOKEN="${GITEA_TOKEN:-${GIT_TOKEN:-}}"
REPO_HOST="$(printf '%s' "$REPO_URL" | sed -E 's#^https?://##; s#/.*$##')"
# 私有库拉代码:把用户名/令牌临时拼进 URL 用一次,用完把 origin 改回干净地址,
# 令牌不会留在 .git/config 里,也不会进 ps(只在这一次参数里)。
cred_url() {
if [ -z "$TOKEN" ]; then printf '%s' "$REPO_URL"; return; fi
printf '%s' "$REPO_URL" | sed -E "s#^(https?://)#\1${GIT_USER}:${TOKEN}@#"
}
c_reset=$'\033[0m'; c_dim=$'\033[2m'; c_cyan=$'\033[36m'; c_green=$'\033[32m'; c_red=$'\033[31m'; c_bold=$'\033[1m'
log() { printf '%s▸%s %s\n' "$c_cyan" "$c_reset" "$*"; }
ok() { printf '%s✓%s %s\n' "$c_green" "$c_reset" "$*"; }
warn() { printf '%s!%s %s\n' "$c_red" "$c_reset" "$*" >&2; }
die() { warn "$*"; exit 1; }
usage() {
cat <<'EOF'
DevBox 一键安装脚本
--dir <路径> 安装目录,默认 /opt/devbox
--port <端口> HTTP 端口,默认 8090
--nginx <域名> 额外写一份 Nginx 站点配置并 reload(域名可写 _ 表示默认站点)
--no-service 只拉代码,不装 systemd 服务(容器 / 没 systemd 时用)
--token <令牌> Gitea 访问令牌(只有私有库才需要;公开库可不填)
--user <用户名> Gitea 用户名,默认 gitea
--repo <地址> 换一个仓库地址(默认脚本里写死的那个)
--update 更新到最新代码并重启服务
--uninstall 停服务、删 unit(不删代码目录)
-h, --help 显示这份帮助
EOF
}
while [ $# -gt 0 ]; do
case "$1" in
--dir) APP_DIR="${2:?缺少目录}"; shift 2 ;;
--port) PORT="${2:?缺少端口}"; shift 2 ;;
--nginx) NGINX_DOMAIN="${2:-_}"; shift 2 ;;
--token) TOKEN="${2:?缺少令牌}"; shift 2 ;;
--user) GIT_USER="${2:?缺少用户名}"; shift 2 ;;
--repo) REPO_URL="${2:?缺少仓库地址}"; shift 2
REPO_HOST="$(printf '%s' "$REPO_URL" | sed -E 's#^https?://##; s#/.*$##')" ;;
--no-service) NO_SERVICE=1; shift ;;
--update) MODE="update"; shift ;;
--uninstall) MODE="uninstall"; shift ;;
-h|--help) usage; exit 0 ;;
*) warn "未知参数:$1"; usage; exit 1 ;;
esac
done
case "$PORT" in ''|*[!0-9]*) die "端口要填数字:$PORT" ;; esac
[ "$PORT" -ge 1 ] && [ "$PORT" -le 65535 ] || die "端口范围 1-65535:$PORT"
if [ "$(id -u)" -ne 0 ]; then
die "需要 root:把这条命令前面加上 sudo 再跑"
fi
pm_install() {
if command -v apt-get >/dev/null 2>&1; then
DEBIAN_FRONTEND=noninteractive apt-get update -qq >/dev/null 2>&1 || true
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null
elif command -v dnf >/dev/null 2>&1; then dnf install -y "$@" >/dev/null
elif command -v yum >/dev/null 2>&1; then yum install -y "$@" >/dev/null
elif command -v apk >/dev/null 2>&1; then apk add --no-cache "$@" >/dev/null
else die "没认出你的发行版,请手动装:$*"
fi
}
need() {
command -v "$1" >/dev/null 2>&1 && return 0
log "安装 $1"
pm_install "$2"
command -v "$1" >/dev/null 2>&1 || die "$1 装完还是找不到,请手动安装后重跑"
}
NODE_DIR="/opt/devbox-node"
NODE_MIN="22.5.0"
node_ok() {
command -v node >/dev/null 2>&1 || return 1
node -e 'const [a,b]=process.versions.node.split(".").map(Number);process.exit((a>22||(a===22&&b>=5))?0:1)' 2>/dev/null
}
ensure_node() {
if node_ok; then
log "用现有的 node $(node -v)"
NODE_BIN="$(command -v node)"
return 0
fi
local arch url
case "$(uname -m)" in
x86_64|amd64) arch="x64" ;;
aarch64|arm64) arch="arm64" ;;
*) die "不认识的 CPU 架构:$(uname -m),请手动装 Node 22 后重跑" ;;
esac
local ver="v22.20.0"
url="https://npmmirror.com/mirrors/node/${ver}/node-${ver}-linux-${arch}.tar.xz"
log "装便携版 Node ${ver}(${arch})到 ${NODE_DIR}"
need curl curl
need tar tar
mkdir -p "$NODE_DIR"
curl -fsSL --connect-timeout 15 --max-time 900 -o /tmp/devbox-node.tar.xz "$url" \
|| die "下载 Node 失败,检查网络;也可以自己装 Node >= 22.5 后重跑"
tar -xJf /tmp/devbox-node.tar.xz -C "$NODE_DIR" --strip-components=1
rm -f /tmp/devbox-node.tar.xz
NODE_BIN="${NODE_DIR}/bin/node"
[ -x "$NODE_BIN" ] || die "Node 解压后找不到可执行文件"
"$NODE_BIN" -v
}
unit_path="/etc/systemd/system/${SERVICE}.service"
nginx_conf="/etc/nginx/conf.d/${SERVICE}.conf"
stop_service() {
if command -v systemctl >/dev/null 2>&1 && systemctl list-unit-files 2>/dev/null | grep -q "^${SERVICE}.service"; then
systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
fi
}
write_unit() {
cat >"$unit_path" <<EOF
[Unit]
Description=DevBox 开发者工具箱
After=network.target
[Service]
Type=simple
WorkingDirectory=${APP_DIR}
Environment=PORT=${PORT}
Environment=HOST=0.0.0.0
Environment=DEVBOX_DATA=/var/lib/devbox
ExecStart=${NODE_BIN} ${APP_DIR}/server.mjs
Restart=always
RestartSec=3
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now "$SERVICE" >/dev/null 2>&1 || true
systemctl restart "$SERVICE"
}
write_nginx() {
need nginx nginx
mkdir -p /etc/nginx/conf.d
cat >"$nginx_conf" <<EOF
server {
listen 80;
server_name ${NGINX_DOMAIN};
location / {
proxy_pass http://127.0.0.1:${PORT};
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
location ~* \\.(js|css|png|jpg|jpeg|webp|svg|woff2?)\$ {
expires 7d;
add_header Cache-Control "public";
}
}
EOF
if nginx -t >/dev/null 2>&1; then
systemctl reload nginx >/dev/null 2>&1 || service nginx reload >/dev/null 2>&1 || true
ok "Nginx 站点已写入 $nginx_conf(server_name ${NGINX_DOMAIN})"
else
warn "Nginx 配置检查没过,已保留 $nginx_conf,手动执行 nginx -t 看看"
fi
}
uninstall() {
log "卸载 $SERVICE"
stop_service
rm -f "$unit_path"
rm -f "$nginx_conf"
command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload || true
command -v nginx >/dev/null 2>&1 && nginx -t >/dev/null 2>&1 && (systemctl reload nginx >/dev/null 2>&1 || true) || true
ok "服务已移除;代码目录保留在 ${APP_DIR},要彻底删就 rm -rf ${APP_DIR}"
}
pull_code() {
need git git
need curl curl
# 创作者工具依赖(失败不阻断安装)
if ! command -v ffmpeg >/dev/null 2>&1; then
log "装 ffmpeg(创作者工具用)"
pkgs ffmpeg >/dev/null 2>&1 || true
if command -v dnf >/dev/null 2>&1 || command -v yum >/dev/null 2>&1; then pkgs vulkan-loader >/dev/null 2>&1 || true; fi
fi
if ! command -v yt-dlp >/dev/null 2>&1; then
log "装 yt-dlp(视频/音乐解析用)"
pkgs python3-pip >/dev/null 2>&1 || true
(python3 -m pip install -q --break-system-packages yt-dlp 2>/dev/null || pip3 install -q --break-system-packages yt-dlp 2>/dev/null || true) || true
fi
if [ -d "${APP_DIR}/.git" ]; then
log "更新 ${APP_DIR}"
GIT_TERMINAL_PROMPT=0 git -C "$APP_DIR" fetch --depth=1 "$(cred_url)" main
git -C "$APP_DIR" reset --hard FETCH_HEAD
else
log "克隆仓库到 ${APP_DIR}"
mkdir -p "$(dirname "$APP_DIR")"
rm -rf "$APP_DIR"
GIT_TERMINAL_PROMPT=0 git clone --depth=1 "$(cred_url)" "$APP_DIR" \
|| die "拉代码失败。若这个仓库是私有的,加 --token <令牌>(或先 export GITEA_TOKEN=…);公开库检查网络与地址。"
git -C "$APP_DIR" remote set-url origin "$REPO_URL"
fi
[ -f "${APP_DIR}/server.mjs" ] || die "拉下来的目录里没有 server.mjs,仓库地址对吗?"
[ -f "${APP_DIR}/index.html" ] || die "拉下来的目录里没有 index.html,仓库地址对吗?"
}
ensure_data() {
mkdir -p /var/lib/devbox
chmod 700 /var/lib/devbox
}
health() {
command -v curl >/dev/null 2>&1 || return 0
for _ in $(seq 1 15); do
if curl -fsS "http://127.0.0.1:${PORT}/" 2>/dev/null | grep -q '<title>DevBox'; then
ok "服务已就绪"
return 0
fi
sleep 1
done
warn "15 秒内没起来,看看日志:journalctl -u ${SERVICE} -n 50 --no-pager"
return 1
}
summary() {
local ip
ip="$(hostname -I 2>/dev/null | awk '{print $1}')"
echo
printf '%sDevBox 已就位%s\n' "$c_bold" "$c_reset"
printf ' 地址 http://%s:%s/\n' "${ip:-服务器IP}" "$PORT"
printf ' 目录 %s\n' "$APP_DIR"
printf ' 命令 systemctl status %s / journalctl -u %s -f\n' "$SERVICE" "$SERVICE"
printf ' 更新 curl -fsSL %s/install.sh | sudo bash -s -- --update\n' "$RAW_URL"
printf ' 卸载 curl -fsSL %s/install.sh | sudo bash -s -- --uninstall\n' "$RAW_URL"
echo
printf '%s如果外面访问不到,记得放行端口:ufw allow %s/tcp(云服务器还要在安全组里放行)%s\n' "$c_dim" "$PORT" "$c_reset"
[ -n "$NGINX_DOMAIN" ] && printf '%sHTTPS:certbot --nginx -d %s%s\n' "$c_dim" "$NGINX_DOMAIN" "$c_reset"
}
case "$MODE" in
uninstall) uninstall; exit 0 ;;
update) pull_code; ensure_node; ensure_data; [ "$NO_SERVICE" = 1 ] || write_unit; health || true; summary; exit 0 ;;
install) pull_code; ensure_node; ensure_data ;;
esac
if [ "$NO_SERVICE" = 1 ]; then
ok "代码已就位:${APP_DIR}"
echo " 手动启动:PORT=${PORT} DEVBOX_DATA=/var/lib/devbox ${NODE_BIN} ${APP_DIR}/server.mjs"
exit 0
fi
command -v systemctl >/dev/null 2>&1 || die "这台机器没有 systemd,请加 --no-service 只拉代码"
log "写入 systemd 服务 /etc/systemd/system/${SERVICE}.service"
write_unit
[ -n "$NGINX_DOMAIN" ] && write_nginx
health || true
summary