commands

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CommandCheck = &cli.Command{
		Name:    "check",
		Aliases: []string{"c"},
		Usage:   "Check IP against range of CIDR blocks",
		UsageText: `
IPs that are provided without a mask will be assumed to be /32

CIDR blocks require a mask be provided.
`,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:     "ips",
				Aliases:  []string{"i"},
				Usage:    "CSV list of IPs with masks (csv)",
				Required: true,
			},
			&cli.StringFlag{
				Name:     "blocks",
				Aliases:  []string{"b"},
				Usage:    "CIDR blocks to be checked (csv)",
				Required: true,
			},
		},
		Action: func(c *cli.Context) error {

			blocks, err := parseInputCIDRs(c.String("blocks"))
			if err != nil {
				return err
			}

			ips, err := parseInputIPs(c.String("ips"))
			if err != nil {
				return err
			}

			for _, ip := range ips {
				if strings.HasPrefix(ip.String(), "0.0.0.0") {
					fmt.Printf("%s is in ALL CIDR sets\n", ip.String())
				} else if ok, cidr := isCiderIn(ip, blocks); ok {
					fmt.Printf("%s is in CIDR %s\n", ip.String(), cidr)
				} else {
					fmt.Printf("%s is NOT in CIDR set\n", ip.String())
				}
			}

			return nil
		},
	}
)
View Source
var (
	CommandFilter = &cli.Command{
		Name:    "filter",
		Aliases: []string{"f"},
		Usage:   "Filters lines in log files of pipe input",
		UsageText: `
Filters lines in log files of pipe input, printing to STDOUT the lines that
contain an IP that is within the provided CIDR blocks.

NOTE: If '--path, -p' is NOT set, then a pipe is assumed to be the input.

The '--inverse, -i' flag will output the lines that do not contain an IP
within the provided CIDR blocks. 
`,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:     "blocks",
				Aliases:  []string{"b"},
				Usage:    "CIDR blocks to be checked (csv)",
				Required: true,
			},
			&cli.StringFlag{
				Name:    "path",
				Aliases: []string{"p"},
				Usage:   "File path to files to filter, can be a glob. If not set, a pipe is assumed.",
			},
			&cli.BoolFlag{
				Name:    "inverse",
				Aliases: []string{"i"},
				Usage:   "Print out lines that DO NOT match the CIDR check",
			},
		},
		Action: func(c *cli.Context) error {

			blocks, err := parseInputCIDRs(c.String("blocks"))
			if err != nil {
				return err
			}

			if c.String("path") != "" {
				kf.Printf("globbing files with pattern: %s", c.String("path"))
				files, err := filepathx.Glob(c.String("path"))
				if err != nil {
					return err
				}
				kf.Printf("found %d files", len(files))
				kf.Log(files)

				for _, fPath := range files {
					kf.Printf("processing file: %s", fPath)
					file, err := os.Open(fPath)
					if err != nil {
						return err
					}

					reader := bufio.NewReader(file)
					err = processReader(&processReaderInput{
						reader:  reader,
						blocks:  blocks,
						inverse: c.Bool("inverse"),
					})
					if err != nil {
						return err
					}
				}

				return nil
			}

			info, err := os.Stdin.Stat()
			if err != nil {
				return err
			}

			noNamedPipe := info.Mode()&os.ModeNamedPipe == 0
			noUnixPipe := info.Mode()&os.ModeCharDevice != 0 || info.Size() <= 0
			k.Printf("noNamedPipe: %t noUnixPipe: %t\n", noNamedPipe, noUnixPipe)

			if noNamedPipe && noUnixPipe {

				return fmt.Errorf("please use this command with a pipe or the --path flag set")
			}

			reader := bufio.NewReader(os.Stdin)
			err = processReader(&processReaderInput{
				reader:  reader,
				blocks:  blocks,
				inverse: c.Bool("inverse"),
			})
			if err != nil {
				return err
			}

			return nil
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL