24 lines
669 B
Go
24 lines
669 B
Go
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 // 返回国家代码或地理位置信息
|
||
}
|