[windows_kext] fix all linter error

This commit is contained in:
Vladimir Stoilov
2024-05-16 16:21:27 +03:00
parent 5610c88208
commit 1d6228ea7b
18 changed files with 137 additions and 131 deletions

View File

@@ -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> {

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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() {}

View File

@@ -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) {
}
}
}
}

View File

@@ -1,4 +1,4 @@
# Protocol
Defines protocol that communicates with `kext_interface` / Portmaster.
Defines protocol that communicates with `kextinterface` / Portmaster.

View File

@@ -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();

View File

@@ -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.

View File

@@ -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() {

View File

@@ -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 ========================