Spaces:
Running
Running
| set -eu | |
| : "${PORT:=7860}" | |
| : "${XRAY_PORT:=10000}" | |
| : "${UUID:?UUID environment variable is required}" | |
| : "${WS_PATH:?WS_PATH environment variable is required}" | |
| case "$WS_PATH" in | |
| /*) ;; | |
| *) | |
| echo "ERROR: WS_PATH must start with /" | |
| exit 1 | |
| ;; | |
| esac | |
| cat > /tmp/xray-config.json <<EOF_CONFIG | |
| { | |
| "log": { | |
| "loglevel": "warning" | |
| }, | |
| "inbounds": [ | |
| { | |
| "listen": "127.0.0.1", | |
| "port": ${XRAY_PORT}, | |
| "protocol": "vless", | |
| "settings": { | |
| "clients": [ | |
| { | |
| "id": "${UUID}", | |
| "email": "mobile-user" | |
| } | |
| ], | |
| "decryption": "none" | |
| }, | |
| "streamSettings": { | |
| "network": "ws", | |
| "security": "none", | |
| "wsSettings": { | |
| "path": "${WS_PATH}" | |
| } | |
| } | |
| } | |
| ], | |
| "outbounds": [ | |
| { | |
| "protocol": "freedom", | |
| "tag": "direct" | |
| } | |
| ] | |
| } | |
| EOF_CONFIG | |
| cat > /etc/nginx/http.d/default.conf <<EOF_NGINX | |
| server { | |
| listen ${PORT}; | |
| server_name _; | |
| location = / { | |
| default_type text/plain; | |
| return 200 "ok\\n"; | |
| } | |
| location ${WS_PATH} { | |
| proxy_redirect off; | |
| proxy_pass http://127.0.0.1:${XRAY_PORT}; | |
| proxy_http_version 1.1; | |
| proxy_set_header Upgrade \$http_upgrade; | |
| proxy_set_header Connection "upgrade"; | |
| proxy_set_header Host \$host; | |
| proxy_set_header X-Real-IP \$remote_addr; | |
| proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; | |
| } | |
| location / { | |
| return 404; | |
| } | |
| } | |
| EOF_NGINX | |
| /usr/local/bin/xray run -config /tmp/xray-config.json & | |
| XRAY_PID=$! | |
| nginx -g 'daemon off;' & | |
| NGINX_PID=$! | |
| trap 'kill $XRAY_PID $NGINX_PID 2>/dev/null || true' INT TERM | |
| while kill -0 "$XRAY_PID" 2>/dev/null && kill -0 "$NGINX_PID" 2>/dev/null; do | |
| sleep 2 | |
| done | |
| kill "$XRAY_PID" "$NGINX_PID" 2>/dev/null || true | |
| wait | |