mh_goadmin_server/tools/ip.go

60 lines
1.2 KiB
Go

package tools
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
)
// 获取外网ip地址
func GetLocation(ip string) string {
if ip == "127.0.0.1" || ip == "localhost" || ip == "::1" {
return "内部IP"
}
resp, err := http.Get("https://restapi.amap.com/v3/ip?ip=" + ip + "&key=3fabc36c20379fbb9300c79b19d5d05e")
if err != nil {
panic(err)
}
defer resp.Body.Close()
s, err := io.ReadAll(resp.Body)
fmt.Printf(string(s))
m := make(map[string]string)
err = json.Unmarshal(s, &m)
if err != nil {
fmt.Println("Umarshal failed:", err)
}
if m["province"] == "" {
return "未知位置"
}
return m["province"] + "-" + m["city"]
}
// 获取局域网ip地址
func GetLocaHonst() string {
netInterfaces, err := net.Interfaces()
if err != nil {
fmt.Println("net.Interfaces failed, err:", err.Error())
}
for i := 0; i < len(netInterfaces); i++ {
if (netInterfaces[i].Flags & net.FlagUp) != 0 {
addrs, _ := netInterfaces[i].Addrs()
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
}
}
return ""
}