mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-15 23:26:35 +08:00
ip2region增加IP格式检查
This commit is contained in:
@@ -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发生
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user