wifictld-analyzer/cmd/controller.go

68 lines
1.6 KiB
Go
Raw Normal View History

2018-06-03 20:37:52 +02:00
package cmd
import (
"os"
"os/signal"
"strings"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"dev.sum7.eu/wifictld/analyzer/capture"
"dev.sum7.eu/wifictld/analyzer/controller"
"dev.sum7.eu/wifictld/analyzer/database"
)
var (
central bool
)
// queryCmd represents the query command
var controllerCmd = &cobra.Command{
Use: "controller <interfaces>",
Short: "simulate a wifictld controller",
Example: `analyzer controller "eth0,wlan0"`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ifaces := strings.Split(args[0], ",")
log.Infof("listen on: %s", ifaces)
var ifacesConfigs []capture.IFaceConfig
for _, iface := range ifaces {
ifaceConfig := capture.IFaceConfig{
InterfaceName: iface,
Port: port,
IPAddress: ipAddress,
}
ifacesConfigs = append(ifacesConfigs, ifaceConfig)
}
db := database.NewDB()
ctr := controller.NewController(db, central)
defer ctr.Close()
coll := capture.NewCollector(ctr.Handler, ifacesConfigs)
defer coll.Close()
ctr.Send = coll.Send
ctr.SendTo = coll.SendTo
// Wait for INT/TERM
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
log.Println("received", sig)
},
}
func init() {
RootCmd.AddCommand(controllerCmd)
controllerCmd.Flags().IntVar(&port, "port", capture.Port, "define a port to listen (if not set or set to 0 the kernel will use a random free port at its own)")
controllerCmd.Flags().StringVar(&ipAddress, "listen", capture.MulticastAddressDefault, "")
controllerCmd.Flags().BoolVar(&central, "central", false, "")
}