admin_backend/util/geoip.go
2026-02-25 10:16:35 +08:00

24 lines
669 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package util
import (
"net"
"github.com/oschwald/geoip2-golang"
)
func GetGeoInfo(ip string) (string, string, error) {
// 这里可以使用第三方库或服务来获取地理位置信息
// 例如,使用 "github.com/oschwald/geoip2-golang" 库读取 MaxMind 的 GeoIP 数据库
// 或者调用第三方 API如 ipinfo.io、ipstack.com 等
db, err := geoip2.Open("./GeoLite2-Country/GeoLite2-City.mmdb")
if err != nil {
return "", "", err
}
defer db.Close()
city, err := db.City(net.ParseIP(ip))
if err != nil {
return "", "", err
}
return city.Country.Names["zh-CN"], city.City.Names["zh-CN"], nil // 返回国家代码或地理位置信息
}