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

93 lines
1.7 KiB
Go

package cmd
import (
"bufio"
"fmt"
"log"
"os"
"runtime"
"syscall"
"github.com/spf13/cobra"
"github.com/zu1k/nali/internal/app"
"github.com/zu1k/nali/internal/ipdb"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
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.lgf.im +short | nali
#5 Use with nslookup
$ nslookup nali.lgf.im 8.8.8.8 | nali
#6 Use with any other program
bash abc.sh | nali
#7 IPV6 support
$ nslookup google.com | nali
`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
app.InitIPDB(ipdb.GetIPDBType())
app.InitCDNDB()
if len(args) == 0 {
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
line := stdin.Text()
if runtime.GOOS == "windows" {
ftype, _ := syscall.GetFileType(syscall.Handle(os.Stdin.Fd()))
if ftype == 3 {
line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line)
}
}
if line == "quit" || line == "exit" {
return
}
fmt.Printf("%s\n", app.ReplaceIPInString(app.ReplaceCDNInString(line)))
}
} else {
app.ParseIPs(args)
}
},
}
// Execute parse subcommand and run
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
}