move wifictld structs from data to capture
This commit is contained in:
parent
16fdeb85d4
commit
39ff9a43b0
|
@ -3,20 +3,19 @@ package capture
|
|||
import (
|
||||
"net"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
"github.com/bdlm/log"
|
||||
)
|
||||
|
||||
//Collector for capture
|
||||
type Collector struct {
|
||||
connections map[string]*net.UDPConn
|
||||
handler data.Handler
|
||||
handler Handler
|
||||
queue chan *Packet
|
||||
stop chan interface{}
|
||||
}
|
||||
|
||||
// NewCollector creates a Collector struct
|
||||
func NewCollector(handler data.Handler, ifaces []*IFaceConfig) *Collector {
|
||||
func NewCollector(handler Handler, ifaces []*IFaceConfig) *Collector {
|
||||
|
||||
coll := &Collector{
|
||||
handler: handler,
|
||||
|
@ -96,7 +95,7 @@ type Packet struct {
|
|||
|
||||
func (coll *Collector) parser() {
|
||||
for obj := range coll.queue {
|
||||
msg, err := data.NewSocketMSG(obj.Raw)
|
||||
msg, err := NewSocketMSG(obj.Raw)
|
||||
if err != nil {
|
||||
log.Warnf("unable to unmarshal request from %s: %s", obj.Address.String(), err)
|
||||
continue
|
||||
|
@ -114,7 +113,7 @@ func (coll *Collector) parser() {
|
|||
}
|
||||
|
||||
// SendTo a specifical address
|
||||
func (coll *Collector) SendTo(addr *net.UDPAddr, msg *data.SocketMSG) {
|
||||
func (coll *Collector) SendTo(addr *net.UDPAddr, msg *SocketMSG) {
|
||||
log.Debugf("send[%s]: %s", addr, msg.String())
|
||||
data, err := msg.Marshal()
|
||||
if err != nil {
|
||||
|
@ -132,7 +131,7 @@ func (coll *Collector) SendTo(addr *net.UDPAddr, msg *data.SocketMSG) {
|
|||
}
|
||||
|
||||
// Send to every connection to default address
|
||||
func (coll *Collector) Send(msg *data.SocketMSG) {
|
||||
func (coll *Collector) Send(msg *SocketMSG) {
|
||||
log.Debugf("send: %s", msg.String())
|
||||
data, err := msg.Marshal()
|
||||
if err != nil {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package data
|
||||
package capture
|
||||
|
||||
import "net"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package data
|
||||
package capture
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
@ -6,6 +6,8 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/bdlm/log"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
)
|
||||
|
||||
var DEBUG = false
|
||||
|
@ -22,7 +24,6 @@ const (
|
|||
|
||||
func (a SocketMSGType) Is(b SocketMSGType) bool {
|
||||
if DEBUG {
|
||||
|
||||
log.Debugf("SocketType: %x & %x = %x -> %t", a, b, (a & b), (a&b) > 0)
|
||||
}
|
||||
return (a & b) > 0
|
||||
|
@ -99,7 +100,7 @@ func (msg *SocketMSG) Unmarshal(obj []byte) error {
|
|||
|
||||
if msg.Types.Is(SocketMSGTypeClient) {
|
||||
msg.Client = &WifiClient{
|
||||
Addr: HardwareAddr{HardwareAddr: obj[pos:(pos + 6)]},
|
||||
Addr: data.HardwareAddr{HardwareAddr: obj[pos:(pos + 6)]},
|
||||
Time: time.Unix(int64(binary.BigEndian.Uint32(obj[(pos+6):(pos+10)])), 0),
|
||||
TryProbe: binary.BigEndian.Uint16(obj[(pos + 10):(pos + 12)]),
|
||||
TryAuth: binary.BigEndian.Uint16(obj[(pos + 12):(pos + 14)]),
|
|
@ -1,4 +1,4 @@
|
|||
package data
|
||||
package capture
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
|
@ -0,0 +1,20 @@
|
|||
package capture
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
)
|
||||
|
||||
// WifiClient datatype of wifictld
|
||||
type WifiClient struct {
|
||||
Addr data.HardwareAddr `json:"addr"`
|
||||
Time time.Time `json:"time"`
|
||||
TryProbe uint16 `json:"try_probe"`
|
||||
TryAuth uint16 `json:"try_auth"`
|
||||
Connected bool `json:"connected"`
|
||||
Authed bool `json:"authed"`
|
||||
FreqHighest uint16 `json:"freq_highest"`
|
||||
SignalLowFreq int16 `json:"signal_low_freq"`
|
||||
SignalHighFreq int16 `json:"signal_high_freq"`
|
||||
}
|
|
@ -11,13 +11,18 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/config"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/controller"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/database"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/web"
|
||||
)
|
||||
|
||||
type ControllerConfig struct {
|
||||
Database string `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>",
|
||||
|
@ -25,35 +30,35 @@ var controllerCmd = &cobra.Command{
|
|||
Example: `analyzer controller "/etc/wifictld.conf"`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
configObj := &config.Config{}
|
||||
config := &ControllerConfig{}
|
||||
|
||||
file.ReadTOML(args[0], configObj)
|
||||
file.ReadTOML(args[0], config)
|
||||
|
||||
db := database.NewDB(configObj.StatePath)
|
||||
db := database.NewDB(config.Database)
|
||||
|
||||
ctr := controller.NewController(db)
|
||||
defer ctr.Close()
|
||||
|
||||
var handlers []data.Handler
|
||||
var handlers []capture.Handler
|
||||
|
||||
if configObj.Webserver.Enable {
|
||||
log.Infof("starting webserver on %s", configObj.Webserver.Bind)
|
||||
srv := web.New(configObj.Webserver)
|
||||
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 *data.SocketMSG) (*data.SocketMSG, error) {
|
||||
coll := capture.NewCollector(func(addr *net.UDPAddr, msg *capture.SocketMSG) (*capture.SocketMSG, error) {
|
||||
for _, a := range handlers {
|
||||
a(addr, msg)
|
||||
}
|
||||
if !configObj.Answer {
|
||||
if !config.Answer {
|
||||
ctr.Handler(addr, msg)
|
||||
return nil, nil
|
||||
}
|
||||
return ctr.Handler(addr, msg)
|
||||
}, configObj.Interfaces)
|
||||
}, config.Interfaces)
|
||||
defer coll.Close()
|
||||
|
||||
ctr.Send = coll.Send
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -40,9 +39,9 @@ var dumpCmd = &cobra.Command{
|
|||
ifacesConfigs = append(ifacesConfigs, ifaceConfig)
|
||||
}
|
||||
|
||||
data.DEBUG = debug
|
||||
capture.DEBUG = debug
|
||||
|
||||
coll := capture.NewCollector(func(addr *net.UDPAddr, msg *data.SocketMSG) (*data.SocketMSG, error) {
|
||||
coll := capture.NewCollector(func(addr *net.UDPAddr, msg *capture.SocketMSG) (*capture.SocketMSG, error) {
|
||||
log.Infof("recv[%s]: %s", addr, msg.String())
|
||||
return nil, nil
|
||||
}, ifacesConfigs)
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/web"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
StatePath string `toml:"state_path"`
|
||||
Answer bool `toml:"answer"`
|
||||
Webserver *web.Config `toml:"webserver"`
|
||||
Interfaces []*capture.IFaceConfig `toml:"interfaces"`
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
package config
|
|
@ -1,4 +1,4 @@
|
|||
state_path = "/tmp/wifictld.json"
|
||||
database = "/tmp/wifictld.json"
|
||||
answer = false
|
||||
|
||||
|
||||
|
|
|
@ -5,20 +5,20 @@ import (
|
|||
|
||||
// "github.com/bdlm/log"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
||||
)
|
||||
|
||||
func (c *Controller) Handler(addr *net.UDPAddr, msg *data.SocketMSG) (*data.SocketMSG, error) {
|
||||
func (c *Controller) Handler(addr *net.UDPAddr, msg *capture.SocketMSG) (*capture.SocketMSG, error) {
|
||||
ignore := false
|
||||
if msg.Types.Is(data.SocketMSGTypeClient) && msg.Client != nil {
|
||||
if msg.Types.Is(capture.SocketMSGTypeClient) && msg.Client != nil {
|
||||
ignore = c.db.LearnClient(addr.IP, msg.Client)
|
||||
}
|
||||
if !msg.Types.Is(data.SocketMSGTypeRequest) {
|
||||
if !msg.Types.Is(capture.SocketMSGTypeRequest) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
msg = &data.SocketMSG{
|
||||
Types: (data.SocketMSGTypeResponse | data.SocketMSGTypeClient),
|
||||
msg = &capture.SocketMSG{
|
||||
Types: (capture.SocketMSGTypeResponse | capture.SocketMSGTypeClient),
|
||||
Client: c.db.GetClient(msg.Client.Addr),
|
||||
}
|
||||
|
||||
|
|
|
@ -6,13 +6,13 @@ import (
|
|||
|
||||
"github.com/bdlm/log"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/database"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
SendTo func(addr *net.UDPAddr, msg *data.SocketMSG)
|
||||
Send func(msg *data.SocketMSG)
|
||||
SendTo func(addr *net.UDPAddr, msg *capture.SocketMSG)
|
||||
Send func(msg *capture.SocketMSG)
|
||||
db *database.DB
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
package data
|
|
@ -1,27 +0,0 @@
|
|||
package data
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
// default multicast group used by announced
|
||||
MulticastAddressDefault = "ff02::31f1"
|
||||
|
||||
// default udp port used by announced
|
||||
Port = 1000
|
||||
|
||||
// maximum receivable size
|
||||
MaxDataGramSize = 256
|
||||
)
|
||||
|
||||
// WifiClient datatype of wifictld
|
||||
type WifiClient struct {
|
||||
Addr HardwareAddr `json:"addr"`
|
||||
Time time.Time `json:"time"`
|
||||
TryProbe uint16 `json:"try_probe"`
|
||||
TryAuth uint16 `json:"try_auth"`
|
||||
Connected bool `json:"connected"`
|
||||
Authed bool `json:"authed"`
|
||||
FreqHighest uint16 `json:"freq_highest"`
|
||||
SignalLowFreq int16 `json:"signal_low_freq"`
|
||||
SignalHighFreq int16 `json:"signal_high_freq"`
|
||||
}
|
|
@ -7,6 +7,7 @@ import (
|
|||
// "github.com/bdlm/log"
|
||||
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
)
|
||||
|
||||
|
@ -24,7 +25,7 @@ type Client struct {
|
|||
Lastseen jsontime.Time `json:"lastseen"`
|
||||
}
|
||||
|
||||
func (db *DB) LearnClient(apIP net.IP, clientWifictl *data.WifiClient) bool {
|
||||
func (db *DB) LearnClient(apIP net.IP, clientWifictl *capture.WifiClient) bool {
|
||||
ret := false
|
||||
|
||||
// learn ap
|
||||
|
@ -70,9 +71,9 @@ func (db *DB) LearnClient(apIP net.IP, clientWifictl *data.WifiClient) bool {
|
|||
return ret
|
||||
}
|
||||
|
||||
func (db *DB) GetClient(addr data.HardwareAddr) *data.WifiClient {
|
||||
func (db *DB) GetClient(addr data.HardwareAddr) *capture.WifiClient {
|
||||
client, ok := db.Clients[addr.String()]
|
||||
wClient := &data.WifiClient{
|
||||
wClient := &capture.WifiClient{
|
||||
Addr: addr,
|
||||
Time: time.Now(),
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
"dev.sum7.eu/genofire/golang-lib/websocket"
|
||||
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/data"
|
||||
"dev.sum7.eu/genofire/wifictld-analyzer/capture"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
|
@ -31,7 +31,7 @@ func New(config *Config) *Server {
|
|||
}
|
||||
}
|
||||
|
||||
func (srv *Server) Handler(addr *net.UDPAddr, msg *data.SocketMSG) (*data.SocketMSG, error) {
|
||||
func (srv *Server) Handler(addr *net.UDPAddr, msg *capture.SocketMSG) (*capture.SocketMSG, error) {
|
||||
srv.ws.SendAll(&websocket.Message{
|
||||
Subject: "wifictld_pkg",
|
||||
Body: map[string]interface{}{
|
||||
|
|
Loading…
Reference in New Issue