Files
zapret-discord-youtube-linux/autotest.sh
OpenCode Agent 62ec6c5749 fix(core): critical fixes for service menu, autotest, strategy runner, dependencies
- service.sh: split service_remove into internal (no prompts) and interactive
  versions to prevent 'eternal wait' when installing new strategy.
  Install now calls service_remove_internal > /dev/null instead of service_remove.
  Also filter strategy list to general*.sh only to avoid clutter.
  Systemd ExecStart now points to run_strategy.sh <name> consistently.

- run_strategy.sh: add SIGTERM/SIGINT trap cleanup_strategy() that kills nfqws
  and cleans up firewall. Prevents stale nfqws/firewall rules after autotest kill.
  Also handles nfqws exit gracefully with final cleanup.

- autotest.sh: rewritten to test strategies by config name (not wrapper filename).
  Stop now sends SIGTERM to wrapper (which triggers trap cleanup) instead of
  bare kill. Added extra sleep after stop to let trap fire.
  Auto-install ExecStart fixed to run_strategy.sh <strategy>.

- setup.sh: added libmnl-dev and zlib1g-dev to Ubuntu/Debian dependency install
  to prevent build failures (missing libmnl/libmnl.h and zlib.h).

- general*.sh: removed UTF-8 BOM (0xEF 0xBB 0xBF) that caused 'exec format error'
  when running scripts on Linux. All 19 wrappers cleaned.

- ensure_wrappers.sh: always rewrite wrappers to ensure no stale BOM or paths.

Fixes: eternal wait on menu option 1, nfqws build failure, stale processes after test.
2026-05-10 22:47:48 +04:00

272 lines
7.5 KiB
Bash
Executable File

#!/bin/bash
# autotest.sh - Automatic strategy tester v2
# Iterates through all strategies, checks connectivity, suggests installation
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/functions.sh"
# Test targets (must be accessible when bypass works)
TEST_URLS=(
"https://www.youtube.com|YouTube"
"https://discord.com|Discord"
"https://discordapp.com|DiscordApp"
)
# Strategy order (most common first)
STRATEGIES=(
general
ALT
ALT2
ALT3
ALT4
ALT6
ALT7
ALT8
ALT9
ALT10
ALT11
FAKE_TLS_AUTO
FAKE_TLS_AUTO_ALT
FAKE_TLS_AUTO_ALT2
FAKE_TLS_AUTO_ALT3
SIMPLE_FAKE
SIMPLE_FAKE_ALT
SIMPLE_FAKE_ALT2
ALT5 # NOT RECOMMENDED, last
)
# Test a single URL
test_url() {
local url="$1"
local timeout=8
local code
code=$(curl -o /dev/null -s -w "%{http_code}" --max-time "$timeout" -L "$url" 2>/dev/null)
if [[ "$code" == "200" ]] || [[ "$code" == "301" ]] || [[ "$code" == "302" ]] || [[ "$code" == "307" ]]; then
return 0
fi
return 1
}
# Test all targets for a strategy
test_strategy() {
local passed=0
local total=${#TEST_URLS[@]}
echo " [*] Testing connectivity..."
for entry in "${TEST_URLS[@]}"; do
IFS='|' read -r url name <<< "$entry"
if test_url "$url"; then
echo " $(print_green "[OK]") $name"
((passed++))
else
echo " $(print_red "[FAIL]") $name"
fi
done
echo " [*] Result: $passed/$total passed"
if [[ "$passed" -eq "$total" ]]; then
return 0
fi
return 1
}
# Run a strategy with timeout
run_strategy_test() {
local strategy="$1"
local strategy_path="$SCRIPT_DIR/run_strategy.sh"
local strategy_name="$strategy"
echo ""
echo "=============================================="
echo " Testing: $strategy_name"
echo " $(describe_strategy "$SCRIPT_DIR/general_${strategy}.sh")"
echo "=============================================="
# Check if strategy config exists
if [[ ! -f "$SCRIPT_DIR/strategies/${strategy}.conf" ]]; then
print_yellow " [!] Strategy config not found, skipping..."
return 1
fi
# Ensure clean firewall state before starting
cleanup_firewall >/dev/null 2>&1 || true
sleep 1
# Start strategy in background (run_strategy.sh handles its own cleanup on signals)
echo " [*] Starting $strategy_name..."
"$strategy_path" "$strategy" >/dev/null 2>&1 &
local strategy_pid=$!
# Wait for nfqws to initialize
sleep 3
# Check if nfqws actually started (not just the wrapper)
if ! pgrep -f "nfqws.*qnum=$NFQUEUE_NUM" >/dev/null 2>&1; then
print_yellow " [!] nfqws did not start for $strategy_name, skipping..."
# Kill wrapper if still alive
kill "$strategy_pid" 2>/dev/null || true
wait "$strategy_pid" 2>/dev/null || true
cleanup_firewall >/dev/null 2>&1 || true
sleep 1
return 1
fi
# Run connectivity tests
local success=0
if test_strategy; then
success=1
fi
# Stop strategy
echo " [*] Stopping $strategy_name..."
# Send SIGTERM to wrapper — it will cleanup firewall + nfqws via trap
kill -TERM "$strategy_pid" 2>/dev/null || true
# Give trap time to fire and cleanup
sleep 2
# Ensure nfqws is gone
pkill -f "nfqws.*qnum=$NFQUEUE_NUM" 2>/dev/null || true
wait "$strategy_pid" 2>/dev/null || true
cleanup_firewall >/dev/null 2>&1 || true
sleep 1
if [[ "$success" -eq 1 ]]; then
return 0
fi
return 1
}
# Main autotest logic
main() {
local auto_install="${1:-}"
echo ""
echo "=============================================="
echo " Zapret Auto-Test v$LOCAL_VERSION"
echo " Will test ${#STRATEGIES[@]} strategies"
echo "=============================================="
echo ""
# Check prerequisites
if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
print_red "[!] This script must be run as root (or with sudo)"
echo " sudo ./autotest.sh"
exit 1
fi
check_nfqws || {
echo ""
echo "[*] nfqws not found. Building first..."
bash "$SCRIPT_DIR/install_nfqws.sh"
}
echo "[*] Test targets:"
for entry in "${TEST_URLS[@]}"; do
IFS='|' read -r url name <<< "$entry"
echo " - $name ($url)"
done
echo ""
if [[ "$auto_install" != "--auto" ]]; then
read -rp "Press Enter to start testing... or Ctrl+C to cancel"
fi
# Ensure clean state
cleanup_firewall >/dev/null 2>&1 || true
sleep 1
local working_strategy=""
local working_desc=""
for strat in "${STRATEGIES[@]}"; do
if run_strategy_test "$strat"; then
working_strategy="$strat"
working_desc="$(describe_strategy "$SCRIPT_DIR/general_${strat}.sh")"
break
fi
done
echo ""
echo "=============================================="
if [[ -n "$working_strategy" ]]; then
print_green " WORKING STRATEGY FOUND!"
print_green " Strategy: $working_strategy"
print_green " Desc: $working_desc"
echo "=============================================="
echo ""
if [[ "$auto_install" == "--auto" ]]; then
echo "[*] Auto-installing to systemd..."
# Write strategy marker directly
mkdir -p "$SCRIPT_DIR/.service"
echo "$working_strategy" > "$SCRIPT_DIR/.service/installed_strategy"
local service_path="$SYSTEMD_DIR/zapret.service"
cat > "$service_path" <<EOF
[Unit]
Description=Zapret DPI bypass (strategy: $working_strategy)
After=network.target
[Service]
Type=simple
ExecStart=$SCRIPT_DIR/run_strategy.sh $working_strategy
Restart=on-failure
RestartSec=5
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
cp "$service_path" /etc/systemd/system/zapret.service
systemctl daemon-reload
systemctl enable zapret.service
systemctl start zapret.service
print_green "[OK] Installed and started!"
echo ""
systemctl status zapret --no-pager
else
echo "[*] Next steps:"
echo " 1. Test in browser: open youtube.com and discord.com"
echo " 2. If everything works, install it:"
echo ""
echo " sudo ./service.sh"
echo " → 1. Install Service"
echo " → Choose: $working_strategy"
echo ""
read -rp "Install this strategy to systemd now? [Y/n]: " ans
if [[ "${ans:-Y}" == [yY]* ]]; then
bash "$SCRIPT_DIR/service.sh"
fi
fi
else
print_red " NO WORKING STRATEGY FOUND"
echo "=============================================="
echo ""
echo "[*] Suggestions:"
echo " - Try adding custom domains to lists/list-general-user.txt"
echo " - Check that Secure DNS is enabled"
echo " - Try modifying strategy parameters manually in strategies/*.conf"
echo " - Check diagnostics: sudo ./service.sh → 10. Run Diagnostics"
fi
echo ""
echo "[*] Auto-test complete."
echo ""
}
# Entry point
case "${1:-interactive}" in
--auto|-a)
main --auto
;;
interactive|"")
main interactive
;;
*)
echo "Usage: sudo ./autotest.sh [--auto|-a]"
exit 1
;;
esac