diff --git a/cmd/root.go b/cmd/root.go index 342f7ca..ff285f8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,7 @@ package cmd import ( + "bufio" "fmt" "log" "os" @@ -14,13 +15,20 @@ var rootCmd = &cobra.Command{ Use: "nali", Short: "", Long: ``, - Args: cobra.MinimumNArgs(1), + Args: cobra.MinimumNArgs(0), Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { - fmt.Println("Usage: balabala") - return + stdin := bufio.NewScanner(os.Stdin) + for stdin.Scan() { + line := stdin.Text() + if line == "quit" || line == "exit" { + return + } + fmt.Println(app.ReplaceInString(line)) + } + } else { + app.ParseIPs(args) } - app.ParseIPs(args) }, } diff --git a/go.sum b/go.sum index 3012cc5..ae053a3 100644 --- a/go.sum +++ b/go.sum @@ -32,6 +32,7 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -146,6 +147,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= diff --git a/internal/app/parse.go b/internal/app/parse.go index bd12e4b..54606aa 100644 --- a/internal/app/parse.go +++ b/internal/app/parse.go @@ -3,6 +3,9 @@ package app import ( "fmt" "path/filepath" + "strings" + + "github.com/zu1k/nali/internal/iptools" "github.com/zu1k/nali/constant" @@ -39,14 +42,23 @@ func SetDB(dbName ipdb.IPDBType) { // parse several ips func ParseIPs(ips []string) { for _, ip := range ips { - ParseIP(ip) + if iptools.ValidIP4(ip) { + result := db.Find(ip) + fmt.Println(formatResult(ip, result)) + } else { + fmt.Println(ReplaceInString(ip)) + } } } -// parse one ip -func ParseIP(ip string) { - result := db.Find(ip) - fmt.Println(formatResult(ip, result)) +func ReplaceInString(str string) (result string) { + result = str + ips := iptools.GetIP4FromString(str) + for _, ip := range ips { + info := db.Find(ip) + result = strings.ReplaceAll(result, ip, formatResult(ip, info)) + } + return } func formatResult(ip string, result string) string { diff --git a/internal/iptools/ipparser.go b/internal/iptools/ipparser.go new file mode 100644 index 0000000..a362cd4 --- /dev/null +++ b/internal/iptools/ipparser.go @@ -0,0 +1,26 @@ +package iptools + +import ( + "regexp" + "strings" +) + +var ( + ipv4re0 *regexp.Regexp + ipv4re *regexp.Regexp +) + +func init() { + ipv4re0 = regexp.MustCompile(`^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$`) + ipv4re = regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`) +} + +func ValidIP4(str string) bool { + str = strings.Trim(str, " ") + return ipv4re0.MatchString(str) +} + +func GetIP4FromString(str string) []string { + str = strings.Trim(str, " ") + return ipv4re.FindAllString(str, -1) +} diff --git a/internal/iptools/iptools_test.go b/internal/iptools/iptools_test.go new file mode 100644 index 0000000..9cd3fa3 --- /dev/null +++ b/internal/iptools/iptools_test.go @@ -0,0 +1,12 @@ +package iptools + +import ( + "fmt" + "testing" +) + +func TestIP4Re(t *testing.T) { + str := "aaa1.1.11.23a36.36.32.200" + fmt.Println(GetIP4FromString(str)) + fmt.Println(ValidIP4(str)) +}