From fa686fe62fc03691e2281820f153f198d0bbc90b Mon Sep 17 00:00:00 2001 From: jingrow Date: Fri, 8 Aug 2025 05:42:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96PUBLIC=5FSITE=5FURL=E7=9A=84?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- install_jsite.sh | 82 ++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/install_jsite.sh b/install_jsite.sh index 6becd8d..0a2fd66 100755 --- a/install_jsite.sh +++ b/install_jsite.sh @@ -39,6 +39,48 @@ SERVER_URL="https://admin.jingrow.com" API_KEY="535bc122f3e364c" API_SECRET="8629a3b12fc1cc2" +# ======================================== +# 工具函数 +# ======================================== + +# 智能IP选择:优先使用内网IP,没有内网IP时使用公网IP +get_optimal_host_ip() { + local host_ip="" + + # 方法1: 尝试获取内网IP + local private_ip=$(ip route get 8.8.8.8 2>/dev/null | awk '{print $7}' | head -1) + if [ -n "$private_ip" ] && [ "$private_ip" != "8.8.8.8" ]; then + # 测试内网IP是否可达 + if ping -c 2 -W 1 "$private_ip" >/dev/null 2>&1; then + host_ip="$private_ip" + log_success "使用内网IP: $host_ip" + else + log_warning "内网IP不可达: $private_ip" + fi + fi + + # 方法2: 如果没有内网IP或内网IP不可达,使用公网IP + if [ -z "$host_ip" ]; then + if [ -n "$PUBLIC_IP" ]; then + host_ip="$PUBLIC_IP" + log_info "使用指定的公网IP: $host_ip" + else + # 尝试自动获取公网IP + local auto_public_ip=$(curl -s --max-time 5 ifconfig.me 2>/dev/null || curl -s --max-time 5 ipinfo.io/ip 2>/dev/null) + if [ -n "$auto_public_ip" ]; then + host_ip="$auto_public_ip" + log_info "自动获取公网IP: $host_ip" + else + # 最后回退:使用第一个可用IP + host_ip=$(hostname -I | awk '{print $1}') + log_warning "使用备用IP: $host_ip" + fi + fi + fi + + echo "$host_ip" +} + # 解析命令行参数 while [[ $# -gt 0 ]]; do case $1 in @@ -560,8 +602,11 @@ create_env_file() { # 获取项目端口 local project_port=$(get_or_assign_port "$SITE_NAME") - # 构建本地地址URL(用于PUBLIC_SITE_URL) - local public_site_url="http://127.0.0.1:$project_port" + # 智能IP选择:优先使用内网IP,没有内网IP时使用公网IP(与traefik配置保持一致) + local host_ip=$(get_optimal_host_ip) + + # 构建PUBLIC_SITE_URL(优先使用内网IP,没有内网IP时使用公网IP) + local public_site_url="http://$host_ip:$project_port" # 检查.env文件是否已存在 if [ -f "/home/jingrow/jsite/$SITE_NAME/.env" ]; then @@ -909,38 +954,7 @@ create_traefik_website_config() { local project_port=$(get_or_assign_port "$SITE_NAME") # 智能IP选择:优先使用内网IP,没有内网IP时使用公网IP - local host_ip="" - - # 方法1: 尝试获取内网IP - local private_ip=$(ip route get 8.8.8.8 2>/dev/null | awk '{print $7}' | head -1) - if [ -n "$private_ip" ] && [ "$private_ip" != "8.8.8.8" ]; then - # 测试内网IP是否可达 - if ping -c 2 -W 1 "$private_ip" >/dev/null 2>&1; then - host_ip="$private_ip" - log_success "使用内网IP: $host_ip" - else - log_warning "内网IP不可达: $private_ip" - fi - fi - - # 方法2: 如果没有内网IP或内网IP不可达,使用公网IP - if [ -z "$host_ip" ]; then - if [ -n "$PUBLIC_IP" ]; then - host_ip="$PUBLIC_IP" - log_info "使用指定的公网IP: $host_ip" - else - # 尝试自动获取公网IP - local auto_public_ip=$(curl -s --max-time 5 ifconfig.me 2>/dev/null || curl -s --max-time 5 ipinfo.io/ip 2>/dev/null) - if [ -n "$auto_public_ip" ]; then - host_ip="$auto_public_ip" - log_info "自动获取公网IP: $host_ip" - else - # 最后回退:使用第一个可用IP - host_ip=$(hostname -I | awk '{print $1}') - log_warning "使用备用IP: $host_ip" - fi - fi - fi + local host_ip=$(get_optimal_host_ip) # 生成Host规则 local host_rule=$(generate_host_rule "$SITE_URL")