-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl-validator-lib.sh
More file actions
executable file
·276 lines (229 loc) · 8.46 KB
/
url-validator-lib.sh
File metadata and controls
executable file
·276 lines (229 loc) · 8.46 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
# ============================================================================
# Socrates Blade v3.2 - URL Validation Library
# Contains URL validation and error handling functions
# ============================================================================
# Source this file to get URL validation functions
# Example: source url-validator-lib.sh
# --- URL Validation Configuration ---
: "${MAX_URL_RETRIES:=3}"
: "${URL_CHECK_TIMEOUT:=5}"
# ============================================================================
# URL Validation Functions
# ============================================================================
extract_hostname() {
local url="$1"
local hostname=""
hostname=$(echo "$url" | sed -E 's|^https?://||' | cut -d':' -f1 | cut -d'/' -f1)
declare -g LAST_VALIDATED_HOSTNAME="$hostname"
echo "$hostname"
}
check_dns_resolution() {
local hostname="$1"
local resolved=false
if getent hosts "$hostname" > /dev/null 2>&1; then
resolved=true
elif ping -c 1 -W 1 "$hostname" > /dev/null 2>&1; then
resolved=true
elif host "$hostname" > /dev/null 2>&1; then
resolved=true
fi
echo "$resolved"
}
test_http_connection() {
local url="$1"
local timeout="${2:-$URL_CHECK_TIMEOUT}"
local response=""
local http_code=""
if command -v curl &> /dev/null; then
http_code=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout "$timeout" -L "$url" 2>/dev/null || echo "000")
echo "$http_code"
else
echo "curl_not_found"
fi
}
test_https_ssl() {
local url="$1"
local timeout="${2:-$URL_CHECK_TIMEOUT}"
if command -v curl &> /dev/null; then
curl -s -o /dev/null --connect-timeout "$timeout" -w "%{http_code}" "$url" 2>&1 | grep -q "SSL" && echo "ssl_error" && return 0
curl -s -o /dev/null --connect-timeout "$timeout" -w "%{http_code}" "$url" 2>&1 | grep -q "certificate" && echo "cert_error" && return 0
echo "ssl_ok"
else
echo "curl_not_found"
fi
}
detect_known_typos() {
local hostname="$1"
local suggestions=()
case "$hostname" in
bloware*)
suggestions+=("blogware.site (Did you mean 'blogware' instead of 'bloware'?)")
;;
blogware*)
suggestions+=("localhost (for local testing)")
;;
loclahost)
suggestions+=("localhost (typo detected)")
;;
localhose)
suggestions+=("localhost (typo detected)")
;;
localhot)
suggestions+=("localhost (typo detected)")
;;
esac
printf '%s\n' "${suggestions[@]}"
}
show_url_suggestions() {
local hostname="${1:-$LAST_VALIDATED_HOSTNAME}"
local error_type="$2"
if [ -z "$hostname" ]; then
hostname="$LAST_VALIDATED_HOSTNAME"
fi
echo ""
echo -e "${YELLOW}=== Suggestions ===${NC}"
echo ""
case "$error_type" in
dns_fail)
echo -e "${YELLOW}The hostname '${hostname}' could not be resolved.${NC}"
echo ""
echo -e "${BOLD}Possible solutions:${NC}"
echo " 1. Check if the hostname is correct (no typos?)"
local typos
typos=$(detect_known_typos "$hostname")
if [ -n "$typos" ]; then
echo ""
echo -e "${CYAN}Possible typo corrections:${NC}"
echo "$typos" | sed 's/^/ - /'
fi
echo ""
echo " 2. For local testing, use: ${GREEN}http://localhost${NC}"
echo ""
if grep -q "127.0.0.1.*$hostname" /etc/hosts 2>/dev/null; then
echo -e "${GREEN}* '$hostname' is configured in /etc/hosts${NC}"
else
echo -e "${CYAN}* Add to /etc/hosts: 127.0.0.1 $hostname${NC}"
fi
;;
connection_refused)
echo -e "${YELLOW}Connection to '${hostname}' was refused.${NC}"
echo ""
echo -e "${BOLD}Possible solutions:${NC}"
echo " 1. Make sure the web server is running"
echo " 2. Check if the correct port is being used (default: 80/443)"
echo " 3. Check firewall settings"
;;
timeout)
echo -e "${YELLOW}Connection to '${hostname}' timed out.${NC}"
echo ""
echo -e "${BOLD}Possible solutions:${NC}"
echo " 1. Check network connectivity"
echo " 2. Check firewall/proxy settings"
echo " 3. Try increasing timeout with --timeout option"
;;
ssl_error)
echo -e "${YELLOW}SSL certificate verification failed for '${hostname}'.${NC}"
echo ""
echo -e "${BOLD}Possible solutions:${NC}"
echo " 1. Use HTTP instead: ${GREEN}http://${hostname}${NC}"
echo " 2. Add --no-verify-ssl flag (if supported)"
echo " 3. Install valid SSL certificate"
;;
esac
echo ""
}
validate_target_url() {
local url="$1"
local hostname=""
local dns_resolved=""
local http_code=""
local result=0
declare -g URL_ERROR=""
declare -g URL_ERROR_TYPE=""
hostname=$(extract_hostname "$url")
if [ -z "$hostname" ]; then
URL_ERROR="Could not extract hostname from URL"
URL_ERROR_TYPE="invalid_url"
return 1
fi
echo -e "${BLUE}* Validating target: ${hostname}...${NC}"
dns_resolved=$(check_dns_resolution "$hostname")
if [ "$dns_resolved" != "true" ]; then
URL_ERROR="DNS resolution failed: Name or service not known for '$hostname'"
URL_ERROR_TYPE="dns_fail"
return 1
fi
echo -e "${BLUE} - DNS resolution: OK${NC}"
if [[ "$url" == https://* ]]; then
local ssl_result
ssl_result=$(test_https_ssl "$url")
if [ "$ssl_result" = "ssl_error" ] || [ "$ssl_result" = "cert_error" ]; then
URL_ERROR="SSL certificate verification failed"
URL_ERROR_TYPE="ssl_error"
result=1
fi
fi
http_code=$(test_http_connection "$url")
if [ "$http_code" = "curl_not_found" ]; then
echo -e "${YELLOW} ! curl not found, skipping HTTP check${NC}"
return 0
fi
if [ "$http_code" = "000" ]; then
URL_ERROR="Connection refused or timed out (HTTP code: $http_code)"
URL_ERROR_TYPE="timeout"
return 1
fi
if [ "$http_code" -ge 200 ] && [ "$http_code" -lt 400 ]; then
echo -e "${GREEN} - HTTP connection: OK (HTTP $http_code)${NC}"
elif [ "$http_code" -eq 400 ]; then
echo -e "${YELLOW} ! HTTP 400 Bad Request (server reachable)${NC}"
elif [ "$http_code" -eq 401 ]; then
echo -e "${YELLOW} ! HTTP 401 Unauthorized (server reachable)${NC}"
elif [ "$http_code" -eq 403 ]; then
echo -e "${YELLOW} ! HTTP 403 Forbidden (server reachable)${NC}"
elif [ "$http_code" -eq 404 ]; then
echo -e "${YELLOW} ! HTTP 404 Not Found (server reachable)${NC}"
elif [ "$http_code" -ge 500 ]; then
echo -e "${YELLOW} ! HTTP $http_code Server Error (server reachable)${NC}"
fi
return $result
}
prompt_for_url() {
local attempt=1
local new_url=""
local hostname=""
while [ $attempt -le $MAX_URL_RETRIES ]; do
echo ""
echo -e "${YELLOW}Attempt $attempt of $MAX_URL_RETRIES${NC}"
echo ""
if [ -n "$URL_ERROR" ]; then
echo -e "${RED}Error: $URL_ERROR${NC}"
show_url_suggestions "$hostname" "$URL_ERROR_TYPE"
fi
echo -e "${BOLD}Please enter a valid target URL:${NC}"
echo -e "Examples:"
echo -e " - ${GREEN}http://localhost${NC}"
echo -e " - ${GREEN}http://127.0.0.1${NC}"
echo -e " - ${GREEN}http://blogware.site${NC}"
echo ""
read -r -p "URL> " new_url
if [ -z "$new_url" ]; then
echo -e "${RED}URL cannot be empty${NC}"
((attempt++))
continue
fi
if ! [[ "$new_url" =~ ^https?:// ]]; then
new_url="http://$new_url"
fi
hostname=$(extract_hostname "$new_url")
if validate_target_url "$new_url"; then
TARGET_URL="$new_url"
echo ""
echo -e "${GREEN}* URL validated successfully!${NC}"
return 0
fi
((attempt++))
done
return 1
}