-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·438 lines (372 loc) · 11.3 KB
/
setup.sh
File metadata and controls
executable file
·438 lines (372 loc) · 11.3 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/bin/bash
# Neko Master One-Click Setup Script
# Automatically detects port conflicts and provides solutions
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default ports
DEFAULT_WEB_PORT=3000
DEFAULT_API_PORT=3001
DEFAULT_WS_PORT=3002
# Current ports (will be modified)
WEB_PORT=$DEFAULT_WEB_PORT
API_PORT=$DEFAULT_API_PORT
WS_PORT=$DEFAULT_WS_PORT
# Print colored messages
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
print_error() {
echo -e "${RED}[✗]${NC} $1"
}
download_to_file() {
local url=$1
local output=$2
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$output"
return 0
fi
if command -v wget >/dev/null 2>&1; then
wget -qO "$output" "$url"
return 0
fi
return 1
}
prompt_if_empty() {
local var_name=$1
local prompt=$2
local secret=${3:-false}
local current_value="${!var_name:-}"
if [ -n "$current_value" ]; then
return 0
fi
if [ "$secret" = "true" ]; then
read -s -p "$prompt: " current_value
echo ""
else
read -p "$prompt: " current_value
fi
if [ -z "$current_value" ]; then
print_error "$var_name is required"
exit 1
fi
export "$var_name=$current_value"
}
run_agent_installer() {
local script_url="https://raw.githubusercontent.com/foru17/neko-master/main/apps/agent/install.sh"
echo ""
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Neko Agent - One-Click Install ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""
print_info "Prepare required variables for agent install"
print_info "Tip: you can pre-export env vars to run non-interactively"
prompt_if_empty "NEKO_SERVER" "Neko server URL (e.g. http://10.0.0.2:3000)"
prompt_if_empty "NEKO_BACKEND_ID" "Backend ID"
prompt_if_empty "NEKO_BACKEND_TOKEN" "Backend token" true
prompt_if_empty "NEKO_GATEWAY_TYPE" "Gateway type (clash/surge)"
prompt_if_empty "NEKO_GATEWAY_URL" "Gateway URL (e.g. http://127.0.0.1:9090)"
if [ -z "${NEKO_GATEWAY_TOKEN:-}" ]; then
read -s -p "Gateway token (optional, press Enter to skip): " NEKO_GATEWAY_TOKEN
echo ""
export NEKO_GATEWAY_TOKEN
fi
if [ -z "${NEKO_AGENT_VERSION:-}" ]; then
read -p "Agent version [latest]: " NEKO_AGENT_VERSION
export NEKO_AGENT_VERSION="${NEKO_AGENT_VERSION:-latest}"
fi
print_info "Downloading installer..."
local tmp_script
tmp_script="$(mktemp)"
trap 'rm -f "$tmp_script"' EXIT INT TERM
if ! download_to_file "$script_url" "$tmp_script"; then
print_error "Failed to download agent installer (curl/wget required)"
exit 1
fi
print_info "Running agent installer..."
env \
NEKO_SERVER="$NEKO_SERVER" \
NEKO_BACKEND_ID="$NEKO_BACKEND_ID" \
NEKO_BACKEND_TOKEN="$NEKO_BACKEND_TOKEN" \
NEKO_GATEWAY_TYPE="$NEKO_GATEWAY_TYPE" \
NEKO_GATEWAY_URL="$NEKO_GATEWAY_URL" \
NEKO_GATEWAY_TOKEN="${NEKO_GATEWAY_TOKEN:-}" \
NEKO_AGENT_VERSION="${NEKO_AGENT_VERSION:-latest}" \
sh "$tmp_script"
print_success "Agent install flow finished"
}
# Check if port is in use
check_port() {
local port=$1
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
return 0 # Port in use
else
return 1 # Port available
fi
}
# Find available port
find_available_port() {
local start_port=$1
local port=$start_port
while check_port $port; do
port=$((port + 1))
done
echo $port
}
# Show welcome message
show_welcome() {
echo ""
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Neko Master - One-Click Setup ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""
print_info "This script will help you quickly configure Neko Master"
echo ""
}
# Check port conflicts and ask user
check_ports() {
print_info "Checking if default ports are available..."
echo ""
local conflict_found=false
local suggested_web=$DEFAULT_WEB_PORT
local suggested_api=$DEFAULT_API_PORT
local suggested_ws=$DEFAULT_WS_PORT
# Check each port
if check_port $DEFAULT_WEB_PORT; then
suggested_web=$(find_available_port $DEFAULT_WEB_PORT)
print_warning "Port $DEFAULT_WEB_PORT (Web UI) is already in use"
print_info " Suggested alternative: $suggested_web"
conflict_found=true
fi
if check_port $DEFAULT_API_PORT; then
suggested_api=$(find_available_port $DEFAULT_API_PORT)
print_warning "Port $DEFAULT_API_PORT (API) is already in use"
print_info " Suggested alternative: $suggested_api"
conflict_found=true
fi
if check_port $DEFAULT_WS_PORT; then
suggested_ws=$(find_available_port $DEFAULT_WS_PORT)
print_warning "Port $DEFAULT_WS_PORT (WebSocket) is already in use"
print_info " Suggested alternative: $suggested_ws"
conflict_found=true
fi
echo ""
if [ "$conflict_found" = true ]; then
print_warning "Port conflicts detected!"
echo ""
echo "Please choose an option:"
echo " 1) Use suggested ports ($suggested_web/$suggested_api/$suggested_ws)"
echo " 2) Enter custom ports manually"
echo " 3) Exit and configure manually later"
echo ""
read -p "Enter your choice [1-3]: " choice
case $choice in
1)
WEB_PORT=$suggested_web
API_PORT=$suggested_api
WS_PORT=$suggested_ws
print_success "Using suggested ports"
;;
2)
echo ""
read -p "Web UI port [$suggested_web]: " input_web
read -p "API port [$suggested_api]: " input_api
read -p "WebSocket port [$suggested_ws]: " input_ws
WEB_PORT=${input_web:-$suggested_web}
API_PORT=${input_api:-$suggested_api}
WS_PORT=${input_ws:-$suggested_ws}
;;
3)
echo ""
print_info "You can manually configure later using one of these methods:"
echo " 1. Copy .env.example to .env and modify ports"
echo " 2. Edit docker-compose.yml directly"
echo ""
exit 0
;;
*)
print_error "Invalid option"
exit 1
;;
esac
else
print_success "All default ports are available"
fi
echo ""
}
# Create .env file
create_env_file() {
print_info "Creating configuration file..."
cat >.env <<EOF
# =============================================================================
# Neko Master Environment Configuration
# =============================================================================
# Generated at: $(date)
# -----------------------------------------------------------------------------
# Port Configuration (Change these if ports are already in use)
# -----------------------------------------------------------------------------
# These ports will be exposed on your host machine
# Web UI access port (default: 3000)
# Access the dashboard at http://your-host:WEB_EXTERNAL_PORT
WEB_EXTERNAL_PORT=$WEB_PORT
# API port (default: 3001)
# API_EXTERNAL_PORT=$API_PORT
# WebSocket port (default: 3002)
# WS_EXTERNAL_PORT=$WS_PORT
# -----------------------------------------------------------------------------
# Advanced Configuration (Usually don't need to change)
# -----------------------------------------------------------------------------
# Internal ports used inside the container
# WEB_PORT=3000
# API_PORT=3001
# COLLECTOR_WS_PORT=3002
# Database path inside container
# DB_PATH=/app/data/stats.db
# Node environment
# NODE_ENV=production
# =============================================================================
# After configuration, start the service with:
# docker compose up -d
#
# Then access: http://localhost:$WEB_PORT
# =============================================================================
EOF
print_success "Configuration file created: .env"
echo ""
}
# Create data directory
create_data_dir() {
if [ ! -d "./data" ]; then
mkdir -p ./data
print_success "Data directory created: ./data"
fi
}
# Show configuration summary
show_summary() {
echo ""
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Configuration Summary ║"
echo "╚════════════════════════════════════════════════════════╝"
echo ""
echo " Web UI Port: $WEB_PORT"
echo " API Port: $API_PORT"
echo " WebSocket Port: $WS_PORT"
echo ""
echo " Access URL:"
echo " http://localhost:$WEB_PORT"
echo ""
echo " Config file: .env"
echo ""
}
# Ask if user wants to start service
ask_start() {
read -p "Start the service now? [Y/n]: " start_now
start_now=${start_now:-Y}
if [[ $start_now =~ ^[Yy]$ ]]; then
echo ""
print_info "Starting Neko Master..."
echo ""
if command -v docker-compose &>/dev/null; then
docker-compose up -d
else
docker compose up -d
fi
echo ""
print_success "Service started!"
echo ""
echo " Please wait 10-20 seconds for services to fully initialize"
echo " Then access: http://localhost:$WEB_PORT"
echo ""
echo " Common commands:"
echo " View logs: docker logs -f neko-master"
echo " Stop: docker compose down"
echo " Restart: docker compose restart"
echo ""
else
echo ""
print_info "Configuration complete. Start later with:"
echo " docker compose up -d"
echo ""
fi
}
# Download docker-compose.yml if not present
download_compose_file() {
if [ ! -f "docker-compose.yml" ]; then
print_info "Downloading docker-compose.yml..."
if command -v curl &>/dev/null; then
curl -fsSL -o docker-compose.yml https://raw.githubusercontent.com/foru17/neko-master/main/docker-compose.yml
elif command -v wget &>/dev/null; then
wget -qO docker-compose.yml https://raw.githubusercontent.com/foru17/neko-master/main/docker-compose.yml
else
print_error "Neither curl nor wget is available"
exit 1
fi
print_success "docker-compose.yml downloaded"
fi
}
# Main function
main() {
show_welcome
# Check if Docker is installed
if ! command -v docker &>/dev/null; then
print_error "Docker is not installed"
echo ""
echo "Please install Docker first:"
echo " https://docs.docker.com/get-docker/"
echo ""
exit 1
fi
# Check docker-compose
if ! command -v docker-compose &>/dev/null && ! docker compose version &>/dev/null; then
print_error "Docker Compose is not installed"
echo ""
exit 1
fi
# Check if .env already exists
if [ -f ".env" ]; then
print_warning "Existing .env file detected"
read -p "Reconfigure? [y/N]: " reconfig
if [[ ! $reconfig =~ ^[Yy]$ ]]; then
print_info "Using existing configuration"
show_summary
ask_start
exit 0
fi
fi
# Check ports
check_ports
# Create configuration
create_env_file
create_data_dir
download_compose_file
# Show summary
show_summary
# Ask to start
ask_start
}
# Entry point dispatcher
case "${1:-}" in
agent | agent-install)
run_agent_installer
;;
"")
main
;;
*)
print_error "Unknown command: $1"
echo "Usage:"
echo " ./setup.sh # setup Neko Master service"
echo " ./setup.sh agent-install # install and run Neko Agent"
exit 1
;;
esac