diff --git a/README.md b/README.md index 07157eb..a36042d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # go-lanscan -![Coverage](https://img.shields.io/badge/Coverage-91.3%25-brightgreen) +![Coverage](https://img.shields.io/badge/Coverage-91.6%25-brightgreen) A network cli and golang package that allows you to perform arp and syn scanning on a local area network. diff --git a/pkg/scanner/arpscan.go b/pkg/scanner/arpscan.go index 3635444..a2cdba3 100644 --- a/pkg/scanner/arpscan.go +++ b/pkg/scanner/arpscan.go @@ -28,13 +28,11 @@ type ArpScanner struct { resultChan chan *ScanResult requestNotifier chan *Request scanning bool - lastPacketSentAt time.Time timing time.Duration idleTimeout time.Duration vendorRepo oui.VendorRepo hostNamesEnables bool scanningMux *sync.RWMutex - packetSentAtMux *sync.RWMutex debug logger.DebugLogger } @@ -45,18 +43,16 @@ func NewArpScanner( options ...Option, ) *ArpScanner { scanner := &ArpScanner{ - cancel: make(chan struct{}), - targets: targets, - cap: &defaultPacketCapture{}, - networkInfo: networkInfo, - resultChan: make(chan *ScanResult), - timing: defaultTiming, - idleTimeout: defaultIdleTimeout, - scanning: false, - lastPacketSentAt: time.Time{}, - scanningMux: &sync.RWMutex{}, - packetSentAtMux: &sync.RWMutex{}, - debug: logger.NewDebugLogger(), + cancel: make(chan struct{}), + targets: targets, + cap: &defaultPacketCapture{}, + networkInfo: networkInfo, + resultChan: make(chan *ScanResult), + timing: defaultTiming, + idleTimeout: defaultIdleTimeout, + scanning: false, + scanningMux: &sync.RWMutex{}, + debug: logger.NewDebugLogger(), } for _, o := range options { @@ -110,9 +106,6 @@ func (s *ArpScanner) Scan() error { s.scanningMux.Unlock() s.handle = handle - timeout := make(chan struct{}) - - go s.startPacketReceiveTimeout(timeout) go s.readPackets() limiter := time.NewTicker(s.timing) @@ -134,11 +127,7 @@ func (s *ArpScanner) Scan() error { }) } - s.packetSentAtMux.Lock() - s.lastPacketSentAt = time.Now() - s.packetSentAtMux.Unlock() - - <-timeout + time.Sleep(s.idleTimeout) go s.Stop() @@ -197,31 +186,6 @@ func (s *ArpScanner) SetPacketCapture(cap PacketCapture) { s.cap = cap } -func (s *ArpScanner) startPacketReceiveTimeout(timeout chan<- struct{}) { - for { - select { - case <-s.cancel: - go func() { - timeout <- struct{}{} - }() - return - default: - s.packetSentAtMux.RLock() - packetSentAt := s.lastPacketSentAt - s.packetSentAtMux.RUnlock() - - if !packetSentAt.IsZero() && time.Since(packetSentAt) >= s.idleTimeout { - go func() { - timeout <- struct{}{} - }() - return - } - - time.Sleep(time.Millisecond * 100) - } - } -} - func (s *ArpScanner) readPackets() { for { select { @@ -367,8 +331,4 @@ func (s *ArpScanner) reset() { s.scanningMux.Lock() s.scanning = false s.scanningMux.Unlock() - - s.packetSentAtMux.Lock() - s.lastPacketSentAt = time.Time{} - s.packetSentAtMux.Unlock() } diff --git a/pkg/scanner/synscan.go b/pkg/scanner/synscan.go index bb7d5a2..4f169ed 100644 --- a/pkg/scanner/synscan.go +++ b/pkg/scanner/synscan.go @@ -26,23 +26,21 @@ type SynPacket struct { // SynScanner implements the Scanner interface for SYN scanning type SynScanner struct { - cancel chan struct{} - networkInfo network.Network - targets []*ArpScanResult - ports []string - listenPort uint16 - cap PacketCapture - handle PacketCaptureHandle - resultChan chan *ScanResult - requestNotifier chan *Request - scanning bool - lastPacketSentAt time.Time - timing time.Duration - idleTimeout time.Duration - scanningMux *sync.RWMutex - packetSentAtMux *sync.RWMutex - serviceQueryMux *sync.Mutex - debug logger.DebugLogger + cancel chan struct{} + networkInfo network.Network + targets []*ArpScanResult + ports []string + listenPort uint16 + cap PacketCapture + handle PacketCaptureHandle + resultChan chan *ScanResult + requestNotifier chan *Request + scanning bool + timing time.Duration + idleTimeout time.Duration + scanningMux *sync.RWMutex + serviceQueryMux *sync.Mutex + debug logger.DebugLogger } // NewSynScanner returns a new instance of SYNScanner @@ -54,21 +52,19 @@ func NewSynScanner( options ...Option, ) *SynScanner { scanner := &SynScanner{ - cancel: make(chan struct{}), - targets: targets, - networkInfo: networkInfo, - cap: &defaultPacketCapture{}, - ports: ports, - listenPort: listenPort, - resultChan: make(chan *ScanResult), - timing: defaultTiming, - idleTimeout: defaultIdleTimeout, - scanning: false, - lastPacketSentAt: time.Time{}, - scanningMux: &sync.RWMutex{}, - packetSentAtMux: &sync.RWMutex{}, - serviceQueryMux: &sync.Mutex{}, - debug: logger.NewDebugLogger(), + cancel: make(chan struct{}), + targets: targets, + networkInfo: networkInfo, + cap: &defaultPacketCapture{}, + ports: ports, + listenPort: listenPort, + resultChan: make(chan *ScanResult), + timing: defaultTiming, + idleTimeout: defaultIdleTimeout, + scanning: false, + scanningMux: &sync.RWMutex{}, + serviceQueryMux: &sync.Mutex{}, + debug: logger.NewDebugLogger(), } for _, o := range options { @@ -138,9 +134,6 @@ func (s *SynScanner) Scan() error { s.handle = handle - timeout := make(chan struct{}) - - go s.startPacketReceiveTimeout(timeout) go s.readPackets() limiter := time.NewTicker(s.timing) @@ -163,11 +156,7 @@ func (s *SynScanner) Scan() error { } } - s.packetSentAtMux.Lock() - s.lastPacketSentAt = time.Now() - s.packetSentAtMux.Unlock() - - <-timeout + time.Sleep(s.idleTimeout) go s.Stop() @@ -228,31 +217,6 @@ func (s *SynScanner) SetTargets(targets []*ArpScanResult) { s.targets = targets } -func (s *SynScanner) startPacketReceiveTimeout(timeout chan<- struct{}) { - for { - select { - case <-s.cancel: - go func() { - timeout <- struct{}{} - }() - return - default: - s.packetSentAtMux.RLock() - packetSentAt := s.lastPacketSentAt - s.packetSentAtMux.RUnlock() - - if !packetSentAt.IsZero() && time.Since(packetSentAt) >= s.idleTimeout { - go func() { - timeout <- struct{}{} - }() - return - } - - time.Sleep(time.Millisecond * 100) - } - } -} - func (s *SynScanner) readPackets() { for { select { @@ -418,8 +382,4 @@ func (s *SynScanner) reset() { s.scanningMux.Lock() s.scanning = false s.scanningMux.Unlock() - - s.packetSentAtMux.Lock() - s.lastPacketSentAt = time.Time{} - s.packetSentAtMux.Unlock() }