Add bandwidth stats support

This commit is contained in:
Vladimir Stoilov
2024-01-29 22:23:54 +02:00
parent e308543f4f
commit 7babfb13ab
6 changed files with 185 additions and 75 deletions

View File

@@ -6,6 +6,7 @@ package windowskext
import (
"context"
"fmt"
"net"
"time"
"github.com/safing/portmaster/process"
@@ -28,7 +29,7 @@ func (v *VersionInfo) String() string {
}
// Handler transforms received packets to the Packet interface.
func Handler(ctx context.Context, packets chan packet.Packet) {
func Handler(ctx context.Context, packets chan packet.Packet, bandwidthUpdate chan *packet.BandwidthUpdate) {
for {
packetInfo, err := RecvVerdictRequest()
if err != nil {
@@ -36,71 +37,157 @@ func Handler(ctx context.Context, packets chan packet.Packet) {
return
}
if packetInfo.Connection != nil {
log.Tracef("packet: %+v", packetInfo.Connection)
conn := packetInfo.Connection
// New Packet
new := &Packet{
verdictRequest: conn.Id,
verdictSet: abool.NewBool(false),
switch {
case packetInfo.ConnectionV4 != nil:
{
log.Tracef("packet: %+v", packetInfo.ConnectionV4)
conn := packetInfo.ConnectionV4
// New Packet
new := &Packet{
verdictRequest: conn.Id,
verdictSet: abool.NewBool(false),
}
info := new.Info()
info.Inbound = conn.Direction > 0
info.InTunnel = false
info.Protocol = packet.IPProtocol(conn.Protocol)
info.PID = int(conn.ProcessId)
info.SeenAt = time.Now()
// Check PID
if info.PID == 0 {
// Windows does not have zero PIDs.
// Set to UndefinedProcessID.
info.PID = process.UndefinedProcessID
}
// Set IP version
info.Version = packet.IPv4
// Set IPs
if info.Inbound {
// Inbound
info.Src = conn.RemoteIp[:]
info.Dst = conn.LocalIp[:]
} else {
// Outbound
info.Src = conn.LocalIp[:]
info.Dst = conn.RemoteIp[:]
}
// Set Ports
if info.Inbound {
// Inbound
info.SrcPort = conn.RemotePort
info.DstPort = conn.LocalPort
} else {
// Outbound
info.SrcPort = conn.LocalPort
info.DstPort = conn.RemotePort
}
packets <- new
}
info := new.Info()
info.Inbound = conn.Direction > 0
info.InTunnel = false
info.Protocol = packet.IPProtocol(conn.Protocol)
info.PID = int(conn.ProcessId)
info.SeenAt = time.Now()
case packetInfo.ConnectionV6 != nil:
{
log.Tracef("packet: %+v", packetInfo.ConnectionV6)
conn := packetInfo.ConnectionV6
// New Packet
new := &Packet{
verdictRequest: conn.Id,
verdictSet: abool.NewBool(false),
}
info := new.Info()
info.Inbound = conn.Direction > 0
info.InTunnel = false
info.Protocol = packet.IPProtocol(conn.Protocol)
info.PID = int(conn.ProcessId)
info.SeenAt = time.Now()
// Check PID
if info.PID == 0 {
// Windows does not have zero PIDs.
// Set to UndefinedProcessID.
info.PID = process.UndefinedProcessID
// Check PID
if info.PID == 0 {
// Windows does not have zero PIDs.
// Set to UndefinedProcessID.
info.PID = process.UndefinedProcessID
}
// Set IP version
info.Version = packet.IPv6
// Set IPs
if info.Inbound {
// Inbound
info.Src = conn.RemoteIp[:]
info.Dst = conn.LocalIp[:]
} else {
// Outbound
info.Src = conn.LocalIp[:]
info.Dst = conn.RemoteIp[:]
}
// Set Ports
if info.Inbound {
// Inbound
info.SrcPort = conn.RemotePort
info.DstPort = conn.LocalPort
} else {
// Outbound
info.SrcPort = conn.LocalPort
info.DstPort = conn.RemotePort
}
packets <- new
}
// Set IP version
info.Version = packet.IPv4
// Set IPs
if info.Inbound {
// Inbound
info.Src = conn.RemoteIp[:]
info.Dst = conn.LocalIp[:]
} else {
// Outbound
info.Src = conn.LocalIp[:]
info.Dst = conn.RemoteIp[:]
case packetInfo.LogLine != nil:
{
line := packetInfo.LogLine
switch line.Severity {
case byte(log.DebugLevel):
log.Debugf("kext: %s", line.Line)
case byte(log.InfoLevel):
log.Infof("kext: %s", line.Line)
case byte(log.WarningLevel):
log.Warningf("kext: %s", line.Line)
case byte(log.ErrorLevel):
log.Errorf("kext: %s", line.Line)
case byte(log.CriticalLevel):
log.Criticalf("kext: %s", line.Line)
}
}
// Set Ports
if info.Inbound {
// Inbound
info.SrcPort = conn.RemotePort
info.DstPort = conn.LocalPort
} else {
// Outbound
info.SrcPort = conn.LocalPort
info.DstPort = conn.RemotePort
case packetInfo.BandwidthStats != nil:
{
bandwidthStats := packetInfo.BandwidthStats
for _, stat := range bandwidthStats.ValuesV4 {
connID := packet.CreateConnectionID(
packet.IPProtocol(bandwidthStats.Protocol),
net.IP(stat.LocalIP[:]), stat.LocalPort,
net.IP(stat.RemoteIP[:]), stat.RemotePort,
false,
)
update := &packet.BandwidthUpdate{
ConnID: connID,
BytesReceived: stat.ReceivedBytes,
BytesSent: stat.TransmittedBytes,
Method: packet.Additive,
}
bandwidthUpdate <- update
}
for _, stat := range bandwidthStats.ValuesV6 {
connID := packet.CreateConnectionID(
packet.IPProtocol(bandwidthStats.Protocol),
net.IP(stat.LocalIP[:]), stat.LocalPort,
net.IP(stat.RemoteIP[:]), stat.RemotePort,
false,
)
update := &packet.BandwidthUpdate{
ConnID: connID,
BytesReceived: stat.ReceivedBytes,
BytesSent: stat.TransmittedBytes,
Method: packet.Additive,
}
bandwidthUpdate <- update
}
}
packets <- new
}
// if packetInfo.LogLines != nil {
// for _, line := range *packetInfo.LogLines {
// switch line.Severity {
// case int(log.DebugLevel):
// log.Debugf("kext: %s", line.Line)
// case int(log.InfoLevel):
// log.Infof("kext: %s", line.Line)
// case int(log.WarningLevel):
// log.Warningf("kext: %s", line.Line)
// case int(log.ErrorLevel):
// log.Errorf("kext: %s", line.Line)
// case int(log.CriticalLevel):
// log.Criticalf("kext: %s", line.Line)
// }
// }
// }
}
}