From 4e22a5cf2b18d87e5c021f79f0173cb2a7ca5e7c Mon Sep 17 00:00:00 2001 From: zu1k Date: Thu, 16 Dec 2021 13:01:32 +0800 Subject: [PATCH] fix: Match IPv4 in IPv6 #70 Signed-off-by: zu1k --- README.md | 10 ++-------- README_zh-CN.md | 8 +------- internal/entity/parse.go | 20 +++++++++++++++----- internal/re/re.go | 2 +- internal/re/re_test.go | 14 ++++++++++++++ 5 files changed, 33 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d1ffbb5..9b0f60c 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,10 @@ However the C version has too few functions, and the js version is too big and t ### Install from source -Nali Requires Go >= 1.14. You can build it from source: +Nali Requires Go >= 1.18. You can build it from source: ```sh -$ go get -u -v github.com/zu1k/nali +$ go install github.com/zu1k/nali ``` ### Install pre-build binariy @@ -57,12 +57,6 @@ Pre-built binaries are available here: [release](https://github.com/zu1k/nali/re Download the binary compatible with your platform, unpack and copy to the directory in path -### Install from docker - -``` -docker pull docker.pkg.github.com//zu1k/nali/nali:latest -``` - ## Usage ### Query a simple IP address diff --git a/README_zh-CN.md b/README_zh-CN.md index cde8a7f..090d669 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -47,7 +47,7 @@ Nali 需要预先安装 Go. 安装后可以从源码安装软件: ```sh -$ go get -u -v github.com/zu1k/nali +$ go install github.com/zu1k/nali ``` ### 下载预编译的可执行程序 @@ -56,12 +56,6 @@ $ go get -u -v github.com/zu1k/nali 你需要选择适合你系统和硬件架构的版本下载,解压后可直接运行 -### 使用 Docker 版本 - -``` -docker pull docker.pkg.github.com//zu1k/nali/nali:latest -``` - ## 使用说明 ### 查询一个IP的地理信息 diff --git a/internal/entity/parse.go b/internal/entity/parse.go index cfe38e1..38a6f69 100644 --- a/internal/entity/parse.go +++ b/internal/entity/parse.go @@ -1,6 +1,7 @@ package entity import ( + "net/netip" "sort" "github.com/zu1k/nali/internal/db" @@ -23,11 +24,20 @@ func ParseLine(line string) Entities { }) } for _, e := range ip6sLoc { - tmp = append(tmp, &Entity{ - Loc: e, - Type: TypeIPv6, - Text: line[e[0]:e[1]], - }) + text := line[e[0]:e[1]] + if ip, _ := netip.ParseAddr(text); ip.Is4In6() { + tmp = append(tmp, &Entity{ + Loc: e, + Type: TypeIPv4, + Text: ip.Unmap().String(), + }) + } else { + tmp = append(tmp, &Entity{ + Loc: e, + Type: TypeIPv6, + Text: text, + }) + } } for _, e := range domainsLoc { tmp = append(tmp, &Entity{ diff --git a/internal/re/re.go b/internal/re/re.go index 2e6fda6..a6bae06 100644 --- a/internal/re/re.go +++ b/internal/re/re.go @@ -6,5 +6,5 @@ var ( DomainRe = regexp.MustCompile(`([a-zA-Z0-9][-a-zA-Z0-9]{0,62}\.)+([a-zA-Z][-a-zA-Z]{0,62})`) IPv4Re = regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`) - IPv6Re = regexp.MustCompile(`fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?`) + IPv6Re = regexp.MustCompile(`fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::[fF]{4}:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?`) ) diff --git a/internal/re/re_test.go b/internal/re/re_test.go index b3cf43b..989d5a0 100644 --- a/internal/re/re_test.go +++ b/internal/re/re_test.go @@ -21,3 +21,17 @@ func TestDomainRe(t *testing.T) { fmt.Println(DomainRe.FindAllString(domain, -1)) } } + +var validIPv6List = []string{ + "::ffff:104.26.11.119", +} + +func TestIPv6Re(t *testing.T) { + for _, ip := range validIPv6List { + if !IPv6Re.MatchString(ip) { + t.Error(ip) + t.Fail() + } + fmt.Println(IPv6Re.FindAllString(ip, -1)) + } +}