mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
feat: use embed geoip database (#396)
* feat: use embed geoip database * mention ipinfo * only read once * comments comments * chore: installer version --------- Co-authored-by: naiba <hi@nai.ba>
This commit is contained in:
@@ -4,10 +4,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/naiba/nezha/pkg/ddns"
|
||||
"github.com/naiba/nezha/pkg/geoip"
|
||||
"github.com/naiba/nezha/pkg/grpcx"
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
|
||||
@@ -217,3 +219,27 @@ func (s *NezhaHandler) IOStream(stream pb.NezhaService_IOStreamServer) error {
|
||||
iw.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NezhaHandler) LookupGeoIP(c context.Context, r *pb.GeoIP) (*pb.GeoIP, error) {
|
||||
var clientID uint64
|
||||
var err error
|
||||
if clientID, err = s.Auth.Check(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 根据内置数据库查询 IP 地理位置
|
||||
record := &geoip.IPInfo{}
|
||||
ip := r.GetIp()
|
||||
netIP := net.ParseIP(ip)
|
||||
location, err := geoip.Lookup(netIP, record)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 将地区码写入到 Host
|
||||
singleton.ServerLock.RLock()
|
||||
defer singleton.ServerLock.RUnlock()
|
||||
singleton.ServerList[clientID].Host.CountryCode = location
|
||||
|
||||
return &pb.GeoIP{Ip: ip, CountryCode: location}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user