ip2region增加IP格式检查

This commit is contained in:
刘祥超
2021-06-27 17:29:16 +08:00
parent 4bc6f93902
commit 4869c11d60
2 changed files with 28 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/lionsoul2014/ip2region/binding/golang/ip2region"
"net"
"strings"
)
@@ -27,10 +28,13 @@ func (this *IP2RegionLibrary) Lookup(ip string) (*Result, error) {
return nil, errors.New("library has not been loaded")
}
// 暂不支持IPv6
// 支持IPv4
if strings.Contains(ip, ":") {
return nil, nil
}
if net.ParseIP(ip) == nil {
return nil, nil
}
defer func() {
// 防止panic发生

View File

@@ -24,6 +24,29 @@ func TestIP2RegionLibrary_Lookup(t *testing.T) {
logs.PrintAsJSON(result, t)
}
func TestIP2RegionLibrary_Lookup_Valid_IP(t *testing.T) {
library := &IP2RegionLibrary{}
err := library.Load(Tea.Root + "/resources/ipdata/ip2region/ip2region.db")
if err != nil {
t.Fatal(err)
}
{
result, err := library.Lookup("114.240.223")
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(result, t)
}
{
result, err := library.Lookup("abc")
if err != nil {
t.Fatal(err)
}
logs.PrintAsJSON(result, t)
}
}
func TestIP2RegionLibrary_Memory(t *testing.T) {
library := &IP2RegionLibrary{}
err := library.Load(Tea.Root + "/resources/ipdata/ip2region/ip2region.db")