diff --git a/README.md b/README.md index df11251..7e3244d 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,7 @@ 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 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 be2fdcb..351a705 100644 --- a/setup.sh +++ b/setup.sh @@ -9,12 +9,37 @@ echo " https://github.com/alec-hs/coder-cloudflare-setup" echo echo "------------------------------------------------------------" echo -read -s -p "Please enter a password for Coder web GUI: " password -echo -echo -read -p "Please enter your domain: " domain -echo -read -s -p "Please enter your Cloudflare API Token: " token +read -s -p "Enter a password for Coder web GUI: " password +echo +echo +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:" +echo "https://github.com/alec-hs/coder-cloudflare-setup/#port-proxy" +echo +echo " 0 - wildcard record" +echo " 1 - specific ports" +echo +unset subOption +until [[ $subOption == @(0|1) ]] ; do + read -r -p "Your selection: " subOption +done +echo +echo "Enter your proxy domain, eg: mydomain.com" +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" + read -p "Your ports: " ports +fi +echo +read -s -p "Enter your Cloudflare API Token: " token echo echo echo "------------------------------------------------------------" @@ -22,6 +47,7 @@ echo echo " Setting up Caddy and Coder services..." echo echo "------------------------------------------------------------" +sleep 3 # Hash the password hash=$(printf $password | sha256sum | cut -d' ' -f1) @@ -47,6 +73,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 @@ -78,8 +107,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