-
Notifications
You must be signed in to change notification settings - Fork 9
/
nmap-viewer.sh
executable file
·250 lines (211 loc) · 7.37 KB
/
nmap-viewer.sh
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
#!/bin/bash
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Minimum required Node.js version
MIN_NODE_VERSION="14.0.0"
# Function to print colored output
print_color() {
printf "${2}${1}${NC}\n"
}
# Function to compare versions
version_compare() {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
# Function to check Node.js version
check_node_version() {
if ! command -v node >/dev/null 2>&1; then
print_color "Node.js is not installed." "$RED"
return 1
fi
local current_version=$(node -v | cut -d 'v' -f 2)
version_compare $current_version $MIN_NODE_VERSION
case $? in
0) print_color "Node.js version $current_version is compatible." "$GREEN" ;;
1) print_color "Node.js version $current_version is compatible." "$GREEN" ;;
2) print_color "Node.js version $current_version is not compatible. Minimum required version is $MIN_NODE_VERSION." "$RED"
return 1 ;;
esac
}
# Function to install Node.js using the system package manager
install_nodejs_system() {
print_color "Installing Node.js using system package manager..." "$YELLOW"
if command -v apt-get >/dev/null 2>&1; then
sudo apt-get update && sudo apt-get install -y nodejs npm
elif command -v yum >/dev/null 2>&1; then
sudo yum install -y nodejs npm
elif command -v brew >/dev/null 2>&1; then
brew install node
else
print_color "Unable to detect a supported package manager. Please install Node.js manually." "$RED"
exit 1
fi
}
# Function to start the application
start_application() {
print_color "Starting the application..." "$GREEN"
serve -s build -l 3001 &
SERVER_PID=$!
# Wait for the server to start
sleep 5
# Open the application in the default browser
print_color "Opening Nmap Viewer in your default browser..." "$GREEN"
xdg-open http://localhost:3001 || open http://localhost:3001 || start http://localhost:3001
print_color "Nmap Viewer is now running on http://localhost:3001" "$GREEN"
print_color "To stop the application, press CTRL+C." "$GREEN"
# Wait for the server process to finish
wait $SERVER_PID
}
# Function to clean up and exit
cleanup_and_exit() {
print_color "Stopping the application..." "$GREEN"
pkill -f "serve -s build -l 3001"
exit 0
}
# Function to update the application
update_application() {
print_color "Updating Nmap Viewer..." "$GREEN"
if [ ! -d "nmap-viewer" ]; then
print_color "Nmap Viewer is not installed. Please run './nmap-viewer.sh install' first." "$RED"
exit 1
fi
cd nmap-viewer || exit
# Pull the latest changes
git pull origin master || {
print_color "Error: Failed to pull the latest changes. Please check your internet connection and try again." "$RED"
exit 1
}
# Install dependencies (in case there are new ones)
print_color "Updating dependencies..." "$GREEN"
npm ci
# Rebuild the project
print_color "Rebuilding the project..." "$GREEN"
npm run build
print_color "Nmap Viewer has been successfully updated." "$GREEN"
# Start the application after update
start_application
}
# Function to install the application
install_application() {
if [ -d "nmap-viewer" ]; then
print_color "Nmap Viewer directory already exists. Updating instead..." "$YELLOW"
update_application
return
fi
# Check if asdf is installed
if command -v asdf >/dev/null 2>&1; then
print_color "asdf detected. Using asdf for Node.js installation..." "$GREEN"
# Add the Node.js plugin if not already done
if ! asdf plugin list | grep -q nodejs; then
asdf plugin add nodejs
fi
# Install the latest LTS version of Node.js
NODEJS_VERSION=$(asdf latest nodejs)
asdf install nodejs $NODEJS_VERSION
asdf global nodejs $NODEJS_VERSION
print_color "Node.js $NODEJS_VERSION installed with asdf." "$GREEN"
else
print_color "asdf is not installed. We recommend using asdf for managing Node.js versions." "$YELLOW"
print_color "asdf allows you to easily switch between Node.js versions and ensures consistency across different systems." "$YELLOW"
print_color "Visit https://asdf-vm.com for installation instructions." "$YELLOW"
read -p "Do you want to proceed with system package manager installation instead (sudo required)? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
install_nodejs_system
else
print_color "Installation aborted. Please install asdf or Node.js manually and run this script again." "$RED"
exit 1
fi
fi
# Check Node.js version
if ! check_node_version; then
print_color "Please update Node.js to version $MIN_NODE_VERSION or higher and run this script again." "$RED"
exit 1
fi
# Create a new directory for the project
mkdir -p nmap-viewer
cd nmap-viewer
# Clone the repository
print_color "Cloning the Nmap Viewer repository..." "$GREEN"
git clone https://github.com/psyray/nmap-viewer.git . || {
print_color "Error: Failed to clone the repository. Please check your internet connection and try again." "$RED"
exit 1
}
# Install dependencies
print_color "Installing dependencies..." "$GREEN"
npm ci
# Build the project
print_color "Building the project..." "$GREEN"
npm run build
# Install serve globally
print_color "Installing serve..." "$GREEN"
npm install -g serve
print_color "Nmap Viewer has been successfully installed." "$GREEN"
# Start the application after installation
start_application
}
# Main execution
main() {
# Check for required commands
for cmd in git npm xdg-open; do
if ! command -v $cmd &> /dev/null; then
echo "Error: $cmd is not installed or not in PATH"
exit 1
fi
done
case "$1" in
start)
if [ -d "nmap-viewer" ]; then
cd nmap-viewer || exit
start_application
else
print_color "Nmap Viewer is not installed. Please run './nmap-viewer.sh install' to install it first." "$RED"
exit 1
fi
;;
install)
install_application
;;
update)
update_application
;;
*)
print_color "Usage: ./nmap-viewer.sh [install|start|update]" "$YELLOW"
print_color " install: Install Nmap Viewer and start it" "$YELLOW"
print_color " start: Start Nmap Viewer (must be installed first)" "$YELLOW"
print_color " update: Update Nmap Viewer to the latest version and start it" "$YELLOW"
exit 1
;;
esac
}
# Capture SIGINT signal (CTRL+C)
trap cleanup_and_exit SIGINT
# Run the main function
main "$@"