Skip to content

Commit

Permalink
feat: support db select
Browse files Browse the repository at this point in the history
  • Loading branch information
zu1k committed Aug 11, 2021
1 parent a6a8d29 commit 063ffff
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 103 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ However the C version has too few functions, and the js version is too big and t
- Interactive query
- Offline query
- Both ipv4 and ipv6 supported
- Multilingual support
- CDN provider query
- Full platform support
- Color print
Expand Down Expand Up @@ -193,17 +194,17 @@ Flags:
Use "nali [command] --help" for more information about a command.
```

### Update chunzhen IP database
### Update database

```
$ nali update
2020/07/17 12:53:46 正在下载最新纯真 IP 库...
2020/07/17 12:54:05 已将最新的纯真 IP 库保存到本地 /root/.nali/qqwry.dat
```

### Use other database
### Select database

Set environment variables `NALI_DB`
Users can specify which database to use, set environment variables `NALI_DB_IP4`, `NALI_DB_IP6` or both.

supported database:

Expand All @@ -216,35 +217,46 @@ supported database:
##### Use geoip db

```
set NALI_DB=geoip
set NALI_DB_IP4=geoip
or use powershell
$env:NALI_DB="geoip"
$env:NALI_DB_IP4="geoip"
```

##### Use ipip db

```
set NALI_DB=ipip
set NALI_DB_IP6=ipip
or use powershell
$env:NALI_DB="ipip"
$env:NALI_DB_IP6="ipip"
```

#### Linux

##### Use geoip db

```
export NALI_DB=geoip
export NALI_DB_IP4=geoip
```

##### Use ipip db

```
export NALI_DB=ipip
export NALI_DB_IP6=ipip
```

### Multilingual support

Specify the language to be used by modifying the environment variable `NALI_LANG`, when using a non-Chinese language only the GeoIP2 database is supported

The values that can be set for this parameter can be found in the list of supported databases for GeoIP2

```
# NALI_LANG=en nali 1.1.1.1
1.1.1.1 [Australia]
```

### Change database directory
Expand Down
64 changes: 23 additions & 41 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
- 支持管道处理
- 支持交互式查询
- 同时支持IPv4和IPv6
- 支持多语言
- 查询完全离线
- 全平台支持
- 支持彩色输出

## 安装

Expand Down Expand Up @@ -159,28 +161,6 @@ Address: 2a00:1450:400e:809::200e [荷兰Amsterdam Google Inc. 服务器网段]

因为 CDN 服务通常使用 CNAME 的域名解析方式,所以推荐与 `nslookup` 或者 `dig` 配合使用,在已经知道 CNAME 后可单独使用

#### 只查询 CDN 服务提供商

```
$ nslookup www.gov.cn | nali cdn
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
www.gov.cn canonical name = www.gov.cn.bsgslb.cn [白山云 CDN].
www.gov.cn.bsgslb.cn [白山云 CDN] canonical name = zgovweb.v.bsgslb.cn [白山云 CDN].
Name: zgovweb.v.bsgslb.cn [白山云 CDN]
Address: 185.232.56.148
Name: zgovweb.v.bsgslb.cn [白山云 CDN]
Address: 185.232.56.147
Name: zgovweb.v.bsgslb.cn [白山云 CDN]
Address: 2001:428:6402:21b::6
Name: zgovweb.v.bsgslb.cn [白山云 CDN]
Address: 2001:428:6402:21b::5
```

#### 查询所有信息

```
$ nslookup www.gov.cn | nali
Server: 127.0.0.53 [局域网 IP]
Expand All @@ -197,14 +177,6 @@ Name: zgovweb.v.bsgslb.cn [白山云 CDN]
Address: 2001:428:6402:21b::6 [美国Louisiana州Monroe Qwest Communications Company, LLC (CenturyLink)]
```

#### 单独使用

需要提前查询到 CNAME 域名

```
$ nali cdn cdn.somecdncname.com
```

## 用户交互

### 查看帮助
Expand All @@ -216,9 +188,7 @@ Usage:
nali [command]
Available Commands:
cdn Query cdn service provider
help Help about any command
parse Query IP information
update update chunzhen ip database
Flags:
Expand All @@ -228,57 +198,69 @@ Flags:
Use "nali [command] --help" for more information about a command.
```

### 更新纯真数据库
### 更新数据库

```
$ nali update
2020/07/17 12:53:46 正在下载最新纯真 IP 库...
2020/07/17 12:54:05 已将最新的纯真 IP 库保存到本地 /root/.nali/qqwry.dat
```

### 使用 Geoip2 数据库
### 自选数据库

需要设置环境变量: `NALI_DB`
用户可以指定使用哪个数据库,需要设置环境变量: `NALI_DB_IP4``NALI_DB_IP6` 或者两个同时设置

支持的变量内容:

- Geoip2 `['geoip', 'geoip2', 'geo']`
- Chunzhen `['chunzhen', 'qqip', 'qqwry']`
- IPIP `['ipip', 'ipipfree', 'ipip.net']`

#### Windows平台

##### 使用geoip数据库

```
set NALI_DB=geoip
set NALI_DB_IP4=geoip
或者使用 powershell
$env:NALI_DB="geoip"
$env:NALI_DB_IP4="geoip"
```

##### 使用ipip数据库

```
set NALI_DB=ipip
set NALI_DB_IP6=ipip
或者使用 powershell
$env:NALI_DB="ipip"
$env:NALI_DB_IP6="ipip"
```

#### Linux平台

##### 使用geoip数据库

```
export NALI_DB=geoip
export NALI_DB_IP4=geoip
```

##### 使用ipip数据库

```
export NALI_DB=ipip
export NALI_DB_IP4=ipip
```

### 多语言支持

通过修改环境变量 `NALI_LANG` 来指定使用的语言,当使用非中文语言时仅支持GeoIP2这个数据库

该参数可设置的值见 GeoIP2 这个数据库的支持列表

```
# NALI_LANG=en nali 1.1.1.1
1.1.1.1 [Australia]
```

### 更换数据库目录
Expand Down
74 changes: 70 additions & 4 deletions internal/db/db.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package db

import (
"os"
"path/filepath"

"github.com/zu1k/nali/pkg/ipip"

"github.com/zu1k/nali/pkg/geoip"

"github.com/zu1k/nali/internal/constant"
"github.com/zu1k/nali/pkg/cdn"
"github.com/zu1k/nali/pkg/dbif"
Expand All @@ -16,30 +21,91 @@ var (
GeoLite2CityPath = filepath.Join(constant.HomePath, "GeoLite2-City.mmdb")
IPIPFreePath = filepath.Join(constant.HomePath, "ipipfree.ipdb")
CDNPath = filepath.Join(constant.HomePath, "cdn.json")

Language = "zh-CN"
IPv4DBSelected = ""
IPv6DBSelected = ""
)

func init() {
lang := os.Getenv("NALI_LANG")
if lang != "" {
Language = lang
}

ipv4DB := os.Getenv("NALI_DB_IP4")
if ipv4DB != "" {
IPv4DBSelected = ipv4DB
}

ipv6DB := os.Getenv("NALI_DB_IP6")
if ipv6DB != "" {
IPv6DBSelected = ipv6DB
}
}

func GetDB(typ dbif.QueryType) (db dbif.DB) {
if db, found := dbCache[typ]; found {
return db
}

switch typ {
case dbif.TypeIPv4:
db = qqwry.NewQQwry(QQWryPath)
if IPv4DBSelected != "" {
db = GetIPDBbyName(IPv4DBSelected)
} else {
if Language == "zh-CN" {
db = qqwry.NewQQwry(QQWryPath)
} else {
db = geoip.NewGeoIP(GeoLite2CityPath)
}
}
case dbif.TypeIPv6:
db = zxipv6wry.NewZXwry(ZXIPv6WryPath)
// geoip2.NewGeoIP(GeoLite2CityPath)
// ipip.NewIPIPFree(IPIPFreePath)
if IPv6DBSelected != "" {
db = GetIPDBbyName(IPv6DBSelected)
} else {
if Language == "zh-CN" {
db = zxipv6wry.NewZXwry(ZXIPv6WryPath)
} else {
geoip.NewGeoIP(GeoLite2CityPath)
}
}
case dbif.TypeDomain:
db = cdn.NewCDN(CDNPath)
default:
panic("Query type not supported!")
}

dbCache[typ] = db
return
}

func GetIPDBbyName(name string) (db dbif.DB) {
switch name {
case "geo", "geoip", "geoip2":
return geoip.NewGeoIP(GeoLite2CityPath)
case "chunzhen", "qqip", "qqwry":
return qqwry.NewQQwry(QQWryPath)
case "ipip", "ipipfree", "ipip.net":
return ipip.NewIPIPFree(IPIPFreePath)
default:
return qqwry.NewQQwry(QQWryPath)
}
}

func Update() {
qqwry.Download(QQWryPath)
zxipv6wry.Download(ZXIPv6WryPath)
cdn.Download(CDNPath)
}

func Find(typ dbif.QueryType, query string) string {
if result, found := queryCache[query]; found {
return result
}
result, err := GetDB(typ).Find(query, Language)
if err != nil {
return ""
}
return result.String()
}
17 changes: 0 additions & 17 deletions internal/db/find.go

This file was deleted.

30 changes: 0 additions & 30 deletions internal/db/ipdb.go

This file was deleted.

Loading

0 comments on commit 063ffff

Please sign in to comment.