mirror of
https://github.com/zu1k/nali.git
synced 2025-01-22 13:19:02 +08:00
feat: support db select
This commit is contained in:
parent
2d99679810
commit
6b76f4a50b
30
README.md
30
README.md
@ -36,6 +36,7 @@ However the C version has too few functions, and the js version is too big and t
|
|||||||
- Interactive query
|
- Interactive query
|
||||||
- Offline query
|
- Offline query
|
||||||
- Both ipv4 and ipv6 supported
|
- Both ipv4 and ipv6 supported
|
||||||
|
- Multilingual support
|
||||||
- CDN provider query
|
- CDN provider query
|
||||||
- Full platform support
|
- Full platform support
|
||||||
- Color print
|
- Color print
|
||||||
@ -193,7 +194,7 @@ Flags:
|
|||||||
Use "nali [command] --help" for more information about a command.
|
Use "nali [command] --help" for more information about a command.
|
||||||
```
|
```
|
||||||
|
|
||||||
### Update chunzhen IP database
|
### Update database
|
||||||
|
|
||||||
```
|
```
|
||||||
$ nali update
|
$ nali update
|
||||||
@ -201,9 +202,9 @@ $ nali update
|
|||||||
2020/07/17 12:54:05 已将最新的纯真 IP 库保存到本地 /root/.nali/qqwry.dat
|
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:
|
supported database:
|
||||||
|
|
||||||
@ -216,21 +217,21 @@ supported database:
|
|||||||
##### Use geoip db
|
##### Use geoip db
|
||||||
|
|
||||||
```
|
```
|
||||||
set NALI_DB=geoip
|
set NALI_DB_IP4=geoip
|
||||||
|
|
||||||
or use powershell
|
or use powershell
|
||||||
|
|
||||||
$env:NALI_DB="geoip"
|
$env:NALI_DB_IP4="geoip"
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Use ipip db
|
##### Use ipip db
|
||||||
|
|
||||||
```
|
```
|
||||||
set NALI_DB=ipip
|
set NALI_DB_IP6=ipip
|
||||||
|
|
||||||
or use powershell
|
or use powershell
|
||||||
|
|
||||||
$env:NALI_DB="ipip"
|
$env:NALI_DB_IP6="ipip"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Linux
|
#### Linux
|
||||||
@ -238,13 +239,24 @@ $env:NALI_DB="ipip"
|
|||||||
##### Use geoip db
|
##### Use geoip db
|
||||||
|
|
||||||
```
|
```
|
||||||
export NALI_DB=geoip
|
export NALI_DB_IP4=geoip
|
||||||
```
|
```
|
||||||
|
|
||||||
##### Use ipip db
|
##### 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
|
### Change database directory
|
||||||
|
@ -35,8 +35,10 @@
|
|||||||
- 支持管道处理
|
- 支持管道处理
|
||||||
- 支持交互式查询
|
- 支持交互式查询
|
||||||
- 同时支持IPv4和IPv6
|
- 同时支持IPv4和IPv6
|
||||||
|
- 支持多语言
|
||||||
- 查询完全离线
|
- 查询完全离线
|
||||||
- 全平台支持
|
- 全平台支持
|
||||||
|
- 支持彩色输出
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
@ -159,28 +161,6 @@ Address: 2a00:1450:400e:809::200e [荷兰Amsterdam Google Inc. 服务器网段]
|
|||||||
|
|
||||||
因为 CDN 服务通常使用 CNAME 的域名解析方式,所以推荐与 `nslookup` 或者 `dig` 配合使用,在已经知道 CNAME 后可单独使用
|
因为 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
|
$ nslookup www.gov.cn | nali
|
||||||
Server: 127.0.0.53 [局域网 IP]
|
Server: 127.0.0.53 [局域网 IP]
|
||||||
@ -197,14 +177,6 @@ Name: zgovweb.v.bsgslb.cn [白山云 CDN]
|
|||||||
Address: 2001:428:6402:21b::6 [美国Louisiana州Monroe Qwest Communications Company, LLC (CenturyLink)]
|
Address: 2001:428:6402:21b::6 [美国Louisiana州Monroe Qwest Communications Company, LLC (CenturyLink)]
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 单独使用
|
|
||||||
|
|
||||||
需要提前查询到 CNAME 域名
|
|
||||||
|
|
||||||
```
|
|
||||||
$ nali cdn cdn.somecdncname.com
|
|
||||||
```
|
|
||||||
|
|
||||||
## 用户交互
|
## 用户交互
|
||||||
|
|
||||||
### 查看帮助
|
### 查看帮助
|
||||||
@ -216,9 +188,7 @@ Usage:
|
|||||||
nali [command]
|
nali [command]
|
||||||
|
|
||||||
Available Commands:
|
Available Commands:
|
||||||
cdn Query cdn service provider
|
|
||||||
help Help about any command
|
help Help about any command
|
||||||
parse Query IP information
|
|
||||||
update update chunzhen ip database
|
update update chunzhen ip database
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
@ -228,7 +198,7 @@ Flags:
|
|||||||
Use "nali [command] --help" for more information about a command.
|
Use "nali [command] --help" for more information about a command.
|
||||||
```
|
```
|
||||||
|
|
||||||
### 更新纯真数据库
|
### 更新数据库
|
||||||
|
|
||||||
```
|
```
|
||||||
$ nali update
|
$ nali update
|
||||||
@ -236,35 +206,36 @@ $ nali update
|
|||||||
2020/07/17 12:54:05 已将最新的纯真 IP 库保存到本地 /root/.nali/qqwry.dat
|
2020/07/17 12:54:05 已将最新的纯真 IP 库保存到本地 /root/.nali/qqwry.dat
|
||||||
```
|
```
|
||||||
|
|
||||||
### 使用 Geoip2 数据库
|
### 自选数据库
|
||||||
|
|
||||||
需要设置环境变量: `NALI_DB`
|
用户可以指定使用哪个数据库,需要设置环境变量: `NALI_DB_IP4`、`NALI_DB_IP6` 或者两个同时设置
|
||||||
|
|
||||||
支持的变量内容:
|
支持的变量内容:
|
||||||
|
|
||||||
- Geoip2 `['geoip', 'geoip2', 'geo']`
|
- Geoip2 `['geoip', 'geoip2', 'geo']`
|
||||||
- Chunzhen `['chunzhen', 'qqip', 'qqwry']`
|
- Chunzhen `['chunzhen', 'qqip', 'qqwry']`
|
||||||
|
- IPIP `['ipip', 'ipipfree', 'ipip.net']`
|
||||||
|
|
||||||
#### Windows平台
|
#### Windows平台
|
||||||
|
|
||||||
##### 使用geoip数据库
|
##### 使用geoip数据库
|
||||||
|
|
||||||
```
|
```
|
||||||
set NALI_DB=geoip
|
set NALI_DB_IP4=geoip
|
||||||
|
|
||||||
或者使用 powershell
|
或者使用 powershell
|
||||||
|
|
||||||
$env:NALI_DB="geoip"
|
$env:NALI_DB_IP4="geoip"
|
||||||
```
|
```
|
||||||
|
|
||||||
##### 使用ipip数据库
|
##### 使用ipip数据库
|
||||||
|
|
||||||
```
|
```
|
||||||
set NALI_DB=ipip
|
set NALI_DB_IP6=ipip
|
||||||
|
|
||||||
或者使用 powershell
|
或者使用 powershell
|
||||||
|
|
||||||
$env:NALI_DB="ipip"
|
$env:NALI_DB_IP6="ipip"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Linux平台
|
#### Linux平台
|
||||||
@ -272,13 +243,24 @@ $env:NALI_DB="ipip"
|
|||||||
##### 使用geoip数据库
|
##### 使用geoip数据库
|
||||||
|
|
||||||
```
|
```
|
||||||
export NALI_DB=geoip
|
export NALI_DB_IP4=geoip
|
||||||
```
|
```
|
||||||
|
|
||||||
##### 使用ipip数据库
|
##### 使用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]
|
||||||
```
|
```
|
||||||
|
|
||||||
### 更换数据库目录
|
### 更换数据库目录
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/zu1k/nali/pkg/ipip"
|
||||||
|
|
||||||
|
"github.com/zu1k/nali/pkg/geoip"
|
||||||
|
|
||||||
"github.com/zu1k/nali/internal/constant"
|
"github.com/zu1k/nali/internal/constant"
|
||||||
"github.com/zu1k/nali/pkg/cdn"
|
"github.com/zu1k/nali/pkg/cdn"
|
||||||
"github.com/zu1k/nali/pkg/dbif"
|
"github.com/zu1k/nali/pkg/dbif"
|
||||||
@ -16,8 +21,29 @@ var (
|
|||||||
GeoLite2CityPath = filepath.Join(constant.HomePath, "GeoLite2-City.mmdb")
|
GeoLite2CityPath = filepath.Join(constant.HomePath, "GeoLite2-City.mmdb")
|
||||||
IPIPFreePath = filepath.Join(constant.HomePath, "ipipfree.ipdb")
|
IPIPFreePath = filepath.Join(constant.HomePath, "ipipfree.ipdb")
|
||||||
CDNPath = filepath.Join(constant.HomePath, "cdn.json")
|
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) {
|
func GetDB(typ dbif.QueryType) (db dbif.DB) {
|
||||||
if db, found := dbCache[typ]; found {
|
if db, found := dbCache[typ]; found {
|
||||||
return db
|
return db
|
||||||
@ -25,21 +51,61 @@ func GetDB(typ dbif.QueryType) (db dbif.DB) {
|
|||||||
|
|
||||||
switch typ {
|
switch typ {
|
||||||
case dbif.TypeIPv4:
|
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:
|
case dbif.TypeIPv6:
|
||||||
db = zxipv6wry.NewZXwry(ZXIPv6WryPath)
|
if IPv6DBSelected != "" {
|
||||||
// geoip2.NewGeoIP(GeoLite2CityPath)
|
db = GetIPDBbyName(IPv6DBSelected)
|
||||||
// ipip.NewIPIPFree(IPIPFreePath)
|
} else {
|
||||||
|
if Language == "zh-CN" {
|
||||||
|
db = zxipv6wry.NewZXwry(ZXIPv6WryPath)
|
||||||
|
} else {
|
||||||
|
geoip.NewGeoIP(GeoLite2CityPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
case dbif.TypeDomain:
|
case dbif.TypeDomain:
|
||||||
db = cdn.NewCDN(CDNPath)
|
db = cdn.NewCDN(CDNPath)
|
||||||
default:
|
default:
|
||||||
panic("Query type not supported!")
|
panic("Query type not supported!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbCache[typ] = db
|
||||||
return
|
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() {
|
func Update() {
|
||||||
qqwry.Download(QQWryPath)
|
qqwry.Download(QQWryPath)
|
||||||
zxipv6wry.Download(ZXIPv6WryPath)
|
zxipv6wry.Download(ZXIPv6WryPath)
|
||||||
cdn.Download(CDNPath)
|
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()
|
||||||
|
}
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/zu1k/nali/pkg/dbif"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Find(typ dbif.QueryType, query string) string {
|
|
||||||
if result, found := queryCache[query]; found {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
result, err := GetDB(typ).Find(query)
|
|
||||||
if err != nil {
|
|
||||||
//log.Printf("Query [%s] error: %s\n", query, err)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return result.String()
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package db
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ip database type
|
|
||||||
type IPDBType int
|
|
||||||
|
|
||||||
const (
|
|
||||||
GEOIP2 = iota // geoip2
|
|
||||||
QQIP // chunzhen
|
|
||||||
IPIP // ipip.net
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetIPDBType() IPDBType {
|
|
||||||
dbname := os.Getenv("NALI_DB")
|
|
||||||
dbname = strings.ToLower(dbname)
|
|
||||||
switch dbname {
|
|
||||||
case "geo", "geoip", "geoip2":
|
|
||||||
return GEOIP2
|
|
||||||
case "chunzhen", "qqip", "qqwry":
|
|
||||||
return QQIP
|
|
||||||
case "ipip", "ipipfree", "ipip.net":
|
|
||||||
return IPIP
|
|
||||||
default:
|
|
||||||
return QQIP
|
|
||||||
}
|
|
||||||
}
|
|
@ -42,9 +42,16 @@ func (g GeoIP) Find(query string, params ...string) (result fmt.Stringer, err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lang := "zh-CN"
|
||||||
|
if len(params) > 0 {
|
||||||
|
if _, ok := record.Country.Names[params[0]]; ok {
|
||||||
|
lang = params[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result = Result{
|
result = Result{
|
||||||
Country: record.Country.Names["zh-CN"],
|
Country: record.Country.Names[lang],
|
||||||
City: record.City.Names["zh-CN"],
|
City: record.City.Names[lang],
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user