Merge pull request #32 from Plaud-AI/feat_config

feat: 增加获取公网ip
This commit is contained in:
Leo 2025-12-09 16:26:24 +08:00 committed by GitHub
commit 20344c518d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 82 additions and 13 deletions

View file

@ -5,19 +5,11 @@ server:
port: 8000
# http服务的端口,用于简单OTA接口(单服务部署),以及视觉分析接口
http_port: 8003
# 这个websocket配置是指ota接口向设备发送的websocket地址
# 如果按默认的写法,ota接口会自动生成websocket地址,并输出在启动日志里,这个地址你可以直接用浏览器访问ota接口确认一下
# 当你使用docker部署或使用公网部署(使用ssl、域名)时,不一定准确
# 所以如果你使用docker部署时,将websocket设置成局域网地址
# 如果你使用公网部署时,将vwebsocket设置成公网地址
websocket: ws://你的ip或者域名:端口号/xiaozhi/v1/
# 视觉分析接口地址
# 向设备发送的视觉分析的接口地址
# 如果按下面默认的写法,系统会自动生成视觉识别地址,并输出在启动日志里,这个地址你可以直接用浏览器访问确认一下
# 当你使用docker部署或使用公网部署(使用ssl、域名)时,不一定准确
# 所以如果你使用docker部署时,将vision_explain设置成局域网地址
# 如果你使用公网部署时,将vision_explain设置成公网地址
vision_explain: http://你的ip或者域名:端口号/mcp/vision/explain
# WebSocket URL for OTA response (auto-detected by start.sh)
# Uses environment variable WEBSOCKET_URL, falls back to auto-detection
websocket: ${WEBSOCKET_URL:-ws://localhost:8000/xiaozhi/v1/}
# Vision explain URL (auto-detected by start.sh)
vision_explain: http://${PUBLIC_IP:-localhost}:8003/mcp/vision/explain
# OTA返回信息时区偏移量
timezone_offset: +8
# 认证配置

View file

@ -135,6 +135,83 @@ if [ ! -f "$COMPOSE_FILE" ]; then
exit 1
fi
# ============================================================================
# Auto-detect public IP and update .env file
# ============================================================================
get_public_ip() {
local ip=""
# Method 1: AWS EC2 Metadata Service (IMDSv1)
ip=$(curl -s --connect-timeout 2 http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null || true)
# Method 2: AWS EC2 Metadata Service (IMDSv2)
if [ -z "$ip" ]; then
local token=$(curl -s --connect-timeout 2 -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" 2>/dev/null || true)
if [ -n "$token" ]; then
ip=$(curl -s --connect-timeout 2 -H "X-aws-ec2-metadata-token: $token" http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null || true)
fi
fi
# Method 3: External service
if [ -z "$ip" ]; then
ip=$(curl -s --connect-timeout 3 https://ifconfig.me 2>/dev/null || true)
fi
# Method 4: Backup external service
if [ -z "$ip" ]; then
ip=$(curl -s --connect-timeout 3 https://api.ipify.org 2>/dev/null || true)
fi
echo "$ip"
}
init_env_file() {
local ENV_FILE=".env"
local public_ip=""
# For local region, skip public IP detection
if [ "$REGION" = "local" ] || [ "$REGION" = "LOCAL" ]; then
echo -e "${BLUE}→ Local region detected, using localhost${NC}"
public_ip="localhost"
else
echo -e "${BLUE}→ Detecting public IP for ${REGION_NAME}...${NC}"
public_ip=$(get_public_ip)
if [ -z "$public_ip" ]; then
echo -e "${YELLOW} Warning: Cannot detect public IP, using localhost${NC}"
public_ip="localhost"
else
echo -e "${GREEN} Public IP: ${public_ip}${NC}"
fi
fi
# Build URLs
local websocket_url="ws://${public_ip}:8000/xiaozhi/v1/"
local ota_url="http://${public_ip}:8003/xiaozhi/ota/"
# Create or update .env file
if [ -f "$ENV_FILE" ]; then
# Remove old entries (keep other configs)
grep -v "^PUBLIC_IP=" "$ENV_FILE" | grep -v "^WEBSOCKET_URL=" | grep -v "^OTA_URL=" | grep -v "^# Auto-generated" > "${ENV_FILE}.tmp" 2>/dev/null || true
mv "${ENV_FILE}.tmp" "$ENV_FILE" 2>/dev/null || touch "$ENV_FILE"
else
touch "$ENV_FILE"
fi
# Append new config
echo "" >> "$ENV_FILE"
echo "# Auto-generated by start.sh at $(date '+%Y-%m-%d %H:%M:%S') for region: ${REGION_NAME}" >> "$ENV_FILE"
echo "PUBLIC_IP=$public_ip" >> "$ENV_FILE"
echo "WEBSOCKET_URL=$websocket_url" >> "$ENV_FILE"
echo "OTA_URL=$ota_url" >> "$ENV_FILE"
echo -e "${GREEN} WEBSOCKET_URL=${websocket_url}${NC}"
echo ""
}
# Initialize environment variables
init_env_file
echo -e "${GREEN}Configuration:${NC}"
echo " Region: ${REGION_NAME} (${REGION_ENV})"
echo " Action: ${ACTION_NAME}"