2020-07-17 01:05:25 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-03-02 04:43:41 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2020-07-17 01:41:09 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2022-03-02 04:43:41 +00:00
|
|
|
"strings"
|
2020-07-17 01:05:25 +00:00
|
|
|
|
2022-03-02 04:43:41 +00:00
|
|
|
"github.com/fatih/color"
|
2021-08-02 23:54:35 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-03-02 04:43:41 +00:00
|
|
|
"golang.org/x/text/encoding/simplifiedchinese"
|
|
|
|
"golang.org/x/text/transform"
|
2022-05-09 06:48:28 +00:00
|
|
|
|
|
|
|
"github.com/zu1k/nali/internal/constant"
|
2022-08-14 14:40:52 +00:00
|
|
|
"github.com/zu1k/nali/pkg/common"
|
2022-05-09 06:48:28 +00:00
|
|
|
"github.com/zu1k/nali/pkg/entity"
|
2020-07-17 01:05:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "nali",
|
2020-07-17 05:49:11 +00:00
|
|
|
Short: "An offline tool for querying IP geographic information",
|
2020-07-18 02:53:44 +00:00
|
|
|
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
|
|
|
|
|
2022-04-03 09:25:55 +00:00
|
|
|
$ dig nali.zu1k.com +short | nali
|
2020-07-18 02:53:44 +00:00
|
|
|
|
|
|
|
#5 Use with nslookup
|
|
|
|
|
2022-04-03 09:25:55 +00:00
|
|
|
$ nslookup nali.zu1k.com 8.8.8.8 | nali
|
2020-07-18 02:53:44 +00:00
|
|
|
|
|
|
|
#6 Use with any other program
|
|
|
|
|
|
|
|
bash abc.sh | nali
|
|
|
|
|
|
|
|
#7 IPV6 support
|
|
|
|
`,
|
2021-12-17 05:24:03 +00:00
|
|
|
Version: constant.Version,
|
|
|
|
Args: cobra.MinimumNArgs(0),
|
2020-07-17 01:05:25 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-08-02 23:54:35 +00:00
|
|
|
gbk, _ := cmd.Flags().GetBool("gbk")
|
2022-03-02 04:43:41 +00:00
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
stdin := bufio.NewScanner(os.Stdin)
|
2022-08-14 13:39:53 +00:00
|
|
|
stdin.Split(common.ScanLines)
|
2022-03-02 04:43:41 +00:00
|
|
|
for stdin.Scan() {
|
|
|
|
line := stdin.Text()
|
|
|
|
if gbk {
|
|
|
|
line, _, _ = transform.String(simplifiedchinese.GBK.NewDecoder(), line)
|
|
|
|
}
|
2022-08-15 01:40:14 +00:00
|
|
|
if line := strings.TrimSpace(line); line == "quit" || line == "exit" {
|
2022-03-02 04:43:41 +00:00
|
|
|
return
|
|
|
|
}
|
2022-08-14 14:54:24 +00:00
|
|
|
_, _ = fmt.Fprintf(color.Output, "%s", entity.ParseLine(line).ColorString())
|
2022-03-02 04:43:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(color.Output, "%s\n", entity.ParseLine(strings.Join(args, " ")).ColorString())
|
|
|
|
}
|
2020-07-17 01:05:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-17 01:55:24 +00:00
|
|
|
// Execute parse subcommand and run
|
2020-07-17 01:05:25 +00:00
|
|
|
func Execute() {
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2020-07-17 01:41:09 +00:00
|
|
|
log.Fatal(err.Error())
|
2020-07-17 01:05:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-02 23:54:35 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.Flags().Bool("gbk", false, "Use GBK decoder")
|
|
|
|
}
|