From 802b42d7f30b46bfbec63c3c275ea326ededa204 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 24 Aug 2023 11:50:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=20--=20=E5=85=81=E8=AE=B8=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E7=9A=84=E5=9B=BD=E5=AE=B6=E5=92=8C=E5=9C=B0=E5=8C=BA=20?= =?UTF-8?q?=E4=B8=AD=E4=B8=8D=E8=83=BD=E4=BD=BF=E7=94=A8=E4=B8=AD=E5=9B=BD?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=8C=BA=E5=9F=9F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/web/helpers/utils.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/internal/web/helpers/utils.go b/internal/web/helpers/utils.go index 3df5ca4e..cc38f110 100644 --- a/internal/web/helpers/utils.go +++ b/internal/web/helpers/utils.go @@ -2,10 +2,10 @@ package helpers import ( "github.com/TeaOSLab/EdgeAdmin/internal/events" - nodes "github.com/TeaOSLab/EdgeAdmin/internal/rpc" "github.com/TeaOSLab/EdgeAdmin/internal/utils" "github.com/TeaOSLab/EdgeCommon/pkg/configutils" - "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/TeaOSLab/EdgeCommon/pkg/iplibrary" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/regionconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs" "github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/logs" @@ -77,23 +77,25 @@ func checkIPWithoutCache(config *systemconfigs.SecurityConfig, ipAddr string) bo // 检查位置 if len(config.AllowCountryIds) > 0 || len(config.AllowProvinceIds) > 0 { - rpc, err := nodes.SharedRPC() - if err != nil { - logs.Println("[USER_MUST_AUTH][ERROR]" + err.Error()) + var userRegion = iplibrary.Lookup(ip) + if userRegion == nil || !userRegion.IsOk() { return false } - resp, err := rpc.IPLibraryRPC().LookupIPRegion(rpc.Context(0), &pb.LookupIPRegionRequest{Ip: ipAddr}) - if err != nil { - logs.Println("[USER_MUST_AUTH][ERROR]" + err.Error()) - return false + if len(config.AllowCountryIds) > 0 { + // 检查大中华区 + var found = false + for _, countryId := range config.AllowCountryIds { + if regionconfigs.MatchUserRegion(userRegion.CountryId(), userRegion.ProvinceId(), countryId) { + found = true + break + } + } + + if !found { + return false + } } - if resp.IpRegion == nil { - return true - } - if len(config.AllowCountryIds) > 0 && !lists.ContainsInt64(config.AllowCountryIds, resp.IpRegion.CountryId) { - return false - } - if len(config.AllowProvinceIds) > 0 && !lists.ContainsInt64(config.AllowProvinceIds, resp.IpRegion.ProvinceId) { + if len(config.AllowProvinceIds) > 0 && !lists.ContainsInt64(config.AllowProvinceIds, userRegion.ProvinceId()) { return false } }