-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathfunctions.sh
More file actions
147 lines (130 loc) · 3.72 KB
/
Copy pathfunctions.sh
File metadata and controls
147 lines (130 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# Sanitizer helpers shared across scripts. Each function echoes the sanitized
# value on success; on failure it writes an error to stderr and returns non-zero.
sanitize_port() {
local candidate="$1"
if [[ "$candidate" =~ ^[0-9]+$ ]] && (( candidate >= 1 && candidate <= 65535 )); then
echo "$candidate"
else
echo "Error: Invalid PORT value: $candidate" >&2
return 1
fi
}
sanitize_domain() {
local candidate="$1"
if [ -z "$candidate" ]; then
echo ""
return 0
fi
if [[ "$candidate" =~ ^(\*\.)?[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$ ]]; then
echo "$candidate"
else
echo "Error: Invalid DOMAIN value: $candidate" >&2
return 1
fi
}
sanitize_target_endpoint() {
local candidate="$1"
if [ -z "$candidate" ]; then
echo ""
return 0
fi
if [[ "$candidate" =~ ^(grpc|https?)://[A-Za-z0-9._-]+(:[0-9]{1,5})?(/[A-Za-z0-9._~:/?&=%-]*)?$ ]]; then
echo "$candidate"
else
echo "Error: Invalid TARGET_ENDPOINT value: $candidate" >&2
return 1
fi
}
sanitize_client_max_body_size() {
local candidate="$1"
if [ -z "$candidate" ]; then
echo ""
return 0
fi
if [[ "$candidate" =~ ^[0-9]+[kKmMgG]?$ ]]; then
echo "$candidate"
else
echo "Warning: Ignoring invalid CLIENT_MAX_BODY_SIZE value: $candidate" >&2
echo ""
fi
}
sanitize_dns_label() {
local candidate="$1"
if [ -z "$candidate" ]; then
echo "Error: TXT_PREFIX cannot be empty" >&2
return 1
fi
if [[ "$candidate" =~ ^[A-Za-z0-9_-]+$ ]]; then
echo "$candidate"
else
echo "Error: Invalid TXT_PREFIX value: $candidate" >&2
return 1
fi
}
sanitize_proxy_timeout() {
local candidate="$1"
if [ -z "$candidate" ]; then
echo ""
return 0
fi
if [[ "$candidate" =~ ^[0-9]+[smh]?$ ]]; then
echo "$candidate"
else
echo "Warning: Ignoring invalid proxy timeout value: $candidate" >&2
echo ""
fi
}
sanitize_proxy_buffer_size() {
local candidate="$1"
if [ -z "$candidate" ]; then
echo ""
return 0
fi
if [[ "$candidate" =~ ^[0-9]+[kKmM]?$ ]]; then
echo "$candidate"
else
echo "Warning: Ignoring invalid proxy buffer size value: $candidate" >&2
echo ""
fi
}
sanitize_proxy_buffers() {
local candidate="$1"
if [ -z "$candidate" ]; then
echo ""
return 0
fi
# Format: number size (e.g., "4 256k")
if [[ "$candidate" =~ ^[0-9]+[[:space:]]+[0-9]+[kKmM]?$ ]]; then
echo "$candidate"
else
echo "Warning: Ignoring invalid proxy buffers value: $candidate (expected format: 'number size', e.g., '4 256k')" >&2
echo ""
fi
}
# Get the certbot certificate directory name for a domain.
# Certbot stores wildcard certs without the "*." prefix:
# *.example.com → /etc/letsencrypt/live/example.com/
cert_dir_name() {
local domain="$1"
echo "${domain#\*.}"
}
get_letsencrypt_account_path() {
local base_path="/etc/letsencrypt/accounts"
local api_endpoint="acme-v02.api.letsencrypt.org"
if [[ "$CERTBOT_STAGING" == "true" ]]; then
api_endpoint="acme-staging-v02.api.letsencrypt.org"
fi
echo "${base_path}/${api_endpoint}/directory/*/regr.json"
}
get_letsencrypt_account_file() {
local account_pattern
account_pattern=$(get_letsencrypt_account_path)
local account_files
account_files=( $account_pattern )
if [[ ! -f "${account_files[0]}" ]]; then
echo "Error: Let's Encrypt account file not found at $account_pattern" >&2
return 1
fi
echo "${account_files[0]}"
}