Skip to content

Commit

Permalink
switch to golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Dec 5, 2020
1 parent edc08a1 commit ae12475
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 70 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: lint

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
lint:
name: lint

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: golangci/golangci-lint-action@v2
with:
version: v1.33
args: >
--disable=errcheck
--enable=gofmt
--enable=golint
--enable=misspell
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ help:
@echo ""
@echo " mod-tidy run go mod tidy"
@echo " format format source files"
@echo " test run available tests"
@echo " test run tests"
@echo " lint run linter"
@echo " run ARGS=args run app"
@echo " release build release assets"
@echo " dockerhub build and push docker hub images"
Expand Down Expand Up @@ -51,6 +52,15 @@ test-nodocker:
$(eval export CGO_ENABLED=0)
go build -o /dev/null .

lint:
docker run --rm -v $(PWD):/app -w /app \
golangci/golangci-lint:v1.33.0 \
golangci-lint run -v \
--disable=errcheck \
--enable=gofmt \
--enable=golint \
--enable=misspell

define DOCKERFILE_RUN
FROM amd64/$(BASE_IMAGE)
WORKDIR /s
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# landiscover

[![Test](https://github.com/aler9/landiscover/workflows/test/badge.svg)](https://github.com/aler9/landiscover/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/aler9/landiscover)](https://goreportcard.com/report/github.com/aler9/landiscover)
[![Lint](https://github.com/aler9/landiscover/workflows/lint/badge.svg)](https://github.com/aler9/landiscover/actions)
[![Docker Hub](https://img.shields.io/badge/docker-aler9%2Flandiscover-blue)](https://hub.docker.com/r/aler9/landiscover)

![](README.gif)
Expand Down
8 changes: 4 additions & 4 deletions layer-mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (

const mdnsPort = 5353

var reMdnsQueryLocal = regexp.MustCompile("^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.in-addr\\.arpa$")
var reMdnsQueryLocal = regexp.MustCompile(`^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.in-addr\.arpa$`)

var layerTypeMdns gopacket.LayerType

type layerMdns struct {
layers.BaseLayer
TransactionId uint16
TransactionID uint16
IsResponse bool
Opcode uint8
Questions []mdnsQuestion
Expand Down Expand Up @@ -81,7 +81,7 @@ func (l *layerMdns) Payload() []byte {
func (l *layerMdns) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
l.BaseLayer = layers.BaseLayer{Contents: data[:]}

l.TransactionId = binary.BigEndian.Uint16(data[0:2])
l.TransactionID = binary.BigEndian.Uint16(data[0:2])
l.IsResponse = (data[3] >> 7) == 0x01
l.Opcode = uint8((data[3] >> 3) & 0x0F)
questionCount := binary.BigEndian.Uint16(data[4:6])
Expand Down Expand Up @@ -135,7 +135,7 @@ func (l *layerMdns) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.Serial
panic(err)
}

binary.BigEndian.PutUint16(data[0:2], l.TransactionId)
binary.BigEndian.PutUint16(data[0:2], l.TransactionID)
if l.IsResponse {
data[3] |= 0x01 << 7
}
Expand Down
6 changes: 3 additions & 3 deletions layer-nbns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var layerTypeNbns gopacket.LayerType

type layerNbns struct {
layers.BaseLayer
TransactionId uint16
TransactionID uint16
IsResponse bool
Opcode uint8
Truncated bool
Expand Down Expand Up @@ -92,7 +92,7 @@ func (l *layerNbns) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) err
return fmt.Errorf("invalid packet")
}

l.TransactionId = binary.BigEndian.Uint16(data[0:2])
l.TransactionID = binary.BigEndian.Uint16(data[0:2])
l.IsResponse = (data[3] >> 7) == 0x01
l.Opcode = uint8((data[3] >> 3) & 0x0F)
l.Truncated = (data[3] >> 1) == 0x01
Expand Down Expand Up @@ -153,7 +153,7 @@ func (l *layerNbns) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.Serial
panic(err)
}

binary.BigEndian.PutUint16(data[0:2], l.TransactionId)
binary.BigEndian.PutUint16(data[0:2], l.TransactionID)
if l.IsResponse {
data[3] |= 0x01 << 7
}
Expand Down
34 changes: 17 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type node struct {

type arpReq struct {
srcMac net.HardwareAddr
srcIp net.IP
srcIP net.IP
}

type dnsReq struct {
Expand All @@ -46,13 +46,13 @@ type dnsReq struct {

type mdnsReq struct {
srcMac net.HardwareAddr
srcIp net.IP
srcIP net.IP
domainName string
}

type nbnsReq struct {
srcMac net.HardwareAddr
srcIp net.IP
srcIP net.IP
name string
}

Expand All @@ -64,7 +64,7 @@ type uiGetDataReq struct {
type program struct {
passiveMode bool
intf *net.Interface
ownIp net.IP
ownIP net.IP
ls *listener
ma *methodArp
mm *methodMdns
Expand Down Expand Up @@ -123,7 +123,7 @@ func newProgram() error {
return err
}

ownIp, err := func() (net.IP, error) {
ownIP, err := func() (net.IP, error) {
addrs, err := intf.Addrs()
if err != nil {
return nil, err
Expand All @@ -148,7 +148,7 @@ func newProgram() error {
p := &program{
passiveMode: *argPassiveMode,
intf: intf,
ownIp: ownIp,
ownIP: ownIP,
arp: make(chan arpReq),
dns: make(chan dnsReq),
mdns: make(chan mdnsReq),
Expand Down Expand Up @@ -177,7 +177,7 @@ func newProgram() error {
return err
}

err = newUi(p)
err = newUI(p)
if err != nil {
return err
}
Expand All @@ -200,19 +200,19 @@ outer:
for {
select {
case req := <-p.arp:
key := newNodeKey(req.srcMac, req.srcIp)
key := newNodeKey(req.srcMac, req.srcIP)

if _, ok := nodes[key]; !ok {
nodes[key] = &node{
lastSeen: time.Now(),
mac: req.srcMac,
ip: req.srcIp,
ip: req.srcIP,
}

if p.passiveMode == false {
go p.dnsRequest(key, req.srcIp)
go p.mm.request(req.srcIp)
go p.mn.request(req.srcIp)
if !p.passiveMode {
go p.dnsRequest(key, req.srcIP)
go p.mm.request(req.srcIP)
go p.mn.request(req.srcIP)
}

// update last seen
Expand All @@ -224,13 +224,13 @@ outer:
nodes[req.key].dns = req.dns

case req := <-p.mdns:
key := newNodeKey(req.srcMac, req.srcIp)
key := newNodeKey(req.srcMac, req.srcIP)

if _, ok := nodes[key]; !ok {
nodes[key] = &node{
lastSeen: time.Now(),
mac: req.srcMac,
ip: req.srcIp,
ip: req.srcIP,
}
}

Expand All @@ -240,13 +240,13 @@ outer:
}

case req := <-p.nbns:
key := newNodeKey(req.srcMac, req.srcIp)
key := newNodeKey(req.srcMac, req.srcIP)

if _, has := nodes[key]; !has {
nodes[key] = &node{
lastSeen: time.Now(),
mac: req.srcMac,
ip: req.srcIp,
ip: req.srcIP,
}
}

Expand Down
14 changes: 7 additions & 7 deletions method-arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func newMethodArp(p *program) error {
func (ma *methodArp) run() {
go ma.runListener()

if ma.p.passiveMode == false {
if !ma.p.passiveMode {
go ma.runPeriodicRequests()
}
}
Expand All @@ -60,21 +60,21 @@ func (ma *methodArp) runListener() {
return
}

if bytes.Equal(arp.SourceProtAddress, []byte{0, 0, 0, 0}) == true {
if bytes.Equal(arp.SourceProtAddress, []byte{0, 0, 0, 0}) {
return
}

srcMac := copyMac(arp.SourceHwAddress)
srcIp := copyIp(arp.SourceProtAddress)
srcIP := copyIP(arp.SourceProtAddress)

// ethernet mac and arp mac must correspond
if bytes.Equal(arp.SourceHwAddress, eth.SrcMAC) == false {
if !bytes.Equal(arp.SourceHwAddress, eth.SrcMAC) {
return
}

ma.p.arp <- arpReq{
srcMac: srcMac,
srcIp: srcIp,
srcIP: srcIP,
}
}

Expand All @@ -97,7 +97,7 @@ func (ma *methodArp) runPeriodicRequests() {
ProtAddressSize: 4,
Operation: layers.ARPRequest,
SourceHwAddress: ma.p.intf.HardwareAddr,
SourceProtAddress: ma.p.ownIp,
SourceProtAddress: ma.p.ownIP,
DstHwAddress: []byte{0, 0, 0, 0, 0, 0},
}

Expand All @@ -108,7 +108,7 @@ func (ma *methodArp) runPeriodicRequests() {
}

for {
for _, dstAddr := range randAvailableIps(ma.p.ownIp) {
for _, dstAddr := range randAvailableIps(ma.p.ownIP) {
arp.DstProtAddress = dstAddr
if err := gopacket.SerializeLayers(buf, opts, &eth, &arp); err != nil {
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions method-dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"net"
)

func (p *program) dnsRequest(key nodeKey, destIp net.IP) {
names, err := net.LookupAddr(destIp.String())
func (p *program) dnsRequest(key nodeKey, destIP net.IP) {
names, err := net.LookupAddr(destIP.String())
if err != nil {
return
}
Expand Down
21 changes: 10 additions & 11 deletions method-mdns.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"fmt"
"net"
"strings"
Expand Down Expand Up @@ -34,7 +33,7 @@ func newMethodMdns(p *program) error {
func (mm *methodMdns) run() {
go mm.runListener()

if mm.p.passiveMode == false {
if !mm.p.passiveMode {
// continuously poll mdns in order to detect changes or skipped hosts
go mm.runPeriodicRequests()
}
Expand Down Expand Up @@ -67,7 +66,7 @@ func (mm *methodMdns) runListener() {
}

srcMac := copyMac(eth.SrcMAC)
srcIp := copyIp(ip.SrcIP)
srcIP := copyIP(ip.SrcIP)

domainName := func() string {
for _, a := range mdns.Answers {
Expand All @@ -82,8 +81,8 @@ func (mm *methodMdns) runListener() {
}

// accept only if mdns ip matches with sender ip
mdnsIp := net.ParseIP(fmt.Sprintf("%s.%s.%s.%s", m[4], m[3], m[2], m[1])).To4()
if bytes.Equal(mdnsIp, srcIp) == false {
mdnsIP := net.ParseIP(fmt.Sprintf("%s.%s.%s.%s", m[4], m[3], m[2], m[1])).To4()
if !mdnsIP.Equal(srcIP) {
continue
}

Expand All @@ -99,7 +98,7 @@ func (mm *methodMdns) runListener() {

mm.p.mdns <- mdnsReq{
srcMac: srcMac,
srcIp: srcIp,
srcIP: srcIP,
domainName: domainName,
}
}
Expand All @@ -110,7 +109,7 @@ func (mm *methodMdns) runListener() {
}
}

func (mm *methodMdns) request(destIp net.IP) {
func (mm *methodMdns) request(destIP net.IP) {
mac, _ := net.ParseMAC("01:00:5e:00:00:fb")
eth := layers.Ethernet{
SrcMAC: mm.p.intf.HardwareAddr,
Expand All @@ -122,7 +121,7 @@ func (mm *methodMdns) request(destIp net.IP) {
TTL: 255,
Id: randUint16(),
Protocol: layers.IPProtocolUDP,
SrcIP: mm.p.ownIp,
SrcIP: mm.p.ownIP,
DstIP: net.ParseIP("224.0.0.251"), // TODO: provare unicast
}
udp := layers.UDP{
Expand All @@ -131,10 +130,10 @@ func (mm *methodMdns) request(destIp net.IP) {
}
udp.SetNetworkLayerForChecksum(&ip)
mdns := layerMdns{
TransactionId: 0,
TransactionID: 0,
Questions: []mdnsQuestion{
{
Query: fmt.Sprintf("%d.%d.%d.%d.in-addr.arpa", destIp[3], destIp[2], destIp[1], destIp[0]),
Query: fmt.Sprintf("%d.%d.%d.%d.in-addr.arpa", destIP[3], destIP[2], destIP[1], destIP[0]),
Type: 0x0C, // domain pointer
Class: 1, // IN
},
Expand All @@ -158,7 +157,7 @@ func (mm *methodMdns) request(destIp net.IP) {

func (mm *methodMdns) runPeriodicRequests() {
for {
for _, dstAddr := range randAvailableIps(mm.p.ownIp) {
for _, dstAddr := range randAvailableIps(mm.p.ownIP) {
mm.request(dstAddr)
time.Sleep(mdnsPeriod) // about 1 minute for a full scan
}
Expand Down
Loading

0 comments on commit ae12475

Please sign in to comment.