-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
325 lines (281 loc) · 10.8 KB
/
Copy pathinstall.sh
File metadata and controls
325 lines (281 loc) · 10.8 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/bash
#===============================================================================
# NetGuard AI - Installation Script
# AI-Powered Network Monitoring System for Ubuntu
#
# Usage: sudo ./install.sh [OPTIONS]
#
# Options:
# --interface IFACE Network interface to monitor (default: auto-detect)
# --skip-docker Skip Docker installation (if already installed)
# --minimal Minimal installation (fewer resources)
# --help Show this help message
#===============================================================================
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
INSTALL_DIR="/opt/netguard-ai"
INTERFACE=""
SKIP_DOCKER=false
MINIMAL=false
#===============================================================================
# Helper Functions
#===============================================================================
print_banner() {
echo -e "${BLUE}"
echo "╔═══════════════════════════════════════════════════════════════════╗"
echo "║ ║"
echo "║ ███╗ ██╗███████╗████████╗ ██████╗ ██╗ ██╗ █████╗ ██████╗ ║"
echo "║ ████╗ ██║██╔════╝╚══██╔══╝██╔════╝ ██║ ██║██╔══██╗██╔══██╗ ║"
echo "║ ██╔██╗ ██║█████╗ ██║ ██║ ███╗██║ ██║███████║██████╔╝ ║"
echo "║ ██║╚██╗██║██╔══╝ ██║ ██║ ██║██║ ██║██╔══██║██╔══██╗ ║"
echo "║ ██║ ╚████║███████╗ ██║ ╚██████╔╝╚██████╔╝██║ ██║██║ ██║ ║"
echo "║ ╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ║"
echo "║ ║"
echo "║ AI-Powered Network Monitoring System ║"
echo "║ Version 1.0.0 ║"
echo "║ ║"
echo "╚═══════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
}
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "\n${BLUE}==>${NC} $1"
}
check_root() {
if [ "$EUID" -ne 0 ]; then
log_error "This script must be run as root (use sudo)"
exit 1
fi
}
check_ubuntu() {
if [ ! -f /etc/os-release ]; then
log_error "Cannot determine OS. This script is designed for Ubuntu."
exit 1
fi
. /etc/os-release
if [ "$ID" != "ubuntu" ]; then
log_warn "This script is designed for Ubuntu. Your OS: $ID"
read -p "Continue anyway? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
log_info "Detected: $PRETTY_NAME"
}
detect_interface() {
if [ -z "$INTERFACE" ]; then
INTERFACE=$(ip route | grep default | awk '{print $5}' | head -1)
if [ -z "$INTERFACE" ]; then
INTERFACE=$(ip link | grep -E "^[0-9]+" | grep -v "lo:" | head -1 | awk -F: '{print $2}' | tr -d ' ')
fi
if [ -z "$INTERFACE" ]; then
log_error "Could not detect network interface. Please specify with --interface"
exit 1
fi
fi
log_info "Using network interface: $INTERFACE"
}
show_help() {
echo "NetGuard AI - Installation Script"
echo ""
echo "Usage: sudo ./install.sh [OPTIONS]"
echo ""
echo "Options:"
echo " --interface IFACE Network interface to monitor (default: auto-detect)"
echo " --skip-docker Skip Docker installation (if already installed)"
echo " --minimal Minimal installation (fewer resources)"
echo " --help Show this help message"
}
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--interface)
INTERFACE="$2"
shift 2
;;
--skip-docker)
SKIP_DOCKER=true
shift
;;
--minimal)
MINIMAL=true
shift
;;
--help)
show_help
exit 0
;;
*)
log_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
#===============================================================================
# Installation Functions
#===============================================================================
install_dependencies() {
log_step "Installing system dependencies..."
apt-get update
apt-get install -y curl wget git ca-certificates gnupg lsb-release net-tools jq htop
log_info "System dependencies installed"
}
install_docker() {
if [ "$SKIP_DOCKER" = true ]; then
log_info "Skipping Docker installation"
return
fi
if command -v docker &> /dev/null; then
log_info "Docker is already installed"
docker --version
return
fi
log_step "Installing Docker..."
apt-get remove -y docker docker-engine docker.io containerd runc 2>/dev/null || true
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable docker
systemctl start docker
log_info "Docker installed successfully"
}
configure_system() {
log_step "Configuring system settings..."
grep -q "vm.max_map_count" /etc/sysctl.conf || echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -w vm.max_map_count=262144
log_info "System configured"
}
setup_netguard() {
log_step "Setting up NetGuard AI..."
mkdir -p ${INSTALL_DIR}
cp -r . ${INSTALL_DIR}/
chmod +x ${INSTALL_DIR}/scripts/*.sh
cat > ${INSTALL_DIR}/.env << EOF
MONITOR_INTERFACE=${INTERFACE}
DETECTION_INTERVAL=300
ANOMALY_THRESHOLD=0.01
THREAT_INTEL_INTERVAL=86400
# AI Firewall Settings
FIREWALL_MODE=monitor
AUTO_BLOCK_THRESHOLD=3
BLOCK_DURATION_MINUTES=60
RATE_LIMIT_THRESHOLD=100
SYNC_INTERVAL=30
EOF
if [ "$MINIMAL" = true ]; then
log_info "Applying minimal configuration..."
fi
log_info "NetGuard AI files installed to ${INSTALL_DIR}"
}
build_containers() {
log_step "Building Docker containers..."
cd ${INSTALL_DIR}
docker compose build
log_info "Containers built successfully"
}
create_systemd_service() {
log_step "Creating systemd service..."
cat > /etc/systemd/system/netguard-ai.service << EOF
[Unit]
Description=NetGuard AI - Network Monitoring System
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=${INSTALL_DIR}
ExecStart=/usr/bin/docker compose up -d
ExecStop=/usr/bin/docker compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable netguard-ai.service
log_info "Systemd service created"
}
start_netguard() {
log_step "Starting NetGuard AI..."
cd ${INSTALL_DIR}
docker compose up -d
log_info "NetGuard AI started"
}
wait_for_services() {
log_step "Waiting for services..."
echo -n "Waiting for OpenSearch"
for i in {1..60}; do
if curl -s http://localhost:9200 > /dev/null 2>&1; then
echo -e " ${GREEN}Ready!${NC}"
break
fi
echo -n "."
sleep 2
done
}
print_success() {
echo -e "\n${GREEN}"
echo "╔═══════════════════════════════════════════════════════════════════╗"
echo "║ NetGuard AI Installation Complete! ║"
echo "╚═══════════════════════════════════════════════════════════════════╝"
echo -e "${NC}"
echo ""
echo "Access your monitoring dashboards:"
echo " 📊 OpenSearch Dashboards: http://localhost:5601"
echo " 📈 Grafana Dashboard: http://localhost:3000 (admin/netguard)"
echo " 🤖 AI Detection API: http://localhost:8080"
echo " 🛡️ AI Firewall: http://localhost:8888"
echo " 🔔 Alert Manager: http://localhost:9093"
echo ""
echo "Useful commands:"
echo " cd ${INSTALL_DIR} && docker compose logs -f # View logs"
echo " cd ${INSTALL_DIR} && docker compose ps # Check status"
echo " sudo systemctl stop netguard-ai # Stop"
echo " sudo systemctl start netguard-ai # Start"
echo " curl http://localhost:8080/alerts # View AI alerts"
echo " curl http://localhost:8888/status # Firewall status"
echo ""
echo "⚠️ Firewall starts in MONITOR mode (no blocking)."
echo " To enable blocking: edit .env and set FIREWALL_MODE=enforce"
echo ""
echo "Monitoring interface: ${INTERFACE}"
}
#===============================================================================
# Main
#===============================================================================
main() {
print_banner
parse_args "$@"
check_root
check_ubuntu
detect_interface
install_dependencies
install_docker
configure_system
setup_netguard
build_containers
create_systemd_service
start_netguard
wait_for_services
print_success
}
main "$@"