-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstart.sh
More file actions
147 lines (127 loc) · 3.94 KB
/
Copy pathstart.sh
File metadata and controls
147 lines (127 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
set -uo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
cd "${SCRIPT_DIR}"
pick_python() {
if command -v python3 >/dev/null 2>&1; then
echo "python3"
return 0
fi
if command -v python >/dev/null 2>&1; then
echo "python"
return 0
fi
return 1
}
install_requirements() {
local py_bin="$1"
"${py_bin}" -m pip install --disable-pip-version-check -r requirements.txt && return 0
"${py_bin}" -m pip install --user --disable-pip-version-check -r requirements.txt && return 0
"${py_bin}" -m ensurepip --upgrade >/dev/null 2>&1 || true
"${py_bin}" -m pip install --disable-pip-version-check -r requirements.txt && return 0
"${py_bin}" -m pip install --user --disable-pip-version-check -r requirements.txt && return 0
return 1
}
if ! PYTHON_SYSTEM_BIN="$(pick_python)"; then
echo "Python was not found. Install Python 3.10+ and relaunch."
read -r -p "Press Enter to close..."
exit 1
fi
# Check if a virtual environment is already active
if [[ -n "${VIRTUAL_ENV:-}" ]]; then
echo "Using already active virtual environment: ${VIRTUAL_ENV}"
PYTHON_BIN="python"
else
# Detect existing venv folders (checks both venv and .venv)
VENV_DIR=""
if [[ -f "venv/bin/activate" ]]; then
VENV_DIR="venv"
elif [[ -f ".venv/bin/activate" ]]; then
VENV_DIR=".venv"
fi
if [[ -n "${VENV_DIR}" ]]; then
echo "Found existing virtual environment: ${VENV_DIR}"
# shellcheck source=/dev/null
source "${VENV_DIR}/bin/activate"
PYTHON_BIN="python"
else
# Clean up incomplete venv folder if present (missing activation script)
if [[ -d "venv" ]]; then
echo "Detected incomplete venv folder. Cleaning it up..."
rm -rf "venv"
fi
echo "Creating Python virtual environment (venv)..."
if "${PYTHON_SYSTEM_BIN}" -m venv venv; then
# shellcheck source=/dev/null
source "venv/bin/activate"
PYTHON_BIN="python"
else
echo "Warning: Failed to create virtual environment via '${PYTHON_SYSTEM_BIN} -m venv venv'."
echo "Please make sure 'python3-venv' package is installed (e.g., 'sudo apt install python3-venv')."
echo "Falling back to using system Python environment..."
PYTHON_BIN="${PYTHON_SYSTEM_BIN}"
fi
fi
fi
if ! "${PYTHON_BIN}" - <<'PY'
import requests
import socks
import rich
PY
then
echo "Missing required Python packages in the virtual environment. Installing from requirements.txt..."
if ! install_requirements "${PYTHON_BIN}"; then
echo
echo "Failed to install required packages automatically."
echo "Please ensure you have active internet access and 'pip' is installed."
echo "You can also install manually inside the virtual environment: source venv/bin/activate && pip install -r requirements.txt"
read -r -p "Press Enter to close..."
exit 1
fi
fi
if ! "${PYTHON_BIN}" - <<'PY'
from sni_finder.settings import load_settings
import sys
settings = load_settings()
sys.exit(0 if str(getattr(settings, "vless_source", "")).strip() else 1)
PY
then
echo "Starting first-time setup wizard..."
if ! "${PYTHON_BIN}" scanner.py onboarding; then
echo
echo "Setup was cancelled or failed."
echo "Log file: logs/scanner.log"
read -r -p "Press Enter to close..."
exit 1
fi
if ! "${PYTHON_BIN}" - <<'PY'
from sni_finder.settings import load_settings
import sys
settings = load_settings()
sys.exit(0 if str(getattr(settings, "vless_source", "")).strip() else 1)
PY
then
echo
echo "vless_source is still empty. Please set it and relaunch."
echo "Log file: logs/scanner.log"
read -r -p "Press Enter to close..."
exit 1
fi
fi
if command -v clear >/dev/null 2>&1; then
clear
fi
if ! "${PYTHON_BIN}" scanner.py; then
EXIT_CODE=$?
else
EXIT_CODE=0
fi
echo
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo "Scanner exited with an error. Code=${EXIT_CODE}"
else
echo "Scanner closed."
fi
echo "Log file: logs/scanner.log"
read -r -p "Press Enter to close..."
exit ${EXIT_CODE}