[windows_kext] Fix some Go linter warnings

This commit is contained in:
Daniel 2024-05-16 13:51:10 +02:00
parent 6f9b0a8249
commit 8a6f925c8b
5 changed files with 114 additions and 71 deletions

View file

@ -5,6 +5,7 @@ import (
"io" "io"
) )
// Command IDs.
const ( const (
CommandShutdown = 0 CommandShutdown = 0
CommandVerdict = 1 CommandVerdict = 1
@ -17,8 +18,10 @@ const (
CommandCleanEndedConnections = 8 CommandCleanEndedConnections = 8
) )
// KextVerdict is the verdict ID used to with the kext.
type KextVerdict uint8 type KextVerdict uint8
// Kext Verdicts.
// Make sure this is in sync with the Rust version. // Make sure this is in sync with the Rust version.
const ( const (
// VerdictUndecided is the default status of new connections. // VerdictUndecided is the default status of new connections.
@ -37,20 +40,20 @@ const (
type Verdict struct { type Verdict struct {
command uint8 command uint8
Id uint64 ID uint64
Verdict uint8 Verdict uint8
} }
type RedirectV4 struct { type RedirectV4 struct {
command uint8 command uint8
Id uint64 ID uint64
RemoteAddress [4]byte RemoteAddress [4]byte
RemotePort uint16 RemotePort uint16
} }
type RedirectV6 struct { type RedirectV6 struct {
command uint8 command uint8
Id uint64 ID uint64
RemoteAddress [16]byte RemoteAddress [16]byte
RemotePort uint16 RemotePort uint16
} }
@ -75,46 +78,55 @@ type UpdateV6 struct {
Verdict uint8 Verdict uint8
} }
// SendShutdownCommand sends a Shutdown command to the kext.
func SendShutdownCommand(writer io.Writer) error { func SendShutdownCommand(writer io.Writer) error {
_, err := writer.Write([]byte{CommandShutdown}) _, err := writer.Write([]byte{CommandShutdown})
return err return err
} }
// SendVerdictCommand sends a Verdict command to the kext.
func SendVerdictCommand(writer io.Writer, verdict Verdict) error { func SendVerdictCommand(writer io.Writer, verdict Verdict) error {
verdict.command = CommandVerdict verdict.command = CommandVerdict
return binary.Write(writer, binary.LittleEndian, verdict) return binary.Write(writer, binary.LittleEndian, verdict)
} }
// SendUpdateV4Command sends a UpdateV4 command to the kext.
func SendUpdateV4Command(writer io.Writer, update UpdateV4) error { func SendUpdateV4Command(writer io.Writer, update UpdateV4) error {
update.command = CommandUpdateV4 update.command = CommandUpdateV4
return binary.Write(writer, binary.LittleEndian, update) return binary.Write(writer, binary.LittleEndian, update)
} }
// SendUpdateV6Command sends a UpdateV6 command to the kext.
func SendUpdateV6Command(writer io.Writer, update UpdateV6) error { func SendUpdateV6Command(writer io.Writer, update UpdateV6) error {
update.command = CommandUpdateV6 update.command = CommandUpdateV6
return binary.Write(writer, binary.LittleEndian, update) return binary.Write(writer, binary.LittleEndian, update)
} }
// SendClearCacheCommand sends a ClearCache command to the kext.
func SendClearCacheCommand(writer io.Writer) error { func SendClearCacheCommand(writer io.Writer) error {
_, err := writer.Write([]byte{CommandClearCache}) _, err := writer.Write([]byte{CommandClearCache})
return err return err
} }
// SendGetLogsCommand sends a GetLogs command to the kext.
func SendGetLogsCommand(writer io.Writer) error { func SendGetLogsCommand(writer io.Writer) error {
_, err := writer.Write([]byte{CommandGetLogs}) _, err := writer.Write([]byte{CommandGetLogs})
return err return err
} }
// SendGetBandwidthStatsCommand sends a GetBandwidthStats command to the kext.
func SendGetBandwidthStatsCommand(writer io.Writer) error { func SendGetBandwidthStatsCommand(writer io.Writer) error {
_, err := writer.Write([]byte{CommandBandwidthStats}) _, err := writer.Write([]byte{CommandBandwidthStats})
return err return err
} }
// SendPrintMemoryStatsCommand sends a PrintMemoryStats command to the kext.
func SendPrintMemoryStatsCommand(writer io.Writer) error { func SendPrintMemoryStatsCommand(writer io.Writer) error {
_, err := writer.Write([]byte{CommandPrintMemoryStats}) _, err := writer.Write([]byte{CommandPrintMemoryStats})
return err return err
} }
// SendCleanEndedConnectionsCommand sends a CleanEndedConnections command to the kext.
func SendCleanEndedConnectionsCommand(writer io.Writer) error { func SendCleanEndedConnectionsCommand(writer io.Writer) error {
_, err := writer.Write([]byte{CommandCleanEndedConnections}) _, err := writer.Write([]byte{CommandCleanEndedConnections})
return err return err

View file

@ -23,8 +23,8 @@ type connectionV4Internal struct {
ProcessId uint64 ProcessId uint64
Direction byte Direction byte
Protocol byte Protocol byte
LocalIp [4]byte LocalIP [4]byte
RemoteIp [4]byte RemoteIP [4]byte
LocalPort uint16 LocalPort uint16
RemotePort uint16 RemotePort uint16
PayloadLayer uint8 PayloadLayer uint8
@ -40,19 +40,19 @@ func (c *ConnectionV4) Compare(other *ConnectionV4) bool {
c.ProcessId == other.ProcessId && c.ProcessId == other.ProcessId &&
c.Direction == other.Direction && c.Direction == other.Direction &&
c.Protocol == other.Protocol && c.Protocol == other.Protocol &&
c.LocalIp == other.LocalIp && c.LocalIP == other.LocalIP &&
c.RemoteIp == other.RemoteIp && c.RemoteIP == other.RemoteIP &&
c.LocalPort == other.LocalPort && c.LocalPort == other.LocalPort &&
c.RemotePort == other.RemotePort c.RemotePort == other.RemotePort
} }
type connectionV6Internal struct { type connectionV6Internal struct {
Id uint64 Id uint64
ProcessId uint64 ProcessID uint64
Direction byte Direction byte
Protocol byte Protocol byte
LocalIp [16]byte LocalIP [16]byte
RemoteIp [16]byte RemoteIP [16]byte
LocalPort uint16 LocalPort uint16
RemotePort uint16 RemotePort uint16
PayloadLayer uint8 PayloadLayer uint8
@ -65,11 +65,11 @@ type ConnectionV6 struct {
func (c ConnectionV6) Compare(other *ConnectionV6) bool { func (c ConnectionV6) Compare(other *ConnectionV6) bool {
return c.Id == other.Id && return c.Id == other.Id &&
c.ProcessId == other.ProcessId && c.ProcessID == other.ProcessID &&
c.Direction == other.Direction && c.Direction == other.Direction &&
c.Protocol == other.Protocol && c.Protocol == other.Protocol &&
c.LocalIp == other.LocalIp && c.LocalIP == other.LocalIP &&
c.RemoteIp == other.RemoteIp && c.RemoteIP == other.RemoteIP &&
c.LocalPort == other.LocalPort && c.LocalPort == other.LocalPort &&
c.RemotePort == other.RemotePort c.RemotePort == other.RemotePort
} }

View file

@ -9,4 +9,4 @@ func (f *KextFile) Read(buffer []byte) (int, error) {
return 0, nil return 0, nil
} }
func (f *KextFile) flush_buffer() {} func (f *KextFile) flushBuffer() {}

View file

@ -2,6 +2,7 @@ package kext_interface
import ( import (
"bytes" "bytes"
"errors"
"io" "io"
"math/rand" "math/rand"
"os" "os"
@ -9,36 +10,42 @@ import (
) )
func TestRustInfoFile(t *testing.T) { func TestRustInfoFile(t *testing.T) {
t.Parallel()
file, err := os.Open("../protocol/rust_info_test.bin") file, err := os.Open("../protocol/rust_info_test.bin")
if err != nil { if err != nil {
panic(err) panic(err)
} }
defer file.Close() defer func() {
_ = file.Close()
}()
for { for {
info, err := RecvInfo(file) info, err := RecvInfo(file)
if err != nil { if err != nil {
if err != io.EOF { if errors.Is(err, io.EOF) {
t.Errorf("unexpected error: %s\n", err) t.Errorf("unexpected error: %s\n", err)
} }
return return
} }
if info.LogLine != nil {
switch {
case info.LogLine != nil:
if info.LogLine.Severity != 1 { if info.LogLine.Severity != 1 {
t.Errorf("unexpected Log severity: %d\n", info.LogLine.Severity) t.Errorf("unexpected Log severity: %d\n", info.LogLine.Severity)
} }
if info.LogLine.Line != "prefix: test log" { if info.LogLine.Line != "prefix: test log" {
t.Errorf("unexpected Log line: %s\n", info.LogLine.Line) t.Errorf("unexpected Log line: %s\n", info.LogLine.Line)
} }
} else if info.ConnectionV4 != nil {
case info.ConnectionV4 != nil:
conn := info.ConnectionV4 conn := info.ConnectionV4
expected := connectionV4Internal{ expected := connectionV4Internal{
Id: 1, Id: 1,
ProcessId: 2, ProcessId: 2,
Direction: 3, Direction: 3,
Protocol: 4, Protocol: 4,
LocalIp: [4]byte{1, 2, 3, 4}, LocalIP: [4]byte{1, 2, 3, 4},
RemoteIp: [4]byte{2, 3, 4, 5}, RemoteIP: [4]byte{2, 3, 4, 5},
LocalPort: 5, LocalPort: 5,
RemotePort: 6, RemotePort: 6,
PayloadLayer: 7, PayloadLayer: 7,
@ -49,15 +56,16 @@ func TestRustInfoFile(t *testing.T) {
if !bytes.Equal(conn.Payload, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) { if !bytes.Equal(conn.Payload, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {
t.Errorf("unexpected ConnectionV4 payload: %+v\n", conn.Payload) t.Errorf("unexpected ConnectionV4 payload: %+v\n", conn.Payload)
} }
} else if info.ConnectionV6 != nil {
case info.ConnectionV6 != nil:
conn := info.ConnectionV6 conn := info.ConnectionV6
expected := connectionV6Internal{ expected := connectionV6Internal{
Id: 1, Id: 1,
ProcessId: 2, ProcessID: 2,
Direction: 3, Direction: 3,
Protocol: 4, Protocol: 4,
LocalIp: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, LocalIP: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
RemoteIp: [16]byte{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, RemoteIP: [16]byte{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
LocalPort: 5, LocalPort: 5,
RemotePort: 6, RemotePort: 6,
PayloadLayer: 7, PayloadLayer: 7,
@ -68,7 +76,8 @@ func TestRustInfoFile(t *testing.T) {
if !bytes.Equal(conn.Payload, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) { if !bytes.Equal(conn.Payload, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {
t.Errorf("unexpected ConnectionV6 payload: %+v\n", conn.Payload) t.Errorf("unexpected ConnectionV6 payload: %+v\n", conn.Payload)
} }
} else if info.ConnectionEndV4 != nil {
case info.ConnectionEndV4 != nil:
endEvent := info.ConnectionEndV4 endEvent := info.ConnectionEndV4
expected := ConnectionEndV4{ expected := ConnectionEndV4{
ProcessId: 1, ProcessId: 1,
@ -82,7 +91,8 @@ func TestRustInfoFile(t *testing.T) {
if *endEvent != expected { if *endEvent != expected {
t.Errorf("unexpected ConnectionEndV4: %+v\n", endEvent) t.Errorf("unexpected ConnectionEndV4: %+v\n", endEvent)
} }
} else if info.ConnectionEndV6 != nil {
case info.ConnectionEndV6 != nil:
endEvent := info.ConnectionEndV6 endEvent := info.ConnectionEndV6
expected := ConnectionEndV6{ expected := ConnectionEndV6{
ProcessId: 1, ProcessId: 1,
@ -96,7 +106,8 @@ func TestRustInfoFile(t *testing.T) {
if *endEvent != expected { if *endEvent != expected {
t.Errorf("unexpected ConnectionEndV6: %+v\n", endEvent) t.Errorf("unexpected ConnectionEndV6: %+v\n", endEvent)
} }
} else if info.BandwidthStats != nil {
case info.BandwidthStats != nil:
stats := info.BandwidthStats stats := info.BandwidthStats
if stats.Protocol != 1 { if stats.Protocol != 1 {
t.Errorf("unexpected Bandwidth stats protocol: %d\n", stats.Protocol) t.Errorf("unexpected Bandwidth stats protocol: %d\n", stats.Protocol)
@ -163,11 +174,15 @@ func TestRustInfoFile(t *testing.T) {
} }
func TestGenerateCommandFile(t *testing.T) { func TestGenerateCommandFile(t *testing.T) {
t.Parallel()
file, err := os.Create("go_command_test.bin") file, err := os.Create("go_command_test.bin")
if err != nil { if err != nil {
t.Errorf("failed to create file: %s", err) t.Errorf("failed to create file: %s", err)
} }
defer file.Close() defer func() {
_ = file.Close()
}()
enums := []byte{ enums := []byte{
CommandShutdown, CommandShutdown,
CommandVerdict, CommandVerdict,
@ -181,25 +196,28 @@ func TestGenerateCommandFile(t *testing.T) {
selected := make([]byte, 5000) selected := make([]byte, 5000)
for i := range selected { for i := range selected {
selected[i] = enums[rand.Intn(len(enums))] selected[i] = enums[rand.Intn(len(enums))] //nolint:gosec
} }
for _, value := range selected { for _, value := range selected {
switch value { switch value {
case CommandShutdown: case CommandShutdown:
{ err := SendShutdownCommand(file)
SendShutdownCommand(file) if err != nil {
t.Fatal(err)
} }
case CommandVerdict: case CommandVerdict:
{ err := SendVerdictCommand(file, Verdict{
SendVerdictCommand(file, Verdict{ ID: 1,
Id: 1,
Verdict: 2, Verdict: 2,
}) })
if err != nil {
t.Fatal(err)
} }
case CommandUpdateV4: case CommandUpdateV4:
{ err := SendUpdateV4Command(file, UpdateV4{
SendUpdateV4Command(file, UpdateV4{
Protocol: 1, Protocol: 1,
LocalAddress: [4]byte{1, 2, 3, 4}, LocalAddress: [4]byte{1, 2, 3, 4},
LocalPort: 2, LocalPort: 2,
@ -207,11 +225,12 @@ func TestGenerateCommandFile(t *testing.T) {
RemotePort: 3, RemotePort: 3,
Verdict: 4, Verdict: 4,
}) })
if err != nil {
t.Fatal(err)
} }
case CommandUpdateV6:
{
SendUpdateV6Command(file, UpdateV6{ case CommandUpdateV6:
err := SendUpdateV6Command(file, UpdateV6{
Protocol: 1, Protocol: 1,
LocalAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, LocalAddress: [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
LocalPort: 2, LocalPort: 2,
@ -219,26 +238,38 @@ func TestGenerateCommandFile(t *testing.T) {
RemotePort: 3, RemotePort: 3,
Verdict: 4, Verdict: 4,
}) })
if err != nil {
t.Fatal(err)
} }
case CommandClearCache: case CommandClearCache:
{ err := SendClearCacheCommand(file)
SendClearCacheCommand(file) if err != nil {
t.Fatal(err)
} }
case CommandGetLogs: case CommandGetLogs:
{ err := SendGetLogsCommand(file)
SendGetLogsCommand(file) if err != nil {
t.Fatal(err)
} }
case CommandBandwidthStats: case CommandBandwidthStats:
{ err := SendGetBandwidthStatsCommand(file)
SendGetBandwidthStatsCommand(file) if err != nil {
t.Fatal(err)
} }
case CommandPrintMemoryStats: case CommandPrintMemoryStats:
{ err := SendPrintMemoryStatsCommand(file)
SendPrintMemoryStatsCommand(file) if err != nil {
t.Fatal(err)
} }
case CommandCleanEndedConnections: case CommandCleanEndedConnections:
{ err := SendCleanEndedConnectionsCommand(file)
SendCleanEndedConnectionsCommand(file) if err != nil {
t.Fatal(err)
} }
} }
} }