82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/database"
|
|
"dev.sum7.eu/genofire/golang-lib/file"
|
|
"github.com/bdlm/log"
|
|
"github.com/spf13/cobra"
|
|
|
|
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
|
"dev.sum7.eu/genofire/wifictld-analyzer/controller"
|
|
"dev.sum7.eu/genofire/wifictld-analyzer/web"
|
|
)
|
|
|
|
type ControllerConfig struct {
|
|
Database database.Config `toml:"database"`
|
|
Answer bool `toml:"answer"`
|
|
Webserver *web.Config `toml:"webserver"`
|
|
Interfaces []*capture.IFaceConfig `toml:"interfaces"`
|
|
}
|
|
|
|
// queryCmd represents the query command
|
|
var controllerCmd = &cobra.Command{
|
|
Use: "controller <interfaces>",
|
|
Short: "simulate a wifictld controller",
|
|
Example: `analyzer controller "/etc/wifictld.conf"`,
|
|
Args: cobra.ExactArgs(1),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
config := &ControllerConfig{}
|
|
|
|
file.ReadTOML(args[0], config)
|
|
|
|
if err := database.Open(config.Database); err != nil {
|
|
log.Panicf("no database connection: %s", err)
|
|
}
|
|
defer database.Close()
|
|
|
|
ctr := controller.NewController()
|
|
defer ctr.Close()
|
|
|
|
var handlers []capture.Handler
|
|
|
|
if config.Webserver.Enable {
|
|
log.Infof("starting webserver on %s", config.Webserver.Bind)
|
|
srv := web.New(config.Webserver)
|
|
go srv.Start()
|
|
handlers = append(handlers, srv.Handler)
|
|
defer srv.Close()
|
|
}
|
|
|
|
coll := capture.NewCollector(func(addr *net.UDPAddr, msg *capture.SocketMSG) (*capture.SocketMSG, error) {
|
|
for _, a := range handlers {
|
|
a(addr, msg)
|
|
}
|
|
if !config.Answer {
|
|
ctr.Handler(addr, msg)
|
|
return nil, nil
|
|
}
|
|
return ctr.Handler(addr, msg)
|
|
}, config.Interfaces)
|
|
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)
|
|
}
|