Skip to content

Commit

Permalink
重新实现了 wry 格式数据库解析,包括 qqwry 和 zxipv6wry,支持并发查询
Browse files Browse the repository at this point in the history
修复wry文件不完整时crash的问题

对 nali 的数据库和配置文件目录进行了规范,具体见 [文档-工作目录](https://github.com/zu1k/nali#%E5%B7%A5%E4%BD%9C%E7%9B%AE%E5%BD%95)

优先使用环境变量 NALI_HOME、NALI_CONFIG_HOME、NALI_DB_HOME 指定的目录
未指定 nali 特定环境变量的情况下默认使用 XDG 规范,配置文件目录在 $XDG_CONFIG_HOME/nali,数据库文件目录在 $XDG_DATA_HOME/nali
若未检测到 XDG 相关环境变量,将根据平台使用默认目录,具体见 XDG Base Directory 中 XDG_CONFIG_HOME 和 XDG_DATA_HOME 部分
初次运行此版本将会进行目录和数据的迁移,将 ~/.nali 下的配置文件和数据库转移到相应目录,并删除 ~/.nali 目录

支持使用环境变量指定的代理下载数据库,thanks to @jingjingxyk zu1k#126
修复了 pipe mtr 时无法获取内容和格式错乱的问题,thanks to @mzz2017 zu1k#132 , fix zu1k#12, zu1k#61, zu1k#85, zu1k#115, zu1k#123.
cache map 使用并发安全的版本,thanks to @lhcn zu1k#125
升级 Go 版本到 1.19,更新了依赖
不再支持 ip2region 旧数据库格式,目前仅支持 ip2region xdb 格式

去除了已过时的数据库下载代码
从 git 历史记录中去除了数据库文件

修复自动迁移导致生成空配置文件的bug
更新纯真IP数据库的下载地址
  • Loading branch information
fcwys committed Oct 21, 2022
1 parent 220d5db commit 9ff1074
Show file tree
Hide file tree
Showing 44 changed files with 2,008 additions and 1,953 deletions.
184 changes: 93 additions & 91 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,93 @@
package cmd

import (
"bufio"
"fmt"
"log"
"os"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"

"github.com/zu1k/nali/internal/constant"
"github.com/zu1k/nali/pkg/entity"
)

var rootCmd = &cobra.Command{
Use: "nali",
Short: "An offline tool for querying IP geographic information",
Long: `An offline tool for querying IP geographic information.
Find document on: https://github.com/zu1k/nali
#1 Query a simple IP address
$ nali 1.2.3.4
or use pipe
$ echo IP 6.6.6.6 | nali
#2 Query multiple IP addresses
$ nali 1.2.3.4 4.3.2.1 123.23.3.0
#3 Interactive query
$ nali
123.23.23.23
123.23.23.23 [越南 越南邮电集团公司]
quit
#4 Use with dig
$ dig nali.zu1k.com +short | nali
#5 Use with nslookup
$ nslookup nali.zu1k.com 8.8.8.8 | nali
#6 Use with any other program
bash abc.sh | nali
#7 IPV6 support
`,
Version: constant.Version,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
gbk, _ := cmd.Flags().GetBool("gbk")

if len(args) == 0 {
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
line := stdin.Text()
if gbk {
line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line)
}
if line == "quit" || line == "exit" {
return
}
_, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).ColorString())
}
} else {
_, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString())
}
},
}

// Execute parse subcommand and run
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err.Error())
}
}

func init() {
rootCmd.Flags().Bool("gbk", false, "Use GBK decoder")
}
package cmd

import (
"bufio"
"fmt"
"log"
"os"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"

"github.com/zu1k/nali/internal/constant"
"github.com/zu1k/nali/pkg/common"
"github.com/zu1k/nali/pkg/entity"
)

var rootCmd = &cobra.Command{
Use: "nali",
Short: "An offline tool for querying IP geographic information",
Long: `An offline tool for querying IP geographic information.
Find document on: https://github.com/zu1k/nali
#1 Query a simple IP address
$ nali 1.2.3.4
or use pipe
$ echo IP 6.6.6.6 | nali
#2 Query multiple IP addresses
$ nali 1.2.3.4 4.3.2.1 123.23.3.0
#3 Interactive query
$ nali
123.23.23.23
123.23.23.23 [越南 越南邮电集团公司]
quit
#4 Use with dig
$ dig nali.zu1k.com +short | nali
#5 Use with nslookup
$ nslookup nali.zu1k.com 8.8.8.8 | nali
#6 Use with any other program
bash abc.sh | nali
#7 IPV6 support
`,
Version: constant.Version,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
gbk, _ := cmd.Flags().GetBool("gbk")

if len(args) == 0 {
stdin := bufio.NewScanner(os.Stdin)
stdin.Split(common.ScanLines)
for stdin.Scan() {
line := stdin.Text()
if gbk {
line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line)
}
if line := strings.TrimSpace(line); line == "quit" || line == "exit" {
return
}
_, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).ColorString())
}
} else {
_, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString())
}
},
}

// Execute parse subcommand and run
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err.Error())
}
}

func init() {
rootCmd.Flags().Bool("gbk", false, "Use GBK decoder")
}
8 changes: 4 additions & 4 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (
"github.com/zu1k/nali/pkg/entity"
)

//查询到的IP信息
// 查询到的IP信息
type Ipinfo struct {
Code int `json:"code" xml:"code"`
Ip string `json:"ip" xml:"ip"`
Addr string `json:"addr" xml:"addr"`
Cdn string `json:"cdn" xml:"cdn"`
}

//错误状态信息
// 错误状态信息
type Errinfo struct {
Code int `json:"code" xml:"code"`
Msg string `json:"msg" xml:"msg"`
}

//解析cname
// 解析cname
func getCname(host string) string {
host = strings.TrimSpace(host)
cname, err := net.LookupCNAME(host)
Expand All @@ -40,7 +40,7 @@ func getCname(host string) string {
return strings.TrimRight(cname, ".")
}

//域名解析ip
// 域名解析ip
func getIp(host string) string {
host = strings.TrimSpace(host)
ipall, err := net.LookupIP(host)
Expand Down
60 changes: 30 additions & 30 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package cmd

import (
"strings"

"github.com/zu1k/nali/internal/db"

"github.com/spf13/cobra"
)

// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "update qqwry, zxipv6wry, ip2region ip database and cdn",
Long: `update qqwry, zxipv6wry, ip2region ip database and cdn. Use commas to separate`,
Example: "nali update --db qqwry,cdn",
Run: func(cmd *cobra.Command, args []string) {
DBs, _ := cmd.Flags().GetString("db")
var DBNameArray []string
if DBs != "" {
DBNameArray = strings.Split(DBs, ",")
}
db.UpdateDB(DBNameArray...)
},
}

func init() {
rootCmd.AddCommand(updateCmd)
rootCmd.PersistentFlags().String("db", "", "choose db you want to update")
}
package cmd

import (
"strings"

"github.com/zu1k/nali/internal/db"

"github.com/spf13/cobra"
)

// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "update qqwry, zxipv6wry, ip2region ip database and cdn",
Long: `update qqwry, zxipv6wry, ip2region ip database and cdn. Use commas to separate`,
Example: "nali update --db qqwry,cdn",
Run: func(cmd *cobra.Command, args []string) {
DBs, _ := cmd.Flags().GetString("db")
var DBNameArray []string
if DBs != "" {
DBNameArray = strings.Split(DBs, ",")
}
db.UpdateDB(DBNameArray...)
},
}

func init() {
rootCmd.AddCommand(updateCmd)
rootCmd.PersistentFlags().String("db", "", "choose db you want to update")
}
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module github.com/zu1k/nali
go 1.18

require (
github.com/adrg/xdg v0.4.0
github.com/fatih/color v1.13.0
github.com/google/martian v2.1.0+incompatible
github.com/ip2location/ip2location-go/v9 v9.2.0
github.com/ipipdotnet/ipdb-go v1.3.1
github.com/labstack/echo/v4 v4.7.2
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
github.com/labstack/echo/v4 v4.9.1
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20221017063954-c5c24655eb63
github.com/oschwald/geoip2-golang v1.7.0
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda
github.com/spf13/cobra v1.4.0
Expand All @@ -21,7 +23,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand All @@ -40,7 +42,7 @@ require (
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/tools v0.1.7 // indirect
Expand Down
20 changes: 12 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -100,6 +102,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -142,12 +145,12 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.7.2 h1:Kv2/p8OaQ+M6Ex4eGimg9b9e6icoxA42JSlOR3msKtI=
github.com/labstack/echo/v4 v4.7.2/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks=
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible h1:1qp9iks+69h7IGLazAplzS9Ca14HAxuD5c0rbFdPGy4=
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible/go.mod h1:+ZBN7PBoh5gG6/y0ZQ85vJDBe21WnfbRrQQwTfliJJI=
github.com/labstack/echo/v4 v4.9.1 h1:GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y=
github.com/labstack/echo/v4 v4.9.1/go.mod h1:Pop5HLc+xoc4qhTZ1ip6C0RtP7Z+4VzRLWZZFKqbbjo=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20221017063954-c5c24655eb63 h1:29A0rIOPpaXCQNjb2eNhf/fAxCFxS0KpRbJVcPL63fQ=
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20221017063954-c5c24655eb63/go.mod h1:bChUKvbKVC3zL/lLLIcu6alhQaL8uWD/DA+jRdyggdI=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down Expand Up @@ -228,8 +231,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -353,6 +356,7 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Loading

0 comments on commit 9ff1074

Please sign in to comment.