Skip to content

Commit 3227471

Browse files
author
Atakan Çolak
committed
reworked & added interface IP for win & lin
1 parent bdc30bb commit 3227471

12 files changed

+430
-240
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
.idea

gateway.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package gateway
2+
3+
import (
4+
"errors"
5+
"net"
6+
"runtime"
7+
)
8+
9+
var (
10+
errNoGateway = errors.New("no gateway found")
11+
errCantParse = errors.New("can't parse string output")
12+
errNotImplemented = errors.New("not implemented for OS: " + runtime.GOOS)
13+
)
14+
15+
// DiscoverGateway is the OS independent function to get the default gateway
16+
func DiscoverGateway() (ip net.IP, err error) {
17+
return discoverGatewayOSSpecific()
18+
}
19+
20+
// DiscoverInterface is the OS independent function to call to get the default network interface IP that uses the default gateway
21+
func DiscoverInterface() (ip net.IP, err error) {
22+
return discoverGatewayInterfaceOSSpecific()
23+
}

gateway_common.go

Lines changed: 0 additions & 183 deletions
This file was deleted.

gateway_darwin.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// +build darwin
2+
13
package gateway
24

35
import (
46
"net"
57
"os/exec"
68
)
79

8-
func DiscoverGateway() (net.IP, error) {
10+
func discoverGatewayOSSpecific() (net.IP, error) {
911
routeCmd := exec.Command("/sbin/route", "-n", "get", "0.0.0.0")
1012
output, err := routeCmd.CombinedOutput()
1113
if err != nil {
@@ -14,3 +16,7 @@ func DiscoverGateway() (net.IP, error) {
1416

1517
return parseDarwinRouteGet(output)
1618
}
19+
20+
func discoverGatewayInterfaceOSSpecific() (ip net.IP, err error) {
21+
return nil, errNotImplemented
22+
}

gateway_freebsd.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// +build freebsd
2+
13
package gateway
24

35
import (
46
"net"
57
"os/exec"
68
)
79

8-
func DiscoverGateway() (ip net.IP, err error) {
10+
func discoverGatewayOSSpecific() (ip net.IP, err error) {
911
routeCmd := exec.Command("netstat", "-rn")
1012
output, err := routeCmd.CombinedOutput()
1113
if err != nil {
@@ -14,3 +16,7 @@ func DiscoverGateway() (ip net.IP, err error) {
1416

1517
return parseBSDSolarisNetstat(output)
1618
}
19+
20+
func discoverGatewayInterfaceOSSpecific() (ip net.IP, err error) {
21+
return nil, errNotImplemented
22+
}

gateway_linux.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build linux
2+
13
package gateway
24

35
import (
@@ -7,13 +9,12 @@ import (
79
"os"
810
)
911

10-
1112
const (
1213
// See http://man7.org/linux/man-pages/man8/route.8.html
13-
file = "/proc/net/route"
14+
file = "/proc/net/route"
1415
)
1516

16-
func DiscoverGateway() (ip net.IP, err error) {
17+
func discoverGatewayOSSpecific() (ip net.IP, err error) {
1718
f, err := os.Open(file)
1819
if err != nil {
1920
return nil, fmt.Errorf("Can't access %s", file)
@@ -24,7 +25,19 @@ func DiscoverGateway() (ip net.IP, err error) {
2425
if err != nil {
2526
return nil, fmt.Errorf("Can't read %s", file)
2627
}
27-
return parseLinuxProcNetRoute(bytes)
28+
return parseLinuxGatewayIP(bytes)
2829
}
2930

31+
func discoverGatewayInterfaceOSSpecific() (ip net.IP, err error) {
32+
f, err := os.Open(file)
33+
if err != nil {
34+
return nil, fmt.Errorf("Can't access %s", file)
35+
}
36+
defer f.Close()
3037

38+
bytes, err := ioutil.ReadAll(f)
39+
if err != nil {
40+
return nil, fmt.Errorf("Can't read %s", file)
41+
}
42+
return parseLinuxInterfaceIP(bytes)
43+
}

0 commit comments

Comments
 (0)