Skip to content

Commit

Permalink
Fix issue with missing events on arp discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
robgonnella committed Sep 12, 2023
1 parent 36971c1 commit a64e1d3
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions internal/discovery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ func (s *ScannerService) pollNetwork() {
Type: DiscoveryArpUpdateEvent,
ID: res.MAC.String(),
IP: res.IP.String(),
Hostname: "",
OS: "",
Hostname: "Unknown",
OS: "Unknown",
Vendor: res.Vendor,
Status: ServerOnline,
Port: Port{
ID: 22,
Status: PortClosed,
},
}
go s.handleDiscoveryResult(dr)
go s.handleArpDiscoveryResult(dr)
case scanner.SYNResult:
res := r.Payload.(*scanner.SynScanResult)
dr := &DiscoveryResult{
Expand All @@ -122,7 +122,7 @@ func (s *ScannerService) pollNetwork() {
Status: PortStatus(res.Port.Status),
},
}
go s.handleDiscoveryResult(dr)
go s.handleSynDiscoveryResult(dr)
}
case err := <-s.errorChan:
s.log.Error().Err(err).Msg("discovery service encountered an error")
Expand All @@ -138,7 +138,7 @@ func (s *ScannerService) pollNetwork() {
}
}

func (s *ScannerService) getSSHPort(result *DiscoveryResult) *string {
func (s *ScannerService) getConfiguredSSHPort(result *DiscoveryResult) *string {
resultStrPort := strconv.Itoa(int(result.Port.ID))
sshPort := s.conf.SSH.Port

Expand All @@ -158,21 +158,50 @@ func (s *ScannerService) getSSHPort(result *DiscoveryResult) *string {
return &sshPort
}

func (s *ScannerService) handleArpDiscoveryResult(result *DiscoveryResult) {
if result.Type != DiscoveryArpUpdateEvent {
return
}

fields := map[string]interface{}{
"type": result.Type,
"id": result.ID,
"hostname": result.Hostname,
"ip": result.IP,
"os": result.OS,
"status": result.Status,
}

s.log.Info().Fields(fields).Msg("found network device")

go func() {
s.eventChan <- &event.Event{
Type: result.Type,
Payload: result,
}
}()
}

// handle results found during polling
func (s *ScannerService) handleDiscoveryResult(result *DiscoveryResult) {
func (s *ScannerService) handleSynDiscoveryResult(result *DiscoveryResult) {
if result.Type != DiscoverySynUpdateEvent {
return
}

fields := map[string]interface{}{
"type": result.Type,
"id": result.ID,
"hostname": result.Hostname,
"ip": result.IP,
"os": result.OS,
"status": result.Status,
"sshPort": result.Port.ID,
"sshStatus": result.Port.Status,
}

s.log.Info().Fields(fields).Msg("found network device")

sshPort := s.getSSHPort(result)
sshPort := s.getConfiguredSSHPort(result)

if sshPort == nil {
s.log.Info().Fields(fields).Msg("ignoring non-ssh port result")
Expand All @@ -198,11 +227,11 @@ func (s *ScannerService) handleDiscoveryResult(result *DiscoveryResult) {
}

if result.Hostname == "" {
result.Hostname = "unknown"
result.Hostname = "Unknown"
}

if result.OS == "" {
result.OS = "unknown"
result.OS = "Unknown"
}

go func() {
Expand Down

0 comments on commit a64e1d3

Please sign in to comment.