1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 13:19:02 +08:00

fix: Match IPv4 in IPv6 #70

Signed-off-by: zu1k <i@lgf.im>
This commit is contained in:
zu1k 2021-12-16 13:01:32 +08:00
parent 1e116c8e70
commit 4e22a5cf2b
5 changed files with 33 additions and 21 deletions

View File

@ -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

View File

@ -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的地理信息

View File

@ -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{

View File

@ -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})?`)
)

View File

@ -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))
}
}