feat: 改进安装脚本的交互体验和自动化流程
- 修复 curl | bash 管道模式下无法交互式输入的问题 - 使用 /dev/tty 检测终端可用性替代 stdin 检测 - 所有 read 命令从 /dev/tty 读取用户输入 - 安装完成后自动启动服务和启用开机自启 - 使用 ipinfo.io API 获取公网 IP 用于显示访问地址 - 简化安装完成后的输出信息
This commit is contained in:
@@ -128,6 +128,15 @@ declare -A MSG_ZH=(
|
|||||||
["server_port_hint"]="建议使用 1024-65535 之间的端口"
|
["server_port_hint"]="建议使用 1024-65535 之间的端口"
|
||||||
["server_config_summary"]="服务器配置"
|
["server_config_summary"]="服务器配置"
|
||||||
["invalid_port"]="无效端口号,请输入 1-65535 之间的数字"
|
["invalid_port"]="无效端口号,请输入 1-65535 之间的数字"
|
||||||
|
|
||||||
|
# Service management
|
||||||
|
["starting_service"]="正在启动服务..."
|
||||||
|
["service_started"]="服务已启动"
|
||||||
|
["service_start_failed"]="服务启动失败,请检查日志"
|
||||||
|
["enabling_autostart"]="正在设置开机自启..."
|
||||||
|
["autostart_enabled"]="开机自启已启用"
|
||||||
|
["getting_public_ip"]="正在获取公网 IP..."
|
||||||
|
["public_ip_failed"]="无法获取公网 IP,使用本地 IP"
|
||||||
)
|
)
|
||||||
|
|
||||||
# English strings
|
# English strings
|
||||||
@@ -225,6 +234,15 @@ declare -A MSG_EN=(
|
|||||||
["server_port_hint"]="Recommended range: 1024-65535"
|
["server_port_hint"]="Recommended range: 1024-65535"
|
||||||
["server_config_summary"]="Server configuration"
|
["server_config_summary"]="Server configuration"
|
||||||
["invalid_port"]="Invalid port number, please enter a number between 1-65535"
|
["invalid_port"]="Invalid port number, please enter a number between 1-65535"
|
||||||
|
|
||||||
|
# Service management
|
||||||
|
["starting_service"]="Starting service..."
|
||||||
|
["service_started"]="Service started"
|
||||||
|
["service_start_failed"]="Service failed to start, please check logs"
|
||||||
|
["enabling_autostart"]="Enabling auto-start on boot..."
|
||||||
|
["autostart_enabled"]="Auto-start enabled"
|
||||||
|
["getting_public_ip"]="Getting public IP..."
|
||||||
|
["public_ip_failed"]="Failed to get public IP, using local IP"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get message based on current language
|
# Get message based on current language
|
||||||
@@ -254,9 +272,11 @@ print_error() {
|
|||||||
echo -e "${RED}[$(msg 'error')]${NC} $1"
|
echo -e "${RED}[$(msg 'error')]${NC} $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if running interactively (stdin is a terminal)
|
# Check if running interactively (can access terminal)
|
||||||
|
# When piped (curl | bash), stdin is not a terminal, but /dev/tty may still be available
|
||||||
is_interactive() {
|
is_interactive() {
|
||||||
[ -t 0 ]
|
# Check if /dev/tty is available (works even when piped)
|
||||||
|
[ -e /dev/tty ] && [ -r /dev/tty ] && [ -w /dev/tty ]
|
||||||
}
|
}
|
||||||
|
|
||||||
# Select language
|
# Select language
|
||||||
@@ -276,7 +296,7 @@ select_language() {
|
|||||||
echo " 2) $(msg 'lang_en')"
|
echo " 2) $(msg 'lang_en')"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
read -p "$(msg 'enter_choice'): " lang_input
|
read -p "$(msg 'enter_choice'): " lang_input < /dev/tty
|
||||||
|
|
||||||
case "$lang_input" in
|
case "$lang_input" in
|
||||||
2|en|EN|english|English)
|
2|en|EN|english|English)
|
||||||
@@ -317,7 +337,7 @@ configure_server() {
|
|||||||
|
|
||||||
# Server host
|
# Server host
|
||||||
echo -e "${YELLOW}$(msg 'server_host_hint')${NC}"
|
echo -e "${YELLOW}$(msg 'server_host_hint')${NC}"
|
||||||
read -p "$(msg 'server_host_prompt') [${SERVER_HOST}]: " input_host
|
read -p "$(msg 'server_host_prompt') [${SERVER_HOST}]: " input_host < /dev/tty
|
||||||
if [ -n "$input_host" ]; then
|
if [ -n "$input_host" ]; then
|
||||||
SERVER_HOST="$input_host"
|
SERVER_HOST="$input_host"
|
||||||
fi
|
fi
|
||||||
@@ -327,7 +347,7 @@ configure_server() {
|
|||||||
# Server port
|
# Server port
|
||||||
echo -e "${YELLOW}$(msg 'server_port_hint')${NC}"
|
echo -e "${YELLOW}$(msg 'server_port_hint')${NC}"
|
||||||
while true; do
|
while true; do
|
||||||
read -p "$(msg 'server_port_prompt') [${SERVER_PORT}]: " input_port
|
read -p "$(msg 'server_port_prompt') [${SERVER_PORT}]: " input_port < /dev/tty
|
||||||
if [ -z "$input_port" ]; then
|
if [ -z "$input_port" ]; then
|
||||||
# Use default
|
# Use default
|
||||||
break
|
break
|
||||||
@@ -566,13 +586,61 @@ prepare_for_setup() {
|
|||||||
print_success "$(msg 'ready_for_setup')"
|
print_success "$(msg 'ready_for_setup')"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get public IP address
|
||||||
|
get_public_ip() {
|
||||||
|
print_info "$(msg 'getting_public_ip')"
|
||||||
|
|
||||||
|
# Try to get public IP from ipinfo.io
|
||||||
|
local response
|
||||||
|
response=$(curl -s --connect-timeout 5 --max-time 10 "https://ipinfo.io/json" 2>/dev/null)
|
||||||
|
|
||||||
|
if [ -n "$response" ]; then
|
||||||
|
# Extract IP from JSON response using grep and sed (no jq dependency)
|
||||||
|
PUBLIC_IP=$(echo "$response" | grep -o '"ip": *"[^"]*"' | sed 's/"ip": *"\([^"]*\)"/\1/')
|
||||||
|
if [ -n "$PUBLIC_IP" ]; then
|
||||||
|
print_success "Public IP: $PUBLIC_IP"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Fallback to local IP
|
||||||
|
print_warning "$(msg 'public_ip_failed')"
|
||||||
|
PUBLIC_IP=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "YOUR_SERVER_IP")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Start service
|
||||||
|
start_service() {
|
||||||
|
print_info "$(msg 'starting_service')"
|
||||||
|
|
||||||
|
if systemctl start sub2api; then
|
||||||
|
print_success "$(msg 'service_started')"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
print_error "$(msg 'service_start_failed')"
|
||||||
|
print_info "sudo journalctl -u sub2api -n 50"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Enable service auto-start
|
||||||
|
enable_autostart() {
|
||||||
|
print_info "$(msg 'enabling_autostart')"
|
||||||
|
|
||||||
|
if systemctl enable sub2api 2>/dev/null; then
|
||||||
|
print_success "$(msg 'autostart_enabled')"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
print_warning "Failed to enable auto-start"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Print completion message
|
# Print completion message
|
||||||
print_completion() {
|
print_completion() {
|
||||||
local ip_addr
|
# Use PUBLIC_IP which was set by get_public_ip()
|
||||||
ip_addr=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "YOUR_SERVER_IP")
|
|
||||||
|
|
||||||
# Determine display address
|
# Determine display address
|
||||||
local display_host="$ip_addr"
|
local display_host="${PUBLIC_IP:-YOUR_SERVER_IP}"
|
||||||
if [ "$SERVER_HOST" = "127.0.0.1" ]; then
|
if [ "$SERVER_HOST" = "127.0.0.1" ]; then
|
||||||
display_host="127.0.0.1"
|
display_host="127.0.0.1"
|
||||||
fi
|
fi
|
||||||
@@ -586,21 +654,9 @@ print_completion() {
|
|||||||
echo "$(msg 'server_config_summary'): ${SERVER_HOST}:${SERVER_PORT}"
|
echo "$(msg 'server_config_summary'): ${SERVER_HOST}:${SERVER_PORT}"
|
||||||
echo ""
|
echo ""
|
||||||
echo "=============================================="
|
echo "=============================================="
|
||||||
echo " $(msg 'next_steps')"
|
echo " $(msg 'step4_open_wizard')"
|
||||||
echo "=============================================="
|
echo "=============================================="
|
||||||
echo ""
|
echo ""
|
||||||
echo " 1. $(msg 'step1_check_services')"
|
|
||||||
echo " sudo systemctl status postgresql"
|
|
||||||
echo " sudo systemctl status redis"
|
|
||||||
echo ""
|
|
||||||
echo " 2. $(msg 'step2_start_service')"
|
|
||||||
echo " sudo systemctl start sub2api"
|
|
||||||
echo ""
|
|
||||||
echo " 3. $(msg 'step3_enable_autostart')"
|
|
||||||
echo " sudo systemctl enable sub2api"
|
|
||||||
echo ""
|
|
||||||
echo " 4. $(msg 'step4_open_wizard')"
|
|
||||||
echo ""
|
|
||||||
print_info " http://${display_host}:${SERVER_PORT}"
|
print_info " http://${display_host}:${SERVER_PORT}"
|
||||||
echo ""
|
echo ""
|
||||||
echo " $(msg 'wizard_guide')"
|
echo " $(msg 'wizard_guide')"
|
||||||
@@ -667,7 +723,7 @@ uninstall() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
read -p "$(msg 'are_you_sure') " -n 1 -r
|
read -p "$(msg 'are_you_sure') " -n 1 -r < /dev/tty
|
||||||
echo
|
echo
|
||||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||||
print_info "$(msg 'uninstall_cancelled')"
|
print_info "$(msg 'uninstall_cancelled')"
|
||||||
@@ -752,6 +808,9 @@ main() {
|
|||||||
setup_directories
|
setup_directories
|
||||||
install_service
|
install_service
|
||||||
prepare_for_setup
|
prepare_for_setup
|
||||||
|
get_public_ip
|
||||||
|
start_service
|
||||||
|
enable_autostart
|
||||||
print_completion
|
print_completion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user