|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Check if both LOGIN and PASSWORD are provided |
| 4 | +if [ -z "$1" ] || [ -z "$2" ]; then |
| 5 | + echo "Usage: $0 <LOGIN> <PASSWORD>" |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +LOGIN="$1" |
| 10 | +PASSWORD="$2" |
| 11 | + |
| 12 | +# Check if curl is installed |
| 13 | +if ! command -v curl &> /dev/null; then |
| 14 | + echo "curl could not be found, please install it." |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +# Check if jq is installed |
| 19 | +if ! command -v jq &> /dev/null; then |
| 20 | + echo "jq could not be found, please install it." |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +# Fetch the token |
| 25 | +TOKEN=$(curl -s --user "$LOGIN:$PASSWORD" "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull" | jq -r .token) |
| 26 | + |
| 27 | +# Check if the token was successfully retrieved |
| 28 | +if [ -z "$TOKEN" ]; then |
| 29 | + echo "Failed to retrieve token. Please check your LOGIN and PASSWORD." |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Fetch the rate limit information |
| 34 | +RESPONSE=$(curl -s --head -H "Authorization: Bearer $TOKEN" https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest) |
| 35 | + |
| 36 | +# Check if the request was successful |
| 37 | +if [ -z "$RESPONSE" ]; then |
| 38 | + echo "Failed to fetch rate limit information." |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +# Check for rate limit headers |
| 43 | +RATELIMIT_LIMIT=$(echo "$RESPONSE" | grep -i "ratelimit-limit" || true) |
| 44 | +RATELIMIT_REMAINING=$(echo "$RESPONSE" | grep -i "ratelimit-remaining" || true) |
| 45 | + |
| 46 | +# Display the rate limit information |
| 47 | +if [ -n "$RATELIMIT_LIMIT" ] || [ -n "$RATELIMIT_REMAINING" ]; then |
| 48 | + echo "Rate Limit Information:" |
| 49 | + echo "$RATELIMIT_LIMIT" |
| 50 | + echo "$RATELIMIT_REMAINING" |
| 51 | +else |
| 52 | + echo "No rate limit information found in the response. This might be a business account or the headers are not exposed." |
| 53 | + echo "Full response headers for debugging:" |
| 54 | + echo "$RESPONSE" |
| 55 | +fi |
0 commit comments