From 1d6228ea7b658e1178e80cec609e98bf8d8f8ab9 Mon Sep 17 00:00:00 2001 From: Vladimir Stoilov <vladimir@safing.io> Date: Thu, 16 May 2024 16:21:27 +0300 Subject: [PATCH] [windows_kext] fix all linter error --- .../interception/windowskext2/handler.go | 24 ++-- .../interception/windowskext2/kext.go | 41 ++++--- .../interception/windowskext2/packet.go | 18 +-- .../interception/windowskext2/service.go | 6 +- windows_kext/driver/src/entry.rs | 2 +- .../command.go | 16 +-- .../{kext_interface => kextinterface}/info.go | 106 +++++++++++------- .../ioctl.go | 3 +- .../{kext_interface => kextinterface}/kext.go | 9 +- .../kext_file.go | 3 +- .../kext_file_test.go | 4 +- .../protocol_test.go | 24 ++-- .../version.txt | 0 windows_kext/protocol/README.md | 2 +- windows_kext/protocol/src/command.rs | 2 +- windows_kext/release/README.md | 2 +- windows_kext/release/src/main.rs | 2 +- windows_kext/test_protocol.sh | 4 +- 18 files changed, 137 insertions(+), 131 deletions(-) rename windows_kext/{kext_interface => kextinterface}/command.go (92%) rename windows_kext/{kext_interface => kextinterface}/info.go (67%) rename windows_kext/{kext_interface => kextinterface}/ioctl.go (97%) rename windows_kext/{kext_interface => kextinterface}/kext.go (97%) rename windows_kext/{kext_interface => kextinterface}/kext_file.go (98%) rename windows_kext/{kext_interface => kextinterface}/kext_file_test.go (68%) rename windows_kext/{kext_interface => kextinterface}/protocol_test.go (94%) rename windows_kext/{kext_interface => kextinterface}/version.txt (100%) diff --git a/service/firewall/interception/windowskext2/handler.go b/service/firewall/interception/windowskext2/handler.go index b6339b50..bb6348dd 100644 --- a/service/firewall/interception/windowskext2/handler.go +++ b/service/firewall/interception/windowskext2/handler.go @@ -44,7 +44,7 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch conn := packetInfo.ConnectionV4 // New Packet newPacket := &Packet{ - verdictRequest: conn.Id, + verdictRequest: conn.ID, payload: conn.Payload, verdictSet: abool.NewBool(false), } @@ -52,7 +52,7 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch info.Inbound = conn.Direction > 0 info.InTunnel = false info.Protocol = packet.IPProtocol(conn.Protocol) - info.PID = int(conn.ProcessId) + info.PID = int(conn.ProcessID) info.SeenAt = time.Now() // Check PID @@ -68,12 +68,12 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch // Set IPs if info.Inbound { // Inbound - info.Src = conn.RemoteIp[:] - info.Dst = conn.LocalIp[:] + info.Src = conn.RemoteIP[:] + info.Dst = conn.LocalIP[:] } else { // Outbound - info.Src = conn.LocalIp[:] - info.Dst = conn.RemoteIp[:] + info.Src = conn.LocalIP[:] + info.Dst = conn.RemoteIP[:] } // Set Ports @@ -95,7 +95,7 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch conn := packetInfo.ConnectionV6 // New Packet newPacket := &Packet{ - verdictRequest: conn.Id, + verdictRequest: conn.ID, payload: conn.Payload, verdictSet: abool.NewBool(false), } @@ -103,7 +103,7 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch info.Inbound = conn.Direction > 0 info.InTunnel = false info.Protocol = packet.IPProtocol(conn.Protocol) - info.PID = int(conn.ProcessId) + info.PID = int(conn.ProcessID) info.SeenAt = time.Now() // Check PID @@ -119,12 +119,12 @@ func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate ch // Set IPs if info.Inbound { // Inbound - info.Src = conn.RemoteIp[:] - info.Dst = conn.LocalIp[:] + info.Src = conn.RemoteIP[:] + info.Dst = conn.LocalIP[:] } else { // Outbound - info.Src = conn.LocalIp[:] - info.Dst = conn.RemoteIp[:] + info.Src = conn.LocalIP[:] + info.Dst = conn.RemoteIP[:] } // Set Ports diff --git a/service/firewall/interception/windowskext2/kext.go b/service/firewall/interception/windowskext2/kext.go index 4cbf9041..fd6adb72 100644 --- a/service/firewall/interception/windowskext2/kext.go +++ b/service/firewall/interception/windowskext2/kext.go @@ -8,7 +8,7 @@ import ( "github.com/safing/portbase/log" "github.com/safing/portmaster/service/network" - "github.com/safing/portmaster/windows_kext/kext_interface" + "github.com/safing/portmaster/windows_kext/kextinterface" "golang.org/x/sys/windows" ) @@ -16,8 +16,8 @@ import ( var ( driverPath string - service *kext_interface.KextService - kextFile *kext_interface.KextFile + service *kextinterface.KextService + kextFile *kextinterface.KextFile ) const ( @@ -31,10 +31,9 @@ func Init(path string) error { // Start intercepting. func Start() error { - // initialize and start driver service var err error - service, err = kext_interface.CreateKextService(driverName, driverPath) + service, err = kextinterface.CreateKextService(driverName, driverPath) if err != nil { return fmt.Errorf("failed to create service: %w", err) } @@ -86,46 +85,46 @@ func Stop() error { // Sends a shutdown request. func shutdownRequest() error { - return kext_interface.SendShutdownCommand(kextFile) + return kextinterface.SendShutdownCommand(kextFile) } // Send request for logs of the kext. func SendLogRequest() error { - return kext_interface.SendGetLogsCommand(kextFile) + return kextinterface.SendGetLogsCommand(kextFile) } func SendBandwidthStatsRequest() error { - return kext_interface.SendGetBandwidthStatsCommand(kextFile) + return kextinterface.SendGetBandwidthStatsCommand(kextFile) } func SendPrintMemoryStatsCommand() error { - return kext_interface.SendPrintMemoryStatsCommand(kextFile) + return kextinterface.SendPrintMemoryStatsCommand(kextFile) } func SendCleanEndedConnection() error { - return kext_interface.SendCleanEndedConnectionsCommand(kextFile) + return kextinterface.SendCleanEndedConnectionsCommand(kextFile) } // RecvVerdictRequest waits for the next verdict request from the kext. If a timeout is reached, both *VerdictRequest and error will be nil. -func RecvVerdictRequest() (*kext_interface.Info, error) { - return kext_interface.RecvInfo(kextFile) +func RecvVerdictRequest() (*kextinterface.Info, error) { + return kextinterface.RecvInfo(kextFile) } // SetVerdict sets the verdict for a packet and/or connection. -func SetVerdict(pkt *Packet, verdict kext_interface.KextVerdict) error { - verdictCommand := kext_interface.Verdict{Id: pkt.verdictRequest, Verdict: uint8(verdict)} - return kext_interface.SendVerdictCommand(kextFile, verdictCommand) +func SetVerdict(pkt *Packet, verdict kextinterface.KextVerdict) error { + verdictCommand := kextinterface.Verdict{ID: pkt.verdictRequest, Verdict: uint8(verdict)} + return kextinterface.SendVerdictCommand(kextFile, verdictCommand) } // Clears the internal connection cache. func ClearCache() error { - return kext_interface.SendClearCacheCommand(kextFile) + return kextinterface.SendClearCacheCommand(kextFile) } // Updates a specific connection verdict. func UpdateVerdict(conn *network.Connection) error { if conn.IPVersion == 4 { - update := kext_interface.UpdateV4{ + update := kextinterface.UpdateV4{ Protocol: conn.Entity.Protocol, LocalAddress: [4]byte(conn.LocalIP), LocalPort: conn.LocalPort, @@ -134,9 +133,9 @@ func UpdateVerdict(conn *network.Connection) error { Verdict: uint8(conn.Verdict), } - return kext_interface.SendUpdateV4Command(kextFile, update) + return kextinterface.SendUpdateV4Command(kextFile, update) } else if conn.IPVersion == 6 { - update := kext_interface.UpdateV6{ + update := kextinterface.UpdateV6{ Protocol: conn.Entity.Protocol, LocalAddress: [16]byte(conn.LocalIP), LocalPort: conn.LocalPort, @@ -145,14 +144,14 @@ func UpdateVerdict(conn *network.Connection) error { Verdict: uint8(conn.Verdict), } - return kext_interface.SendUpdateV6Command(kextFile, update) + return kextinterface.SendUpdateV6Command(kextFile, update) } return nil } // Returns the kext version. func GetVersion() (*VersionInfo, error) { - data, err := kext_interface.ReadVersion(kextFile) + data, err := kextinterface.ReadVersion(kextFile) if err != nil { return nil, err } diff --git a/service/firewall/interception/windowskext2/packet.go b/service/firewall/interception/windowskext2/packet.go index f785efe3..3ea9c009 100644 --- a/service/firewall/interception/windowskext2/packet.go +++ b/service/firewall/interception/windowskext2/packet.go @@ -10,7 +10,7 @@ import ( "github.com/safing/portbase/log" "github.com/safing/portmaster/service/network/packet" - "github.com/safing/portmaster/windows_kext/kext_interface" + "github.com/safing/portmaster/windows_kext/kextinterface" ) // Packet represents an IP packet. @@ -70,7 +70,7 @@ func (pkt *Packet) LoadPacketData() error { // Accept accepts the packet. func (pkt *Packet) Accept() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictAccept) + return SetVerdict(pkt, kextinterface.VerdictAccept) } return nil } @@ -78,7 +78,7 @@ func (pkt *Packet) Accept() error { // Block blocks the packet. func (pkt *Packet) Block() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictBlock) + return SetVerdict(pkt, kextinterface.VerdictBlock) } return nil } @@ -86,7 +86,7 @@ func (pkt *Packet) Block() error { // Drop drops the packet. func (pkt *Packet) Drop() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictDrop) + return SetVerdict(pkt, kextinterface.VerdictDrop) } return nil } @@ -94,7 +94,7 @@ func (pkt *Packet) Drop() error { // PermanentAccept permanently accepts connection (and the current packet). func (pkt *Packet) PermanentAccept() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictPermanentAccept) + return SetVerdict(pkt, kextinterface.VerdictPermanentAccept) } return nil } @@ -102,7 +102,7 @@ func (pkt *Packet) PermanentAccept() error { // PermanentBlock permanently blocks connection (and the current packet). func (pkt *Packet) PermanentBlock() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictPermanentBlock) + return SetVerdict(pkt, kextinterface.VerdictPermanentBlock) } return nil } @@ -110,7 +110,7 @@ func (pkt *Packet) PermanentBlock() error { // PermanentDrop permanently drops connection (and the current packet). func (pkt *Packet) PermanentDrop() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictPermanentDrop) + return SetVerdict(pkt, kextinterface.VerdictPermanentDrop) } return nil } @@ -118,7 +118,7 @@ func (pkt *Packet) PermanentDrop() error { // RerouteToNameserver permanently reroutes the connection to the local nameserver (and the current packet). func (pkt *Packet) RerouteToNameserver() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictRerouteToNameserver) + return SetVerdict(pkt, kextinterface.VerdictRerouteToNameserver) } return nil } @@ -126,7 +126,7 @@ func (pkt *Packet) RerouteToNameserver() error { // RerouteToTunnel permanently reroutes the connection to the local tunnel entrypoint (and the current packet). func (pkt *Packet) RerouteToTunnel() error { if pkt.verdictSet.SetToIf(false, true) { - return SetVerdict(pkt, kext_interface.VerdictRerouteToTunnel) + return SetVerdict(pkt, kextinterface.VerdictRerouteToTunnel) } return nil } diff --git a/service/firewall/interception/windowskext2/service.go b/service/firewall/interception/windowskext2/service.go index 5f6dc20e..5c939f62 100644 --- a/service/firewall/interception/windowskext2/service.go +++ b/service/firewall/interception/windowskext2/service.go @@ -3,8 +3,8 @@ package windowskext -import "github.com/safing/portmaster/windows_kext/kext_interface" +import "github.com/safing/portmaster/windows_kext/kextinterface" -func createKextService(driverName string, driverPath string) (*kext_interface.KextService, error) { - return kext_interface.CreateKextService(driverName, driverPath) +func createKextService(driverName string, driverPath string) (*kextinterface.KextService, error) { + return kextinterface.CreateKextService(driverName, driverPath) } diff --git a/windows_kext/driver/src/entry.rs b/windows_kext/driver/src/entry.rs index b52a2848..479fe42a 100644 --- a/windows_kext/driver/src/entry.rs +++ b/windows_kext/driver/src/entry.rs @@ -7,7 +7,7 @@ use wdk::{err, info, interface}; use windows_sys::Wdk::Foundation::{DEVICE_OBJECT, DRIVER_OBJECT, IRP}; use windows_sys::Win32::Foundation::{NTSTATUS, STATUS_SUCCESS}; -static VERSION: [u8; 4] = include!("../../kext_interface/version.txt"); +static VERSION: [u8; 4] = include!("../../kextinterface/version.txt"); static mut DEVICE: *mut device::Device = core::ptr::null_mut(); pub fn get_device() -> Option<&'static mut device::Device> { diff --git a/windows_kext/kext_interface/command.go b/windows_kext/kextinterface/command.go similarity index 92% rename from windows_kext/kext_interface/command.go rename to windows_kext/kextinterface/command.go index 57ba00ed..b40be18c 100644 --- a/windows_kext/kext_interface/command.go +++ b/windows_kext/kextinterface/command.go @@ -1,4 +1,4 @@ -package kext_interface +package kextinterface import ( "encoding/binary" @@ -44,20 +44,6 @@ type Verdict struct { Verdict uint8 } -type RedirectV4 struct { - command uint8 - ID uint64 - RemoteAddress [4]byte - RemotePort uint16 -} - -type RedirectV6 struct { - command uint8 - ID uint64 - RemoteAddress [16]byte - RemotePort uint16 -} - type UpdateV4 struct { command uint8 Protocol uint8 diff --git a/windows_kext/kext_interface/info.go b/windows_kext/kextinterface/info.go similarity index 67% rename from windows_kext/kext_interface/info.go rename to windows_kext/kextinterface/info.go index 98efe877..b7df9314 100644 --- a/windows_kext/kext_interface/info.go +++ b/windows_kext/kextinterface/info.go @@ -1,4 +1,4 @@ -package kext_interface +package kextinterface import ( "encoding/binary" @@ -16,11 +16,14 @@ const ( InfoBandwidthStatsV6 = 6 ) -var ErrorUnknownInfoType = errors.New("unknown info type") +var ( + ErrUnknownInfoType = errors.New("unknown info type") + ErrUnexpectedReadError = errors.New("unexpected read error") +) type connectionV4Internal struct { - Id uint64 - ProcessId uint64 + ID uint64 + ProcessID uint64 Direction byte Protocol byte LocalIP [4]byte @@ -36,8 +39,8 @@ type ConnectionV4 struct { } func (c *ConnectionV4) Compare(other *ConnectionV4) bool { - return c.Id == other.Id && - c.ProcessId == other.ProcessId && + return c.ID == other.ID && + c.ProcessID == other.ProcessID && c.Direction == other.Direction && c.Protocol == other.Protocol && c.LocalIP == other.LocalIP && @@ -47,7 +50,7 @@ func (c *ConnectionV4) Compare(other *ConnectionV4) bool { } type connectionV6Internal struct { - Id uint64 + ID uint64 ProcessID uint64 Direction byte Protocol byte @@ -64,7 +67,7 @@ type ConnectionV6 struct { } func (c ConnectionV6) Compare(other *ConnectionV6) bool { - return c.Id == other.Id && + return c.ID == other.ID && c.ProcessID == other.ProcessID && c.Direction == other.Direction && c.Protocol == other.Protocol && @@ -75,21 +78,21 @@ func (c ConnectionV6) Compare(other *ConnectionV6) bool { } type ConnectionEndV4 struct { - ProcessId uint64 + ProcessID uint64 Direction byte Protocol byte - LocalIp [4]byte - RemoteIp [4]byte + LocalIP [4]byte + RemoteIP [4]byte LocalPort uint16 RemotePort uint16 } type ConnectionEndV6 struct { - ProcessId uint64 + ProcessID uint64 Direction byte Protocol byte - LocalIp [16]byte - RemoteIp [16]byte + LocalIP [16]byte + RemoteIP [16]byte LocalPort uint16 RemotePort uint16 } @@ -142,6 +145,9 @@ func RecvInfo(reader io.Reader) (*Info, error) { // Read size of data var size uint32 err = binary.Read(reader, binary.LittleEndian, &size) + if err != nil { + return nil, err + } // Read data switch infoType { @@ -150,16 +156,19 @@ func RecvInfo(reader io.Reader) (*Info, error) { var fixedSizeValues connectionV4Internal err = binary.Read(reader, binary.LittleEndian, &fixedSizeValues) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } // Read size of payload var size uint32 err = binary.Read(reader, binary.LittleEndian, &size) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } newInfo := ConnectionV4{connectionV4Internal: fixedSizeValues, Payload: make([]byte, size)} err = binary.Read(reader, binary.LittleEndian, &newInfo.Payload) + if err != nil { + return nil, errors.Join(ErrUnexpectedReadError, err) + } return &Info{ConnectionV4: &newInfo}, nil } case InfoConnectionIpv6: @@ -167,47 +176,53 @@ func RecvInfo(reader io.Reader) (*Info, error) { var fixedSizeValues connectionV6Internal err = binary.Read(reader, binary.LittleEndian, &fixedSizeValues) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } // Read size of payload var size uint32 err = binary.Read(reader, binary.LittleEndian, &size) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } newInfo := ConnectionV6{connectionV6Internal: fixedSizeValues, Payload: make([]byte, size)} err = binary.Read(reader, binary.LittleEndian, &newInfo.Payload) + if err != nil { + return nil, errors.Join(ErrUnexpectedReadError, err) + } return &Info{ConnectionV6: &newInfo}, nil } case InfoConnectionEndEventV4: { - var new ConnectionEndV4 - err = binary.Read(reader, binary.LittleEndian, &new) + var connectionEnd ConnectionEndV4 + err = binary.Read(reader, binary.LittleEndian, &connectionEnd) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } - return &Info{ConnectionEndV4: &new}, nil + return &Info{ConnectionEndV4: &connectionEnd}, nil } case InfoConnectionEndEventV6: { - var new ConnectionEndV6 - err = binary.Read(reader, binary.LittleEndian, &new) + var connectionEnd ConnectionEndV6 + err = binary.Read(reader, binary.LittleEndian, &connectionEnd) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } - return &Info{ConnectionEndV6: &new}, nil + return &Info{ConnectionEndV6: &connectionEnd}, nil } case InfoLogLine: { - var logLine = LogLine{} + logLine := LogLine{} // Read severity err = binary.Read(reader, binary.LittleEndian, &logLine.Severity) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } // Read string - var line = make([]byte, size-1) // -1 for the severity enum. + line := make([]byte, size-1) // -1 for the severity enum. err = binary.Read(reader, binary.LittleEndian, &line) + if err != nil { + return nil, errors.Join(ErrUnexpectedReadError, err) + } logLine.Line = string(line) return &Info{LogLine: &logLine}, nil } @@ -217,21 +232,24 @@ func RecvInfo(reader io.Reader) (*Info, error) { var protocol uint8 err = binary.Read(reader, binary.LittleEndian, &protocol) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } // Read size of array var size uint32 err = binary.Read(reader, binary.LittleEndian, &size) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } // Read array - var stats_array = make([]BandwidthValueV4, size) + statsArray := make([]BandwidthValueV4, size) for i := 0; i < int(size); i++ { - binary.Read(reader, binary.LittleEndian, &stats_array[i]) + err = binary.Read(reader, binary.LittleEndian, &statsArray[i]) + if err != nil { + return nil, errors.Join(ErrUnexpectedReadError, err) + } } - return &Info{BandwidthStats: &BandwidthStatsArray{Protocol: protocol, ValuesV4: stats_array}}, nil + return &Info{BandwidthStats: &BandwidthStatsArray{Protocol: protocol, ValuesV4: statsArray}}, nil } case InfoBandwidthStatsV6: { @@ -239,25 +257,31 @@ func RecvInfo(reader io.Reader) (*Info, error) { var protocol uint8 err = binary.Read(reader, binary.LittleEndian, &protocol) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } // Read size of array var size uint32 err = binary.Read(reader, binary.LittleEndian, &size) if err != nil { - return nil, err + return nil, errors.Join(ErrUnexpectedReadError, err) } // Read array - var stats_array = make([]BandwidthValueV6, size) + statsArray := make([]BandwidthValueV6, size) for i := 0; i < int(size); i++ { - binary.Read(reader, binary.LittleEndian, &stats_array[i]) + err = binary.Read(reader, binary.LittleEndian, &statsArray[i]) + if err != nil { + return nil, errors.Join(ErrUnexpectedReadError, err) + } } - return &Info{BandwidthStats: &BandwidthStatsArray{Protocol: protocol, ValuesV6: stats_array}}, nil + return &Info{BandwidthStats: &BandwidthStatsArray{Protocol: protocol, ValuesV6: statsArray}}, nil } } + // Command not recognized, read until the end of command and return. + // During normal operation this should not happen. unknownData := make([]byte, size) - reader.Read(unknownData) - return nil, ErrorUnknownInfoType + _, _ = reader.Read(unknownData) + + return nil, ErrUnknownInfoType } diff --git a/windows_kext/kext_interface/ioctl.go b/windows_kext/kextinterface/ioctl.go similarity index 97% rename from windows_kext/kext_interface/ioctl.go rename to windows_kext/kextinterface/ioctl.go index ef165a88..89ae0392 100644 --- a/windows_kext/kext_interface/ioctl.go +++ b/windows_kext/kextinterface/ioctl.go @@ -1,7 +1,7 @@ //go:build windows // +build windows -package kext_interface +package kextinterface import ( "golang.org/x/sys/windows" @@ -28,7 +28,6 @@ var ( func ReadVersion(file *KextFile) ([]uint8, error) { data := make([]uint8, 4) _, err := file.deviceIOControl(IOCTL_VERSION, nil, data) - if err != nil { return nil, err } diff --git a/windows_kext/kext_interface/kext.go b/windows_kext/kextinterface/kext.go similarity index 97% rename from windows_kext/kext_interface/kext.go rename to windows_kext/kextinterface/kext.go index 47b15630..2707a791 100644 --- a/windows_kext/kext_interface/kext.go +++ b/windows_kext/kextinterface/kext.go @@ -1,7 +1,7 @@ //go:build windows // +build windows -package kext_interface +package kextinterface import ( _ "embed" @@ -36,8 +36,10 @@ var ( }() ) -const winInvalidHandleValue = windows.Handle(^uintptr(0)) // Max value -const stopServiceTimeoutDuration = time.Duration(30 * time.Second) +const ( + winInvalidHandleValue = windows.Handle(^uintptr(0)) // Max value + stopServiceTimeoutDuration = time.Duration(30 * time.Second) +) type KextService struct { handle windows.Handle @@ -88,7 +90,6 @@ func (s *KextService) Start(wait bool) error { // Start the service: err := windows.StartService(s.handle, 0, nil) - if err != nil { err = windows.GetLastError() if err != windows.ERROR_SERVICE_ALREADY_RUNNING { diff --git a/windows_kext/kext_interface/kext_file.go b/windows_kext/kextinterface/kext_file.go similarity index 98% rename from windows_kext/kext_interface/kext_file.go rename to windows_kext/kextinterface/kext_file.go index 2dba124e..045ee06e 100644 --- a/windows_kext/kext_interface/kext_file.go +++ b/windows_kext/kextinterface/kext_file.go @@ -1,7 +1,7 @@ //go:build windows // +build windows -package kext_interface +package kextinterface import ( "golang.org/x/sys/windows" @@ -85,7 +85,6 @@ func (f *KextFile) deviceIOControl(code uint32, inData []byte, outData []byte) ( inDataPtr, inDataSize, outDataPtr, outDataSize, nil, overlapped) - if err != nil { return nil, err } diff --git a/windows_kext/kext_interface/kext_file_test.go b/windows_kext/kextinterface/kext_file_test.go similarity index 68% rename from windows_kext/kext_interface/kext_file_test.go rename to windows_kext/kextinterface/kext_file_test.go index 83b7d33e..7dbd4d99 100644 --- a/windows_kext/kext_interface/kext_file_test.go +++ b/windows_kext/kextinterface/kext_file_test.go @@ -1,7 +1,7 @@ //go:build linux // +build linux -package kext_interface +package kextinterface type KextFile struct{} @@ -9,4 +9,4 @@ func (f *KextFile) Read(buffer []byte) (int, error) { return 0, nil } -func (f *KextFile) flushBuffer() {} +// func (f *KextFile) flushBuffer() {} diff --git a/windows_kext/kext_interface/protocol_test.go b/windows_kext/kextinterface/protocol_test.go similarity index 94% rename from windows_kext/kext_interface/protocol_test.go rename to windows_kext/kextinterface/protocol_test.go index c908be07..6cf81581 100644 --- a/windows_kext/kext_interface/protocol_test.go +++ b/windows_kext/kextinterface/protocol_test.go @@ -1,9 +1,8 @@ -package kext_interface +package kextinterface import ( "bytes" "errors" - "io" "math/rand" "os" "testing" @@ -22,7 +21,7 @@ func TestRustInfoFile(t *testing.T) { for { info, err := RecvInfo(file) if err != nil { - if errors.Is(err, io.EOF) { + if errors.Is(err, ErrUnexpectedReadError) { t.Errorf("unexpected error: %s\n", err) } return @@ -40,8 +39,8 @@ func TestRustInfoFile(t *testing.T) { case info.ConnectionV4 != nil: conn := info.ConnectionV4 expected := connectionV4Internal{ - Id: 1, - ProcessId: 2, + ID: 1, + ProcessID: 2, Direction: 3, Protocol: 4, LocalIP: [4]byte{1, 2, 3, 4}, @@ -60,7 +59,7 @@ func TestRustInfoFile(t *testing.T) { case info.ConnectionV6 != nil: conn := info.ConnectionV6 expected := connectionV6Internal{ - Id: 1, + ID: 1, ProcessID: 2, Direction: 3, Protocol: 4, @@ -80,11 +79,11 @@ func TestRustInfoFile(t *testing.T) { case info.ConnectionEndV4 != nil: endEvent := info.ConnectionEndV4 expected := ConnectionEndV4{ - ProcessId: 1, + ProcessID: 1, Direction: 2, Protocol: 3, - LocalIp: [4]byte{1, 2, 3, 4}, - RemoteIp: [4]byte{2, 3, 4, 5}, + LocalIP: [4]byte{1, 2, 3, 4}, + RemoteIP: [4]byte{2, 3, 4, 5}, LocalPort: 4, RemotePort: 5, } @@ -95,11 +94,11 @@ func TestRustInfoFile(t *testing.T) { case info.ConnectionEndV6 != nil: endEvent := info.ConnectionEndV6 expected := ConnectionEndV6{ - ProcessId: 1, + ProcessID: 1, Direction: 2, Protocol: 3, - 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}, + 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}, LocalPort: 4, RemotePort: 5, } @@ -273,5 +272,4 @@ func TestGenerateCommandFile(t *testing.T) { } } } - } diff --git a/windows_kext/kext_interface/version.txt b/windows_kext/kextinterface/version.txt similarity index 100% rename from windows_kext/kext_interface/version.txt rename to windows_kext/kextinterface/version.txt diff --git a/windows_kext/protocol/README.md b/windows_kext/protocol/README.md index 58de329d..cde5d85c 100644 --- a/windows_kext/protocol/README.md +++ b/windows_kext/protocol/README.md @@ -1,4 +1,4 @@ # Protocol -Defines protocol that communicates with `kext_interface` / Portmaster. +Defines protocol that communicates with `kextinterface` / Portmaster. diff --git a/windows_kext/protocol/src/command.rs b/windows_kext/protocol/src/command.rs index 6f17e2fc..53e4b81e 100644 --- a/windows_kext/protocol/src/command.rs +++ b/windows_kext/protocol/src/command.rs @@ -86,7 +86,7 @@ use std::panic; #[test] fn test_go_command_file() { - let mut file = File::open("../kext_interface/go_command_test.bin").unwrap(); + let mut file = File::open("../kextinterface/go_command_test.bin").unwrap(); loop { let mut command: [u8; 1] = [0]; let bytes_count = file.read(&mut command).unwrap(); diff --git a/windows_kext/release/README.md b/windows_kext/release/README.md index a66c03a9..7060c52a 100644 --- a/windows_kext/release/README.md +++ b/windows_kext/release/README.md @@ -1,7 +1,7 @@ # Kext release tool ### Generate the zip file -- Make sure `kext_interface/version.txt` is up to date +- Make sure `kextinterface/version.txt` is up to date - Execute: `cargo run` * This will generate release `kext_release_vX-X-X.zip` file. Which contains all the necessary files to make the release. diff --git a/windows_kext/release/src/main.rs b/windows_kext/release/src/main.rs index 39eacc1b..c1e76a4e 100644 --- a/windows_kext/release/src/main.rs +++ b/windows_kext/release/src/main.rs @@ -5,7 +5,7 @@ use handlebars::Handlebars; use serde_json::json; use zip::{write::FileOptions, ZipWriter}; -static VERSION: [u8; 4] = include!("../../kext_interface/version.txt"); +static VERSION: [u8; 4] = include!("../../kextinterface/version.txt"); static LIB_PATH: &'static str = "./build/x86_64-pc-windows-msvc/release/driver.lib"; fn main() { diff --git a/windows_kext/test_protocol.sh b/windows_kext/test_protocol.sh index ea7a4c54..43ad5058 100755 --- a/windows_kext/test_protocol.sh +++ b/windows_kext/test_protocol.sh @@ -5,7 +5,7 @@ echo ======================== cd protocol cargo test info::generate_test_info_file -cd ../kext_interface +cd ../kextinterface go test -v -run TestGenerateCommandFile cd .. @@ -15,7 +15,7 @@ echo ======================== cd protocol cargo test command::test_go_command_file -cd ../kext_interface +cd ../kextinterface go test -v -run TestRustInfoFile echo ========================