Skip to content

Commit

Permalink
Listens to packets to find mac addresses
Browse files Browse the repository at this point in the history
Unfortunately this implementation would require the user to run with
root privileges.
  • Loading branch information
robgonnella committed Aug 7, 2023
1 parent 8e0d090 commit 4c0d94c
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 343 deletions.
2 changes: 1 addition & 1 deletion cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Root(props *CommandProps) *cobra.Command {
level := zerolog.InfoLevel

if verbose || debug {
level = zerolog.DebugLevel
level = zerolog.ErrorLevel
}

if silent {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/gdamore/tcell/v2 v2.6.0
github.com/golang/mock v1.6.0
github.com/google/gopacket v1.1.19
github.com/imdario/mergo v0.3.16
github.com/jackpal/gateway v1.0.10
github.com/projectdiscovery/mapcidr v1.1.2
github.com/rivo/tview v0.0.0-20230621164836-6cc0565babaf
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand Down
2 changes: 2 additions & 0 deletions internal/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func TestCore(t *testing.T) {

mockScanner := mock_discovery.NewMockScanner(ctrl)
mockDetailsScanner := mock_discovery.NewMockDetailScanner(ctrl)
mockPacketScanner := mock_discovery.NewMockPacketScanner(ctrl)
mockConfig := mock_config.NewMockService(ctrl)
mockServerService := mock_server.NewMockService(ctrl)

Expand All @@ -40,6 +41,7 @@ func TestCore(t *testing.T) {
discoveryService := discovery.NewScannerService(
mockScanner,
mockDetailsScanner,
mockPacketScanner,
mockServerService,
)

Expand Down
9 changes: 8 additions & 1 deletion internal/core/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,24 @@ func CreateNewAppCore(networkInfo *util.NetworkInfo) (*Core, error) {
serverRepo := server.NewSqliteRepo(db)
serverService := server.NewService(*conf, serverRepo)

netScanner, err := discovery.NewPacketScanner(networkInfo, conf.Targets)
netScanner, err := discovery.NewNetScanner(networkInfo, conf.Targets)

if err != nil {
return nil, err
}

detailScanner := discovery.NewAnsibleIpScanner(*conf)

packetScanner, err := discovery.NewPCapScanner(conf.Targets, networkInfo)

if err != nil {
return nil, err
}

scannerService := discovery.NewScannerService(
netScanner,
detailScanner,
packetScanner,
serverService,
)

Expand Down
1 change: 1 addition & 0 deletions internal/core/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (c *Core) handleServerEvent(evt *event.Event) {
fields := map[string]interface{}{
"type": evt.Type,
"id": payload.ID,
"mac": payload.MAC,
"hostname": payload.Hostname,
"os": payload.OS,
"ip": payload.IP,
Expand Down
13 changes: 11 additions & 2 deletions internal/discovery/interface.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package discovery

import "context"
import (
"context"

//go:generate mockgen -destination=../mock/discovery/mock_discovery.go -package=mock_discovery . DetailScanner,Scanner
"github.com/robgonnella/ops/internal/server"
)

//go:generate mockgen -destination=../mock/discovery/mock_discovery.go -package=mock_discovery . DetailScanner,PacketScanner,Scanner

// DetailScanner interface for gathering more details about a device
type DetailScanner interface {
GetServerDetails(ctx context.Context, ip string) (*Details, error)
}

// PacketScanner
type PacketScanner interface {
ListenForPackets(resultChan chan *server.Server)
}

// Scanner interface for scanning a network for devices
type Scanner interface {
Scan(resultChan chan *DiscoveryResult) error
Expand Down
3 changes: 2 additions & 1 deletion internal/discovery/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/projectdiscovery/mapcidr"
"github.com/robgonnella/ops/internal/logger"
"github.com/robgonnella/ops/internal/server"
"github.com/robgonnella/ops/internal/util"
)

var cidrSuffix = regexp.MustCompile(`\/\d{2}$`)
Expand All @@ -24,7 +25,7 @@ type NetScanner struct {
log logger.Logger
}

func NewNetScanner(targets []string) (*NetScanner, error) {
func NewNetScanner(networkInfo *util.NetworkInfo, targets []string) (*NetScanner, error) {
ipList := []string{}

for _, t := range targets {
Expand Down
Loading

0 comments on commit 4c0d94c

Please sign in to comment.