Skip to content

Commit 054e551

Browse files
committed
Fix 3x-ui web base path, cookie_jar trap leak, add-domain multi mode
1. Auto-detect 3x-ui web base path from config.json and x-ui.db. Users with custom base paths (e.g., /panel/) could not connect because the script hardcoded API URLs without the base path prefix. 2. Add -L (follow redirects) and -k (accept self-signed certs) to curl calls for 3x-ui API, supporting HTTPS-enabled panels. 3. Fix cookie_jar RETURN trap leaking from create_3xui_inbound into caller do_add_xray, causing "unbound variable" crash under set -u. 4. Enforce multi mode in do_add_domain — if router was in single mode, only the first domain's tunnels were routed. Now checks and switches to multi mode before creating tunnels.
1 parent 4ea0807 commit 054e551

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

dnstm-setup.sh

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,17 @@ detect_xray_panel() {
23352335

23362336
# Method 4: Fall back to default
23372337
XRAY_PANEL_PORT="${XRAY_PANEL_PORT:-2053}"
2338+
2339+
# Detect web base path (many users set this for security)
2340+
XRAY_PANEL_BASEPATH=""
2341+
if [[ -f /usr/local/x-ui/config.json ]]; then
2342+
XRAY_PANEL_BASEPATH=$(jq -r '.webBasePath // empty' /usr/local/x-ui/config.json 2>/dev/null || true)
2343+
fi
2344+
if [[ -z "$XRAY_PANEL_BASEPATH" ]] && command -v sqlite3 &>/dev/null && [[ -f /etc/x-ui/x-ui.db ]]; then
2345+
XRAY_PANEL_BASEPATH=$(sqlite3 /etc/x-ui/x-ui.db "SELECT value FROM settings WHERE key='webBasePath'" 2>/dev/null || true)
2346+
fi
2347+
# Normalize: strip leading/trailing slashes
2348+
XRAY_PANEL_BASEPATH=$(echo "${XRAY_PANEL_BASEPATH:-}" | sed 's|^/||;s|/$||')
23382349
fi
23392350
}
23402351

@@ -2446,13 +2457,16 @@ pick_xray_port() {
24462457
# Requires: XRAY_ADMIN_USER, XRAY_ADMIN_PASS, XRAY_PANEL_PORT, XRAY_PROTOCOL, XRAY_INBOUND_PORT
24472458
# Sets: XRAY_UUID (for vless/vmess) or XRAY_PASSWORD (for ss/trojan)
24482459
create_3xui_inbound() {
2449-
local panel_url="http://127.0.0.1:${XRAY_PANEL_PORT}"
2460+
local base_segment=""
2461+
[[ -n "${XRAY_PANEL_BASEPATH:-}" ]] && base_segment="/${XRAY_PANEL_BASEPATH}"
2462+
# Auto-detect HTTPS: try connecting, if http fails with empty response, try https
2463+
local panel_url="http://127.0.0.1:${XRAY_PANEL_PORT}${base_segment}"
24502464
local cookie_jar
24512465
cookie_jar=$(mktemp)
24522466
chmod 600 "$cookie_jar" 2>/dev/null || true
24532467

24542468
# Ensure cookie jar is cleaned up on any exit path
2455-
trap 'rm -f "$cookie_jar"' RETURN
2469+
trap 'rm -f "${cookie_jar:-}"; trap - RETURN' RETURN
24562470

24572471
# Generate credentials for the inbound
24582472
XRAY_UUID=""
@@ -2466,7 +2480,7 @@ create_3xui_inbound() {
24662480
# Login to panel
24672481
print_info "Logging in to 3x-ui panel..."
24682482
local login_resp
2469-
login_resp=$(curl -s -c "$cookie_jar" -X POST "${panel_url}/login" \
2483+
login_resp=$(curl -s -L -k -c "$cookie_jar" -X POST "${panel_url}/login" \
24702484
-H "Content-Type: application/x-www-form-urlencoded" \
24712485
--data-urlencode "username=${XRAY_ADMIN_USER}" \
24722486
--data-urlencode "password=${XRAY_ADMIN_PASS}" \
@@ -2548,7 +2562,7 @@ create_3xui_inbound() {
25482562
}')
25492563

25502564
local create_resp
2551-
create_resp=$(curl -s -b "$cookie_jar" -X POST "${panel_url}/panel/api/inbounds/add" \
2565+
create_resp=$(curl -s -L -k -b "$cookie_jar" -X POST "${panel_url}/panel/api/inbounds/add" \
25522566
-H "Content-Type: application/json" \
25532567
-d "$inbound_data" \
25542568
--max-time 10 2>/dev/null || true)
@@ -2947,7 +2961,9 @@ do_add_xray() {
29472961
;;
29482962
esac
29492963
else
2950-
print_ok "Detected: 3x-ui (port ${XRAY_PANEL_PORT})"
2964+
local _detect_msg="Detected: 3x-ui (port ${XRAY_PANEL_PORT})"
2965+
[[ -n "${XRAY_PANEL_BASEPATH:-}" ]] && _detect_msg+=", base path: /${XRAY_PANEL_BASEPATH}"
2966+
print_ok "$_detect_msg"
29512967
fi
29522968

29532969
# 2. Get panel credentials (skip for headless — no panel API needed)
@@ -4595,6 +4611,21 @@ do_add_domain() {
45954611
dnstm router start 2>/dev/null || true
45964612
fi
45974613

4614+
# Ensure router is in multi mode (required for multiple domains)
4615+
local current_mode
4616+
current_mode=$(dnstm router mode 2>/dev/null | awk '/[Mm]ode/{for(i=1;i<=NF;i++) if($i=="multi"||$i=="single") print $i}' | head -1 || true)
4617+
if [[ "$current_mode" != "multi" ]]; then
4618+
print_warn "Router mode is '${current_mode:-unknown}', switching to multi..."
4619+
if dnstm router mode multi 2>/dev/null; then
4620+
print_ok "Router mode switched to multi"
4621+
else
4622+
print_fail "Failed to switch router mode to multi. Multiple domains require multi mode."
4623+
exit 1
4624+
fi
4625+
else
4626+
print_ok "Router mode: multi"
4627+
fi
4628+
45984629
# Detect server IP
45994630
SERVER_IP=$(curl -4 -s --max-time 10 https://api.ipify.org 2>/dev/null || true)
46004631
if [[ -z "$SERVER_IP" ]]; then

0 commit comments

Comments
 (0)