requester-lb 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/test.sh +95 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "requester-lb",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": ".",
5
5
  "type": "commonjs",
6
6
  "main": "index.js",
package/test.sh CHANGED
@@ -1 +1,95 @@
1
- echo 'test';
1
+ #!/usr/bin/env bash
2
+ printenv | sort
3
+
4
+ set -euo pipefail
5
+
6
+ domain=""
7
+ path="/"
8
+ method="GET"
9
+ port=""
10
+ use_tls=1
11
+ insecure=0
12
+ headers=()
13
+ body=""
14
+ timeout=5
15
+ record="A"
16
+ resolver=""
17
+ dns_only=0
18
+
19
+ usage() {
20
+ echo "Usage: $0 --domain <host> [--path /] [--method GET] [--port 443] [--no-tls] [--insecure]"
21
+ echo " [--header \"K: V\"] [--body \"data\"] [--timeout 5]"
22
+ echo " [--record A|AAAA|CNAME|TXT|MX|NS|SOA] [--resolver <ip>] [--dns-only]"
23
+ exit 1
24
+ }
25
+
26
+ while [[ $# -gt 0 ]]; do
27
+ case "$1" in
28
+ --domain) domain="$2"; shift 2 ;;
29
+ --path) path="$2"; shift 2 ;;
30
+ --method) method="$2"; shift 2 ;;
31
+ --port) port="$2"; shift 2 ;;
32
+ --no-tls) use_tls=0; shift ;;
33
+ --insecure) insecure=1; shift ;;
34
+ --header) headers+=("$2"); shift 2 ;;
35
+ --body) body="$2"; shift 2 ;;
36
+ --timeout) timeout="$2"; shift 2 ;;
37
+ --record) record="$2"; shift 2 ;;
38
+ --resolver) resolver="$2"; shift 2 ;;
39
+ --dns-only) dns_only=1; shift ;;
40
+ *) usage ;;
41
+ esac
42
+ done
43
+
44
+ [[ -z "$domain" ]] && usage
45
+
46
+ run_dns() {
47
+ echo "Resolving $domain ($record)${resolver:+ via $resolver}..."
48
+
49
+ if command -v dig >/dev/null 2>&1; then
50
+ if [[ -n "$resolver" ]]; then
51
+ dig @"$resolver" "$domain" "$record" +short
52
+ else
53
+ dig "$domain" "$record" +short
54
+ fi
55
+ elif command -v host >/dev/null 2>&1; then
56
+ if [[ -n "$resolver" ]]; then
57
+ host -t "$record" "$domain" "$resolver"
58
+ else
59
+ host -t "$record" "$domain"
60
+ fi
61
+ elif command -v getent >/dev/null 2>&1 && [[ "$record" == "A" || "$record" == "AAAA" ]]; then
62
+ getent hosts "$domain"
63
+ else
64
+ echo "No DNS tool available (dig/host/getent) for record type $record" >&2
65
+ return 1
66
+ fi
67
+ }
68
+
69
+ run_http() {
70
+ local scheme="https"
71
+ [[ "$use_tls" -eq 0 ]] && scheme="http"
72
+
73
+ local url="${scheme}://${domain}${port:+:$port}${path}"
74
+
75
+ local curl_args=(-sS -X "$method" --max-time "$timeout" -i)
76
+ [[ "$insecure" -eq 1 ]] && curl_args+=(-k)
77
+
78
+ for h in "${headers[@]:-}"; do
79
+ [[ -n "$h" ]] && curl_args+=(-H "$h")
80
+ done
81
+
82
+ [[ -n "$body" ]] && curl_args+=(-d "$body")
83
+
84
+ echo "Requesting $url..."
85
+ curl "${curl_args[@]}" "$url"
86
+ }
87
+
88
+ if ! run_dns; then
89
+ echo "Skipping HTTP request due to DNS failure." >&2
90
+ exit 1
91
+ fi
92
+
93
+ [[ "$dns_only" -eq 1 ]] && exit 0
94
+
95
+ run_http