Skip to content

Commit

Permalink
Refine: make lookup command more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
Loyalsoldier committed Oct 24, 2024
1 parent 87404b7 commit 38dfd08
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 97 deletions.
186 changes: 156 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,60 +602,186 @@ $ ./geoip convert -c config.json

### 查找 IP 或 CIDR 所在类别(`lookup`)

可能的返回结果:

- 查询的字符串不是有效的 IP 或 CIDR,返回 `false`
- 查询的 IP 或 CIDR 不存在于任何一个类别中,返回 `false`
- 查询的 IP 或 CIDR 存在于某种格式文件的单个类别中:
- 若该格式文件只包含一个类别,返回 `true`
- 若该格式文件包含多个类别,返回匹配的类别名称
- 查询的 IP 或 CIDR 存在于多个类别中,返回以英文逗号分隔的类别名称,如 `au,cloudflare`

```bash
# lookup one IP from local file
$ ./geoip lookup -f text -u ./cn.txt -n cn 1.0.1.1
cn
# ================= One-time Mode ================= #
# 从 text 格式的本地文件(只包含一个类别)中查找某个 IP 地址
# lookup IP from local file (with only one list) in text format
$ ./geoip lookup -f text -u ./cn.txt 1.0.1.1
true
# lookup one CIDR from local file
$ ./geoip lookup -f text -u ./cn.txt -n cn 1.0.1.1/24
cn
# 从 text 格式的本地文件(只包含一个类别)中查找某个 IP 地址
# lookup IP from local file (with only one list) in text format
$ ./geoip lookup -f text -u ./cn.txt 2.2.2.2
false
# lookup IP or CIDR in REPL mode from local file
$ ./geoip lookup -f text -u ./cn.txt -n cn
Enter IP or CIDR (type `exit` to quit):
>> 1.0.1.1
cn
>> 1.0.1.1/24
# 从 text 格式的本地文件(只包含一个类别)中查找某个 CIDR
# lookup CIDR from local file (with only one list) in text format
$ ./geoip lookup -f text -u ./cn.txt 1.0.1.1/24
true
# 从 text 格式的本地文件(只包含一个类别)中查找某个 CIDR
# lookup CIDR from local file (with only one list) in text format
$ ./geoip lookup -f text -u ./cn.txt 1.0.1.1/23
false
# 从 text 格式的远程 URL(只包含一个类别)中查找某个 IP 地址
# lookup IP from remote URL (with only one list) in text format
$ ./geoip lookup -f text -u https://example.com/cn.txt 1.0.1.1
true
# 从 v2rayGeoIPDat 格式的本地文件(只包含一个类别)中查找某个 IP 地址
# lookup IP from local file (with only one list) in v2rayGeoIPDat format
$ ./geoip lookup -f v2rayGeoIPDat -u ./cn.dat 1.0.1.1
true
# 从 v2rayGeoIPDat 格式的本地文件(包含多个类别)中查找某个 IP 地址
# lookup IP from local file (with multiple lists) in v2rayGeoIPDat format
$ ./geoip lookup -f v2rayGeoIPDat -u ./geoip.dat 1.0.1.1
cn
# lookup IP or CIDR in REPL mode from remote file
$ ./geoip lookup -f text -u https://example.com/cn.txt -n cn
Enter IP or CIDR (type `exit` to quit):
# 从 v2rayGeoIPDat 格式的本地文件(包含多个类别)中查找某个 IP 地址
# lookup IP from local file (with multiple lists) in v2rayGeoIPDat format
$ ./geoip lookup -f v2rayGeoIPDat -u ./geoip.dat 1.0.0.1
au,cloudflare
# 从 v2rayGeoIPDat 格式的远程 URL(包含多个类别)中查找某个 CIDR
# lookup CIDR from remote URL (with multiple lists) in v2rayGeoIPDat format
$ ./geoip lookup -f v2rayGeoIPDat -u https://example.com/geoip.dat 1.0.0.1/24
au,cloudflare
# ================= REPL Mode ================= #
# 从 text 格式的本地文件(只包含一个类别)中查找某个 IP 地址或 CIDR
# lookup IP or CIDR from local file (with only one list) in text format
$ ./geoip lookup -f text -u ./cn.txt
Enter IP or CIDR (type "exit" to quit):
>> 1.0.1.1
cn
true
>> 1.0.1.1/24
cn
true
>> 1.0.1.1/23
false
>> 2.2.2.2
false
>> 2.2.2.2/24
false
# lookup IP or CIDR in REPL mode from local directory, got two lists joined with comma
$ ./geoip lookup -f text -d ./path/to/your/directory/
Enter IP or CIDR (type `exit` to quit):
>> 300.300.300.300
false
>> 300.300.300.300/24
false
>> exit
# 从 text 格式的远程 URL(只包含一个类别)中查找某个 IP 地址或 CIDR
# lookup IP or CIDR from remote URL (with only one list) in text format
$ ./geoip lookup -f text -u https://example.com/cn.txt
Enter IP or CIDR (type "exit" to quit):
>> 1.0.1.1
cn,my-custom-list
true
>> 1.0.1.1/24
cn,my-custom-list
true
>> 1.0.1.1/23
false
>> 2.2.2.2
false
>> 2.2.2.2/24
false
# lookup IP or CIDR in REPL mode from specified lists in local directory
$ ./geoip lookup -f text -d ./path/to/your/directory/ -l cn,us,jp
Enter IP or CIDR (type `exit` to quit):
>> 300.300.300.300
false
>> 300.300.300.300/24
false
>> exit
# 从 v2rayGeoIPDat 格式的本地文件(只包含一个类别)中查找某个 IP 地址或 CIDR
# lookup IP or CIDR from local file (with only one list) in v2rayGeoIPDat format
$ ./geoip lookup -f v2rayGeoIPDat -u ./cn.dat
Enter IP or CIDR (type "exit" to quit):
>> 1.0.1.1
cn
true
>> 1.0.1.1/24
cn
true
>> 1.0.1.1/23
false
# lookup IP or CIDR in REPL mode with another format from specified lists in remote file
$ ./geoip lookup -f v2rayGeoIPDat -u https://example.com/geoip.dat -l cn,us,jp
Enter IP or CIDR (type `exit` to quit):
>> 2.2.2.2
false
>> 2.2.2.2/24
false
>> 300.300.300.300
false
>> 300.300.300.300/24
false
>> exit
# 从 v2rayGeoIPDat 格式的远程 URL(包含多个类别)中查找某个 IP 地址或 CIDR
# lookup IP or CIDR from remote URL (with multiple list) in v2rayGeoIPDat format
$ ./geoip lookup -f v2rayGeoIPDat -u https://example.com/geoip.dat
Enter IP or CIDR (type "exit" to quit):
>> 1.0.1.1
cn
>> 1.0.1.1/24
cn
>> 1.0.1.1/23
false
>> 1.0.0.1
au,cloudflare
>> 1.0.0.1/24
au,cloudflare
>> 300.300.300.300
false
>> 300.300.300.300/24
false
>> exit
```

## 使用本项目的项目
Expand Down
50 changes: 19 additions & 31 deletions lib/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,41 +232,29 @@ func (c *container) lookup(addrOrPrefix any, iptype IPType, searchList ...string
continue
}

var ipset *netipx.IPSet
var err error
switch iptype {
case IPv4:
ipset, err := entry.GetIPv4Set()
if err != nil {
return nil, false, err
}
switch addrOrPrefix := addrOrPrefix.(type) {
case netip.Prefix:
if found := ipset.ContainsPrefix(addrOrPrefix); found {
isfound = true
result = append(result, entry.GetName())
}
case netip.Addr:
if found := ipset.Contains(addrOrPrefix); found {
isfound = true
result = append(result, entry.GetName())
}
}

ipset, err = entry.GetIPv4Set()
case IPv6:
ipset, err := entry.GetIPv6Set()
if err != nil {
return nil, false, err
ipset, err = entry.GetIPv6Set()
}

if err != nil {
return nil, false, err
}

switch addrOrPrefix := addrOrPrefix.(type) {
case netip.Prefix:
if found := ipset.ContainsPrefix(addrOrPrefix); found {
isfound = true
result = append(result, entry.GetName())
}
switch addrOrPrefix := addrOrPrefix.(type) {
case netip.Prefix:
if found := ipset.ContainsPrefix(addrOrPrefix); found {
isfound = true
result = append(result, entry.GetName())
}
case netip.Addr:
if found := ipset.Contains(addrOrPrefix); found {
isfound = true
result = append(result, entry.GetName())
}
case netip.Addr:
if found := ipset.Contains(addrOrPrefix); found {
isfound = true
result = append(result, entry.GetName())
}
}
}
Expand Down
Loading

0 comments on commit 38dfd08

Please sign in to comment.