This commit is contained in:
Quentin McGaw
2020-06-05 19:32:12 -04:00
parent 0fb065eb61
commit e33a6a8503
7 changed files with 88 additions and 53 deletions

View File

@@ -61,14 +61,14 @@ func (r *routing) routeExists(subnet net.IPNet) (exists bool, err error) {
return false, nil
}
func (r *routing) CurrentPublicIP(defaultInterface string) (ip net.IP, err error) {
func (r *routing) VPNGatewayIP(defaultInterface string) (ip net.IP, err error) {
data, err := r.fileManager.ReadFile(string(constants.NetRoute))
if err != nil {
return nil, fmt.Errorf("cannot find current IP address: %w", err)
return nil, fmt.Errorf("cannot find VPN gateway IP address: %w", err)
}
entries, err := parseRoutingTable(data)
if err != nil {
return nil, fmt.Errorf("cannot find current IP address: %w", err)
return nil, fmt.Errorf("cannot find VPN gateway IP address: %w", err)
}
for _, entry := range entries {
if entry.iface == defaultInterface &&
@@ -77,7 +77,7 @@ func (r *routing) CurrentPublicIP(defaultInterface string) (ip net.IP, err error
return entry.destination, nil
}
}
return nil, fmt.Errorf("cannot find current IP address from ip routes")
return nil, fmt.Errorf("cannot find VPN gateway IP address from ip routes")
}
func ipIsPrivate(ip net.IP) bool {

View File

@@ -238,17 +238,17 @@ eth0 0002A8C0 0100000A 0003 0 0 0 00FFFFFF
err error
}{
"no data": {
err: fmt.Errorf("cannot find current IP address from ip routes"),
err: fmt.Errorf("cannot find VPN gateway IP address from ip routes"),
},
"read error": {
readErr: fmt.Errorf("error"),
err: fmt.Errorf("cannot find current IP address: error"),
err: fmt.Errorf("cannot find VPN gateway IP address: error"),
},
"parse error": {
data: []byte(`Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
eth0 x
`),
err: fmt.Errorf("cannot find current IP address: line 1 in /proc/net/route: line \"eth0 x\": not enough fields"),
err: fmt.Errorf("cannot find VPN gateway IP address: line 1 in /proc/net/route: line \"eth0 x\": not enough fields"),
},
"found eth0": {
defaultInterface: "eth0",
@@ -258,7 +258,7 @@ eth0 x
"not found tun0": {
defaultInterface: "tun0",
data: []byte(exampleRouteData),
err: fmt.Errorf("cannot find current IP address from ip routes"),
err: fmt.Errorf("cannot find VPN gateway IP address from ip routes"),
},
}
for name, tc := range tests {
@@ -271,7 +271,7 @@ eth0 x
filemanager.EXPECT().ReadFile(string(constants.NetRoute)).
Return(tc.data, tc.readErr).Times(1)
r := &routing{fileManager: filemanager}
ip, err := r.CurrentPublicIP(tc.defaultInterface)
ip, err := r.VPNGatewayIP(tc.defaultInterface)
if tc.err != nil {
require.Error(t, err)
assert.Equal(t, tc.err.Error(), err.Error())

View File

@@ -12,7 +12,7 @@ import (
type Routing interface {
AddRoutesVia(ctx context.Context, subnets []net.IPNet, defaultGateway net.IP, defaultInterface string) error
DefaultRoute() (defaultInterface string, defaultGateway net.IP, defaultSubnet net.IPNet, err error)
CurrentPublicIP(defaultInterface string) (ip net.IP, err error)
VPNGatewayIP(defaultInterface string) (ip net.IP, err error)
}
type routing struct {