|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +set -euo pipefail |
| 9 | + |
| 10 | +status=0 |
| 11 | +green='\e[1;32m'; red='\e[1;31m'; cyan='\e[1;36m'; yellow='\e[1;33m'; reset='\e[0m' |
| 12 | +user_agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" |
| 13 | +max_jobs=10 |
| 14 | +pids=() |
| 15 | + |
| 16 | +running_jobs() { |
| 17 | + jobs -rp | wc -l |
| 18 | +} |
| 19 | + |
| 20 | +while IFS=: read -r filepath url; do |
| 21 | + fpath="$filepath" |
| 22 | + ( |
| 23 | + code=$(curl -k -gsLm30 --retry 3 --retry-delay 3 --retry-connrefused -o /dev/null -w "%{http_code}" -I "$url") || code=000 |
| 24 | + if [ "$code" -lt 200 ] || [ "$code" -ge 400 ]; then |
| 25 | + code=$(curl -k -gsLm30 --retry 3 --retry-delay 3 --retry-connrefused -o /dev/null -w "%{http_code}" -r 0-0 -A "$user_agent" "$url") || code=000 |
| 26 | + fi |
| 27 | + if [ "$code" -lt 200 ] || [ "$code" -ge 400 ]; then |
| 28 | + request_id=$(curl -sS -G -H 'Accept: application/json' \ |
| 29 | + --data-urlencode "host=$url" \ |
| 30 | + --data-urlencode "max_nodes=1" \ |
| 31 | + --data-urlencode "node=us3.node.check-host.net" \ |
| 32 | + https://check-host.net/check-http \ |
| 33 | + | jq -r .request_id) || request_id="" |
| 34 | + if [ -n "$request_id" ]; then |
| 35 | + sleep 5 |
| 36 | + for _ in {1..5}; do |
| 37 | + new_code=$(curl -sS -H 'Accept: application/json' \ |
| 38 | + "https://check-host.net/check-result/$request_id" \ |
| 39 | + | jq -r -e '.[][0][3]') || new_code=000 |
| 40 | + [[ "$new_code" =~ ^[0-9]+$ ]] || new_code=000 |
| 41 | + if [ "$new_code" -ge 200 ] && [ "$new_code" -lt 400 ]; then |
| 42 | + code=$new_code |
| 43 | + break |
| 44 | + fi |
| 45 | + sleep 5 |
| 46 | + done |
| 47 | + fi |
| 48 | + fi |
| 49 | + if [ "$code" -lt 200 ] || [ "$code" -ge 400 ]; then |
| 50 | + printf "${red}%s${reset} ${yellow}%s${reset} %s\n" "$code" "$url" "$fpath" >&2 |
| 51 | + exit 1 |
| 52 | + else |
| 53 | + printf "${green}%s${reset} ${cyan}%s${reset} %s\n" "$code" "$url" "$fpath" |
| 54 | + exit 0 |
| 55 | + fi |
| 56 | + ) & |
| 57 | + pids+=($!) |
| 58 | + while [ "$(running_jobs)" -ge "$max_jobs" ]; do |
| 59 | + sleep 1 |
| 60 | + done |
| 61 | +done < <( |
| 62 | + git --no-pager grep --no-color -I -P -o \ |
| 63 | + '(?!.*@lint-ignore)(?<!git\+)(?<!\$\{)https?://(?![^\s<>\")]*[<>\{\}\$])[^[:space:]<>\")\[\]\(\\]+' \ |
| 64 | + -- '*' \ |
| 65 | + ':(exclude).*' \ |
| 66 | + ':(exclude,glob)**/.*' \ |
| 67 | + ':(exclude,glob)**/*.lock' \ |
| 68 | + ':(exclude,glob)**/*.svg' \ |
| 69 | + ':(exclude,glob)**/*.xml' \ |
| 70 | + ':(exclude,glob)**/*.gradle*' \ |
| 71 | + ':(exclude,glob)**/*gradle*' \ |
| 72 | + ':(exclude,glob)**/third-party/**' \ |
| 73 | + ':(exclude,glob)**/third_party/**' \ |
| 74 | + | sed -E 's/[^/[:alnum:]]+$//' \ |
| 75 | + | grep -Ev '://(0\.0\.0\.0|127\.0\.0\.1|localhost)([:/])' \ |
| 76 | + | grep -Ev 'fwdproxy:8080' \ |
| 77 | + || true |
| 78 | +) |
| 79 | + |
| 80 | +for pid in "${pids[@]}"; do |
| 81 | + wait "$pid" 2>/dev/null || { |
| 82 | + case $? in |
| 83 | + 1) status=1 ;; |
| 84 | + 127) ;; # ignore "not a child" noise |
| 85 | + *) exit $? ;; |
| 86 | + esac |
| 87 | + } |
| 88 | +done |
| 89 | + |
| 90 | +exit $status |
0 commit comments