diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 05630ea..27314a8 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -43,6 +43,6 @@ jobs: TOKEN: ${{ secrets.TOKEN }} UPLOAD: "/home/runner/sfcraft/build/libs" run: | - cd /bin + cd scripts chmod +x ./special_curl.sh sh ./update.sh diff --git a/scripts/special_curl b/scripts/special_curl new file mode 100755 index 0000000..c823eda --- /dev/null +++ b/scripts/special_curl @@ -0,0 +1,60 @@ +#!/bin/bash + +# Function to get IP address of a domain +get_ip() { + local domain=$1 + # Get IP address (first one found) for the domain + dig +short "$domain" | head -n 1 +} + +# Check if a URL is provided +if [ "$#" -lt 1 ]; then + echo "Usage: $0 [curl options...]" + exit 1 +fi + +# Extract the URL +url="$1" +shift + +# Define the fixed domain for --resolve +fixed_domain="gov.cn" + +# Parse the URL +protocol=$(echo "$url" | awk -F:// '{print $1}') +original_domain_port=$(echo "$url" | awk -F:// '{print $2}' | awk -F/ '{print $1}') +path=$(echo "$url" | sed 's|^[^/]*/||') + +# Extract port from the original URL +original_domain=$(echo "$original_domain_port" | awk -F: '{print $1}') +port=$(echo "$original_domain_port" | awk -F: '{print $2}') + +# If port is not specified, default to 80 for HTTP or 443 for HTTPS +if [ -z "$port" ]; then + if [ "$protocol" == "http" ]; then + port=80 + elif [ "$protocol" == "https" ]; then + port=443 + else + echo "Unknown protocol. Please specify port in URL." + exit 1 + fi +fi + +ip=$(get_ip "$original_domain") + +if [ -z "$ip" ]; then + echo "Could not resolve IP address for domain $fixed_domain" + exit 1 +fi + +# Construct the --resolve argument +resolve_arg="-k --resolve $fixed_domain:$port:$ip" + +# Replace the original domain with the fixed domain in the URL +modified_url=$(echo "$url" | sed "s/$original_domain/$fixed_domain/") + +# Run curl with the resolve argument and any additional curl options +#echo "Running: curl $resolve_arg $@ $modified_url" +curl $resolve_arg "$@" "$modified_url" + diff --git a/scripts/update.sh b/scripts/update.sh new file mode 100755 index 0000000..221db9b --- /dev/null +++ b/scripts/update.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +UPLOAD=$(ls $UPLOAD | tr ' ' '\n' | grep -v "all") + +if ! test -f "$UPLOAD"; then + echo "File does not exist. $UPLOAD" + exit +fi + +MOD=$(special_curl $API_ENDPOINT/api/client/servers/$SERVER_ID/files/list\?directory\=/mods -H "Authorization: Bearer $TOKEN" | jq -r ".data[] | .attributes.name" | grep "sfcraft") +echo "Fetch: $MOD" +if [ -n "$MOD" ]; then + echo "Update target: $MOD" + special_curl "$API_ENDPOINT/api/client/servers/$SERVER_ID/files/delete" -H "Content-Type: application/json" -H "Accept: application/json" -X POST -H "Authorization: Bearer $TOKEN" -d "{\"root\": \"/mods\", \"files\": [\"$MOD\"]}" + echo "Deleted." +fi + +echo "Uploading $UPLOAD" +SIGNED_URL=$(special_curl $API_ENDPOINT/api/client/servers/$SERVER_ID/files/upload -H "Authorization: Bearer $TOKEN" | jq -r ".attributes.url") + +if [ -z "$SIGNED_URL" ]; then + echo "!!! CANNOT FETCH SIGNED URL !!!" + exit +fi + +special_curl "$SIGNED_URL&directory=/mods" -X POST -F "files=@$UPLOAD" +echo "Uploaded!" +