Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion gateway_parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"slices"
"strconv"
"strings"
"unicode"
)

const (
Expand Down Expand Up @@ -146,6 +147,28 @@ func parseToWindowsRouteStruct(output []byte) ([]windowsRouteStruct, error) {
break
}
fields := strings.Fields(inputLine)
// Some Windows commands are localized, so we need to handle the fields in a logical way.
// Baiscally, fields that start with a number will be treated as-is, but consecutive fields
// that start with a letter will be combined into a single field.
{
var logicalFields []string
for f := 0; f < len(fields); f++ {
field := fields[f]
if len(field) > 0 && unicode.IsLetter(rune(field[0])) {
for f+1 < len(fields) {
nextField := fields[f+1]
if len(nextField) > 0 && unicode.IsLetter(rune(nextField[0])) {
field += " " + nextField
f++
} else {
break
}
}
}
logicalFields = append(logicalFields, field)
}
fields = logicalFields
}
if len(fields) < 5 || !ipRegex.MatchString(fields[0]) {
return nil, &ErrCantParse{}
}
Expand Down Expand Up @@ -263,7 +286,8 @@ func parseWindowsGatewayIPs(output []byte) ([]net.IP, error) {

result := make([]net.IP, 0, len(parsedOutputs))
for _, parsedOutput := range parsedOutputs {
if strings.EqualFold(parsedOutput.Gateway, "On-link") {
// Skip "On-link" gateways (these will start with a letter; not all languages will print "On-link").
if len(parsedOutput.Gateway) > 0 && unicode.IsLetter(rune(parsedOutput.Gateway[0])) {
continue
}
ip := net.ParseIP(parsedOutput.Gateway)
Expand All @@ -272,6 +296,9 @@ func parseWindowsGatewayIPs(output []byte) ([]net.IP, error) {
}
result = append(result, ip)
}
if len(result) == 0 {
return nil, &ErrNoGateway{}
}
return result, nil
}

Expand Down
3 changes: 2 additions & 1 deletion gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ func TestParseWindows(t *testing.T) {
testcases := []ipTestCase{
{windows, true, "10.88.88.2", nil},
{windowsLocalized, true, "10.88.88.2", nil},
{windowsLocalized2, true, "192.168.100.1", nil},
{windowsMultipleGateways, true, "10.21.38.1", nil},
{randomData, false, "", &ErrCantParse{}},
{windowsNoRoute, false, "", &ErrNoGateway{}},
{windowsNoDefaultRoute, false, "", &ErrNoGateway{}},
{windowsBadRoute1, false, "", &ErrCantParse{}},
{windowsBadRoute2, false, "", &ErrCantParse{}},
{windowsBadRoute2, false, "", &ErrNoGateway{}},
}

t.Run("parseWindowsGatewayIPs", func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions route-tables/windows.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ IPv4 Route Table
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 10.88.88.2 10.88.88.149 10
0.0.0.0 128.0.0.0 On-link 192.168.0.1 5
127.0.0.0 255.0.0.0 On-link 127.0.0.1 331
127.0.0.1 255.255.255.255 On-link 127.0.0.1 331
127.255.255.255 255.255.255.255 On-link 127.0.0.1 331
192.168.0.0 255.255.255.0 On-link 192.168.0.52 291
192.168.0.52 255.255.255.255 On-link 192.168.0.52 291
192.168.0.255 255.255.255.255 On-link 192.168.0.52 291
===========================================================================
Persistent Routes:
Network Address Netmask Gateway Address Metric
0.0.0.0 0.0.0.0 10.88.88.2 Default
192.168.1.255 255.255.255.255 10.88.88.2 1
---------------------------------------------------------------------------
28 changes: 28 additions & 0 deletions route-tables/windowsLocalized2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
===========================================================================
ILista de interfaces
29...........................SGNAutobahn Tunnel
11...........................SGN Tunnel
18...01 02 03 04 05 60 ......Microsoft Wi-Fi Direct Virtual Adapter
4...01 02 03 04 05 61 ......Microsoft Wi-Fi Direct Virtual Adapter #2
10...01 02 03 04 05 62 ......MediaTek Wi-Fi 6E MT7902 Wireless LAN Card
14...01 02 03 04 05 63 ......Bluetooth Device (Personal Area Network)
1...........................Software Loopback Interface 1
===========================================================================

IPv4 Tabla de enrutamiento
===========================================================================
Rutas activas:
Destino de red Máscara de red Puerta de enlace Interfaz Métrica
0.0.0.0 0.0.0.0 192.168.100.1 192.168.100.80 35
0.0.0.0 192.0.0.0 En vínculo 123.45.0.10 250
===========================================================================
Rutas persistentes:
Ninguno

IPv6 Tabla de enrutamiento
===========================================================================
Rutas activas:
Ninguno
Rutas persistentes:
Ninguno

69 changes: 52 additions & 17 deletions test_route_tables.go

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

Loading