Skip to content

Commit

Permalink
Merge pull request #21 from ndeloof/winFr
Browse files Browse the repository at this point in the history
Support localized output of Windows commands
  • Loading branch information
jackpal authored Nov 18, 2019
2 parents c471441 + 10816db commit 5ceb358
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
16 changes: 15 additions & 1 deletion gateway_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@ var errNoGateway = errors.New("no gateway found")
func parseWindowsRoutePrint(output []byte) (net.IP, error) {
// Windows route output format is always like this:
// ===========================================================================
// Interface List
// 8 ...00 12 3f a7 17 ba ...... Intel(R) PRO/100 VE Network Connection
// 1 ........................... Software Loopback Interface 1
// ===========================================================================
// IPv4 Route Table
// ===========================================================================
// Active Routes:
// Network Destination Netmask Gateway Interface Metric
// 0.0.0.0 0.0.0.0 192.168.1.1 192.168.1.100 20
// ===========================================================================
//
// Windows commands are localized, so we can't just look for "Active Routes:" string
// I'm trying to pick the active route,
// then jump 2 lines and pick the third IP
// Not using regex because output is quite standard from Windows XP to 8 (NEEDS TESTING)
lines := strings.Split(string(output), "\n")
sep := 0
for idx, line := range lines {
if strings.HasPrefix(line, "Active Routes:") {
if sep == 3 {
// We just entered the 2nd section containing "Active Routes:"
if len(lines) <= idx+2 {
return nil, errNoGateway
}
Expand All @@ -39,6 +49,10 @@ func parseWindowsRoutePrint(output []byte) (net.IP, error) {
return ip, nil
}
}
if strings.HasPrefix(line, "=======") {
sep++
continue
}
}
return nil, errNoGateway
}
Expand Down
36 changes: 36 additions & 0 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,67 @@ type testcase struct {

func TestParseWindowsRoutePrint(t *testing.T) {
correctData := []byte(`
===========================================================================
Interface List
8 ...00 12 3f a7 17 ba ...... Intel(R) PRO/100 VE Network Connection
1 ........................... Software Loopback Interface 1
===========================================================================
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
===========================================================================
Persistent Routes:
`)
localizedData := []byte(
`===========================================================================
Liste d'Interfaces
17...00 28 f8 39 61 6b ......Microsoft Wi-Fi Direct Virtual Adapter
1...........................Software Loopback Interface 1
===========================================================================
IPv4 Table de routage
===========================================================================
Itinéraires actifs :
Destination réseau Masque réseau Adr. passerelle Adr. interface Métrique
0.0.0.0 0.0.0.0 10.88.88.2 10.88.88.149 10
===========================================================================
Itinéraires persistants :
Aucun
`)
randomData := []byte(`
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
`)
noRoute := []byte(`
===========================================================================
Interface List
8 ...00 12 3f a7 17 ba ...... Intel(R) PRO/100 VE Network Connection
1 ........................... Software Loopback Interface 1
===========================================================================
IPv4 Route Table
===========================================================================
Active Routes:
`)
badRoute1 := []byte(`
===========================================================================
Interface List
8 ...00 12 3f a7 17 ba ...... Intel(R) PRO/100 VE Network Connection
1 ........................... Software Loopback Interface 1
===========================================================================
IPv4 Route Table
===========================================================================
Active Routes:
===========================================================================
Persistent Routes:
`)
badRoute2 := []byte(`
===========================================================================
Interface List
8 ...00 12 3f a7 17 ba ...... Intel(R) PRO/100 VE Network Connection
1 ........................... Software Loopback Interface 1
===========================================================================
IPv4 Route Table
===========================================================================
Active Routes:
Expand All @@ -50,6 +85,7 @@ Persistent Routes:

testcases := []testcase{
{correctData, true, "10.88.88.2"},
{localizedData, true, "10.88.88.2"},
{randomData, false, ""},
{noRoute, false, ""},
{badRoute1, false, ""},
Expand Down

0 comments on commit 5ceb358

Please sign in to comment.