requester-lb 1.0.3 → 1.0.4

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