Skip to content

Commit

Permalink
add support for golangci-lint (dmachard#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard authored Nov 1, 2022
1 parent 438d5bc commit be40ca2
Show file tree
Hide file tree
Showing 41 changed files with 289 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build
name: go build

on:
push:
Expand Down
31 changes: 0 additions & 31 deletions .github/workflows/code.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/golint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: go lint

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
linter:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: '1.19'

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.50.1

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: GoReleaser
name: go releaser
on:
push:
tags:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing-dnstap.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: dnstap tests
name: tests dnstap

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing-go.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: go testing
name: tests go

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/testing-powerdns.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: powerdns tests
name: tests powerdns

on:
push:
Expand Down
22 changes: 22 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://golangci-lint.run/usage/linters/
linters:
disable-all: true
enable:
# gofmt checks whether code was gofmt-ed.
- gofmt
# bet examines Go source code and reports suspicious constructs
- govet
# finds repeated strings that could be replaced by a constant
- goconst
# checks Go code for unused constants, variables, functions and types
- unused
# remove unnecessary type conversions
- unconvert
# check errors wrapping scheme
- errorlint

# list of linters to use in the future:
#- stylecheck
#- gocritic
#- gosec
#- staticcheck
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ Run from source
go run .
```

Install linter

```
sudo apt install build-essential
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
```

List linters enabled

```
$GOPATH/golangci-lint linters
```

Execute linter before to commit

```
$GOPATH/go/bin/golangci-lint run
```

Execute testunits

```
Expand Down
10 changes: 4 additions & 6 deletions collectors/dnssniffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func (c *DnsSniffer) Stop() {
}

func (c *DnsSniffer) Listen() error {

// raw socket
fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, Htons(syscall.ETH_P_ALL))
if err != nil {
Expand Down Expand Up @@ -288,22 +287,22 @@ func (c *DnsSniffer) Run() {
for _, layertyp := range decodedLayers {
switch layertyp {
case layers.LayerTypeIPv4:
dm.NetworkInfo.Family = "INET"
dm.NetworkInfo.Family = dnsutils.PROTO_IPV4
dm.NetworkInfo.QueryIp = ip4.SrcIP.String()
dm.NetworkInfo.ResponseIp = ip4.DstIP.String()

case layers.LayerTypeIPv6:
dm.NetworkInfo.QueryIp = ip6.SrcIP.String()
dm.NetworkInfo.ResponseIp = ip6.DstIP.String()
dm.NetworkInfo.Family = "INET6"
dm.NetworkInfo.Family = dnsutils.PROTO_IPV6
fmt.Println(eth)

case layers.LayerTypeUDP:
dm.NetworkInfo.QueryPort = fmt.Sprint(int(udp.SrcPort))
dm.NetworkInfo.ResponsePort = fmt.Sprint(int(udp.DstPort))
dm.DNS.Payload = udp.Payload
dm.DNS.Length = len(udp.Payload)
dm.NetworkInfo.Protocol = "UDP"
dm.NetworkInfo.Protocol = dnsutils.PROTO_UDP

case layers.LayerTypeTCP:
// ignore SYN/ACK packet
Expand All @@ -322,8 +321,7 @@ func (c *DnsSniffer) Run() {
dm.NetworkInfo.ResponsePort = fmt.Sprint(int(tcp.DstPort))
dm.DNS.Payload = tcp.Payload[2:]
dm.DNS.Length = len(tcp.Payload[2:])
dm.NetworkInfo.Protocol = "TCP"

dm.NetworkInfo.Protocol = dnsutils.PROTO_TCP
}
}

Expand Down
2 changes: 1 addition & 1 deletion collectors/dnssniffer_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (d *DnsProcessor) Run(sendTo []chan dnsutils.DnsMessage) {
dm.NetworkInfo.ResponsePort = qport
} else {
dm.DNS.Type = dnsutils.DnsQuery
dm.DnsTap.Operation = "CLIENT_QUERY"
dm.DnsTap.Operation = dnsutils.DNSTAP_CLIENT_QUERY
}

if err = dnsutils.DecodePayload(&dm, &dnsHeader, d.config); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion collectors/dnssniffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestDnsSnifferRun(t *testing.T) {
// waiting message in channel
for {
msg := <-g.Channel()
if msg.DnsTap.Operation == "CLIENT_QUERY" && msg.DNS.Qname == "dns.collector" {
if msg.DnsTap.Operation == dnsutils.DNSTAP_CLIENT_QUERY && msg.DNS.Qname == "dns.collector" {
break
}
}
Expand Down
10 changes: 4 additions & 6 deletions collectors/dnstap.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,16 @@ func (c *Dnstap) Listen() error {
config := &tls.Config{Certificates: []tls.Certificate{cer}}

if len(c.sockPath) > 0 {
listener, err = tls.Listen("unix", c.sockPath, config)
listener, err = tls.Listen(dnsutils.SOCKET_UNIX, c.sockPath, config)
} else {
listener, err = tls.Listen("tcp", addrlisten, config)
listener, err = tls.Listen(dnsutils.SOCKET_TCP, addrlisten, config)
}

} else {
// basic listening
if len(c.sockPath) > 0 {
listener, err = net.Listen("unix", c.sockPath)
listener, err = net.Listen(dnsutils.SOCKET_UNIX, c.sockPath)
} else {
listener, err = net.Listen("tcp", addrlisten)
listener, err = net.Listen(dnsutils.SOCKET_TCP, addrlisten)
}
}

Expand Down Expand Up @@ -182,7 +181,6 @@ func (c *Dnstap) Run() {

c.conns = append(c.conns, conn)
go c.HandleConn(conn)

}

c.LogInfo("run terminated")
Expand Down
6 changes: 3 additions & 3 deletions collectors/dnstap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestDnstapTcpRun(t *testing.T) {
}
go c.Run()

conn, err := net.Dial("tcp", ":6000")
conn, err := net.Dial(dnsutils.SOCKET_TCP, ":6000")
if err != nil {
t.Error("could not connect to TCP server: ", err)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestDnstapUnixRun(t *testing.T) {
}
go c.Run()

conn, err := net.Dial("unix", config.Collectors.Dnstap.SockPath)
conn, err := net.Dial(dnsutils.SOCKET_UNIX, config.Collectors.Dnstap.SockPath)
if err != nil {
t.Error("could not connect to unix socket: ", err)
}
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestDnstapUnixRun(t *testing.T) {

// waiting message in channel
msg := <-g.Channel()
if msg.DnsTap.Operation != "CLIENT_QUERY" {
if msg.DnsTap.Operation != dnsutils.DNSTAP_CLIENT_QUERY {
t.Errorf("want CLIENT_QUERY, got %s", msg.DnsTap.Operation)
}
}
4 changes: 2 additions & 2 deletions collectors/powerdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func (c *ProtobufPowerDNS) Listen() error {
c.logger.Fatal("loading certificate failed:", err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
listener, err = tls.Listen("tcp", addrlisten, config)
listener, err = tls.Listen(dnsutils.SOCKET_TCP, addrlisten, config)
} else {
listener, err = net.Listen("tcp", addrlisten)
listener, err = net.Listen(dnsutils.SOCKET_TCP, addrlisten)
}
// something is wrong ?
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion collectors/powerdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestProbufPdnsRun(t *testing.T) {
}
go c.Run()

conn, err := net.Dial("tcp", ":6001")
conn, err := net.Dial(dnsutils.SOCKET_TCP, ":6001")
if err != nil {
t.Error("could not connect to TCP server: ", err)
}
Expand Down
1 change: 0 additions & 1 deletion dnsutils/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func BenchmarkMapSet(b *testing.B) {
}

func BenchmarkMapDelete(b *testing.B) {

mapexpire := NewDnsCache(60 * time.Second)

for i := 0; i < b.N; i++ {
Expand Down
Loading

0 comments on commit be40ca2

Please sign in to comment.