Skip to content

Commit

Permalink
fix: Match IPv4 in IPv6 #70
Browse files Browse the repository at this point in the history
Signed-off-by: zu1k <i@lgf.im>
  • Loading branch information
zu1k committed Dec 16, 2021
1 parent 1e116c8 commit 4e22a5c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 1 addition & 7 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
Nali 需要预先安装 Go. 安装后可以从源码安装软件:

```sh
$ go get -u -v github.com/zu1k/nali
$ go install github.com/zu1k/nali
```

### 下载预编译的可执行程序
Expand All @@ -56,12 +56,6 @@ $ go get -u -v github.com/zu1k/nali

你需要选择适合你系统和硬件架构的版本下载,解压后可直接运行

### 使用 Docker 版本

```
docker pull docker.pkg.github.com//zu1k/nali/nali:latest
```

## 使用说明

### 查询一个IP的地理信息
Expand Down
20 changes: 15 additions & 5 deletions internal/entity/parse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entity

import (
"net/netip"
"sort"

"github.com/zu1k/nali/internal/db"
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/re/re.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})?`)
)
14 changes: 14 additions & 0 deletions internal/re/re_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit 4e22a5c

Please sign in to comment.