diff --git a/utils/ams_export_streams.sh b/utils/ams_export_streams.sh new file mode 100644 index 00000000..0b1d73b2 --- /dev/null +++ b/utils/ams_export_streams.sh @@ -0,0 +1,55 @@ +#!/bin/bash +# +# Export Ant Media Server streams (all or specific stream). +# +# Usage: +# ./export_streams.sh -> export all streams +# ./export_streams.sh -> export only given stream + +APP_NAME=$1 +STREAM_ID=$2 +CONFIG_FILE="/usr/local/antmedia/webapps/$APP_NAME/WEB-INF/red5-web.properties" +BASE_URL="http://localhost:5080" + +if [ -z "$APP_NAME" ]; then + echo "Usage:" + echo " $0 -> export all streams" + echo " $0 -> export only given stream" + exit 1 +fi + +jwt_token() { + iat=$(date +%s) + header='{"alg":"HS256","typ":"JWT"}' + payload="{\"iat\":$iat}" + + header_base64=$(echo -n "$header" | openssl base64 -e | tr -d '=' | tr '/+' '_-' | tr -d '\n') + payload_base64=$(echo -n "$payload" | openssl base64 -e | tr -d '=' | tr '/+' '_-' | tr -d '\n') + + data="$header_base64.$payload_base64" + + secret=$(grep "^jwtSecretKey=" "$CONFIG_FILE" | cut -d'=' -f2) + signature=$(echo -n "$data" | openssl dgst -sha256 -hmac "$secret" -binary | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n') + + echo "$data.$signature" +} + +JWT_TOKEN=$(jwt_token) +JWT_SECRET=$(grep "^jwtSecretKey=" "$CONFIG_FILE" | cut -d'=' -f2) +jwtControlEnabled=$(grep "^jwtControlEnabled=" "$CONFIG_FILE" | cut -d'=' -f2) + +if [ "$jwtControlEnabled" = "true" ] && [ -n "$JWT_SECRET" ]; then + CURL_CMD=(curl -s -H "Authorization: Bearer $JWT_TOKEN" -H "accept: application/json") +else + CURL_CMD=(curl -s -H "accept: application/json") +fi + +if [ -z "$STREAM_ID" ]; then + "${CURL_CMD[@]}" "$BASE_URL/$APP_NAME/rest/v2/broadcasts/list/0/1000" \ + | jq 'map(del(.anyoneWatching))' > "${APP_NAME}_streams.json" + echo "All streams exported to: ${APP_NAME}_streams.json" +else + "${CURL_CMD[@]}" "$BASE_URL/$APP_NAME/rest/v2/broadcasts/$STREAM_ID" \ + | jq 'del(.anyoneWatching)' > "${APP_NAME}_${STREAM_ID}.json" + echo "Stream exported to: ${APP_NAME}_${STREAM_ID}.json" +fi diff --git a/utils/ams_import_streams.sh b/utils/ams_import_streams.sh new file mode 100644 index 00000000..f48e34d7 --- /dev/null +++ b/utils/ams_import_streams.sh @@ -0,0 +1,51 @@ +#!/bin/bash +# +# Import Ant Media Server streams. +# +# Usage: +# ./import_streams.sh +# Example: +# ./import_streams.sh live live_streams.json + +APP_NAME=$1 +FILE=$2 +CONFIG_FILE="/usr/local/antmedia/webapps/$APP_NAME/WEB-INF/red5-web.properties" +BASE_URL="http://localhost:5080" + +if [ -z "$APP_NAME" ] || [ -z "$FILE" ]; then + echo "Usage: $0 " + exit 1 +fi + +jwt_token() { + iat=$(date +%s) + header='{"alg":"HS256","typ":"JWT"}' + payload="{\"iat\":$iat}" + + header_base64=$(echo -n "$header" | openssl base64 -e | tr -d '=' | tr '/+' '_-' | tr -d '\n') + payload_base64=$(echo -n "$payload" | openssl base64 -e | tr -d '=' | tr '/+' '_-' | tr -d '\n') + + data="$header_base64.$payload_base64" + + secret=$(grep "^jwtSecretKey=" "$CONFIG_FILE" | cut -d'=' -f2) + signature=$(echo -n "$data" | openssl dgst -sha256 -hmac "$secret" -binary | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n') + + echo "$data.$signature" +} + +JWT_TOKEN=$(jwt_token) +JWT_SECRET=$(grep "^jwtSecretKey=" "$CONFIG_FILE" | cut -d'=' -f2) +jwtControlEnabled=$(grep "^jwtControlEnabled=" "$CONFIG_FILE" | cut -d'=' -f2) + +if [ "$jwtControlEnabled" = "true" ] && [ -n "$JWT_SECRET" ]; then + CURL_CMD=(curl -s -X POST -H "Authorization: Bearer $JWT_TOKEN" -H "accept: application/json" -H "Content-Type: application/json") +else + CURL_CMD=(curl -s -X POST -H "accept: application/json" -H "Content-Type: application/json") +fi + +URL="$BASE_URL/$APP_NAME/rest/v2/broadcasts/create?autoStart=false" + +jq -c '.[]' "$FILE" | while read -r stream; do + "${CURL_CMD[@]}" "$URL" -d "$stream" + echo "" +done