Skip to content

Commit afd7fc1

Browse files
authored
Improve error handling of JSON output (TCR-599)
1 parent 3dab19c commit afd7fc1

File tree

1 file changed

+119
-58
lines changed

1 file changed

+119
-58
lines changed
Lines changed: 119 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,136 @@
11
#!/bin/bash
2+
# Raw link:
3+
# wget https://raw.githubusercontent.com/cloudlinux/imunify360-documentation/master/docs/control_panel_integration/get-panel-info.sh -O /etc/sysconfig/imunify360/get-panel-info.sh
4+
#
25

3-
# Function to check if a port is open
6+
# Check if a port is open, suppressing output from nc
47
check_port() {
5-
nc -z -w1 $1 $2
6-
return $?
8+
# Ensure nc exists
9+
if ! command -v nc > /dev/null; then
10+
# nc is not available, this script cannot succeed.
11+
return 1 # Return failure
12+
fi
13+
# Run nc, redirect both stdout and stderr to /dev/null
14+
nc -z -w1 "$1" "$2" > /dev/null 2>&1
15+
return $? # Return nc's exit status (0 for open, non-zero for closed/timeout)
716
}
817

918
# Main script execution
1019
main() {
11-
local host="localhost"
20+
# State variables
21+
local result_status="unknown" # Possible values: unknown, ok, not_found, error
22+
local error_message=""
1223
local name=""
13-
local version="unknown" # This script does not fetch versions, it is static for now
24+
local version="unknown"
25+
local host="localhost"
26+
local nc_present=true # Assume nc is present
1427

15-
# Webmin
16-
if check_port $host 10000; then
17-
name="Webmin"
18-
# Cockpit
19-
elif check_port $host 9090; then
20-
name="Cockpit"
21-
# ISPConfig
22-
elif check_port $host 8080; then
23-
name="ISPConfig"
24-
# Ajenti
25-
elif check_port $host 8000; then
26-
name="Ajenti"
27-
# Froxlor
28-
elif check_port $host 4430; then
29-
name="Froxlor"
30-
# Vestacp
31-
elif check_port $host 8083; then
32-
name="VestaCP"
33-
# CentOS Web Panel (CWP)
34-
elif check_port $host 2030 || check_port $host 2031; then
35-
name="CentOS Web Panel"
36-
# HestiaCP
37-
elif check_port $host 8083; then
38-
name="HestiaCP"
39-
# BlueOnyx
40-
elif check_port $host 444; then
41-
name="BlueOnyx"
42-
# WebCP
43-
elif check_port $host 24522; then
44-
name="WebCP"
45-
# InterWorx
46-
elif check_port $host 2443 || check_port $host 2080; then
47-
name="InterWorx"
48-
else
49-
# If no known panel port is open, check for custom port (example range: 8000-9000)
50-
# Note: This can be slow and may lead to false positives.
51-
for port in $(seq 8000 9000); do
52-
if check_port $host $port; then
53-
name="Unknown Panel (custom port $port)"
54-
break
28+
# --- Check for 'nc' command ---
29+
if ! command -v nc > /dev/null; then
30+
nc_present=false
31+
result_status="error"
32+
error_message="Required 'nc' (netcat) not found."
33+
fi
34+
# --- End check ---
35+
36+
# --- Perform port checks, paths and infer panel name ---
37+
if $nc_present; then
38+
# Webmin
39+
if check_port "$host" 10000; then
40+
name="Webmin"
41+
# Cockpit
42+
elif check_port "$host" 9090; then
43+
name="Cockpit"
44+
# ISPConfig
45+
elif check_port "$host" 8080; then
46+
name="ISPConfig"
47+
# Ajenti
48+
elif check_port "$host" 8000; then
49+
name="Ajenti"
50+
# Froxlor
51+
elif check_port "$host" 4430; then
52+
name="Froxlor"
53+
# VestaCP / HestiaCP Conflict Check
54+
elif check_port "$host" 8083; then
55+
if [ -d "/usr/local/hestia/" ]; then
56+
name="HestiaCP"
57+
elif [ -d "/usr/local/vesta/" ]; then
58+
name="VestaCP"
59+
else
60+
name="VestaCP / HestiaCP (Port 8083)"
5561
fi
56-
done
62+
# CentOS Web Panel (CWP)
63+
elif check_port "$host" 2031 || check_port "$host" 2030; then
64+
name="CentOS Web Panel"
65+
# BlueOnyx
66+
elif check_port "$host" 444 || check_port "$host" 81 ; then
67+
name="BlueOnyx"
68+
# WebCP
69+
elif check_port "$host" 24522; then
70+
name="WebCP"
71+
# InterWorx
72+
elif check_port "$host" 2443 || check_port "$host" 2080; then
73+
name="InterWorx"
74+
else
75+
# Custom port scan (only if no standard panel found yet)
76+
for port in $(seq 8000 9000); do
77+
if [[ "$port" == "8000" || "$port" == "8080" || "$port" == "8083" || "$port" == "9090" ]]; then
78+
continue
79+
fi
80+
if check_port "$host" "$port"; then
81+
name="Unknown Panel (custom port $port)"
82+
break
83+
fi
84+
done
85+
fi
5786

58-
if [ -z "$name" ]; then
59-
echo '{"metadata": {"result": "no web panel found"}}'
60-
exit 1
87+
# Determine final output status
88+
if [ -n "$name" ]; then
89+
result_status="ok"
90+
else
91+
result_status="not_found"
6192
fi
93+
fi # End of "if nc_present"
94+
95+
# --- Construct and print JSON output ---
96+
if [ "$result_status" == "ok" ]; then
97+
# Using printf for JSON value construction
98+
printf '{\n'
99+
printf ' "data": {\n'
100+
printf ' "name": "%s",\n' "$name"
101+
printf ' "version": "%s"\n' "$version"
102+
printf ' },\n'
103+
printf ' "metadata": {\n'
104+
printf ' "result": "ok"\n'
105+
printf ' }\n'
106+
printf '}\n'
107+
elif [ "$result_status" == "not_found" ]; then
108+
printf '{\n'
109+
printf ' "metadata": {\n'
110+
printf ' "result": "not_found"\n'
111+
printf ' }\n'
112+
printf '}\n'
113+
elif [ "$result_status" == "error" ]; then
114+
# Basic escaping for the error message
115+
escaped_error_message=$(printf '%s' "$error_message" | sed 's/\\/\\\\/g; s/"/\\"/g')
116+
printf '{\n'
117+
printf ' "metadata": {\n'
118+
printf ' "result": "error",\n'
119+
printf ' "error_message": "%s"\n' "$escaped_error_message"
120+
printf ' }\n'
121+
printf '}\n'
122+
else
123+
# A fallback JSON
124+
printf '{\n'
125+
printf ' "metadata": {\n'
126+
printf ' "result": "internal_script_error",\n'
127+
printf ' "error_message": "Script reached unexpected state."\n'
128+
printf ' }\n'
129+
printf '}\n'
62130
fi
63131

64-
echo "{
65-
\"data\": {
66-
\"name\": \"$name\",
67-
\"version\": \"$version\"
68-
},
69-
\"metadata\": {
70-
\"result\": \"ok\"
71-
}
72-
}"
132+
# The script implicitly exits with 0 after the last command unless an error occurred *running* printf/sed
73133
}
74134

135+
# Run the main function
75136
main

0 commit comments

Comments
 (0)