extract-chrome-keys.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env bash
  2. set -eo pipefail
  3. TMPDIR=$(mktemp -d)
  4. trap 'rm -rf "$TMPDIR"' EXIT
  5. BIN="/opt/google/chrome/chrome"
  6. if [ ! -f "$BIN" ]; then
  7. if [ -f "/tmp/google-chrome-stable_current_amd64.deb" ]; then
  8. >&2 echo "Using existing .deb from /tmp..."
  9. cp "/tmp/google-chrome-stable_current_amd64.deb" "$TMPDIR/chrome.deb"
  10. else
  11. >&2 echo "Fetching google-chrome-stable .deb..."
  12. curl -sSL "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb" -o "$TMPDIR/chrome.deb"
  13. fi
  14. >&2 echo "Extracting binary..."
  15. ar p "$TMPDIR/chrome.deb" data.tar.xz | tar -xJ -C "$TMPDIR" ./opt/google/chrome/chrome
  16. BIN="$TMPDIR/opt/google/chrome/chrome"
  17. fi
  18. >&2 echo "Extracting keys from $BIN..."
  19. # 1. Google API Key
  20. API_KEY=$(strings "$BIN" | grep -o -E 'AIzaSy[A-Za-z0-9_-]{33}' | head -n1 || true)
  21. # 2. Main Chrome Desktop Client ID (77185425430.apps.googleusercontent.com)
  22. CLIENT_ID=$(strings "$BIN" | grep -o -E '77185425430\.apps\.googleusercontent\.com' | head -n1 || true)
  23. if [ -z "$CLIENT_ID" ]; then
  24. CLIENT_ID="77185425430.apps.googleusercontent.com"
  25. fi
  26. # 3. Main Chrome Desktop Client Secret (OTJgUOQcT7lO7GsGZq2G4IlT)
  27. CLIENT_SECRET=$(strings "$BIN" | grep -o -E 'OTJgUOQcT7lO7GsGZq2G4IlT' | head -n1 || true)
  28. if [ -z "$CLIENT_SECRET" ]; then
  29. CLIENT_SECRET="OTJgUOQcT7lO7GsGZq2G4IlT"
  30. fi
  31. if [ -z "$API_KEY" ]; then
  32. >&2 echo "Extraction failed: could not locate API_KEY."
  33. exit 1
  34. fi
  35. EXPORTS=$(cat <<EOF
  36. export GOOGLE_API_KEY="$API_KEY"
  37. export GOOGLE_DEFAULT_CLIENT_ID="$CLIENT_ID"
  38. export GOOGLE_DEFAULT_CLIENT_SECRET="$CLIENT_SECRET"
  39. EOF
  40. )
  41. if [ "$EUID" -ne 0 ]; then
  42. >&2 echo "Not root. Dumping exports to stdout:"
  43. echo "$EXPORTS"
  44. else
  45. mkdir -p /etc/chromium.d
  46. TARGET="/etc/chromium.d/sync"
  47. echo "$EXPORTS" > "$TARGET"
  48. chmod 755 "$TARGET"
  49. >&2 echo "Successfully deployed sync keys to $TARGET"
  50. fi