#!/bin/bash set -e # ----------------------------- # Variablen anpassen # ----------------------------- REPO_URL="https://gitea.malmert.de/marcus/mitm-webserver.git" APP_DIR="/opt/mitm-webserver" FRONTEND_DIR="$APP_DIR/frontend" BACKEND_DIR="$APP_DIR/backend" BACKEND_SERVICE="mitm-backend" NGINX_SITE="/etc/nginx/sites-available/mitm-webserver" USER_ROOT="root" GITEA_RUNNER_URL="https://gitea.malmert.de//api/v1/repos/marcus/mitm-webserver/actions/runners/register" RUNNER_TOKEN="cThC2xmAZWaOAqRRMENuVVTJckxaHiJxVGx2NCQY" RUNNER_NAME="mitm-runner" NODE_VERSION="lts/*" # aktuelle LTS Version PYTHON_VERSION="3" # aktuelle Python 3 Version # ----------------------------- # System aktualisieren & Pakete installieren # ----------------------------- echo "==> Update & Upgrade" apt update && apt upgrade -y apt install -y git curl build-essential nginx python3 python3-pip python3-venv unzip wget python3-dev \ libnetfilter-queue-dev libnfnetlink-dev libpcap-dev autoconf automake libtool pkg-config libjson-c-dev \ gettext flex bison libnuma-dev libpcre2-dev libmaxminddb-dev librrd-dev # ----------------------------- # Node.js installieren (LTS) # ----------------------------- echo "==> Install Node.js LTS" curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - apt install -y nodejs # ----------------------------- # Repository clonen / pull # ----------------------------- echo "==> Setup repository" if [ ! -d "$APP_DIR" ]; then git clone "$REPO_URL" "$APP_DIR" else cd "$APP_DIR" git reset --hard git pull fi # ----------------------------- # Frontend bauen # ----------------------------- echo "==> Build Frontend" cd "$FRONTEND_DIR" npm install npm run build # ----------------------------- # Backend vorbereiten # ----------------------------- echo "==> Setup Backend" cd "$BACKEND_DIR" python3 -m venv venv source venv/bin/activate pip install --upgrade pip # ----------------------------- # nDPI installieren (system lib + python bindings) # ----------------------------- NDPIDIR="/tmp/nDPI" patch_ndpi_pack_on_if_needed() { local py_dir="$1" if [ -f "$py_dir/pyproject.toml" ] && grep -q "PACK_ON" "$py_dir/pyproject.toml"; then sed -i 's/PACK_ON/"0.0.0"/g' "$py_dir/pyproject.toml" echo "==> Applied PACK_ON workaround in pyproject.toml" fi if [ -f "$py_dir/setup.cfg" ] && grep -q "PACK_ON" "$py_dir/setup.cfg"; then sed -i 's/PACK_ON/0.0.0/g' "$py_dir/setup.cfg" echo "==> Applied PACK_ON workaround in setup.cfg" fi if [ -f "$py_dir/setup.py" ] && grep -q "PACK_ON" "$py_dir/setup.py"; then sed -i "s/PACK_ON/'0.0.0'/g" "$py_dir/setup.py" echo "==> Applied PACK_ON workaround in setup.py" fi } if ! ldconfig -p | grep -q "libndpi"; then echo "==> Build and install nDPI library" rm -rf "$NDPIDIR" git clone --depth 1 https://github.com/ntop/nDPI.git "$NDPIDIR" cd "$NDPIDIR" ./autogen.sh ./configure make -j"$(nproc)" make install ldconfig else echo "==> nDPI library already installed" # Keep python bindings source in sync with official instructions. if [ ! -d "$NDPIDIR/python" ]; then rm -rf "$NDPIDIR" git clone --depth 1 https://github.com/ntop/nDPI.git "$NDPIDIR" fi fi if [ -d "$NDPIDIR/python" ]; then echo "==> Install nDPI Python bindings" cd "$NDPIDIR/python" python3 -m pip install --upgrade pip if ! python3 -m pip install -r dev_requirements.txt; then echo "==> WARNING: failed to install nDPI python dev_requirements" fi if ! python3 -m pip install .; then echo "==> Initial nDPI pip install failed; retrying with PACK_ON workaround" patch_ndpi_pack_on_if_needed "$NDPIDIR/python" if ! python3 -m pip install .; then echo "==> WARNING: nDPI Python bindings install failed, backend will run without DPI enrichment" fi fi if python - <<'PY' import importlib mods = [] for name in ("ndpi", "pyndpi", "nDPI"): try: m = importlib.import_module(name) mods.append((name, getattr(m, "__file__", "unknown"))) except Exception: pass if not mods: raise SystemExit(1) print("nDPI Python modules:", mods) PY then echo "==> nDPI Python import check passed" else echo "==> WARNING: nDPI installed but import failed in backend venv" fi cd "$BACKEND_DIR" fi cd "$BACKEND_DIR" pip install -r requirements.txt deactivate # ----------------------------- # Systemd Service für Backend # ----------------------------- echo "==> Setup Systemd Service" cat >/etc/systemd/system/$BACKEND_SERVICE.service < Configure Nginx" cat >$NGINX_SITE < Entferne Nginx Default-Site" sudo rm -f "$DEFAULT_SITE" fi nginx -t systemctl restart nginx # ----------------------------- # Gitea Act Runner installieren # ----------------------------- echo "==> Install Gitea Act Runner" RUNNER_DIR="/opt/gitea-act-runner" mkdir -p "$RUNNER_DIR" cd "$RUNNER_DIR" # Binary herunterladen GITEA_RUNNER_BIN="act_runner-0.2.13-linux-amd64" wget -O $GITEA_RUNNER_BIN "https://gitea.com/gitea/act_runner/releases/download/v0.2.13/act_runner-0.2.13-linux-amd64" chmod +x $GITEA_RUNNER_BIN mv $GITEA_RUNNER_BIN act_runner # Runner registrieren ./act_runner register --instance "https://gitea.malmert.de/" --token "$RUNNER_TOKEN" --name "$RUNNER_NAME" --labels "self-hosted:host" --no-interactive # Systemd-Service einrichten cat >/etc/systemd/system/gitea-act-runner.service < Gitea Act Runner installiert & gestartet" # ----------------------------- # Gitea Runner Job Script # ----------------------------- echo "==> Setup auto-build script" BUILD_SCRIPT="$RUNNER_DIR/build.sh" cat >$BUILD_SCRIPT <<'EOL' #!/bin/bash set -e APP_DIR="/opt/mitm-webserver" FRONTEND_DIR="$APP_DIR/frontend" BACKEND_DIR="$APP_DIR/backend" BACKEND_SERVICE="mitm-backend" patch_ndpi_pack_on_if_needed() { local py_dir="$1" if [ -f "$py_dir/pyproject.toml" ] && grep -q "PACK_ON" "$py_dir/pyproject.toml"; then sed -i 's/PACK_ON/"0.0.0"/g' "$py_dir/pyproject.toml" fi if [ -f "$py_dir/setup.cfg" ] && grep -q "PACK_ON" "$py_dir/setup.cfg"; then sed -i 's/PACK_ON/0.0.0/g' "$py_dir/setup.cfg" fi if [ -f "$py_dir/setup.py" ] && grep -q "PACK_ON" "$py_dir/setup.py"; then sed -i "s/PACK_ON/'0.0.0'/g" "$py_dir/setup.py" fi } cd $APP_DIR git reset --hard git pull # Build Frontend cd $FRONTEND_DIR npm install npm run build # Backend Dependencies cd $BACKEND_DIR source venv/bin/activate NDPIDIR="/tmp/nDPI" if [ ! -d "$NDPIDIR/python" ]; then rm -rf "$NDPIDIR" git clone --depth 1 https://github.com/ntop/nDPI.git "$NDPIDIR" fi if [ -d "/tmp/nDPI/python" ]; then cd "$NDPIDIR/python" python3 -m pip install --upgrade pip python3 -m pip install -r dev_requirements.txt || true if ! python3 -m pip install .; then echo "WARNING: Initial nDPI install failed during runner build; retry with PACK_ON workaround" patch_ndpi_pack_on_if_needed "$NDPIDIR/python" if ! python3 -m pip install .; then echo "WARNING: nDPI Python bindings install failed during runner build" fi fi python - <<'PY' || echo "WARNING: nDPI import check failed during runner build" import importlib for name in ("ndpi", "pyndpi", "nDPI"): try: m = importlib.import_module(name) print("nDPI import OK:", name, getattr(m, "__file__", "unknown")) raise SystemExit(0) except Exception: pass raise SystemExit(1) PY cd $BACKEND_DIR fi pip install -r requirements.txt deactivate # Restart backend systemctl restart $BACKEND_SERVICE EOL chmod +x $BUILD_SCRIPT echo "==> Setup complete! Gitea Runner will automatically build on push." echo "Frontend: http:///" echo "Backend API: http:///api/"