File size: 1,859 Bytes
b223aad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/sh
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