Skip to content

Commit

Permalink
Fix lint and test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lixmal committed Dec 31, 2024
1 parent 9feaa8d commit 1370bfb
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 45 deletions.
4 changes: 2 additions & 2 deletions client/firewall/uspfilter/common/iface.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package common

import (
device2 "golang.zx2c4.com/wireguard/device"
wgdevice "golang.zx2c4.com/wireguard/device"

"github.com/netbirdio/netbird/client/iface"
"github.com/netbirdio/netbird/client/iface/device"
Expand All @@ -11,6 +11,6 @@ import (
type IFaceMapper interface {
SetFilter(device.PacketFilter) error
Address() iface.WGAddress
GetWGDevice() *device2.Device
GetWGDevice() *wgdevice.Device
GetDevice() *device.FilteredDevice
}
4 changes: 2 additions & 2 deletions client/firewall/uspfilter/conntrack/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (t *UDPTracker) TrackOutbound(srcIP net.IP, dstIP net.IP, srcPort uint16, d
conn.established.Store(true)
t.connections[key] = conn

t.logger.Trace("New UDP connection: %s", conn)
t.logger.Trace("New UDP connection: %v", conn)
}
t.mutex.Unlock()

Expand Down Expand Up @@ -127,7 +127,7 @@ func (t *UDPTracker) cleanup() {
t.ipPool.Put(conn.DestIP)
delete(t.connections, key)

t.logger.Trace("UDP connection timed out: %s", conn)
t.logger.Trace("UDP connection timed out: %v", conn)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions client/firewall/uspfilter/forwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func New(iface common.IFaceMapper, logger *nblog.Logger) (*Forwarder, error) {
}

if err := s.CreateNIC(nicID, endpoint); err != nil {
return nil, fmt.Errorf("failed to create NIC: %w", err)
return nil, fmt.Errorf("failed to create NIC: %v", err)
}

_, bits := iface.Address().Network.Mask.Size()
Expand All @@ -68,7 +68,7 @@ func New(iface common.IFaceMapper, logger *nblog.Logger) (*Forwarder, error) {
}

if err := s.AddProtocolAddress(nicID, protoAddr, stack.AddressProperties{}); err != nil {
return nil, fmt.Errorf("failed to add protocol address: %w", err)
return nil, fmt.Errorf("failed to add protocol address: %s", err)
}

defaultSubnet, err := tcpip.NewSubnet(
Expand All @@ -79,11 +79,11 @@ func New(iface common.IFaceMapper, logger *nblog.Logger) (*Forwarder, error) {
return nil, fmt.Errorf("creating default subnet: %w", err)
}

if s.SetPromiscuousMode(nicID, true); err != nil {
return nil, fmt.Errorf("set promiscuous mode: %w", err)
if err := s.SetPromiscuousMode(nicID, true); err != nil {
return nil, fmt.Errorf("set promiscuous mode: %s", err)
}
if s.SetSpoofing(nicID, true); err != nil {
return nil, fmt.Errorf("set spoofing: %w", err)
if err := s.SetSpoofing(nicID, true); err != nil {
return nil, fmt.Errorf("set spoofing: %s", err)
}

s.SetRouteTable([]tcpip.Route{
Expand Down
4 changes: 2 additions & 2 deletions client/firewall/uspfilter/forwarder/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (f *Forwarder) handleUDP(r *udp.ForwarderRequest) {
}

f.udpForwarder.RLock()
pConn, exists := f.udpForwarder.conns[id]
_, exists := f.udpForwarder.conns[id]
f.udpForwarder.RUnlock()
if exists {
f.logger.Trace("forwarder: existing UDP connection for %v", id)
Expand Down Expand Up @@ -159,7 +159,7 @@ func (f *Forwarder) handleUDP(r *udp.ForwarderRequest) {
inConn := gonet.NewUDPConn(f.stack, &wq, ep)
connCtx, connCancel := context.WithCancel(f.ctx)

pConn = &udpPacketConn{
pConn := &udpPacketConn{
conn: inConn,
outConn: outConn,
cancel: connCancel,
Expand Down
25 changes: 2 additions & 23 deletions client/firewall/uspfilter/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,6 @@ var levelStrings = map[Level]string{
LevelTrace: "TRAC",
}

func FromLogrusLevel(level log.Level) Level {
switch level {
case log.TraceLevel:
return LevelTrace
case log.DebugLevel:
return LevelDebug
case log.InfoLevel:
return LevelInfo
case log.WarnLevel:
return LevelWarn
case log.ErrorLevel:
return LevelError
case log.FatalLevel:
return LevelFatal
case log.PanicLevel:
return LevelPanic
default:
return LevelInfo
}
}

// Logger is a high-performance, non-blocking logger
type Logger struct {
output io.Writer
Expand Down Expand Up @@ -128,7 +107,7 @@ func (l *Logger) log(level Level, format string, args ...interface{}) {
if len(*bufp) > maxMessageSize {
*bufp = (*bufp)[:maxMessageSize]
}
l.buffer.Write(*bufp)
_, _ = l.buffer.Write(*bufp)

l.bufPool.Put(bufp)
}
Expand Down Expand Up @@ -184,7 +163,7 @@ func (l *Logger) worker() {
}

// Write batch
l.output.Write(buf[:n])
_, _ = l.output.Write(buf[:n])
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions client/firewall/uspfilter/log/ringbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,3 @@ func (r *ringBuffer) Read(p []byte) (n int, err error) {

return n, nil
}

// min returns the smaller of two integers
func min(a, b int) int {
if a < b {
return a
}
return b
}
9 changes: 9 additions & 0 deletions client/firewall/uspfilter/uspfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/stretchr/testify/require"
wgdevice "golang.zx2c4.com/wireguard/device"

fw "github.com/netbirdio/netbird/client/firewall/manager"
"github.com/netbirdio/netbird/client/firewall/uspfilter/conntrack"
Expand All @@ -22,6 +23,14 @@ type IFaceMock struct {
AddressFunc func() iface.WGAddress
}

func (i *IFaceMock) GetWGDevice() *wgdevice.Device {
return nil
}

func (i *IFaceMock) GetDevice() *device.FilteredDevice {
return nil
}

func (i *IFaceMock) SetFilter(iface device.PacketFilter) error {
if i.SetFilterFunc == nil {
return fmt.Errorf("not implemented")
Expand Down
3 changes: 3 additions & 0 deletions client/iface/device_android.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package iface

import (
wgdevice "golang.zx2c4.com/wireguard/device"

"github.com/netbirdio/netbird/client/iface/bind"
"github.com/netbirdio/netbird/client/iface/device"
)
Expand All @@ -13,4 +15,5 @@ type WGTunDevice interface {
DeviceName() string
Close() error
FilteredDevice() *device.FilteredDevice
Device() *wgdevice.Device
}
9 changes: 7 additions & 2 deletions client/iface/iface_moc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"time"

wgdevice "golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

"github.com/netbirdio/netbird/client/iface/bind"
Expand All @@ -29,6 +30,7 @@ type MockWGIface struct {
SetFilterFunc func(filter device.PacketFilter) error
GetFilterFunc func() device.PacketFilter
GetDeviceFunc func() *device.FilteredDevice
GetWGDeviceFunc func() *wgdevice.Device
GetStatsFunc func(peerKey string) (configurer.WGStats, error)
GetInterfaceGUIDStringFunc func() (string, error)
GetProxyFunc func() wgproxy.Proxy
Expand Down Expand Up @@ -102,11 +104,14 @@ func (m *MockWGIface) GetDevice() *device.FilteredDevice {
return m.GetDeviceFunc()
}

func (m *MockWGIface) GetWGDevice() *wgdevice.Device {
return m.GetWGDeviceFunc()
}

func (m *MockWGIface) GetStats(peerKey string) (configurer.WGStats, error) {
return m.GetStatsFunc(peerKey)
}

func (m *MockWGIface) GetProxy() wgproxy.Proxy {
//TODO implement me
panic("implement me")
return m.GetProxyFunc()
}
30 changes: 30 additions & 0 deletions client/internal/acl/mocks/iface_mapper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1370bfb

Please sign in to comment.