Skip to content

Commit

Permalink
Merge pull request #3 from vokomarov/chore/test-coverage
Browse files Browse the repository at this point in the history
Increase tests coverage
  • Loading branch information
vokomarov committed Mar 8, 2020
2 parents 7821260 + 7038f88 commit 1e98cf7
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 33 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
service:
analyzed-paths:
- scanner/...
golangci-lint-version: 1.23.x
prepare:
- apt-get update && apt-get install -y libpcap-dev
suggested-changes:
disabled: true
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

.PHONY: test

test:
go test -v ./...
5 changes: 3 additions & 2 deletions hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
"github.com/vokomarov/netshark/scanner/host"
)

type HostsCommand struct {
type hostsCommand struct {
Timeout int `short:"t" long:"timeout" default:"5" description:"Timeout in seconds to wait for ARP responses."`
}

func (c *HostsCommand) Execute(_ []string) error {
// Execute will run the command
func (c *hostsCommand) Execute(_ []string) error {
fmt.Printf("Scanning Hosts..\n")

quit := make(chan os.Signal, 1)
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
var parser *flags.Parser

type scanCommand struct {
HostsCommand HostsCommand `command:"hosts" description:"Scan all available neighbor hosts of current local network"`
PortsCommand PortsCommand `command:"ports" description:"Scan open ports on a host"`
HostsCommand hostsCommand `command:"hosts" description:"Scan all available neighbor hosts of current local network"`
PortsCommand portsCommand `command:"ports" description:"Scan open ports on a host"`
}

func registerCommands(parser *flags.Parser) {
Expand All @@ -33,11 +33,11 @@ func main() {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
return
} else {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
return
}

fmt.Printf("Error: %v\n", err)
os.Exit(1)
return
}

os.Exit(0)
Expand Down
5 changes: 3 additions & 2 deletions ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"fmt"
)

type PortsCommand struct {
type portsCommand struct {
}

func (c *PortsCommand) Execute(_ []string) error {
// Execute will run the command
func (c *portsCommand) Execute(_ []string) error {
fmt.Printf("Scanning Ports..\n")
return nil
}
3 changes: 3 additions & 0 deletions scanner/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"io"
)

// Host struct host information about discovered network client
type Host struct {
id string
IP string
MAC string
}

// ID will generate unique MD5 hash of host by his properties
// and cache generated hash for future usage
func (h *Host) ID() string {
if h.id == "" {
hash := md5.New()
Expand Down
75 changes: 52 additions & 23 deletions scanner/host/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,64 @@ import (
"github.com/google/gopacket/pcap"
)

// Scanner provide container for control local network scanning
// process and checking results
type Scanner struct {
mu sync.RWMutex
unique map[string]bool
Hosts []*Host
stop chan struct{}
Done chan struct{}
Error error
mu sync.RWMutex
unique map[string]bool
Hosts []*Host
started bool
stopped bool
stop chan struct{}
Done chan struct{}
Error error
}

// NewScanner will initialise new instance of Scanner
func NewScanner() *Scanner {
return &Scanner{
mu: sync.RWMutex{},
stop: make(chan struct{}),
unique: make(map[string]bool),
Hosts: make([]*Host, 0),
Done: make(chan struct{}),
mu: sync.RWMutex{},
started: false,
stopped: false,
stop: make(chan struct{}),
unique: make(map[string]bool),
Hosts: make([]*Host, 0),
Done: make(chan struct{}),
}
}

// Stop perform manually stopping of scan process with blocking
// until stopping is not finished in case of scanning already started
// Safe to call before or after scanning started or stopped
func (s *Scanner) Stop() {
s.stop <- struct{}{}
close(s.stop)
<-s.Done
if s.started {
s.stop <- struct{}{}
<-s.Done
}

if !s.stopped {
close(s.stop)

if !s.started {
close(s.Done)
}
}

s.stopped = true
}

func (s *Scanner) finish(err error) {
if err != nil {
s.Error = err
}

s.Done <- struct{}{}
close(s.Done)
if s.started && !s.stopped {
s.Done <- struct{}{}
close(s.Done)
}
}

func (s *Scanner) HasHost(host *Host) bool {
func (s *Scanner) hasHost(host *Host) bool {
s.mu.RLock()
defer s.mu.RUnlock()

Expand All @@ -58,8 +81,7 @@ func (s *Scanner) HasHost(host *Host) bool {
return false
}

// Push new detected Host to the list of all detected
func (s *Scanner) AddHost(host *Host) *Scanner {
func (s *Scanner) addHost(host *Host) *Scanner {
s.mu.Lock()
defer s.mu.Unlock()

Expand All @@ -69,9 +91,10 @@ func (s *Scanner) AddHost(host *Host) *Scanner {
return s
}

// Detect system interfaces and go over each one to detect IP addresses
// and read/write ARP packets
// Scan will detect system interfaces and go over each one to detect
// IP addresses to read/write ARP packets
// Blocked until every interfaces unable to write packets or stop call
// so typically should be run as a goroutine
func (s *Scanner) Scan() {
interfaces, err := net.Interfaces()
if err != nil {
Expand Down Expand Up @@ -161,6 +184,12 @@ func (s *Scanner) scanInterface(iface *net.Interface) error {
// Push new Host once any correct response received
// Work until 'stop' is closed.
func (s *Scanner) listenARP(handle *pcap.Handle, iface *net.Interface) {
s.mu.Lock()
if !s.started {
s.started = true
}
s.mu.Unlock()

src := gopacket.NewPacketSource(handle, layers.LayerTypeEthernet)
in := src.Packets()

Expand Down Expand Up @@ -193,8 +222,8 @@ func (s *Scanner) listenARP(handle *pcap.Handle, iface *net.Interface) {
MAC: fmt.Sprintf("%v", net.HardwareAddr(arp.SourceHwAddress)),
}

if !s.HasHost(&host) {
s.AddHost(&host)
if !s.hasHost(&host) {
s.addHost(&host)
}
}
}
Expand Down
Loading

0 comments on commit 1e98cf7

Please sign in to comment.