From 1d1a383df0976c13c829c6ee2fa2cf721bcfa2ab Mon Sep 17 00:00:00 2001 From: alec-hs <6503411+alec-hs@users.noreply.github.com> Date: Mon, 14 Jun 2021 00:42:29 +0100 Subject: [PATCH 1/5] Added port proxy domain support --- README.md | 2 ++ setup.sh | 43 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index df11251..b66e8f7 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,5 @@ This script sets up a [Code Server](https://github.com/cdr/code-server) instance ``` 4. Access your Coder instance at your domain + +## Port Proxy diff --git a/setup.sh b/setup.sh index be2fdcb..94bf210 100644 --- a/setup.sh +++ b/setup.sh @@ -9,12 +9,35 @@ echo " https://github.com/alec-hs/coder-cloudflare-setup" echo echo "------------------------------------------------------------" echo -read -s -p "Please enter a password for Coder web GUI: " password +read -s -p "Enter a password for Coder web GUI: " password echo echo -read -p "Please enter your domain: " domain +echo "Enter your domain to access Code Server (can be subdomain): " +read domain +echo +echo "To allow port proxying, subdomain setup will be used. Please" +echo "enter a number from the below options. For more info:" +echo "https://github.com/alec-hs/coder-cloudflare-setup/#port-proxy" +echo +echo " 0 - wildcard record" +echo " 1 - specific ports" echo -read -s -p "Please enter your Cloudflare API Token: " token +unset suboption +until [[ $suboption == @(0|1) ]] ; do + read -r -p "Your selection: " suboption +done +echo +echo "Enter your proxy TLD, eg: mydomain.com" +read -p "Your domain: " proxydomain +echo +if [ $suboption == 1 ] +then + echo "Enter a space separated list of all the ports you need." + echo " eg: 80 8080 3000 8443" + read -p "Your ports: " ports +fi +echo +read -s -p "Enter your Cloudflare API Token: " token echo echo echo "------------------------------------------------------------" @@ -78,8 +101,20 @@ mv caddy /usr/bin curl https://raw.githubusercontent.com/alec-hs/coder-cloudflare-setup/main/Caddyfile --output /etc/caddy/Caddyfile # Update Caddyfile -sed -i.bak "s/sub.mydomain.com/$domain/" /etc/caddy/Caddyfile sed -i.bak "s/API_TOKEN/$token/" /etc/caddy/Caddyfile +if [ $suboption == 0 ] +then + caddyDomains="$domain, *.$proxydomain" +fi +if [ $suboption == 1 ] +then + proxyPorts="${ports// /.$proxydomain, }" + proxyPorts="$proxyPorts.$proxydomain" + caddyDomains="$domain, $proxyPorts" +fi +sed -i.bak "s/sub.mydomain.com/$caddyDomains/" /etc/caddy/Caddyfile + + # Update Coder config in /home/coder/.config/code-server/config.yaml sed -i.bak "s/password: .*/hashed-password: $hash/" /home/coder/.config/code-server/config.yaml From 9fcf2b0f91c96ec6ed205b464fde0d4289488c19 Mon Sep 17 00:00:00 2001 From: alec-hs <6503411+alec-hs@users.noreply.github.com> Date: Tue, 15 Jun 2021 13:06:57 +0100 Subject: [PATCH 2/5] Added proxy-domain to service file --- code-server.service | 2 +- setup.sh | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/code-server.service b/code-server.service index 68c2043..ac77a05 100644 --- a/code-server.service +++ b/code-server.service @@ -5,7 +5,7 @@ After=network.target [Service] Type=exec User=coder -ExecStart=/usr/bin/code-server +ExecStart=/usr/bin/code-server --proxy-domain mydomain.com Restart=always [Install] diff --git a/setup.sh b/setup.sh index 94bf210..0f8a02e 100644 --- a/setup.sh +++ b/setup.sh @@ -22,15 +22,14 @@ echo echo " 0 - wildcard record" echo " 1 - specific ports" echo -unset suboption -until [[ $suboption == @(0|1) ]] ; do - read -r -p "Your selection: " suboption +unset subOption +until [[ $subOption == @(0|1) ]] ; do + read -r -p "Your selection: " subOption done echo echo "Enter your proxy TLD, eg: mydomain.com" -read -p "Your domain: " proxydomain -echo -if [ $suboption == 1 ] +read -p "Your domain: " proxyDomain +if [ $subOption == 1 ] then echo "Enter a space separated list of all the ports you need." echo " eg: 80 8080 3000 8443" @@ -70,6 +69,9 @@ curl -fsSL https://code-server.dev/install.sh | sh # Download service file from repo curl https://raw.githubusercontent.com/alec-hs/coder-cloudflare-setup/main/code-server.service --output /etc/systemd/system/code-server.service +# Update coder file with proxy domain +sed -i.bak "s/mydomain.com/$proxyDomain/" /etc/systemd/system/code-server.service + # Run Coder & run on boot systemctl enable --now code-server @@ -102,14 +104,14 @@ curl https://raw.githubusercontent.com/alec-hs/coder-cloudflare-setup/main/Caddy # Update Caddyfile sed -i.bak "s/API_TOKEN/$token/" /etc/caddy/Caddyfile -if [ $suboption == 0 ] +if [ $subOption == 0 ] then - caddyDomains="$domain, *.$proxydomain" + caddyDomains="$domain, *.$proxyDomain" fi -if [ $suboption == 1 ] +if [ $subOption == 1 ] then - proxyPorts="${ports// /.$proxydomain, }" - proxyPorts="$proxyPorts.$proxydomain" + proxyPorts="${ports// /.$proxyDomain, }" + proxyPorts="$proxyPorts.$proxyDomain" caddyDomains="$domain, $proxyPorts" fi sed -i.bak "s/sub.mydomain.com/$caddyDomains/" /etc/caddy/Caddyfile From 040207539bdebb48d70e6894e71580952e1d6fa8 Mon Sep 17 00:00:00 2001 From: alec-hs <6503411+alec-hs@users.noreply.github.com> Date: Tue, 15 Jun 2021 13:25:06 +0100 Subject: [PATCH 3/5] Added sleep for clarity --- setup.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.sh b/setup.sh index 0f8a02e..dd94924 100644 --- a/setup.sh +++ b/setup.sh @@ -44,6 +44,7 @@ echo echo " Setting up Caddy and Coder services..." echo echo "------------------------------------------------------------" +sleep 3 # Hash the password hash=$(printf $password | sha256sum | cut -d' ' -f1) From 04803e2068e6c842a21dfb8c7768fd03013f5008 Mon Sep 17 00:00:00 2001 From: alec-hs <6503411+alec-hs@users.noreply.github.com> Date: Tue, 15 Jun 2021 13:27:25 +0100 Subject: [PATCH 4/5] TODO docs on proxy domain --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b66e8f7..7e3244d 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,5 @@ This script sets up a [Code Server](https://github.com/cdr/code-server) instance 4. Access your Coder instance at your domain ## Port Proxy + +Add more info here From 4b4aff60340fe5251f7a47b576580ea56aaa7515 Mon Sep 17 00:00:00 2001 From: alec-hs <6503411+alec-hs@users.noreply.github.com> Date: Tue, 15 Jun 2021 18:11:25 +0100 Subject: [PATCH 5/5] Adjusted to only accept root domains --- setup.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/setup.sh b/setup.sh index dd94924..351a705 100644 --- a/setup.sh +++ b/setup.sh @@ -12,8 +12,11 @@ echo read -s -p "Enter a password for Coder web GUI: " password echo echo -echo "Enter your domain to access Code Server (can be subdomain): " -read domain +echo "Enter root domain to access Code Server (not a subdomain): " +unset domain +until [[ $domain =~ ^[A-Za-z0-9-]+.([A-Za-z]{3,}|[A-Za-z]{2}.[A-Za-z]{2}|[A-Za-z]{2})$ ]] ; do + read -r domain +done echo echo "To allow port proxying, subdomain setup will be used. Please" echo "enter a number from the below options. For more info:" @@ -27,7 +30,7 @@ until [[ $subOption == @(0|1) ]] ; do read -r -p "Your selection: " subOption done echo -echo "Enter your proxy TLD, eg: mydomain.com" +echo "Enter your proxy domain, eg: mydomain.com" read -p "Your domain: " proxyDomain if [ $subOption == 1 ] then