wifictld-analyzer/cmd/controller.go

82 lines
1.9 KiB
Go
Raw Normal View History

2018-06-03 20:37:52 +02:00
package cmd
import (
2019-03-01 10:54:19 +01:00
"net"
2018-06-03 20:37:52 +02:00
"os"
"os/signal"
"syscall"
2019-03-08 15:53:45 +01:00
"dev.sum7.eu/genofire/golang-lib/database"
2019-02-28 17:26:55 +01:00
"dev.sum7.eu/genofire/golang-lib/file"
2019-02-28 16:24:29 +01:00
"github.com/bdlm/log"
2018-06-03 20:37:52 +02:00
"github.com/spf13/cobra"
2019-02-28 16:22:55 +01:00
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
"dev.sum7.eu/genofire/wifictld-analyzer/controller"
2019-03-01 10:54:19 +01:00
"dev.sum7.eu/genofire/wifictld-analyzer/web"
2018-06-03 20:37:52 +02:00
)
type ControllerConfig struct {
2019-03-08 15:53:45 +01:00
Database database.Config `toml:"database"`
Answer bool `toml:"answer"`
Webserver *web.Config `toml:"webserver"`
Interfaces []*capture.IFaceConfig `toml:"interfaces"`
}
2018-06-03 20:37:52 +02:00
// queryCmd represents the query command
var controllerCmd = &cobra.Command{
Use: "controller <interfaces>",
Short: "simulate a wifictld controller",
2018-07-10 21:40:38 +02:00
Example: `analyzer controller "/etc/wifictld.conf"`,
2018-06-03 20:37:52 +02:00
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
config := &ControllerConfig{}
2018-06-03 20:37:52 +02:00
file.ReadTOML(args[0], config)
2018-06-03 20:37:52 +02:00
2019-03-08 15:53:45 +01:00
if err := database.Open(config.Database); err != nil {
log.Panicf("no database connection: %s", err)
}
defer database.Close()
2018-06-03 20:37:52 +02:00
2019-03-08 15:53:45 +01:00
ctr := controller.NewController()
2018-06-03 20:37:52 +02:00
defer ctr.Close()
var handlers []capture.Handler
2019-03-01 10:54:19 +01:00
if config.Webserver.Enable {
log.Infof("starting webserver on %s", config.Webserver.Bind)
srv := web.New(config.Webserver)
2019-03-01 10:54:19 +01:00
go srv.Start()
handlers = append(handlers, srv.Handler)
defer srv.Close()
}
coll := capture.NewCollector(func(addr *net.UDPAddr, msg *capture.SocketMSG) (*capture.SocketMSG, error) {
2019-03-01 10:54:19 +01:00
for _, a := range handlers {
a(addr, msg)
}
if !config.Answer {
2019-03-01 10:54:19 +01:00
ctr.Handler(addr, msg)
return nil, nil
}
return ctr.Handler(addr, msg)
}, config.Interfaces)
2018-06-03 20:37:52 +02:00
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() {
2019-03-01 10:54:19 +01:00
RootCMD.AddCommand(controllerCmd)
2018-06-03 20:37:52 +02:00
}