1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-22 13:19:02 +08:00

color print

This commit is contained in:
zu1k 2021-08-03 07:54:35 +08:00
parent 40274f64a4
commit 592a2eabb5
10 changed files with 64 additions and 91 deletions

View File

@ -38,6 +38,7 @@ However the C version has too few functions, and the js version is too big and t
- Both ipv4 and ipv6 supported
- CDN provider query
- Full platform support
- Color print
## Install
@ -154,28 +155,6 @@ Address: 2a00:1450:400e:809::200e [荷兰Amsterdam Google Inc. 服务器网段]
### Query CDN provider
#### Query CDN provider only
```
$ 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
```
#### Also query IP geo
```
$ nslookup www.gov.cn | nali
Server: 127.0.0.53 [局域网 IP]
@ -192,14 +171,6 @@ Name: zgovweb.v.bsgslb.cn [白山云 CDN]
Address: 2001:428:6402:21b::6 [美国Louisiana州Monroe Qwest Communications Company, LLC (CenturyLink)]
```
#### Use standalone
You should parse cname by yourself
```
$ nali cdn cdn.somecdncname.com
```
## Interface
### Help
@ -211,14 +182,13 @@ Usage:
nali [command]
Available Commands:
cdn Query cdn service provider
completion generate the autocompletion script for the specified shell
help Help about any command
parse Query IP information
update update chunzhen ip database
Flags:
--gbk Use GBK decoder
-h, --help help for nali
-t, --toggle Help message for toggle
Use "nali [command] --help" for more information about a command.
```

View File

@ -4,8 +4,9 @@ import (
"log"
"os"
"github.com/spf13/cobra"
"github.com/zu1k/nali/internal/app"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
@ -52,7 +53,8 @@ Find document on: https://github.com/zu1k/nali
`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
app.Root(args)
gbk, _ := cmd.Flags().GetBool("gbk")
app.Root(args, gbk)
},
}
@ -63,3 +65,7 @@ func Execute() {
os.Exit(1)
}
}
func init() {
rootCmd.Flags().Bool("gbk", false, "Use GBK decoder")
}

View File

@ -4,14 +4,11 @@ import (
"log"
"path/filepath"
"github.com/zu1k/nali/internal/constant"
"github.com/zu1k/nali/pkg/cdn"
"github.com/zu1k/nali/pkg/zxipv6wry"
"github.com/zu1k/nali/pkg/qqwry"
"github.com/spf13/cobra"
"github.com/zu1k/nali/internal/constant"
"github.com/zu1k/nali/pkg/cdn"
"github.com/zu1k/nali/pkg/qqwry"
"github.com/zu1k/nali/pkg/zxipv6wry"
)
// updateCmd represents the update command

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/zu1k/nali
go 1.14
require (
github.com/fatih/color v1.12.0 // indirect
github.com/ipipdotnet/ipdb-go v1.3.1
github.com/oschwald/geoip2-golang v1.5.0
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda

7
go.sum
View File

@ -67,6 +67,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@ -177,7 +179,11 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
@ -372,6 +378,7 @@ golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/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=

View File

@ -2,21 +2,16 @@ package app
import (
"bufio"
"fmt"
"os"
"runtime"
"strings"
"github.com/zu1k/nali/internal/entity"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
var needTransform = false
func init() {
stat, _ := os.Stdin.Stat()
needTransform = ((stat.Mode() & os.ModeNamedPipe) != 0) && runtime.GOOS == "windows"
}
func Root(args []string) {
func Root(args []string, needTransform bool) {
if len(args) == 0 {
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
@ -27,11 +22,9 @@ func Root(args []string) {
if line == "quit" || line == "exit" {
return
}
// TODO: pring line
//fmt.Printf("%s\n", ReplaceIPInString(ReplaceCDNInString(line)))
fmt.Printf("%s\n", entity.ParseLine(line).ColorString())
}
} else {
// TODO: do something
//ParseIPs(args)
fmt.Printf("%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString())
}
}

View File

@ -1,35 +0,0 @@
package app
import (
"path/filepath"
db2 "github.com/zu1k/nali/internal/db"
"github.com/zu1k/nali/internal/constant"
geoip2 "github.com/zu1k/nali/pkg/geoip"
"github.com/zu1k/nali/pkg/ipip"
"github.com/zu1k/nali/pkg/qqwry"
"github.com/zu1k/nali/pkg/zxipv6wry"
)
var (
db []db2.IPDB
qqip qqwry.QQwry
geoip geoip2.GeoIP
)
// InitIPDB init ip database content
func InitIPDB(ipdbtype db2.IPDBType) {
db = make([]db2.IPDB, 1)
switch ipdbtype {
case db2.GEOIP2:
db[0] = geoip2.NewGeoIP(filepath.Join(constant.HomePath, "GeoLite2-City.mmdb"))
case db2.QQIP:
db[0] = qqwry.NewQQwry(filepath.Join(constant.HomePath, "qqwry.dat"))
db = append(db, zxipv6wry.NewZXwry(filepath.Join(constant.HomePath, "ipv6wry.database")))
case db2.IPIP:
db[0] = ipip.NewIPIPFree(filepath.Join(constant.HomePath, "ipipfree.ipdb"))
db = append(db, zxipv6wry.NewZXwry(filepath.Join(constant.HomePath, "ipv6wry.database")))
}
}

View File

@ -3,6 +3,8 @@ package entity
import (
"strings"
"github.com/fatih/color"
"github.com/zu1k/nali/pkg/dbif"
)
@ -52,3 +54,23 @@ func (es Entities) String() string {
}
return result.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)
case TypeIPv6:
s = color.BlueString(e.Text)
case TypeDomain:
s = color.YellowString(e.Text)
}
if e.Type != TypePlain && len(e.Info) > 0 {
s += " [" + color.RedString(e.Info) + "] "
}
line.WriteString(s)
}
return line.String()
}

View File

@ -57,5 +57,13 @@ func ParseLine(line string) Entities {
}
}
if len(es) == 0 {
es = append(es, &Entity{
Loc: []int{0, len(line)},
Type: TypePlain,
Text: line,
})
}
return es
}

View File

@ -9,3 +9,7 @@ func TestParse(t *testing.T) {
fmt.Println(ParseLine("2001:0db8:85a3:0000:0000:8a2e:0370:7334 baidu.com 1.2.3.4 baidu.com"))
fmt.Println(ParseLine("a.cn.b.com.c.org d.com"))
}
func TestColorPrint(t *testing.T) {
fmt.Println(ParseLine("2001:0db8:85a3:0000:0000:8a2e:0370:7334 baidu.com 1.2.3.4 baidu.com").ColorString())
}