package third

import (
	"github.com/oschwald/geoip2-golang"
	"net"
)

func GetCountryByIP(ip string) (string, error) {
	// 打开GeoIP数据库
	db, err := geoip2.Open("GeoLite2-Country.mmdb")
	if err != nil {
		return "default", err
	}
	defer db.Close()

	// 查询IP地址
	country, err := db.Country(net.ParseIP(ip))
	if err != nil {
		return "default", err
	}

	// 返回国家名称
	return country.Country.IsoCode, nil
}
