Skip to content

Commit fbdad32

Browse files
committed
Fix Xray backend: auto-detect HTTPS and base path for 3x-ui v2.0+
3x-ui v2.0+ uses HTTPS and random base paths by default, causing "Could not connect to 3x-ui panel" errors. Now: - Tries 4 URL combinations: HTTP/HTTPS with/without base path - Adds base path detection from x-ui setting -show output - Strips whitespace from base path to prevent malformed URLs - Validates port is numeric before using in URL - Better error message with diagnostic hints
1 parent d96b8d5 commit fbdad32

1 file changed

Lines changed: 46 additions & 13 deletions

File tree

dnstm-setup.sh

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,6 +2508,10 @@ detect_xray_panel() {
25082508
XRAY_PANEL_PORT=$(sqlite3 /etc/x-ui/x-ui.db "SELECT value FROM settings WHERE key='webPort'" 2>/dev/null || true)
25092509
fi
25102510
fi
2511+
# Validate port is numeric
2512+
if [[ -n "$XRAY_PANEL_PORT" && ! "$XRAY_PANEL_PORT" =~ ^[0-9]+$ ]]; then
2513+
XRAY_PANEL_PORT=""
2514+
fi
25112515

25122516
# Method 3: Try common 3x-ui ports (skip 443 — too likely to be nginx)
25132517
if [[ -z "$XRAY_PANEL_PORT" ]]; then
@@ -2522,16 +2526,27 @@ detect_xray_panel() {
25222526
# Method 4: Fall back to default
25232527
XRAY_PANEL_PORT="${XRAY_PANEL_PORT:-2053}"
25242528

2525-
# Detect web base path (many users set this for security)
2529+
# Detect web base path (3x-ui v2.0+ sets a random base path by default)
25262530
XRAY_PANEL_BASEPATH=""
2531+
# Method 1: Parse config.json
25272532
if [[ -f /usr/local/x-ui/config.json ]]; then
25282533
XRAY_PANEL_BASEPATH=$(jq -r '.webBasePath // empty' /usr/local/x-ui/config.json 2>/dev/null || true)
25292534
fi
2535+
# Method 2: Query sqlite database
25302536
if [[ -z "$XRAY_PANEL_BASEPATH" ]] && command -v sqlite3 &>/dev/null && [[ -f /etc/x-ui/x-ui.db ]]; then
25312537
XRAY_PANEL_BASEPATH=$(sqlite3 /etc/x-ui/x-ui.db "SELECT value FROM settings WHERE key='webBasePath'" 2>/dev/null || true)
25322538
fi
2533-
# Normalize: strip leading/trailing slashes
2534-
XRAY_PANEL_BASEPATH=$(echo "${XRAY_PANEL_BASEPATH:-}" | sed 's|^/||;s|/$||')
2539+
# Method 3: Try to read from x-ui process environment or binary output
2540+
if [[ -z "$XRAY_PANEL_BASEPATH" ]]; then
2541+
# Some 3x-ui versions expose base path in config output
2542+
local xui_config_output
2543+
xui_config_output=$(x-ui setting -show 2>/dev/null || true)
2544+
if [[ -n "$xui_config_output" ]]; then
2545+
XRAY_PANEL_BASEPATH=$(echo "$xui_config_output" | grep -i 'webBasePath\|basePath' | sed 's/.*:[[:space:]]*//' | head -1 || true)
2546+
fi
2547+
fi
2548+
# Normalize: strip whitespace and leading/trailing slashes
2549+
XRAY_PANEL_BASEPATH=$(echo "${XRAY_PANEL_BASEPATH:-}" | sed 's|^[[:space:]]*||;s|[[:space:]]*$||;s|^/||;s|/$||')
25352550
fi
25362551
}
25372552

@@ -2645,8 +2660,6 @@ pick_xray_port() {
26452660
create_3xui_inbound() {
26462661
local base_segment=""
26472662
[[ -n "${XRAY_PANEL_BASEPATH:-}" ]] && base_segment="/${XRAY_PANEL_BASEPATH}"
2648-
# Auto-detect HTTPS: try connecting, if http fails with empty response, try https
2649-
local panel_url="http://127.0.0.1:${XRAY_PANEL_PORT}${base_segment}"
26502663
local cookie_jar
26512664
cookie_jar=$(mktemp)
26522665
chmod 600 "$cookie_jar" 2>/dev/null || true
@@ -2663,18 +2676,38 @@ create_3xui_inbound() {
26632676
XRAY_PASSWORD=$(openssl rand -hex 16)
26642677
fi
26652678

2666-
# Login to panel
2679+
# Auto-detect panel URL: try http, then https, with and without base path
26672680
print_info "Logging in to 3x-ui panel..."
2668-
local login_resp
2669-
login_resp=$(curl -s -L -k -c "$cookie_jar" -X POST "${panel_url}/login" \
2670-
-H "Content-Type: application/x-www-form-urlencoded" \
2671-
--data-urlencode "username=${XRAY_ADMIN_USER}" \
2672-
--data-urlencode "password=${XRAY_ADMIN_PASS}" \
2673-
--max-time 10 2>/dev/null || true)
2681+
local panel_url=""
2682+
local login_resp=""
2683+
local try_urls=()
2684+
2685+
# Build list of URLs to try (with base path first, then without)
2686+
if [[ -n "$base_segment" ]]; then
2687+
try_urls+=("http://127.0.0.1:${XRAY_PANEL_PORT}${base_segment}")
2688+
try_urls+=("https://127.0.0.1:${XRAY_PANEL_PORT}${base_segment}")
2689+
fi
2690+
try_urls+=("http://127.0.0.1:${XRAY_PANEL_PORT}")
2691+
try_urls+=("https://127.0.0.1:${XRAY_PANEL_PORT}")
2692+
2693+
for try_url in "${try_urls[@]}"; do
2694+
login_resp=$(curl -s -L -k -c "$cookie_jar" -X POST "${try_url}/login" \
2695+
-H "Content-Type: application/x-www-form-urlencoded" \
2696+
--data-urlencode "username=${XRAY_ADMIN_USER}" \
2697+
--data-urlencode "password=${XRAY_ADMIN_PASS}" \
2698+
--max-time 5 2>/dev/null || true)
2699+
if [[ -n "$login_resp" ]]; then
2700+
panel_url="$try_url"
2701+
break
2702+
fi
2703+
# Clear cookie jar between attempts
2704+
: > "$cookie_jar"
2705+
done
26742706

26752707
if [[ -z "$login_resp" ]]; then
2676-
print_fail "Could not connect to 3x-ui panel at ${panel_url}"
2708+
print_fail "Could not connect to 3x-ui panel on port ${XRAY_PANEL_PORT}"
26772709
print_info "Is the panel running? Check: systemctl status x-ui"
2710+
print_info "If your panel has a custom base path, check: sqlite3 /etc/x-ui/x-ui.db \"SELECT value FROM settings WHERE key='webBasePath'\""
26782711
return 1
26792712
fi
26802713

0 commit comments

Comments
 (0)