mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-04 07:22:13 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
caae7e4603 | ||
|
|
a26db8b3e2 | ||
|
|
8e81e395b3 | ||
|
|
f0e89992f7 | ||
|
|
4eaa0cf14a |
@@ -1,44 +1,39 @@
|
||||
package sysutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
const serviceName = "sub2api"
|
||||
|
||||
// RestartService triggers a service restart via systemd.
|
||||
// RestartService triggers a service restart by gracefully exiting.
|
||||
//
|
||||
// IMPORTANT: This function initiates the restart and returns immediately.
|
||||
// The actual restart happens asynchronously - the current process will be killed
|
||||
// by systemd and a new process will be started.
|
||||
//
|
||||
// We use Start() instead of Run() because:
|
||||
// - systemctl restart will kill the current process first
|
||||
// - Run() waits for completion, but the process dies before completion
|
||||
// - Start() spawns the command independently, allowing systemd to handle the full cycle
|
||||
// This relies on systemd's Restart=always configuration to automatically
|
||||
// restart the service after it exits. This is the industry-standard approach:
|
||||
// - Simple and reliable
|
||||
// - No sudo permissions needed
|
||||
// - No complex process management
|
||||
// - Leverages systemd's native restart capability
|
||||
//
|
||||
// Prerequisites:
|
||||
// - Linux OS with systemd
|
||||
// - NOPASSWD sudo access configured (install.sh creates /etc/sudoers.d/sub2api)
|
||||
// - Service configured with Restart=always in systemd unit file
|
||||
func RestartService() error {
|
||||
if runtime.GOOS != "linux" {
|
||||
return fmt.Errorf("systemd restart only available on Linux")
|
||||
log.Println("Service restart via exit only works on Linux with systemd")
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Println("Initiating service restart...")
|
||||
log.Println("Initiating service restart by graceful exit...")
|
||||
log.Println("systemd will automatically restart the service (Restart=always)")
|
||||
|
||||
// The sub2api user has NOPASSWD sudo access for systemctl commands
|
||||
// (configured by install.sh in /etc/sudoers.d/sub2api).
|
||||
// Use -n (non-interactive) to prevent sudo from waiting for password input
|
||||
cmd := exec.Command("sudo", "-n", "systemctl", "restart", serviceName)
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Errorf("failed to initiate service restart: %w", err)
|
||||
}
|
||||
// Give a moment for logs to flush and response to be sent
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
log.Println("Service restart initiated successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -73,9 +73,6 @@ declare -A MSG_ZH=(
|
||||
["dirs_configured"]="目录配置完成"
|
||||
["installing_service"]="正在安装 systemd 服务..."
|
||||
["service_installed"]="systemd 服务已安装"
|
||||
["setting_up_sudoers"]="正在配置 sudoers..."
|
||||
["sudoers_configured"]="sudoers 配置完成"
|
||||
["sudoers_failed"]="sudoers 验证失败,已移除文件"
|
||||
["ready_for_setup"]="准备就绪,可以启动设置向导"
|
||||
|
||||
# Completion
|
||||
@@ -131,6 +128,15 @@ declare -A MSG_ZH=(
|
||||
["server_port_hint"]="建议使用 1024-65535 之间的端口"
|
||||
["server_config_summary"]="服务器配置"
|
||||
["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
|
||||
@@ -173,9 +179,6 @@ declare -A MSG_EN=(
|
||||
["dirs_configured"]="Directories configured"
|
||||
["installing_service"]="Installing systemd service..."
|
||||
["service_installed"]="Systemd service installed"
|
||||
["setting_up_sudoers"]="Setting up sudoers..."
|
||||
["sudoers_configured"]="Sudoers configured"
|
||||
["sudoers_failed"]="Sudoers validation failed, removing file"
|
||||
["ready_for_setup"]="Ready for Setup Wizard"
|
||||
|
||||
# Completion
|
||||
@@ -231,6 +234,15 @@ declare -A MSG_EN=(
|
||||
["server_port_hint"]="Recommended range: 1024-65535"
|
||||
["server_config_summary"]="Server configuration"
|
||||
["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
|
||||
@@ -260,9 +272,11 @@ print_error() {
|
||||
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() {
|
||||
[ -t 0 ]
|
||||
# Check if /dev/tty is available (works even when piped)
|
||||
[ -e /dev/tty ] && [ -r /dev/tty ] && [ -w /dev/tty ]
|
||||
}
|
||||
|
||||
# Select language
|
||||
@@ -282,7 +296,7 @@ select_language() {
|
||||
echo " 2) $(msg 'lang_en')"
|
||||
echo ""
|
||||
|
||||
read -p "$(msg 'enter_choice'): " lang_input
|
||||
read -p "$(msg 'enter_choice'): " lang_input < /dev/tty
|
||||
|
||||
case "$lang_input" in
|
||||
2|en|EN|english|English)
|
||||
@@ -323,7 +337,7 @@ configure_server() {
|
||||
|
||||
# Server host
|
||||
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
|
||||
SERVER_HOST="$input_host"
|
||||
fi
|
||||
@@ -333,7 +347,7 @@ configure_server() {
|
||||
# Server port
|
||||
echo -e "${YELLOW}$(msg 'server_port_hint')${NC}"
|
||||
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
|
||||
# Use default
|
||||
break
|
||||
@@ -521,35 +535,6 @@ setup_directories() {
|
||||
print_success "$(msg 'dirs_configured')"
|
||||
}
|
||||
|
||||
# Setup sudoers for service restart
|
||||
setup_sudoers() {
|
||||
print_info "$(msg 'setting_up_sudoers')"
|
||||
|
||||
# Always generate sudoers file from script (not from tar.gz)
|
||||
# This ensures the latest configuration is used even with older releases
|
||||
# Support both /bin/systemctl and /usr/bin/systemctl for different distros
|
||||
cat > /etc/sudoers.d/sub2api << 'EOF'
|
||||
# Sudoers configuration for Sub2API
|
||||
sub2api ALL=(ALL) NOPASSWD: /bin/systemctl restart sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /bin/systemctl stop sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /bin/systemctl start sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /usr/bin/systemctl stop sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /usr/bin/systemctl start sub2api
|
||||
EOF
|
||||
|
||||
# Set correct permissions (required for sudoers files)
|
||||
chmod 440 /etc/sudoers.d/sub2api
|
||||
|
||||
# Validate sudoers file
|
||||
if visudo -c -f /etc/sudoers.d/sub2api &>/dev/null; then
|
||||
print_success "$(msg 'sudoers_configured')"
|
||||
else
|
||||
print_warning "$(msg 'sudoers_failed')"
|
||||
rm -f /etc/sudoers.d/sub2api
|
||||
fi
|
||||
}
|
||||
|
||||
# Install systemd service
|
||||
install_service() {
|
||||
print_info "$(msg 'installing_service')"
|
||||
@@ -601,13 +586,61 @@ prepare_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() {
|
||||
local ip_addr
|
||||
ip_addr=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "YOUR_SERVER_IP")
|
||||
|
||||
# Use PUBLIC_IP which was set by get_public_ip()
|
||||
# Determine display address
|
||||
local display_host="$ip_addr"
|
||||
local display_host="${PUBLIC_IP:-YOUR_SERVER_IP}"
|
||||
if [ "$SERVER_HOST" = "127.0.0.1" ]; then
|
||||
display_host="127.0.0.1"
|
||||
fi
|
||||
@@ -621,21 +654,9 @@ print_completion() {
|
||||
echo "$(msg 'server_config_summary'): ${SERVER_HOST}:${SERVER_PORT}"
|
||||
echo ""
|
||||
echo "=============================================="
|
||||
echo " $(msg 'next_steps')"
|
||||
echo " $(msg 'step4_open_wizard')"
|
||||
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}"
|
||||
echo ""
|
||||
echo " $(msg 'wizard_guide')"
|
||||
@@ -702,7 +723,7 @@ uninstall() {
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
read -p "$(msg 'are_you_sure') " -n 1 -r
|
||||
read -p "$(msg 'are_you_sure') " -n 1 -r < /dev/tty
|
||||
echo
|
||||
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||
print_info "$(msg 'uninstall_cancelled')"
|
||||
@@ -716,7 +737,6 @@ uninstall() {
|
||||
|
||||
print_info "$(msg 'removing_files')"
|
||||
rm -f /etc/systemd/system/sub2api.service
|
||||
rm -f /etc/sudoers.d/sub2api
|
||||
systemctl daemon-reload
|
||||
|
||||
print_info "$(msg 'removing_install_dir')"
|
||||
@@ -787,8 +807,10 @@ main() {
|
||||
create_user
|
||||
setup_directories
|
||||
install_service
|
||||
setup_sudoers
|
||||
prepare_for_setup
|
||||
get_public_ip
|
||||
start_service
|
||||
enable_autostart
|
||||
print_completion
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
# Sudoers configuration for Sub2API
|
||||
# This file allows the sub2api service user to restart the service without password
|
||||
#
|
||||
# Installation:
|
||||
# sudo cp sub2api-sudoers /etc/sudoers.d/sub2api
|
||||
# sudo chmod 440 /etc/sudoers.d/sub2api
|
||||
#
|
||||
# SECURITY NOTE: This grants limited sudo access only for service management
|
||||
|
||||
# Allow sub2api user to restart the service without password
|
||||
# Support both /bin/systemctl (Debian/Ubuntu) and /usr/bin/systemctl (RHEL/CentOS)
|
||||
sub2api ALL=(ALL) NOPASSWD: /bin/systemctl restart sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /bin/systemctl stop sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /bin/systemctl start sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /usr/bin/systemctl stop sub2api
|
||||
sub2api ALL=(ALL) NOPASSWD: /usr/bin/systemctl start sub2api
|
||||
@@ -9,4 +9,8 @@ const app = createApp(App)
|
||||
app.use(createPinia())
|
||||
app.use(router)
|
||||
app.use(i18n)
|
||||
app.mount('#app')
|
||||
|
||||
// 等待路由器完成初始导航后再挂载,避免竞态条件导致的空白渲染
|
||||
router.isReady().then(() => {
|
||||
app.mount('#app')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user