From f6248d3ca069a4623f4c1727fdfbc79ccd1dc496 Mon Sep 17 00:00:00 2001 From: Jak Devaney Date: Fri, 23 Aug 2024 16:23:29 +0100 Subject: [PATCH 1/2] Added support for Duck DNS config and service to run on startup --- server-hosting/config.sample.ts | 6 +++- server-hosting/scripts/duck-dns.sh | 22 ++++++++++++ server-hosting/scripts/install.sh | 48 ++++++++++++++++++++++++-- server-hosting/server-hosting-stack.ts | 12 +++---- 4 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 server-hosting/scripts/duck-dns.sh diff --git a/server-hosting/config.sample.ts b/server-hosting/config.sample.ts index b59bac3..b91e7dd 100644 --- a/server-hosting/config.sample.ts +++ b/server-hosting/config.sample.ts @@ -29,5 +29,9 @@ export const Config = { // If vpc is not given specify subnet for default vpc subnetId: '', // Needed if subnetId is specified (i.e. us-west-2a) - availabilityZone: '' + availabilityZone: '', + // If using Duck DNS specify these + // Domain should not include duckdns.org + duckDnsToken: '', + duckDnsDomain: '' }; diff --git a/server-hosting/scripts/duck-dns.sh b/server-hosting/scripts/duck-dns.sh new file mode 100644 index 0000000..879346a --- /dev/null +++ b/server-hosting/scripts/duck-dns.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# Configuration +DOMAIN="your-duckdns-domain" # Replace with your Duck DNS subdomain +TOKEN="your-duckdns-token" # Replace with your Duck DNS token + +# Get the instance's public IPs +IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) +IPV6=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv6) + +# Update Duck DNS +# https://www.duckdns.org/update?domains={YOURVALUE}&token={YOURVALUE}[&ip={YOURVALUE}][&ipv6={YOURVALUE}][&verbose=true][&clear=true] +URL="https://www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=$IP&ipv6=$IPV6" + +RESPONSE=$(curl -s $URL) + +# Check if the update was successful +if [ "$RESPONSE" = "OK" ]; then + echo "Duck DNS updated successfully with IP: $IP" +else + echo "Failed to update Duck DNS" +fi \ No newline at end of file diff --git a/server-hosting/scripts/install.sh b/server-hosting/scripts/install.sh index 33b3718..81e8aea 100644 --- a/server-hosting/scripts/install.sh +++ b/server-hosting/scripts/install.sh @@ -5,7 +5,8 @@ # 2: true|false - whether to use Satisfactory Experimental build (optional, default false) S3_SAVE_BUCKET=$1 USE_EXPERIMENTAL_BUILD=${2-false} - +DUCK_DNS_TOKEN=$3 +DUCK_DNS_DOMAIN=$4 # install steamcmd: https://developer.valvesoftware.com/wiki/SteamCMD?__cf_chl_jschl_tk__=pmd_WNQPOiK18.h0rf16RCYrARI2s8_84hUMwT.7N1xHYcs-1635248050-0-gqNtZGzNAiWjcnBszQiR#Linux.2FmacOS) add-apt-repository multiverse @@ -51,7 +52,7 @@ EOF systemctl enable satisfactory systemctl start satisfactory -# enable auto shutdown: https://github.com/feydan/satisfactory-tools/tree/main/shutdown +# enable auto shutdown: https://github.com/feydan/satisfactory-tools/tree/main/server-hosting/scripts/auto-shutdown.sh cat << 'EOF' > /home/ubuntu/auto-shutdown.sh #!/bin/sh @@ -103,5 +104,48 @@ EOF systemctl enable auto-shutdown systemctl start auto-shutdown +# enable duck dns: https://github.com/feydan/satisfactory-tools/tree/main/server-hosting/scripts/duck-dns.sh +cat << EOF > /home/ubuntu/duck-dns.sh +#!/bin/sh + +# Configuration +DOMAIN="$DUCK_DNS_DOMAIN" +TOKEN="$DUCK_DNS_TOKEN" + +# Get the instance's public IP +IP=\$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) +IPV6=\$(curl -s http://169.254.169.254/latest/meta-data/public-ipv6) + +# Update Duck DNS +# https://www.duckdns.org/update?domains={YOURVALUE}&token={YOURVALUE}[&ip={YOURVALUE}][&ipv6={YOURVALUE}][&verbose=true][&clear=true] +URL="https://www.duckdns.org/update?domains=\$DOMAIN&token=\$TOKEN&ip=\$IP&ipv6=\$IPV6" + +RESPONSE=\$(curl -s \$URL) + +# Check if the update was successful +if [ "\$RESPONSE" = "OK" ]; then + echo "Duck DNS updated successfully with IP: \$IP" +else + echo "Failed to update Duck DNS" +fi +EOF +chmod +x /home/ubuntu/duck-dns.sh +chown ubuntu:ubuntu /home/ubuntu/duck-dns.sh + +cat << 'EOF' > /etc/systemd/system/duck-dns.service +[Unit] +Description=Update Duck DNS with the current IP on startup +After=network.target + +[Service] +Type=oneshot +ExecStart=/home/ubuntu/duck-dns.sh + +[Install] +WantedBy=multi-user.target +EOF +systemctl enable duck-dns +systemctl start duck-dns + # automated backups to s3 every 5 minutes su - ubuntu -c "crontab -l -e ubuntu | { cat; echo \"*/5 * * * * /usr/local/bin/aws s3 sync /home/ubuntu/.config/Epic/FactoryGame/Saved/SaveGames/server s3://$S3_SAVE_BUCKET\"; } | crontab -" diff --git a/server-hosting/server-hosting-stack.ts b/server-hosting/server-hosting-stack.ts index d19355b..71008ba 100644 --- a/server-hosting/server-hosting-stack.ts +++ b/server-hosting/server-hosting-stack.ts @@ -1,12 +1,12 @@ import { Duration, Stack, StackProps } from 'aws-cdk-lib'; -import { Construct } from 'constructs'; -import { Config } from './config'; +import * as apigw from 'aws-cdk-lib/aws-apigateway'; import * as ec2 from 'aws-cdk-lib/aws-ec2'; +import * as iam from 'aws-cdk-lib/aws-iam'; +import * as lambda_nodejs from 'aws-cdk-lib/aws-lambda-nodejs'; import * as s3 from 'aws-cdk-lib/aws-s3'; import * as s3_assets from 'aws-cdk-lib/aws-s3-assets'; -import * as lambda_nodejs from 'aws-cdk-lib/aws-lambda-nodejs'; -import * as iam from 'aws-cdk-lib/aws-iam'; -import * as apigw from 'aws-cdk-lib/aws-apigateway'; +import { Construct } from 'constructs'; +import { Config } from './config'; export class ServerHostingStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { @@ -131,7 +131,7 @@ export class ServerHostingStack extends Stack { }); server.userData.addExecuteFileCommand({ filePath: localPath, - arguments: `${savesBucket.bucketName} ${Config.useExperimentalBuild}` + arguments: `${savesBucket.bucketName} ${Config.useExperimentalBuild} ${Config.duckDnsToken} ${Config.duckDnsDomain}` }); ////////////////////////////// From d565dabcaff9adf4b656804e7a326ddab67112aa Mon Sep 17 00:00:00 2001 From: Jak Devaney Date: Fri, 23 Aug 2024 17:40:10 +0100 Subject: [PATCH 2/2] Removed IPv6 support as it was outside scope, and forced server to start in IPv4 mode --- server-hosting/scripts/duck-dns.sh | 3 +-- server-hosting/scripts/install.sh | 5 ++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/server-hosting/scripts/duck-dns.sh b/server-hosting/scripts/duck-dns.sh index 879346a..432d401 100644 --- a/server-hosting/scripts/duck-dns.sh +++ b/server-hosting/scripts/duck-dns.sh @@ -6,11 +6,10 @@ TOKEN="your-duckdns-token" # Replace with your Duck DNS token # Get the instance's public IPs IP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) -IPV6=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv6) # Update Duck DNS # https://www.duckdns.org/update?domains={YOURVALUE}&token={YOURVALUE}[&ip={YOURVALUE}][&ipv6={YOURVALUE}][&verbose=true][&clear=true] -URL="https://www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=$IP&ipv6=$IPV6" +URL="https://www.duckdns.org/update?domains=$DOMAIN&token=$TOKEN&ip=$IP" RESPONSE=$(curl -s $URL) diff --git a/server-hosting/scripts/install.sh b/server-hosting/scripts/install.sh index 81e8aea..e8f136a 100644 --- a/server-hosting/scripts/install.sh +++ b/server-hosting/scripts/install.sh @@ -38,7 +38,7 @@ After=syslog.target network.target nss-lookup.target network-online.target [Service] Environment="LD_LIBRARY_PATH=./linux64" ExecStartPre=$STEAM_INSTALL_SCRIPT -ExecStart=/home/ubuntu/.steam/steamapps/common/SatisfactoryDedicatedServer/FactoryServer.sh +ExecStart=/home/ubuntu/.steam/steamapps/common/SatisfactoryDedicatedServer/FactoryServer.sh -multihome=0.0.0.0 User=ubuntu Group=ubuntu StandardOutput=journal @@ -114,11 +114,10 @@ TOKEN="$DUCK_DNS_TOKEN" # Get the instance's public IP IP=\$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4) -IPV6=\$(curl -s http://169.254.169.254/latest/meta-data/public-ipv6) # Update Duck DNS # https://www.duckdns.org/update?domains={YOURVALUE}&token={YOURVALUE}[&ip={YOURVALUE}][&ipv6={YOURVALUE}][&verbose=true][&clear=true] -URL="https://www.duckdns.org/update?domains=\$DOMAIN&token=\$TOKEN&ip=\$IP&ipv6=\$IPV6" +URL="https://www.duckdns.org/update?domains=\$DOMAIN&token=\$TOKEN&ip=\$IP" RESPONSE=\$(curl -s \$URL)