package main

import (
	"fmt"
	"io"
	"log"
	"net"
	"net/http"
	"strings"
)

func GetLocalIpv4Addr() string {
	resp, err := http.Get("https://inet-ip.aixiao.me/")
	if err != nil {
		fmt.Printf("请求失败: %s\n", err)
		return "NULL"
	}
	defer resp.Body.Close()

	ip, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("读取响应失败: %s\n", err)
		return "NULL"
	}

	ipStr := strings.TrimSpace(string(ip))
	// ✅ 验证是否是合法 IPv4
	if net.ParseIP(ipStr) == nil || strings.Contains(ipStr, "<") {
		fmt.Printf("无效的IP响应: %q\n", ipStr)
		return "NULL"
	}

	log.Printf(" 公网IP: %s\n", ipStr)
	return ipStr
}
