From ff1449a4d1415ef957a3d784fb5891325464ba43 Mon Sep 17 00:00:00 2001 From: Jason_R4bb1t <1550184338@qq.com> Date: Fri, 19 May 2023 15:40:45 +0800 Subject: [PATCH 1/9] add flag -j/--json. json format output. --- cmd/root.go | 26 ++++++++++++++++++------ go.mod | 1 + go.sum | 47 ++------------------------------------------ internal/db/db.go | 17 ++++++++-------- pkg/entity/entity.go | 40 ++++++++++++++++++++++++++++++------- pkg/entity/parse.go | 20 ++++++++++--------- 6 files changed, 75 insertions(+), 76 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 7893b22e..8ca105d6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,14 +3,13 @@ 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" + "log" + "os" + "strings" "github.com/zu1k/nali/internal/constant" "github.com/zu1k/nali/pkg/common" @@ -61,6 +60,8 @@ Find document on: https://github.com/zu1k/nali Args: cobra.MinimumNArgs(0), Run: func(cmd *cobra.Command, args []string) { gbk, _ := cmd.Flags().GetBool("gbk") + isJson, _ := cmd.Flags().GetBool("json") + isJsonline, _ := cmd.Flags().GetBool("jsonline") if len(args) == 0 { stdin := bufio.NewScanner(os.Stdin) @@ -73,10 +74,21 @@ Find document on: https://github.com/zu1k/nali if line := strings.TrimSpace(line); line == "quit" || line == "exit" { return } - _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).ColorString()) + if isJson { + _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).Json()) + } else { + _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).ColorString()) + } + } } else { - _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString()) + if isJson { + _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).Json()) + } else if isJsonline { + _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).JsonLine()) + } else { + _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString()) + } } }, } @@ -90,4 +102,6 @@ func Execute() { func init() { rootCmd.Flags().Bool("gbk", false, "Use GBK decoder") + rootCmd.PersistentFlags().BoolP("json", "j", false, "Output in JSON format") + rootCmd.PersistentFlags().BoolP("jsonline", "l", false, "JSON output with newlines") } diff --git a/go.mod b/go.mod index b15f389e..2368bf93 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/oschwald/maxminddb-golang v1.10.0 // indirect github.com/pelletier/go-toml/v2 v2.0.7 // indirect + github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f // indirect github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.0 // indirect diff --git a/go.sum b/go.sum index f714d717..7b76aad0 100644 --- a/go.sum +++ b/go.sum @@ -58,10 +58,6 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= -github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w= -github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= @@ -132,11 +128,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= -github.com/ip2location/ip2location-go/v9 v9.5.0 h1:7gqKncm4MhBrpJIK0PmV8o6Bf8YbbSAPjORzyjAv1iM= -github.com/ip2location/ip2location-go/v9 v9.5.0/go.mod h1:s5SV6YZL10TpfPpXw//7fEJC65G/yH7Oh+Tjq9JcQEQ= github.com/ip2location/ip2location-go/v9 v9.6.0 h1:do+hKM2wbVG3lXMavZzWuy0znXxJBvGc7mv0wzRVoYc= github.com/ip2location/ip2location-go/v9 v9.6.0/go.mod h1:MPLnsKxwQlvd2lBNcQCsLoyzJLDBFizuO67wXXdzoyI= github.com/ipipdotnet/ipdb-go v1.3.3 h1:GLSAW9ypLUd6EF9QNK2Uhxew9Jzs4XMJ9gOZEFnJm7U= @@ -150,24 +143,13 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= 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/lionsoul2014/ip2region/binding/golang v0.0.0-20221229040330-76ac73d972ba h1:wK2uDnft+Rp6OXnKWHho2CufJdk9+eaXtvRHitBbUU8= -github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20221229040330-76ac73d972ba/go.mod h1:C5LA5UO2ZXJrLaPLYtE1wUJMiyd/nwWaCO5cw/2pSHs= -github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20230201065125-561cdd83b71d h1:+nfICio7TU/4Yxx4S1Zy/u9pIDN/XQnwrwDhGQD25XM= -github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20230201065125-561cdd83b71d/go.mod h1:C5LA5UO2ZXJrLaPLYtE1wUJMiyd/nwWaCO5cw/2pSHs= -github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20230205110531-05840c74e63c h1:s1xqzuKdzMJlCIh3TD36lZKGfLD4KT340s/NbrXgRIw= -github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20230205110531-05840c74e63c/go.mod h1:C5LA5UO2ZXJrLaPLYtE1wUJMiyd/nwWaCO5cw/2pSHs= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20230415042440-a5e3d8259ae0 h1:LgmjED/yQILqmUED4GaXjrINWe7YJh4HM6z2EvEINPs= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20230415042440-a5e3d8259ae0/go.mod h1:C5LA5UO2ZXJrLaPLYtE1wUJMiyd/nwWaCO5cw/2pSHs= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= -github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= -github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -176,10 +158,6 @@ github.com/oschwald/geoip2-golang v1.8.0 h1:KfjYB8ojCEn/QLqsDU0AzrJ3R5Qa9vFlx3z6 github.com/oschwald/geoip2-golang v1.8.0/go.mod h1:R7bRvYjOeaoenAp9sKRS8GX5bJWcZ0laWO5+DauEktw= github.com/oschwald/maxminddb-golang v1.10.0 h1:Xp1u0ZhqkSuopaKmk1WwHtjF0H9Hd9181uj2MQ5Vndg= github.com/oschwald/maxminddb-golang v1.10.0/go.mod h1:Y2ELenReaLAZ0b400URyGwvYxHV1dLIxBuyOsyYjHK0= -github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= -github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= -github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU= -github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pelletier/go-toml/v2 v2.0.7 h1:muncTPStnKRos5dpVKULv2FVd4bMOhNePj9CjgDb8Us= github.com/pelletier/go-toml/v2 v2.0.7/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -196,24 +174,16 @@ github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f h1:PF9WV5j/x github.com/saracen/go7z-fixtures v0.0.0-20190623165746-aa6b8fba1d2f/go.mod h1:6Ff0ADODZ6S3gYepgZ2w7OqFrTqtFcfwDUhmm8jsUhs= github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f h1:1cJITU3JUI8qNS5T0BlXwANsVdyoJQHQ4hvOxbunPCw= github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f/go.mod h1:LyBTue+RWeyIfN3ZJ4wVxvDuvlGJtDgCLgCb6HCPgps= -github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= -github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= -github.com/spf13/afero v1.9.4 h1:Sd43wM1IWz/s1aVXdOBkjJvuP8UdyqioeE4AmM0QsBs= -github.com/spf13/afero v1.9.4/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM= github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= -github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= -github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.14.0 h1:Rg7d3Lo706X9tHsJMUjdiwMpHB7W8WnSVOssIY+JElU= -github.com/spf13/viper v1.14.0/go.mod h1:WT//axPky3FdvXHzGw33dNdXXXfFQqmEalje+egj8As= github.com/spf13/viper v1.15.0 h1:js3yy885G8xwJa6iOISGFwd+qlUo5AvyXb7CiihdtiU= github.com/spf13/viper v1.15.0/go.mod h1:fFcTBJxvhhzSJiZy8n+PeW6t8l+KeT/uTARa0jHOQLA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -227,8 +197,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs= -github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= @@ -249,7 +217,6 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 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-20220722155217-630584e8d5aa/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= @@ -284,7 +251,7 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -349,7 +316,6 @@ golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -372,14 +338,9 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 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-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= -golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -392,10 +353,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= -golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -448,7 +405,7 @@ golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU= +golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/internal/db/db.go b/internal/db/db.go index 6d75a6f2..24fcad3e 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -1,10 +1,9 @@ package db import ( - "log" - "strings" - "github.com/spf13/viper" + "github.com/zu1k/nali/pkg/wry" + "log" "github.com/zu1k/nali/pkg/cdn" "github.com/zu1k/nali/pkg/dbif" @@ -69,15 +68,15 @@ func GetDB(typ dbif.QueryType) (db dbif.DB) { return } -func Find(typ dbif.QueryType, query string) string { +func Find(typ dbif.QueryType, query string) *wry.Result { if result, found := queryCache.Load(query); found { - return result.(string) + return result.(*wry.Result) } result, err := GetDB(typ).Find(query) if err != nil { - return "" + return nil } - r := strings.Trim(result.String(), " ") - queryCache.Store(query, r) - return r + res := result.(*wry.Result) + queryCache.Store(query, res) + return res } diff --git a/pkg/entity/entity.go b/pkg/entity/entity.go index a3cd31d4..fee458b3 100644 --- a/pkg/entity/entity.go +++ b/pkg/entity/entity.go @@ -1,10 +1,11 @@ package entity import ( - "strings" - + "encoding/json" "github.com/fatih/color" "github.com/zu1k/nali/pkg/dbif" + "log" + "strings" ) type EntityType uint @@ -18,17 +19,26 @@ const ( ) type Entity struct { - Loc [2]int // s[Loc[0]:Loc[1]] - Type EntityType + Loc [2]int `json:"-"` // s[Loc[0]:Loc[1]] + Type EntityType `json:"type"` - Text string - Info string + Text string `json:"ip"` + Info string `json:"tag"` + GEO string `json:"geo"` } func (e Entity) ParseInfo() error { return nil } +func (e Entity) Json() string { + jsonResult, err := json.Marshal(e) + if err != nil { + log.Fatal(err.Error()) + } + return string(jsonResult) +} + type Entities []*Entity func (es Entities) Len() int { @@ -69,7 +79,23 @@ func (es Entities) ColorString() string { if e.Type != TypePlain && len(e.Info) > 0 { s += " [" + color.RedString(e.Info) + "] " } - line.WriteString(s) + line.WriteString(s + "\n") } return line.String() } + +func (es Entities) Json() string { + jsonResult, err := json.Marshal(es) + if err != nil { + log.Fatal(err.Error()) + } + return string(jsonResult) +} + +func (es Entities) JsonLine() string { + var s strings.Builder + for _, e := range es { + s.WriteString(e.Json() + "\n") + } + return s.String() +} diff --git a/pkg/entity/parse.go b/pkg/entity/parse.go index 17110fb0..17c8855c 100644 --- a/pkg/entity/parse.go +++ b/pkg/entity/parse.go @@ -42,20 +42,22 @@ func ParseLine(line string) Entities { } sort.Sort(tmp) - es := make(Entities, 0, len(tmp)) + var es Entities idx := 0 for _, e := range tmp { start := e.Loc[0] if start >= idx { - if start > idx { - es = append(es, &Entity{ - Loc: [2]int{idx, start}, - Type: TypePlain, - Text: line[idx:start], - }) - } - e.Info = db.Find(dbif.QueryType(e.Type), e.Text) + //if start > idx { + // es = append(es, &Entity{ + // Loc: [2]int{idx, start}, + // Type: TypePlain, + // Text: line[idx:start], + // }) + //} + res := db.Find(dbif.QueryType(e.Type), e.Text) + e.Info = res.Area + e.GEO = res.Country es = append(es, e) idx = e.Loc[1] } From d6d26f71f84ebe70a1b243a877c758963fe7af1e Mon Sep 17 00:00:00 2001 From: M09Ic Date: Fri, 19 May 2023 17:34:57 +0800 Subject: [PATCH 2/9] Fix the difference in output between stdin and cmd input. --- cmd/root.go | 18 +++++++----------- internal/db/db.go | 11 ++++++----- pkg/common/result.go | 5 +++++ pkg/entity/entity.go | 39 +++++++++++++++++---------------------- pkg/entity/parse.go | 24 ++++++------------------ 5 files changed, 41 insertions(+), 56 deletions(-) create mode 100644 pkg/common/result.go diff --git a/cmd/root.go b/cmd/root.go index 8ca105d6..7b7e8227 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,13 +3,14 @@ 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" - "log" - "os" - "strings" "github.com/zu1k/nali/internal/constant" "github.com/zu1k/nali/pkg/common" @@ -61,7 +62,6 @@ Find document on: https://github.com/zu1k/nali Run: func(cmd *cobra.Command, args []string) { gbk, _ := cmd.Flags().GetBool("gbk") isJson, _ := cmd.Flags().GetBool("json") - isJsonline, _ := cmd.Flags().GetBool("jsonline") if len(args) == 0 { stdin := bufio.NewScanner(os.Stdin) @@ -75,17 +75,14 @@ Find document on: https://github.com/zu1k/nali return } if isJson { - _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).Json()) + _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).Json()) } else { - _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).ColorString()) + _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).ColorString()) } - } } else { if isJson { _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).Json()) - } else if isJsonline { - _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).JsonLine()) } else { _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString()) } @@ -102,6 +99,5 @@ func Execute() { func init() { rootCmd.Flags().Bool("gbk", false, "Use GBK decoder") - rootCmd.PersistentFlags().BoolP("json", "j", false, "Output in JSON format") - rootCmd.PersistentFlags().BoolP("jsonline", "l", false, "JSON output with newlines") + rootCmd.Flags().BoolP("json", "j", false, "Output in JSON format") } diff --git a/internal/db/db.go b/internal/db/db.go index 24fcad3e..f8a4c2aa 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -1,11 +1,12 @@ package db import ( - "github.com/spf13/viper" - "github.com/zu1k/nali/pkg/wry" "log" + "github.com/spf13/viper" + "github.com/zu1k/nali/pkg/cdn" + "github.com/zu1k/nali/pkg/common" "github.com/zu1k/nali/pkg/dbif" "github.com/zu1k/nali/pkg/geoip" "github.com/zu1k/nali/pkg/qqwry" @@ -68,15 +69,15 @@ func GetDB(typ dbif.QueryType) (db dbif.DB) { return } -func Find(typ dbif.QueryType, query string) *wry.Result { +func Find(typ dbif.QueryType, query string) common.Result { if result, found := queryCache.Load(query); found { - return result.(*wry.Result) + return result.(common.Result) } result, err := GetDB(typ).Find(query) if err != nil { return nil } - res := result.(*wry.Result) + res := result queryCache.Store(query, res) return res } diff --git a/pkg/common/result.go b/pkg/common/result.go new file mode 100644 index 00000000..c66e6bc6 --- /dev/null +++ b/pkg/common/result.go @@ -0,0 +1,5 @@ +package common + +type Result interface { + String() string +} diff --git a/pkg/entity/entity.go b/pkg/entity/entity.go index fee458b3..618cc623 100644 --- a/pkg/entity/entity.go +++ b/pkg/entity/entity.go @@ -22,9 +22,9 @@ type Entity struct { Loc [2]int `json:"-"` // s[Loc[0]:Loc[1]] Type EntityType `json:"type"` - Text string `json:"ip"` - Info string `json:"tag"` - GEO string `json:"geo"` + Text string `json:"ip"` + InfoText string `json:"text"` + Info interface{} `json:"info"` } func (e Entity) ParseInfo() error { @@ -57,8 +57,8 @@ func (es Entities) String() string { var result strings.Builder for _, entity := range es { result.WriteString(entity.Text) - if entity.Type != TypePlain && len(entity.Info) > 0 { - result.WriteString("[" + entity.Info + "] ") + if entity.Type != TypePlain && len(entity.InfoText) > 0 { + result.WriteString("[" + entity.InfoText + "] ") } } return result.String() @@ -67,35 +67,30 @@ func (es Entities) String() string { func (es Entities) ColorString() string { var line strings.Builder for _, e := range es { - s := e.Text switch e.Type { case TypeIPv4: - s = color.GreenString(e.Text) + line.WriteString(color.GreenString(e.Text)) case TypeIPv6: - s = color.BlueString(e.Text) + line.WriteString(color.BlueString(e.Text)) case TypeDomain: - s = color.YellowString(e.Text) + line.WriteString(color.YellowString(e.Text)) + default: + line.WriteString(e.Text) } - if e.Type != TypePlain && len(e.Info) > 0 { - s += " [" + color.RedString(e.Info) + "] " + if e.Type != TypePlain { + if len(e.InfoText) > 0 { + line.WriteString(" [" + color.RedString(e.InfoText) + "] ") + } } - line.WriteString(s + "\n") + line.WriteString("\n") } - return line.String() + return strings.TrimSpace(line.String()) } func (es Entities) Json() string { - jsonResult, err := json.Marshal(es) - if err != nil { - log.Fatal(err.Error()) - } - return string(jsonResult) -} - -func (es Entities) JsonLine() string { var s strings.Builder for _, e := range es { s.WriteString(e.Json() + "\n") } - return s.String() + return strings.TrimSpace(s.String()) } diff --git a/pkg/entity/parse.go b/pkg/entity/parse.go index 17c8855c..93bd99fc 100644 --- a/pkg/entity/parse.go +++ b/pkg/entity/parse.go @@ -48,27 +48,15 @@ func ParseLine(line string) Entities { for _, e := range tmp { start := e.Loc[0] if start >= idx { - //if start > idx { - // es = append(es, &Entity{ - // Loc: [2]int{idx, start}, - // Type: TypePlain, - // Text: line[idx:start], - // }) - //} res := db.Find(dbif.QueryType(e.Type), e.Text) - e.Info = res.Area - e.GEO = res.Country - es = append(es, e) - idx = e.Loc[1] + if res != nil { + e.InfoText = res.String() + e.Info = res + es = append(es, e) + idx = e.Loc[1] + } } } - if total := len(line); idx < total { - es = append(es, &Entity{ - Loc: [2]int{idx, total}, - Type: TypePlain, - Text: line[idx:total], - }) - } return es } From 36025d8f0189ddcfc65bbc9789433ea8c9cff7ec Mon Sep 17 00:00:00 2001 From: M09Ic Date: Sat, 20 May 2023 17:14:03 +0800 Subject: [PATCH 3/9] add json note --- pkg/cdn/cdn.go | 4 ++-- pkg/geoip/geoip.go | 10 +++++----- pkg/ip2location/ip2location.go | 6 +++--- pkg/ipip/ipip.go | 6 +++--- pkg/wry/wry.go | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/cdn/cdn.go b/pkg/cdn/cdn.go index 5db5590a..eba97c53 100644 --- a/pkg/cdn/cdn.go +++ b/pkg/cdn/cdn.go @@ -31,8 +31,8 @@ type CDNReTuple struct { } type CDNResult struct { - Name string `yaml:"name"` - Link string `yaml:"link"` + Name string `yaml:"name" json:"name"` + Link string `yaml:"link" json:"link"` } func (r CDNResult) String() string { diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index 2a0f72f1..e6f817d3 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -49,20 +49,20 @@ func (g GeoIP) Find(query string, params ...string) (result fmt.Stringer, err er result = Result{ Country: record.Country.Names[lang], - City: record.City.Names[lang], + Area: record.City.Names[lang], } return } type Result struct { - Country string - City string + Country string `json:"country"` + Area string `json:"area"` } func (r Result) String() string { - if r.City == "" { + if r.Area == "" { return r.Country } else { - return fmt.Sprintf("%s %s", r.Country, r.City) + return fmt.Sprintf("%s %s", r.Country, r.Area) } } diff --git a/pkg/ip2location/ip2location.go b/pkg/ip2location/ip2location.go index 1e1cd1e7..11f9f0be 100644 --- a/pkg/ip2location/ip2location.go +++ b/pkg/ip2location/ip2location.go @@ -51,9 +51,9 @@ func (x IP2Location) Find(query string, params ...string) (result fmt.Stringer, } type Result struct { - Country string - Region string - City string + Country string `json:"country"` + Region string `json:"region"` + City string `json:"city"` } func (r Result) String() string { diff --git a/pkg/ipip/ipip.go b/pkg/ipip/ipip.go index ec537ed1..eabb6303 100644 --- a/pkg/ipip/ipip.go +++ b/pkg/ipip/ipip.go @@ -28,9 +28,9 @@ func NewIPIP(filePath string) (*IPIPFree, error) { } type Result struct { - Country string - Region string - City string + Country string `json:"country"` + Region string `json:"region"` + City string `json:"city"` } func (r Result) String() string { diff --git a/pkg/wry/wry.go b/pkg/wry/wry.go index 506354fa..9d727726 100644 --- a/pkg/wry/wry.go +++ b/pkg/wry/wry.go @@ -86,8 +86,8 @@ func (r *Reader) readString(seek bool) string { } type Result struct { - Country string - Area string + Country string `json:"country"` + Area string `json:"area"` } func (r *Result) DecodeGBK() *Result { From 05d918528fb28bf8f6ee762840796f2049fbc547 Mon Sep 17 00:00:00 2001 From: M09Ic Date: Sat, 20 May 2023 17:37:24 +0800 Subject: [PATCH 4/9] Fix the bug that the output format is different from the original version. --- cmd/root.go | 4 ++-- pkg/entity/entity.go | 5 ++++- pkg/entity/parse.go | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 7b7e8227..660ec0e5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -75,14 +75,14 @@ Find document on: https://github.com/zu1k/nali return } if isJson { - _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).Json()) + _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).Json()) } else { _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).ColorString()) } } } else { if isJson { - _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).Json()) + _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(strings.Join(args, " ")).Json()) } else { _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString()) } diff --git a/pkg/entity/entity.go b/pkg/entity/entity.go index 618cc623..2c7afbcb 100644 --- a/pkg/entity/entity.go +++ b/pkg/entity/entity.go @@ -90,7 +90,10 @@ func (es Entities) ColorString() string { func (es Entities) Json() string { var s strings.Builder for _, e := range es { + if e.Type == TypePlain { + continue + } s.WriteString(e.Json() + "\n") } - return strings.TrimSpace(s.String()) + return s.String() } diff --git a/pkg/entity/parse.go b/pkg/entity/parse.go index 93bd99fc..2ad9d3ac 100644 --- a/pkg/entity/parse.go +++ b/pkg/entity/parse.go @@ -48,6 +48,13 @@ func ParseLine(line string) Entities { for _, e := range tmp { start := e.Loc[0] if start >= idx { + if start > idx { + es = append(es, &Entity{ + Loc: [2]int{idx, start}, + Type: TypePlain, + Text: line[idx:start], + }) + } res := db.Find(dbif.QueryType(e.Type), e.Text) if res != nil { e.InfoText = res.String() @@ -57,6 +64,12 @@ func ParseLine(line string) Entities { } } } - + if total := len(line); idx < total { + es = append(es, &Entity{ + Loc: [2]int{idx, total}, + Type: TypePlain, + Text: line[idx:total], + }) + } return es } From 3819ae680883566995fc6b6a8a5b2af47d4f4fa1 Mon Sep 17 00:00:00 2001 From: M09Ic Date: Sat, 20 May 2023 17:53:43 +0800 Subject: [PATCH 5/9] Add the source field to the json output. --- internal/db/db.go | 14 ++++++++++---- pkg/cdn/cdn.go | 4 ++++ pkg/dbif/db.go | 1 + pkg/geoip/geoip.go | 4 ++++ pkg/ip2location/ip2location.go | 8 ++++++-- pkg/ip2region/ip2region.go | 4 ++++ pkg/ipip/ipip.go | 4 ++++ pkg/qqwry/qqwry.go | 4 ++++ pkg/zxipv6wry/zxipv6wry.go | 4 ++++ 9 files changed, 41 insertions(+), 6 deletions(-) diff --git a/internal/db/db.go b/internal/db/db.go index f8a4c2aa..91d82e48 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -13,6 +13,11 @@ import ( "github.com/zu1k/nali/pkg/zxipv6wry" ) +type Result struct { + Source string `json:"source"` + common.Result +} + func GetDB(typ dbif.QueryType) (db dbif.DB) { if db, found := dbTypeCache[typ]; found { return db @@ -69,15 +74,16 @@ func GetDB(typ dbif.QueryType) (db dbif.DB) { return } -func Find(typ dbif.QueryType, query string) common.Result { +func Find(typ dbif.QueryType, query string) *Result { if result, found := queryCache.Load(query); found { - return result.(common.Result) + return result.(*Result) } - result, err := GetDB(typ).Find(query) + db := GetDB(typ) + result, err := db.Find(query) if err != nil { return nil } - res := result + res := &Result{db.Name(), result} queryCache.Store(query, res) return res } diff --git a/pkg/cdn/cdn.go b/pkg/cdn/cdn.go index eba97c53..e39cceed 100644 --- a/pkg/cdn/cdn.go +++ b/pkg/cdn/cdn.go @@ -103,6 +103,10 @@ func (db CDN) Find(query string, params ...string) (result fmt.Stringer, err err return nil, errors.New("not found") } +func (db CDN) Name() string { + return "cdn" +} + func parseBaseCname(domain string) (result []string) { parts := strings.Split(domain, ".") size := len(parts) diff --git a/pkg/dbif/db.go b/pkg/dbif/db.go index 90c4bdd4..ef55f5f1 100644 --- a/pkg/dbif/db.go +++ b/pkg/dbif/db.go @@ -22,6 +22,7 @@ const ( type DB interface { Find(query string, params ...string) (result fmt.Stringer, err error) + Name() string } var ( diff --git a/pkg/geoip/geoip.go b/pkg/geoip/geoip.go index e6f817d3..3e16f76c 100644 --- a/pkg/geoip/geoip.go +++ b/pkg/geoip/geoip.go @@ -54,6 +54,10 @@ func (g GeoIP) Find(query string, params ...string) (result fmt.Stringer, err er return } +func (db GeoIP) Name() string { + return "geoip" +} + type Result struct { Country string `json:"country"` Area string `json:"area"` diff --git a/pkg/ip2location/ip2location.go b/pkg/ip2location/ip2location.go index 11f9f0be..7fb4eff7 100644 --- a/pkg/ip2location/ip2location.go +++ b/pkg/ip2location/ip2location.go @@ -31,12 +31,12 @@ func NewIP2Location(filePath string) (*IP2Location, error) { } } -func (x IP2Location) Find(query string, params ...string) (result fmt.Stringer, err error) { +func (db IP2Location) Find(query string, params ...string) (result fmt.Stringer, err error) { ip := net.ParseIP(query) if ip == nil { return nil, errors.New("Query should be valid IP") } - record, err := x.db.Get_all(ip.String()) + record, err := db.db.Get_all(ip.String()) if err != nil { return @@ -50,6 +50,10 @@ func (x IP2Location) Find(query string, params ...string) (result fmt.Stringer, return } +func (db IP2Location) Name() string { + return "ip2location" +} + type Result struct { Country string `json:"country"` Region string `json:"region"` diff --git a/pkg/ip2region/ip2region.go b/pkg/ip2region/ip2region.go index ae6dcc12..b00f0bfb 100644 --- a/pkg/ip2region/ip2region.go +++ b/pkg/ip2region/ip2region.go @@ -67,3 +67,7 @@ func (db Ip2Region) Find(query string, params ...string) (result fmt.Stringer, e return nil, errors.New("ip2region 未初始化") } + +func (db Ip2Region) Name() string { + return "ip2region" +} diff --git a/pkg/ipip/ipip.go b/pkg/ipip/ipip.go index eabb6303..30e80c7a 100644 --- a/pkg/ipip/ipip.go +++ b/pkg/ipip/ipip.go @@ -54,3 +54,7 @@ func (db IPIPFree) Find(query string, params ...string) (result fmt.Stringer, er return } } + +func (db IPIPFree) Name() string { + return "ipip" +} diff --git a/pkg/qqwry/qqwry.go b/pkg/qqwry/qqwry.go index d637075e..2f165f01 100644 --- a/pkg/qqwry/qqwry.go +++ b/pkg/qqwry/qqwry.go @@ -90,6 +90,10 @@ func (db QQwry) Find(query string, params ...string) (result fmt.Stringer, err e return reader.Result.DecodeGBK(), nil } +func (db QQwry) Name() string { + return "qqwry" +} + func CheckFile(data []byte) bool { if len(data) < 8 { return false diff --git a/pkg/zxipv6wry/zxipv6wry.go b/pkg/zxipv6wry/zxipv6wry.go index 9816c6e1..fa321156 100644 --- a/pkg/zxipv6wry/zxipv6wry.go +++ b/pkg/zxipv6wry/zxipv6wry.go @@ -82,6 +82,10 @@ func (db *ZXwry) Find(query string, _ ...string) (result fmt.Stringer, err error return reader.Result, nil } +func (db *ZXwry) Name() string { + return "xzwry" +} + func CheckFile(data []byte) bool { if len(data) < 4 { return false From 97cde2aa94c13f504ffb126641f5175193712e8b Mon Sep 17 00:00:00 2001 From: M09Ic Date: Sat, 20 May 2023 18:12:31 +0800 Subject: [PATCH 6/9] Fix the bug of json nested error --- internal/db/db.go | 2 +- pkg/entity/entity.go | 1 + pkg/entity/parse.go | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/db/db.go b/internal/db/db.go index 91d82e48..6f02359d 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -14,7 +14,7 @@ import ( ) type Result struct { - Source string `json:"source"` + Source string common.Result } diff --git a/pkg/entity/entity.go b/pkg/entity/entity.go index 2c7afbcb..d3946a8c 100644 --- a/pkg/entity/entity.go +++ b/pkg/entity/entity.go @@ -24,6 +24,7 @@ type Entity struct { Text string `json:"ip"` InfoText string `json:"text"` + Source string `json:"source"` Info interface{} `json:"info"` } diff --git a/pkg/entity/parse.go b/pkg/entity/parse.go index 2ad9d3ac..f6827d70 100644 --- a/pkg/entity/parse.go +++ b/pkg/entity/parse.go @@ -58,7 +58,8 @@ func ParseLine(line string) Entities { res := db.Find(dbif.QueryType(e.Type), e.Text) if res != nil { e.InfoText = res.String() - e.Info = res + e.Info = res.Result + e.Source = res.Source es = append(es, e) idx = e.Loc[1] } From 56be0c61cae1ce6e653fefcca974e1be371a6a91 Mon Sep 17 00:00:00 2001 From: M09Ic Date: Sun, 21 May 2023 14:10:31 +0800 Subject: [PATCH 7/9] revolt output behavior, making it consistent with the original. --- cmd/root.go | 6 ++++-- pkg/entity/entity.go | 19 ++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 660ec0e5..d37113da 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -77,14 +77,16 @@ Find document on: https://github.com/zu1k/nali if isJson { _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).Json()) } else { - _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).ColorString()) + _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).ColorString()) } } } else { if isJson { _, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(strings.Join(args, " ")).Json()) } else { - _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString()) + for _, line := range args { + _, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(line).ColorString()) + } } } }, diff --git a/pkg/entity/entity.go b/pkg/entity/entity.go index d3946a8c..19b97dcb 100644 --- a/pkg/entity/entity.go +++ b/pkg/entity/entity.go @@ -68,24 +68,21 @@ func (es Entities) String() string { func (es Entities) ColorString() string { var line strings.Builder for _, e := range es { + s := e.Text switch e.Type { case TypeIPv4: - line.WriteString(color.GreenString(e.Text)) + s = color.GreenString(e.Text) case TypeIPv6: - line.WriteString(color.BlueString(e.Text)) + s = color.BlueString(e.Text) case TypeDomain: - line.WriteString(color.YellowString(e.Text)) - default: - line.WriteString(e.Text) + s = color.YellowString(e.Text) } - if e.Type != TypePlain { - if len(e.InfoText) > 0 { - line.WriteString(" [" + color.RedString(e.InfoText) + "] ") - } + if e.Type != TypePlain && len(e.InfoText) > 0 { + s += " [" + color.RedString(e.InfoText) + "] " } - line.WriteString("\n") + line.WriteString(s) } - return strings.TrimSpace(line.String()) + return line.String() } func (es Entities) Json() string { From a0e6398db8f125865b46a9583ff16b76722459dd Mon Sep 17 00:00:00 2001 From: M09Ic Date: Sun, 21 May 2023 14:41:21 +0800 Subject: [PATCH 8/9] unified dbif Name() return values to db.Name values --- pkg/zxipv6wry/zxipv6wry.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/zxipv6wry/zxipv6wry.go b/pkg/zxipv6wry/zxipv6wry.go index fa321156..f1ec1c3e 100644 --- a/pkg/zxipv6wry/zxipv6wry.go +++ b/pkg/zxipv6wry/zxipv6wry.go @@ -83,7 +83,7 @@ func (db *ZXwry) Find(query string, _ ...string) (result fmt.Stringer, err error } func (db *ZXwry) Name() string { - return "xzwry" + return "zxipv6wry" } func CheckFile(data []byte) bool { From 904c57efa90b7a7efedb007c28d113161c57e857 Mon Sep 17 00:00:00 2001 From: M09Ic Date: Sun, 21 May 2023 14:45:37 +0800 Subject: [PATCH 9/9] move Result struct to type.go --- internal/db/db.go | 6 ------ internal/db/type.go | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/db/db.go b/internal/db/db.go index 6f02359d..cf0bd100 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -6,18 +6,12 @@ import ( "github.com/spf13/viper" "github.com/zu1k/nali/pkg/cdn" - "github.com/zu1k/nali/pkg/common" "github.com/zu1k/nali/pkg/dbif" "github.com/zu1k/nali/pkg/geoip" "github.com/zu1k/nali/pkg/qqwry" "github.com/zu1k/nali/pkg/zxipv6wry" ) -type Result struct { - Source string - common.Result -} - func GetDB(typ dbif.QueryType) (db dbif.DB) { if db, found := dbTypeCache[typ]; found { return db diff --git a/internal/db/type.go b/internal/db/type.go index 5db927bf..e19f56df 100644 --- a/internal/db/type.go +++ b/internal/db/type.go @@ -1,6 +1,7 @@ package db import ( + "github.com/zu1k/nali/pkg/common" "log" "github.com/zu1k/nali/pkg/cdn" @@ -139,3 +140,8 @@ func getDbByName(name string) (db *DB) { log.Fatalf("DB with name %s not found!\n", name) return } + +type Result struct { + Source string + common.Result +}