feat(nginx): directly proxy spwn

This commit is contained in:
2022-01-30 04:34:48 +08:00
parent 00c542b552
commit afe6ad474d
2 changed files with 66 additions and 9 deletions
+21
View File
@@ -83,3 +83,24 @@ Run the script:
Once the script is done, your server should be accessible from Once the script is done, your server should be accessible from
`https://relay1.stream.example.org` and proxying all requests to `https://relay1.stream.example.org` and proxying all requests to
`https://restream.dev`. `https://restream.dev`.
Example 2: setting up a relay for SPWN:
```
./remote_setup.sh \
--domain relay1.stream.example.org \
--upstream-server 'https://vod.spwn.jp:443' \
--upstream-base '/spwn-archives/xxxxx/xxxx' \
--acme-email acme@example.org \
--target-server root@relay1.stream.example.org \
--pass-header User-Agent '[user agent string]' \
--pass-header Cookie '[redacted]' \
--pass-header Referrer 'https://spwn.jp/'
```
**Notes**
- `--upstream-server`: Include the `https://` in front and the `:443` port
number, and _do not_ include a trailing `/`.
- `--upstream-base`: The rest of the URL before the `index.m3u8` file. Start
with a `/` but do not add a `/` at the end.
+45 -9
View File
@@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
positional_args=() positional_args=()
original_args="${@@Q}"
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
@@ -14,6 +15,11 @@ while [[ $# -gt 0 ]]; do
shift # past argument shift # past argument
shift # past value shift # past value
;; ;;
--upstream-base)
upstream_base="$2"
shift # past argument
shift # past value
;;
--target-server) --target-server)
target_server="$2" target_server="$2"
shift # past argument shift # past argument
@@ -28,6 +34,13 @@ while [[ $# -gt 0 ]]; do
is_in_target=1 is_in_target=1
shift # past argument shift # past argument
;; ;;
--pass-header)
pass_headers="$pass_headers
proxy_set_header $2 '$3';"
shift # past argument
shift # past value
shift # past value
;;
-*|--*) -*|--*)
echo "Unknown option $1" echo "Unknown option $1"
exit 1 exit 1
@@ -49,7 +62,7 @@ if [ -z "$is_in_target" ]; then
|| [ -z "$upstream_server" ] \ || [ -z "$upstream_server" ] \
|| [ -z "$acme_email" ] \ || [ -z "$acme_email" ] \
|| [ -z "$target_server" ]; then || [ -z "$target_server" ]; then
echo "Usage: $0 --domain <domain> --upstream-server <https://domain:port> --acme-email <your.email@domain.co> --target-server <user@host>" echo "Usage: $0 --domain <domain> --upstream-server <https://domain:port> --upstream-base </some/path/> --acme-email <your.email@domain.co> --target-server <user@host> [--pass-header Header Value]"
exit 1 exit 1
fi fi
@@ -59,7 +72,7 @@ if [ -z "$is_in_target" ]; then
scp $0 $target_server:$target_filename scp $0 $target_server:$target_filename
echo "Executing setup script on target server..." echo "Executing setup script on target server..."
ssh $target_server "$target_filename --domain $domain --upstream-server $upstream_server --upstream-proto $upstream_proto --acme-email $acme_email --is-in-target" ssh $target_server "$target_filename --is-in-target $original_args"
# exit # exit
exit 0 exit 0
@@ -73,15 +86,36 @@ if [ "$(id -u)" != "0" ]; then
fi fi
echo "Installing dependencies..." echo "Installing dependencies..."
cat <<EOF > /etc/apt/sources.list.d/nginx.list
deb https://nginx.org/packages/ubuntu/ focal nginx
deb-src https://nginx.org/packages/ubuntu/ focal nginx
EOF
while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do
echo "Waiting for dpkg lock to be free..."
sleep 0.5
done
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62 && \
apt-get update && \ apt-get update && \
apt-get upgrade -y && \ apt-get upgrade -y && \
apt-get install -y nginx socat apt-get install -y nginx socat
# Fix firewall
if command -v ufw >/dev/null 2>&1; then
echo "Adding firewall rules..."
ufw allow http
ufw allow https
fi
echo "Downloading acme.sh..." echo "Downloading acme.sh..."
wget -O - https://get.acme.sh | sh -s email=$acme_email wget -O - https://get.acme.sh | sh -s email=$acme_email
echo "Issuing certificate..." echo "Issuing certificate..."
~/.acme.sh/acme.sh --issue -d $domain -w /var/www/html systemctl stop nginx
~/.acme.sh/acme.sh --issue -d $domain --server letsencrypt --standalone
systemctl start nginx
~/.acme.sh/acme.sh --install-cert -d $domain \ ~/.acme.sh/acme.sh --install-cert -d $domain \
--cert-file /etc/nginx/ssl.crt \ --cert-file /etc/nginx/ssl.crt \
--key-file /etc/nginx/ssl.key \ --key-file /etc/nginx/ssl.key \
@@ -172,15 +206,17 @@ http {
add_header X-Edge-Cache-Status \$upstream_cache_status; add_header X-Edge-Cache-Status \$upstream_cache_status;
location ~ \.(m3u8|mpd)\$ {
proxy_cache_valid 200 302 1s;
proxy_ssl_server_name on;
proxy_pass $upstream_server;
}
location / { location / {
$pass_headers
proxy_ssl_server_name on; proxy_ssl_server_name on;
rewrite ^(.*)$ $upstream_base\$1 break;
proxy_pass $upstream_server; proxy_pass $upstream_server;
location ~ \.(m3u8|mpd)$ {
rewrite ^(.*)$ $upstream_base\$1 break;
proxy_cache_valid any 1s;
proxy_pass $upstream_server;
}
} }
} }
} }