douyinWEB/scripts/linux/install_shellcrash.sh
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

285 lines
9.9 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.

#!/usr/bin/env bash
# Install + harden ShellCrash on douyinWEB physical guest (adapted from sh2/d2ypp2).
# Usage (as root):
# LIVE_HTTP_PROXY=http://192.168.21.36:7897 bash install_shellcrash.sh
# Clears LIVE_* / apt / docker proxy drop-ins in a trap on exit.
set -euo pipefail
SHELLCRASH_DIR="${SHELLCRASH_DIR:-/etc/ShellCrash}"
LIVE_HTTP_PROXY="${LIVE_HTTP_PROXY:-}"
LIVE_HTTPS_PROXY="${LIVE_HTTPS_PROXY:-${LIVE_HTTP_PROXY:-}}"
LIVE_NO_PROXY="${LIVE_NO_PROXY:-localhost,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,*.lan,*.local}"
# Keep WG + LAN able to hit :9999 / :7890 after ShellCrash nft rebuild.
SHELLCRASH_TRUSTED_CIDRS="${SHELLCRASH_TRUSTED_CIDRS:-10.8.0.0/24,172.16.0.0/12,192.168.0.0/16,10.0.0.0/8}"
STAGE_YAML="${STAGE_YAML:-}"
ENABLE_NOW="${ENABLE_NOW:-1}"
log() { printf '[install-shellcrash] %s\n' "$*"; }
cpu_core() {
case "$(uname -m)" in
x86_64|amd64) printf '%s' amd64 ;;
aarch64|arm64) printf '%s' arm64 ;;
armv7l|armv7*|armhf) printf '%s' armv7 ;;
*) printf '%s' "$(uname -m)" ;;
esac
}
cfg_default() {
local cfg="$1" key="$2" value="$3"
if ! grep -q "^${key}=" "$cfg" 2>/dev/null; then
printf '%s=%s\n' "$key" "$value" >>"$cfg"
fi
}
apply_proxy() {
if [ -z "${LIVE_HTTP_PROXY}${LIVE_HTTPS_PROXY}" ]; then
return 0
fi
export http_proxy="$LIVE_HTTP_PROXY" HTTP_PROXY="$LIVE_HTTP_PROXY"
export https_proxy="$LIVE_HTTPS_PROXY" HTTPS_PROXY="$LIVE_HTTPS_PROXY"
export no_proxy="$LIVE_NO_PROXY" NO_PROXY="$LIVE_NO_PROXY"
mkdir -p /etc/apt/apt.conf.d
cat >/etc/apt/apt.conf.d/99douyinweb-tmp-proxy <<EOF
Acquire::http::Proxy "${LIVE_HTTP_PROXY}";
Acquire::https::Proxy "${LIVE_HTTPS_PROXY}";
EOF
log "temp proxy on: $LIVE_HTTP_PROXY"
}
clear_proxy() {
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy no_proxy NO_PROXY || true
rm -f /etc/apt/apt.conf.d/99douyinweb-tmp-proxy 2>/dev/null || true
if [ -f /etc/systemd/system/docker.service.d/http-proxy.conf ]; then
# Only remove our temp file if we created the marker
if [ -f /tmp/.douyinweb_docker_proxy_marker ]; then
rm -f /etc/systemd/system/docker.service.d/http-proxy.conf
rm -f /tmp/.douyinweb_docker_proxy_marker
systemctl daemon-reload >/dev/null 2>&1 || true
fi
fi
log "temp proxy cleared"
}
trap clear_proxy EXIT
patch_runtime_yaml() {
local file="$1"
[ -s "$file" ] || return 1
python3 - "$file" <<'PY' || true
import sys
from pathlib import Path
path = Path(sys.argv[1])
lines = []
found_ec = found_mp = False
for line in path.read_text(encoding="utf-8", errors="replace").splitlines():
if line.startswith("global-client-fingerprint:"):
continue
if line.startswith("external-controller:"):
lines.append("external-controller: 0.0.0.0:9999")
found_ec = True
continue
if line.startswith("mixed-port:"):
lines.append("mixed-port: 7890")
found_mp = True
continue
lines.append(line)
if not found_ec:
lines.append("external-controller: 0.0.0.0:9999")
if not found_mp:
lines.append("mixed-port: 7890")
path.write_text("\n".join(lines).rstrip() + "\n", encoding="utf-8")
PY
}
ensure_geo() {
local js="https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release"
local name dest min sz
mkdir -p "$SHELLCRASH_DIR"
for pair in "geosite.dat:GeoSite.dat:1000000" "geoip.dat:GeoIP.dat:1000000" "geoip.metadb:Country.mmdb:1000000"; do
name="${pair%%:*}"
dest="$SHELLCRASH_DIR/${pair#*:}"
dest="${dest%:*}"
min="${pair##*:}"
if [ -s "$dest" ]; then
sz="$(wc -c <"$dest" | tr -d '[:space:]')"
case "$sz" in ''|*[!0-9]*) ;; *) [ "$sz" -ge "$min" ] && continue ;; esac
fi
log "geo $name"
if curl -fsSL --connect-timeout 20 --max-time 180 -o "$dest.tmp" "$js/$name"; then
mv -f "$dest.tmp" "$dest"
chmod 644 "$dest" || true
else
rm -f "$dest.tmp"
log "WARN: geo $name download failed"
fi
done
}
install_trusted_hook() {
mkdir -p /usr/local/sbin /etc/systemd/system/shellcrash.service.d
cat >/usr/local/sbin/shellcrash-allow-trusted.sh <<'EOS'
#!/bin/bash
set -euo pipefail
for i in 1 2 3 4 5 6 7 8 9 10 11 12; do
nft list table inet shellcrash >/dev/null 2>&1 && break
sleep 1
done
nft list table inet shellcrash >/dev/null 2>&1 || exit 0
cidrs="${SHELLCRASH_TRUSTED_CIDRS:-10.8.0.0/24,172.16.0.0/12,192.168.0.0/16,10.0.0.0/8}"
IFS=',; ' read -r -a arr <<<"$cidrs"
for cidr in "${arr[@]}"; do
case "$cidr" in ''|*[!0-9./]*) continue ;; esac
nft list chain inet shellcrash input 2>/dev/null | grep -Fq "ip saddr $cidr accept" && continue
nft insert rule inet shellcrash input ip saddr "$cidr" accept comment \"shellcrash-trusted\" 2>/dev/null || true
done
EOS
chmod +x /usr/local/sbin/shellcrash-allow-trusted.sh
cat >/etc/systemd/system/shellcrash.service.d/allow-trusted.conf <<EOF
[Service]
Environment=SHELLCRASH_TRUSTED_CIDRS=${SHELLCRASH_TRUSTED_CIDRS}
ExecStartPost=/bin/bash -c 'sleep 2; /usr/local/sbin/shellcrash-allow-trusted.sh'
EOF
cat >/etc/systemd/system/shellcrash.service.d/timeout.conf <<'EOF'
[Service]
TimeoutStartSec=600
EOF
}
harden() {
[ -d "$SHELLCRASH_DIR" ] || return 0
log "harden runtime"
mkdir -p "$SHELLCRASH_DIR/yamls" "$SHELLCRASH_DIR/configs" /tmp/ShellCrash
local cfg="$SHELLCRASH_DIR/configs/ShellCrash.cfg"
[ -f "$cfg" ] || printf '%s\n' "#ShellCrash配置文件,不明勿动!" >"$cfg"
cfg_default "$cfg" crashcore meta
if grep -q '^cpucore=' "$cfg" 2>/dev/null; then
sed -i "s/^cpucore=.*/cpucore=$(cpu_core)/" "$cfg" || true
else
cfg_default "$cfg" cpucore "$(cpu_core)"
fi
cfg_default "$cfg" redir_mod "Redir模式"
# Physical app host: 0 = no transparent hijack (WG / FRP / API stay intact).
# Explicit mixed-port :7890 still available for opt-in clients.
if grep -q '^firewall_area=' "$cfg" 2>/dev/null; then
sed -i 's/^firewall_area=.*/firewall_area=0/' "$cfg" || true
else
cfg_default "$cfg" firewall_area 0
fi
cfg_default "$cfg" dns_mod redir_host
cfg_default "$cfg" mix_port 7890
cfg_default "$cfg" redir_port 7892
cfg_default "$cfg" tproxy_port 7893
cfg_default "$cfg" db_port 9999
cfg_default "$cfg" dns_port 1053
cfg_default "$cfg" network_check OFF
cfg_default "$cfg" hostdir ":9999/ui"
install_trusted_hook
ensure_geo
cat >"$SHELLCRASH_DIR/configs/command.env" <<EOF
TMPDIR=/tmp/ShellCrash
BINDIR=$SHELLCRASH_DIR
COMMAND="\$TMPDIR/CrashCore -d \$BINDIR -f \$TMPDIR/config.yaml"
EOF
if systemctl list-unit-files shellcrash.service --no-legend 2>/dev/null | grep -q '^shellcrash.service'; then
local unit_path
unit_path="$(systemctl show -p FragmentPath shellcrash.service 2>/dev/null | sed 's/^FragmentPath=//')"
if [ -n "$unit_path" ] && [ -f "$unit_path" ]; then
sed -i "s#^ExecStart=.*#ExecStart=/tmp/ShellCrash/CrashCore -d $SHELLCRASH_DIR -f /tmp/ShellCrash/config.yaml#" "$unit_path" || true
fi
fi
systemctl daemon-reload >/dev/null 2>&1 || true
if [ -n "$STAGE_YAML" ] && [ -s "$STAGE_YAML" ]; then
install -m 0644 "$STAGE_YAML" "$SHELLCRASH_DIR/yamls/config.yaml"
fi
if [ -s "$SHELLCRASH_DIR/yamls/config.yaml" ]; then
patch_runtime_yaml "$SHELLCRASH_DIR/yamls/config.yaml" || true
install -m 0644 "$SHELLCRASH_DIR/yamls/config.yaml" /tmp/ShellCrash/config.yaml
patch_runtime_yaml /tmp/ShellCrash/config.yaml || true
fi
# eric (ops) write access for UI/API apply later
chgrp -R eric "$SHELLCRASH_DIR/yamls" "$SHELLCRASH_DIR/configs" 2>/dev/null || true
chmod -R g+rwX "$SHELLCRASH_DIR/yamls" "$SHELLCRASH_DIR/configs" 2>/dev/null || true
}
download_installer() {
local url target
rm -f /tmp/install_shellcrash.sh
for url in \
'https://testingcf.jsdelivr.net/gh/juewuy/ShellCrash@dev' \
'https://raw.githubusercontent.com/juewuy/ShellCrash/dev'
do
target="$url/install_en.sh"
log "try $target"
if command -v wget >/dev/null 2>&1; then
wget -q --no-check-certificate -O /tmp/install_shellcrash.sh "$target" && [ -s /tmp/install_shellcrash.sh ] && return 0
env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY \
wget -q --no-check-certificate -O /tmp/install_shellcrash.sh "$target" && [ -s /tmp/install_shellcrash.sh ] && return 0
fi
if command -v curl >/dev/null 2>&1; then
curl -fsSL -o /tmp/install_shellcrash.sh "$target" && [ -s /tmp/install_shellcrash.sh ] && return 0
curl -fsSL --noproxy '*' -o /tmp/install_shellcrash.sh "$target" && [ -s /tmp/install_shellcrash.sh ] && return 0
fi
done
return 1
}
main() {
if [ "$(id -u)" -ne 0 ]; then
log "need root"
exit 1
fi
apply_proxy
if [ -x "$SHELLCRASH_DIR/start.sh" ]; then
log "already installed at $SHELLCRASH_DIR"
else
log "installing ShellCrash arch=$(cpu_core)"
if ! download_installer; then
log "FAIL: cannot download install_en.sh (set LIVE_HTTP_PROXY)"
exit 2
fi
# Upstream interactive menu: accept defaults (option 1 x4)
set +e
printf '1\n1\n1\n1\n' | bash /tmp/install_shellcrash.sh
rc=$?
set -e
if [ "$rc" -ne 0 ]; then
log "WARN: install_en.sh exit=$rc — checking tree"
fi
if [ ! -d "$SHELLCRASH_DIR" ] && [ ! -x /etc/ShellCrash/start.sh ]; then
log "FAIL: ShellCrash dir missing after install"
exit 3
fi
fi
harden
if [ "$ENABLE_NOW" = "1" ]; then
if [ ! -s /tmp/ShellCrash/config.yaml ]; then
log "WARN: no config.yaml staged — unit may fail until YAML applied"
fi
systemctl reset-failed shellcrash.service 2>/dev/null || true
systemctl unmask shellcrash.service 2>/dev/null || true
systemctl enable --now shellcrash.service 2>/dev/null || {
if [ -x "$SHELLCRASH_DIR/start.sh" ]; then
"$SHELLCRASH_DIR/start.sh" start || true
fi
}
sleep 3
fi
log "status: $(systemctl is-active shellcrash.service 2>/dev/null || echo n/a)"
log "dir: $SHELLCRASH_DIR"
ls -la "$SHELLCRASH_DIR/start.sh" /tmp/ShellCrash/CrashCore 2>/dev/null || true
ss -ltn 2>/dev/null | grep -E ':(7890|9999)\s' || true
# trap clears proxy
}
main "$@"